github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/droplet_runner/command_factory/cf_ignore/cf_ignore_test.go (about) 1 package cf_ignore_test 2 3 import ( 4 "errors" 5 "strings" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 10 "github.com/cloudfoundry-incubator/ltc/droplet_runner/command_factory/cf_ignore" 11 ) 12 13 var _ = Describe("CF Ignore", func() { 14 var cfIgnore cf_ignore.CFIgnore 15 16 BeforeEach(func() { 17 cfIgnore = cf_ignore.New() 18 }) 19 20 It("excludes files based on exact path matches", func() { 21 Expect(cfIgnore.Parse(strings.NewReader("the-dir/the-path"))).To(Succeed()) 22 Expect(cfIgnore.ShouldIgnore("the-dir/the-path")).To(BeTrue()) 23 }) 24 25 It("excludes the contents of directories based on exact path matches", func() { 26 Expect(cfIgnore.Parse(strings.NewReader("dir1/dir2"))).To(Succeed()) 27 Expect(cfIgnore.ShouldIgnore("dir1/dir2/the-file")).To(BeTrue()) 28 Expect(cfIgnore.ShouldIgnore("dir1/dir2/dir3/the-file")).To(BeTrue()) 29 }) 30 31 It("excludes files based on star patterns", func() { 32 Expect(cfIgnore.Parse(strings.NewReader("dir1/*.so"))).To(Succeed()) 33 Expect(cfIgnore.ShouldIgnore("dir1/file1.so")).To(BeTrue()) 34 Expect(cfIgnore.ShouldIgnore("dir1/file2.cc")).To(BeFalse()) 35 }) 36 37 It("excludes files based on double-star patterns", func() { 38 Expect(cfIgnore.Parse(strings.NewReader(`dir1/**/*.so`))).To(Succeed()) 39 Expect(cfIgnore.ShouldIgnore("dir1/dir2/dir3/file1.so")).To(BeTrue()) 40 Expect(cfIgnore.ShouldIgnore("different-dir/dir2/file.so")).To(BeFalse()) 41 }) 42 43 It("allows files to be explicitly included", func() { 44 err := cfIgnore.Parse(strings.NewReader(` 45 node_modules/* 46 !node_modules/common 47 `)) 48 Expect(err).NotTo(HaveOccurred()) 49 50 Expect(cfIgnore.ShouldIgnore("node_modules/something-else")).To(BeTrue()) 51 Expect(cfIgnore.ShouldIgnore("node_modules/common")).To(BeFalse()) 52 }) 53 54 It("applies the patterns in order from top to bottom", func() { 55 err := cfIgnore.Parse(strings.NewReader(` 56 stuff/* 57 !stuff/*.c 58 stuff/exclude.c 59 `)) 60 Expect(err).NotTo(HaveOccurred()) 61 62 Expect(cfIgnore.ShouldIgnore("stuff/something.txt")).To(BeTrue()) 63 Expect(cfIgnore.ShouldIgnore("stuff/exclude.c")).To(BeTrue()) 64 Expect(cfIgnore.ShouldIgnore("stuff/include.c")).To(BeFalse()) 65 }) 66 67 It("ignores certain commonly ingored files by default", func() { 68 Expect(cfIgnore.Parse(strings.NewReader(""))).To(Succeed()) 69 Expect(cfIgnore.ShouldIgnore(".git/objects")).To(BeTrue()) 70 71 Expect(cfIgnore.Parse(strings.NewReader("!.git"))).To(Succeed()) 72 Expect(cfIgnore.ShouldIgnore(".git/objects")).To(BeFalse()) 73 }) 74 75 Describe("files named manifest.yml", func() { 76 It("ignores manifest.yml at the top level", func() { 77 Expect(cfIgnore.Parse(strings.NewReader(""))).To(Succeed()) 78 Expect(cfIgnore.ShouldIgnore("manifest.yml")).To(BeTrue()) 79 }) 80 81 It("does not ignore nested manifest.yml files", func() { 82 Expect(cfIgnore.Parse(strings.NewReader(""))).To(Succeed()) 83 Expect(cfIgnore.ShouldIgnore("public/assets/manifest.yml")).To(BeFalse()) 84 }) 85 }) 86 87 Describe("Parse", func() { 88 Context("when the ignored reader returns an error", func() { 89 It("returns an error", func() { 90 err := cfIgnore.Parse(errorReader{}) 91 Expect(err).To(MatchError("no bueno")) 92 }) 93 }) 94 95 Context("when parse is called multiple times", func() { 96 It("adds patterns to the existing ignore patterns", func() { 97 Expect(cfIgnore.Parse(strings.NewReader("dir2"))).To(Succeed()) 98 Expect(cfIgnore.Parse(strings.NewReader("dir4"))).To(Succeed()) 99 Expect(cfIgnore.ShouldIgnore("dir1/dir2")).To(BeTrue()) 100 Expect(cfIgnore.ShouldIgnore("dir3/dir4")).To(BeTrue()) 101 }) 102 }) 103 }) 104 }) 105 106 type errorReader struct{} 107 108 func (errorReader) Read(p []byte) (n int, err error) { 109 return 0, errors.New("no bueno") 110 }