Skip to content

Commit

Permalink
Test current behavior for handling empty leaf nodes
Browse files Browse the repository at this point in the history
We're unsure if this is desired behavior for empty folders,
but making sure it's documented here in our tests.

For example:

foo/bar/baz # where baz is an empty directory

Generates:

foo:
  bar:
    baz: null

We also documented throwing YAML errors for invalid leafs
  • Loading branch information
Zachary Scott committed Jun 15, 2018
1 parent da660f4 commit ce436f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
8 changes: 5 additions & 3 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ type Node struct {
}

func (n Node) MarshalYAML() (interface{}, error) {
switch len(n.Children) {
case 0:
if len(n.Children) == 0 {
return n.marshalLeaf()
default:
} else {
return n.marshalParent()
}
}
Expand All @@ -41,6 +40,9 @@ func (n Node) marshalParent() (interface{}, error) {

func (n Node) marshalLeaf() (interface{}, error) {
var content interface{}
if n.Info.IsDir() {
return content, nil
}

buf, err := ioutil.ReadFile(n.FullPath)
if err != nil {
Expand Down
40 changes: 30 additions & 10 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,31 @@ var _ = Describe("filetree", func() {
})

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

BeforeEach(func() {
rootFile = filepath.Join(tempRoot, "root_file.yml")
subDir = filepath.Join(tempRoot, "sub_dir")
subDirFile = filepath.Join(tempRoot, "sub_dir", "sub_dir_file.yml")
emptyDir = filepath.Join(tempRoot, "empty_dir")

Expect(ioutil.WriteFile(rootFile, []byte("foo:\n bar"), 0600)).To(Succeed())
Expect(os.Mkdir(subDir, 0700)).To(Succeed())
Expect(ioutil.WriteFile(subDirFile, []byte("foo:\n bar:\n baz"), 0600)).To(Succeed())
Expect(os.Mkdir(emptyDir, 0700)).To(Succeed())

})

It("Throws an error if content is unmarshallable", func() {
anotherDir := filepath.Join(tempRoot, "another_dir")
anotherDirFile := filepath.Join(tempRoot, "another_dir", "another_dir_file.yml")
Expect(os.Mkdir(anotherDir, 0700)).To(Succeed())
Expect(ioutil.WriteFile(anotherDirFile, []byte("1some: in: valid: yaml"), 0600)).To(Succeed())
tree, err := filetree.NewTree(tempRoot)
Expect(err).ToNot(HaveOccurred())

_, err = yaml.Marshal(tree)
Expect(err).To(MatchError("yaml: mapping values are not allowed in this context"))
})

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

Expect(tree.Children).To(HaveLen(2))
Expect(tree.Children).To(HaveLen(3))
sort.Slice(tree.Children, func(i, j int) bool {
return tree.Children[i].FullPath < tree.Children[j].FullPath
})
Expect(tree.Children[0].Info.Name()).To(Equal("root_file.yml"))
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.yml"))
Expect(tree.Children[1].Children[0].FullPath).To(Equal(subDirFile))

Expect(tree.Children[0].Info.Name()).To(Equal("empty_dir"))
Expect(tree.Children[0].FullPath).To(Equal(emptyDir))

Expect(tree.Children[1].Info.Name()).To(Equal("root_file.yml"))
Expect(tree.Children[1].FullPath).To(Equal(rootFile))
Expect(tree.Children[2].Info.Name()).To(Equal("sub_dir"))
Expect(tree.Children[2].FullPath).To(Equal(subDir))

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

It("renders to YAML", func() {
Expand All @@ -72,6 +91,7 @@ var _ = Describe("filetree", func() {
Expect(out).To(MatchYAML(`root_file.yml:
foo:
bar
empty_dir: null
sub_dir:
sub_dir_file.yml:
foo:
Expand Down

0 comments on commit ce436f8

Please sign in to comment.