github.com/hairyhenderson/templater@v3.5.0+incompatible/tests/integration/gomplateignore_test.go (about) 1 //+build integration 2 3 package integration 4 5 import ( 6 "os" 7 "path/filepath" 8 "sort" 9 10 . "gopkg.in/check.v1" 11 12 "github.com/gotestyourself/gotestyourself/fs" 13 "github.com/gotestyourself/gotestyourself/icmd" 14 "github.com/spf13/afero" 15 tassert "github.com/stretchr/testify/assert" 16 ) 17 18 type GomplateignoreSuite struct { 19 inBuilder func(inFileOps ...fs.PathOp) 20 tmpDir *fs.Dir 21 } 22 23 var _ = Suite(&GomplateignoreSuite{}) 24 25 func (s *GomplateignoreSuite) SetUpTest(c *C) { 26 const basedir = "gomplate-gomplateignore-tests" 27 s.inBuilder = func(inFileOps ...fs.PathOp) { 28 s.tmpDir = fs.NewDir(c, basedir, 29 fs.WithDir("in", inFileOps...), 30 fs.WithDir("out"), 31 ) 32 } 33 } 34 35 func (s *GomplateignoreSuite) TearDownTest(c *C) { 36 s.tmpDir.Remove() 37 } 38 39 func (s *GomplateignoreSuite) execute(c *C, ignoreContent string, inFileOps ...fs.PathOp) { 40 s.executeOpts(c, ignoreContent, []string{}, inFileOps...) 41 } 42 43 func (s *GomplateignoreSuite) executeOpts(c *C, ignoreContent string, opts []string, inFileOps ...fs.PathOp) { 44 inFileOps = append(inFileOps, fs.WithFile(".gomplateignore", ignoreContent)) 45 s.inBuilder(inFileOps...) 46 47 argv := []string{} 48 argv = append(argv, opts...) 49 argv = append(argv, 50 "--input-dir", s.tmpDir.Join("in"), 51 "--output-dir", s.tmpDir.Join("out"), 52 ) 53 result := icmd.RunCommand(GomplateBin, argv...) 54 result.Assert(c, icmd.Success) 55 } 56 57 func (s *GomplateignoreSuite) collectOutFiles() (files []string, err error) { 58 files = []string{} 59 60 fs := afero.NewBasePathFs(afero.NewOsFs(), s.tmpDir.Join("out")) 61 afero.Walk(fs, "", func(path string, info os.FileInfo, werr error) error { 62 if werr != nil { 63 err = werr 64 return nil 65 } 66 if path != "" && !info.IsDir() { 67 files = append(files, path) 68 } 69 return nil 70 }) 71 72 sort.Strings(files) 73 return 74 } 75 76 func (s *GomplateignoreSuite) TestGomplateignore_Simple(c *C) { 77 s.execute(c, `# all dot files 78 .* 79 *.log`, 80 fs.WithFile("empty.log", ""), 81 fs.WithFile("rain.txt", "")) 82 83 files, err := s.collectOutFiles() 84 tassert.NoError(c, err) 85 tassert.Equal(c, []string{"rain.txt"}, files) 86 } 87 88 func fromSlashes(paths ...string) []string { 89 for i, v := range paths { 90 paths[i] = filepath.FromSlash(v) 91 } 92 return paths 93 } 94 95 func (s *GomplateignoreSuite) TestGomplateignore_Folder(c *C) { 96 s.execute(c, `.gomplateignore 97 f[o]o/bar 98 !foo/bar/tool`, 99 fs.WithDir("foo", 100 fs.WithDir("bar", 101 fs.WithDir("tool", 102 fs.WithFile("lex.txt", ""), 103 ), 104 fs.WithFile("1.txt", ""), 105 ), 106 fs.WithDir("tar", 107 fs.WithFile("2.txt", ""), 108 ), 109 ), 110 ) 111 112 files, err := s.collectOutFiles() 113 tassert.NoError(c, err) 114 tassert.Equal(c, fromSlashes( 115 "foo/bar/tool/lex.txt", "foo/tar/2.txt"), files) 116 } 117 118 func (s *GomplateignoreSuite) TestGomplateignore_Root(c *C) { 119 s.execute(c, `.gomplateignore 120 /1.txt`, 121 fs.WithDir("sub", 122 fs.WithFile("1.txt", ""), 123 fs.WithFile("2.txt", ""), 124 ), 125 fs.WithFile("1.txt", ""), 126 ) 127 128 files, err := s.collectOutFiles() 129 tassert.NoError(c, err) 130 tassert.Equal(c, fromSlashes( 131 "sub/1.txt", "sub/2.txt"), files) 132 } 133 134 func (s *GomplateignoreSuite) TestGomplateignore_Exclusion(c *C) { 135 s.execute(c, `.gomplateignore 136 /e*.txt 137 !/e2.txt 138 en/e3.txt 139 !`, 140 fs.WithFile("!", ""), 141 fs.WithFile("e1.txt", ""), 142 fs.WithFile("e2.txt", ""), 143 fs.WithFile("e3.txt", ""), 144 fs.WithDir("en", 145 fs.WithFile("e1.txt", ""), 146 fs.WithFile("e2.txt", ""), 147 fs.WithFile("e3.txt", ""), 148 ), 149 ) 150 151 files, err := s.collectOutFiles() 152 tassert.NoError(c, err) 153 tassert.Equal(c, fromSlashes( 154 "!", "e2.txt", "en/e1.txt", "en/e2.txt"), files) 155 } 156 157 func (s *GomplateignoreSuite) TestGomplateignore_Nested(c *C) { 158 s.execute(c, `inner/foo.md`, 159 fs.WithDir("inner", 160 fs.WithDir("inner2", 161 fs.WithFile(".gomplateignore", "moss.ini\n!/jess.ini"), 162 fs.WithFile("jess.ini", ""), 163 fs.WithFile("moss.ini", "")), 164 fs.WithFile(".gomplateignore", "*.lst\njess.ini"), 165 fs.WithFile("2.lst", ""), 166 fs.WithFile("foo.md", ""), 167 ), 168 fs.WithFile("1.txt", ""), 169 ) 170 171 files, err := s.collectOutFiles() 172 tassert.NoError(c, err) 173 tassert.Equal(c, fromSlashes(".gomplateignore", "1.txt", 174 "inner/.gomplateignore", 175 "inner/inner2/.gomplateignore", 176 "inner/inner2/jess.ini"), files) 177 } 178 179 func (s *GomplateignoreSuite) TestGomplateignore_ByName(c *C) { 180 s.execute(c, `.gomplateignore 181 world.txt`, 182 fs.WithDir("aa", 183 fs.WithDir("a1", 184 fs.WithDir("a2", 185 fs.WithFile("hello.txt", ""), 186 fs.WithFile("world.txt", "")), 187 fs.WithFile("hello.txt", ""), 188 fs.WithFile("world.txt", "")), 189 fs.WithFile("hello.txt", ""), 190 fs.WithFile("world.txt", "")), 191 fs.WithDir("bb", 192 fs.WithFile("hello.txt", ""), 193 fs.WithFile("world.txt", "")), 194 fs.WithFile("hello.txt", ""), 195 fs.WithFile("world.txt", ""), 196 ) 197 198 files, err := s.collectOutFiles() 199 tassert.NoError(c, err) 200 tassert.Equal(c, fromSlashes( 201 "aa/a1/a2/hello.txt", "aa/a1/hello.txt", 202 "aa/hello.txt", "bb/hello.txt", "hello.txt"), files) 203 } 204 205 func (s *GomplateignoreSuite) TestGomplateignore_BothName(c *C) { 206 s.execute(c, `.gomplateignore 207 loss.txt 208 !2.log 209 `, 210 fs.WithDir("loss.txt", 211 fs.WithFile("1.log", ""), 212 fs.WithFile("2.log", "")), 213 fs.WithDir("foo", 214 fs.WithFile("loss.txt", ""), 215 fs.WithFile("bare.txt", "")), 216 ) 217 218 files, err := s.collectOutFiles() 219 tassert.NoError(c, err) 220 tassert.Equal(c, fromSlashes( 221 "foo/bare.txt", "loss.txt/2.log"), files) 222 } 223 224 func (s *GomplateignoreSuite) TestGomplateignore_LeadingSpace(c *C) { 225 s.execute(c, `.gomplateignore 226 what.txt 227 !inner/ what.txt 228 *.log 229 ! dart.log 230 `, 231 fs.WithDir("inner", 232 fs.WithFile(" what.txt", ""), 233 fs.WithFile(" dart.log", "")), 234 fs.WithDir("inner2", 235 fs.WithFile(" what.txt", "")), 236 fs.WithFile(" what.txt", ""), 237 ) 238 239 files, err := s.collectOutFiles() 240 tassert.NoError(c, err) 241 tassert.Equal(c, fromSlashes( 242 "inner/ dart.log", "inner/ what.txt"), files) 243 } 244 245 func (s *GomplateignoreSuite) TestGomplateignore_WithExcludes(c *C) { 246 s.executeOpts(c, `.gomplateignore 247 *.log 248 `, []string{ 249 "--exclude", "crash.bin", 250 "--exclude", "rules/*.txt", 251 "--exclude", "sprites/*.ini", 252 }, 253 fs.WithDir("logs", 254 fs.WithFile("archive.zip", ""), 255 fs.WithFile("engine.log", ""), 256 fs.WithFile("skills.log", "")), 257 fs.WithDir("rules", 258 fs.WithFile("index.csv", ""), 259 fs.WithFile("fire.txt", ""), 260 fs.WithFile("earth.txt", "")), 261 fs.WithDir("sprites", 262 fs.WithFile("human.csv", ""), 263 fs.WithFile("demon.xml", ""), 264 fs.WithFile("alien.ini", "")), 265 fs.WithFile("manifest.json", ""), 266 fs.WithFile("crash.bin", ""), 267 ) 268 269 files, err := s.collectOutFiles() 270 tassert.NoError(c, err) 271 tassert.Equal(c, fromSlashes( 272 "logs/archive.zip", "manifest.json", "rules/index.csv", 273 "sprites/demon.xml", "sprites/human.csv"), files) 274 }