-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexists_test.go
71 lines (57 loc) · 1.47 KB
/
exists_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
package fs_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testExists(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
dirPath string
filePath string
)
context("Exists", func() {
it.Before(func() {
var err error
dirPath, err = os.MkdirTemp("", "dir")
Expect(err).NotTo(HaveOccurred())
filePath = filepath.Join(dirPath, "some-file")
})
it.After(func() {
Expect(os.RemoveAll(dirPath)).To(Succeed())
})
context("when the file exists", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte("hello file"), 0644)).To(Succeed())
})
it("returns true", func() {
Expect(fs.Exists(filePath)).To(BeTrue())
})
})
context("when the file DOES NOT exists", func() {
it.Before(func() {
Expect(os.RemoveAll(dirPath)).To(Succeed())
})
it("returns false", func() {
Expect(fs.Exists(filePath)).To(BeFalse())
})
})
context("when the file cannot be read", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, nil, 0644)).To(Succeed())
Expect(os.Chmod(dirPath, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(dirPath, os.ModePerm)).To(Succeed())
})
it("returns false and an error", func() {
exists, err := fs.Exists(filePath)
Expect(err.Error()).To(ContainSubstring("permission denied"))
Expect(exists).To(BeFalse())
})
})
})
}