Skip to content

Commit

Permalink
Extend filetree tests
Browse files Browse the repository at this point in the history
To demonstrate nested directories and files. I looked at using
`github.com/Flaque/filet` to create temp dirs/files but it needs a
concrete implementation of `*testing.T` which isn't compatible with
Ginkgo:

- http://onsi.github.io/ginkgo/#using-other-matcher-libraries
- https://godoc.org/github.com/onsi/ginkgo#GinkgoT

It might be worth raising a pull request upstream and switching later,
but the now we can use `ioutil.WriteFile` since we only append to the
file in one place.

I've also added `HaveLen` matchers to check that there aren't more items
in the slice than we are making explicit assertions against.
  • Loading branch information
dcarley committed Jun 15, 2018
1 parent 50c34db commit db3dd10
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ var _ = Describe("filetree", func() {
})

Describe("NewTree", func() {
var rootFile, subDir, subDirFile string

BeforeEach(func() {
var err error
_, err = os.OpenFile(
filepath.Join(tempRoot, "foo"),
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())
rootFile = filepath.Join(tempRoot, "root_file")
subDir = filepath.Join(tempRoot, "sub_dir")
subDirFile = filepath.Join(tempRoot, "sub_dir", "sub_dir_file")

Expect(ioutil.WriteFile(rootFile, []byte{}, 0600)).To(Succeed())
Expect(os.Mkdir(subDir, 0700)).To(Succeed())
Expect(ioutil.WriteFile(subDirFile, []byte{}, 0600)).To(Succeed())
})

It("Builds a tree of the nested file-structure", func() {
Expand All @@ -45,9 +47,15 @@ var _ = Describe("filetree", func() {
Expect(tree.FullPath).To(Equal(tempRoot))
Expect(tree.Info.Name()).To(Equal(filepath.Base(tempRoot)))

fooPath := filepath.Join(tempRoot, "foo")
Expect(tree.Children[0].Info.Name()).To(Equal("foo"))
Expect(tree.Children[0].FullPath).To(Equal(fooPath))
Expect(tree.Children).To(HaveLen(2))
Expect(tree.Children[0].Info.Name()).To(Equal("root_file"))
Expect(tree.Children[0].FullPath).To(Equal(rootFile))
Expect(tree.Children[1].Info.Name()).To(Equal("sub_dir"))
Expect(tree.Children[1].FullPath).To(Equal(subDir))

Expect(tree.Children[1].Children).To(HaveLen(1))
Expect(tree.Children[1].Children[0].Info.Name()).To(Equal("sub_dir_file"))
Expect(tree.Children[1].Children[0].FullPath).To(Equal(subDirFile))
})
})
})
Expand Down

0 comments on commit db3dd10

Please sign in to comment.