diff --git a/filetree/filetree.go b/filetree/filetree.go index 29c2e33a8..399da9517 100644 --- a/filetree/filetree.go +++ b/filetree/filetree.go @@ -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() } } @@ -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 { diff --git a/filetree/filetree_test.go b/filetree/filetree_test.go index 669f75690..1ed158924 100644 --- a/filetree/filetree_test.go +++ b/filetree/filetree_test.go @@ -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() { @@ -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() { @@ -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: