forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory_duplicator_test.go
113 lines (86 loc) · 3.2 KB
/
directory_duplicator_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
package cargo_test
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/cargo"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
destDir string
sourceDir string
directoryDup cargo.DirectoryDuplicator
)
it.Before(func() {
var err error
sourceDir, err = os.MkdirTemp("", "source")
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(sourceDir, "some-file"), []byte("some content"), 0644)).To(Succeed())
Expect(os.MkdirAll(filepath.Join(sourceDir, "some-dir"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(sourceDir, "some-dir", "other-file"), []byte("other content"), 0755)).To(Succeed())
Expect(os.Symlink("other-file", filepath.Join(sourceDir, "some-dir", "link"))).To(Succeed())
destDir, err = os.MkdirTemp("", "dest")
Expect(err).NotTo(HaveOccurred())
directoryDup = cargo.NewDirectoryDuplicator()
})
it.After(func() {
Expect(os.RemoveAll(sourceDir)).To(Succeed())
Expect(os.RemoveAll(destDir)).To(Succeed())
})
context("Duplicate", func() {
it("duplicates the contents of a directory", func() {
Expect(directoryDup.Duplicate(sourceDir, destDir)).To(Succeed())
file, err := os.Open(filepath.Join(destDir, "some-file"))
Expect(err).NotTo(HaveOccurred())
content, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some content"))
info, err := file.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))
Expect(file.Close()).To(Succeed())
info, err = os.Stat(filepath.Join(destDir, "some-dir"))
Expect(err).NotTo(HaveOccurred())
Expect(info.IsDir()).To(BeTrue())
file, err = os.Open(filepath.Join(destDir, "some-dir", "other-file"))
Expect(err).NotTo(HaveOccurred())
content, err = io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("other content"))
info, err = file.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
Expect(file.Close()).To(Succeed())
info, err = os.Lstat(filepath.Join(destDir, "some-dir", "link"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode() & os.ModeSymlink).To(Equal(os.ModeSymlink))
path, err := os.Readlink(filepath.Join(destDir, "some-dir", "link"))
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join("other-file")))
})
})
context("failure cases", func() {
context("source dir does not exist", func() {
it("fails", func() {
err := directoryDup.Duplicate("does-not-exist", destDir)
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
context("when source file has bad permissions", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(sourceDir, "some-file"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(sourceDir, "some-file"), 0644)).To(Succeed())
})
it("fails", func() {
err := directoryDup.Duplicate(sourceDir, destDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
}