forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_test.go
210 lines (161 loc) · 6.46 KB
/
move_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package fs_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/fs"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testMove(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("Move", func() {
var (
sourceDir string
destinationDir string
)
it.Before(func() {
var err error
sourceDir, err = os.MkdirTemp("", "source")
Expect(err).NotTo(HaveOccurred())
destinationDir, err = os.MkdirTemp("", "destination")
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
Expect(os.RemoveAll(sourceDir)).To(Succeed())
Expect(os.RemoveAll(destinationDir)).To(Succeed())
})
context("when the source is a file", func() {
var source, destination string
it.Before(func() {
source = filepath.Join(sourceDir, "source")
destination = filepath.Join(destinationDir, "destination")
err := os.WriteFile(source, []byte("some-content"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it("moves the source file to the destination", func() {
err := fs.Move(source, destination)
Expect(err).NotTo(HaveOccurred())
content, err := os.ReadFile(destination)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-content"))
info, err := os.Stat(destination)
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))
Expect(source).NotTo(BeAnExistingFile())
})
context("failure cases", func() {
context("when the source cannot be read", func() {
it.Before(func() {
Expect(os.Chmod(source, 0000)).To(Succeed())
})
it("returns an error", func() {
err := fs.Move(source, destination)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the destination cannot be removed", func() {
it.Before(func() {
Expect(os.Chmod(destinationDir, 0000)).To(Succeed())
})
it("returns an error", func() {
err := fs.Move(source, destination)
Expect(err).To(MatchError(ContainSubstring("failed to move: failed to copy: destination exists:")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
context("when the source is a directory", func() {
var source, destination, external string
it.Before(func() {
var err error
external, err = os.MkdirTemp("", "external")
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(external, "some-file"), []byte("some-content"), 0644)
Expect(err).NotTo(HaveOccurred())
source = filepath.Join(sourceDir, "source")
destination = filepath.Join(destinationDir, "destination")
Expect(os.MkdirAll(filepath.Join(source, "some-dir"), os.ModePerm)).To(Succeed())
err = os.WriteFile(filepath.Join(source, "some-dir", "some-file"), []byte("some-content"), 0644)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(source, "some-dir", "readonly-file"), []byte("some-content"), 0444)
Expect(err).NotTo(HaveOccurred())
err = os.Symlink("some-file", filepath.Join(source, "some-dir", "some-symlink"))
Expect(err).NotTo(HaveOccurred())
err = os.Symlink(filepath.Join(external, "some-file"), filepath.Join(source, "some-dir", "external-symlink"))
Expect(err).NotTo(HaveOccurred())
})
context("when the destination does not exist", func() {
it("moves the source directory to the destination", func() {
err := fs.Move(source, destination)
Expect(err).NotTo(HaveOccurred())
Expect(destination).To(BeADirectory())
content, err := os.ReadFile(filepath.Join(destination, "some-dir", "some-file"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-content"))
info, err := os.Stat(filepath.Join(destination, "some-dir", "some-file"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))
info, err = os.Stat(filepath.Join(destination, "some-dir", "readonly-file"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0444)))
path, err := os.Readlink(filepath.Join(destination, "some-dir", "some-symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal("some-file"))
path, err = os.Readlink(filepath.Join(destination, "some-dir", "external-symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(external, "some-file")))
Expect(source).NotTo(BeAnExistingFile())
})
})
context("when the destination is a file", func() {
it.Before(func() {
Expect(os.RemoveAll(destination))
Expect(os.WriteFile(destination, []byte{}, os.ModePerm)).To(Succeed())
})
it("moves the source directory to the destination", func() {
err := fs.Move(source, destination)
Expect(err).NotTo(HaveOccurred())
Expect(destination).To(BeADirectory())
content, err := os.ReadFile(filepath.Join(destination, "some-dir", "some-file"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-content"))
info, err := os.Stat(filepath.Join(destination, "some-dir", "some-file"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))
info, err = os.Stat(filepath.Join(destination, "some-dir", "readonly-file"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0444)))
path, err := os.Readlink(filepath.Join(destination, "some-dir", "some-symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal("some-file"))
path, err = os.Readlink(filepath.Join(destination, "some-dir", "external-symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(external, "some-file")))
Expect(source).NotTo(BeAnExistingFile())
})
})
context("failure cases", func() {
context("when the source does not exist", func() {
it("returns an error", func() {
err := fs.Move("no-such-source", destination)
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
context("when the source cannot be walked", func() {
it.Before(func() {
Expect(os.Chmod(source, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(source, 0777)).To(Succeed())
})
it("returns an error", func() {
err := fs.Move(source, destination)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
})
}