This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdir_test.go
111 lines (92 loc) · 2.69 KB
/
dir_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
package parcello_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/phogolabs/parcello"
)
var _ = Describe("Dir", func() {
var dir parcello.Dir
BeforeEach(func() {
path, err := ioutil.TempDir("", "gom_generator")
Expect(err).To(BeNil())
dir = parcello.Dir(path)
Expect(ioutil.WriteFile(filepath.Join(path, "sample.txt"), []byte("test"), 0600)).To(Succeed())
})
Context("Open", func() {
It("opens a file successfully", func() {
file, err := dir.Open("sample.txt")
Expect(err).To(BeNil())
content, err := ioutil.ReadAll(file)
Expect(err).To(BeNil())
Expect(string(content)).To(Equal("test"))
Expect(file.Close()).To(Succeed())
})
})
Context("Add", func() {
It("adds the resource to the manager", func() {
Expect(dir.Add(parcello.BinaryResource([]byte{}))).To(Succeed())
})
})
Context("Dir", func() {
It("creates a sub file system", func() {
d, err := dir.Dir("root")
Expect(err).To(BeNil())
Expect(fmt.Sprintf("%v", d)).To(Equal(filepath.Join(string(dir), "root")))
})
})
Context("OpenFile", func() {
It("opens a file successfully", func() {
file, err := dir.OpenFile("sample.txt", os.O_RDONLY, 0)
Expect(err).To(BeNil())
content, err := ioutil.ReadAll(file)
Expect(err).To(BeNil())
Expect(string(content)).To(Equal("test"))
Expect(file.Close()).To(Succeed())
})
Context("when the underlying file system fails", func() {
It("returns an error", func() {
dir = parcello.Dir("/hello")
file, err := dir.OpenFile("report.txt", os.O_CREATE, 0)
Expect(file).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("mkdir /hello: permission denied"))
})
})
Context("when the file does not exists", func() {
It("returns an error", func() {
file, err := dir.OpenFile("report.txt", os.O_RDONLY, 0)
Expect(file).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no such file or directory"))
})
})
})
Context("Walk", func() {
It("walks through the hierarchy successfully", func() {
count := 0
err := dir.Walk("/", func(path string, info os.FileInfo, err error) error {
count = count + 1
if info.IsDir() {
Expect(path).To(Equal("."))
} else {
Expect(path).To(Equal("sample.txt"))
}
return nil
})
Expect(count).To(Equal(2))
Expect(err).NotTo(HaveOccurred())
})
Context("when the walking fails", func() {
It("returns an error", func() {
err := dir.Walk("/wrong", func(path string, info os.FileInfo, err error) error {
return fmt.Errorf("Oh no!")
})
Expect(err).To(MatchError("Oh no!"))
})
})
})
})