github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/tests/integration/gomplateignore_test.go (about) 1 package integration 2 3 import ( 4 "io/fs" 5 "os" 6 "path/filepath" 7 "sort" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 tfs "gotest.tools/v3/fs" 13 ) 14 15 func setupGomplateignoreTest(t *testing.T) func(inFileOps ...tfs.PathOp) *tfs.Dir { 16 basedir := "gomplate-gomplateignore-tests" 17 18 inBuilder := func(inFileOps ...tfs.PathOp) *tfs.Dir { 19 tmpDir := tfs.NewDir(t, basedir, 20 tfs.WithDir("in", inFileOps...), 21 tfs.WithDir("out"), 22 ) 23 t.Cleanup(tmpDir.Remove) 24 return tmpDir 25 } 26 27 return inBuilder 28 } 29 30 func execute(t *testing.T, ignoreContent string, inFileOps ...tfs.PathOp) ([]string, error) { 31 return executeOpts(t, ignoreContent, []string{}, inFileOps...) 32 } 33 34 func executeOpts(t *testing.T, ignoreContent string, opts []string, inFileOps ...tfs.PathOp) ([]string, error) { 35 inBuilder := setupGomplateignoreTest(t) 36 37 inFileOps = append(inFileOps, tfs.WithFile(".gomplateignore", ignoreContent)) 38 tmpDir := inBuilder(inFileOps...) 39 40 argv := []string{} 41 argv = append(argv, opts...) 42 argv = append(argv, 43 "--input-dir", tmpDir.Join("in"), 44 "--output-dir", tmpDir.Join("out"), 45 ) 46 o, e, err := cmd(t, argv...).run() 47 assertSuccess(t, o, e, err, "") 48 49 files := []string{} 50 51 fsys := os.DirFS(tmpDir.Join("out") + "/") 52 err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { 53 if err != nil { 54 return err 55 } 56 57 if path != "" && !d.IsDir() { 58 path = filepath.FromSlash(path) 59 files = append(files, path) 60 } 61 return nil 62 }) 63 64 sort.Strings(files) 65 66 return files, err 67 } 68 69 func TestGomplateignore_Simple(t *testing.T) { 70 files, err := execute(t, `# all dot files 71 .* 72 *.log`, 73 tfs.WithFile("foo.log", "..."), 74 tfs.WithFile("rain.txt", "...")) 75 76 require.NoError(t, err) 77 assert.Equal(t, []string{"rain.txt"}, files) 78 } 79 80 func fromSlashes(paths ...string) []string { 81 for i, v := range paths { 82 paths[i] = filepath.FromSlash(v) 83 } 84 return paths 85 } 86 87 func TestGomplateignore_Folder(t *testing.T) { 88 files, err := execute(t, `.gomplateignore 89 f[o]o/bar 90 !foo/bar/tool`, 91 tfs.WithDir("foo", 92 tfs.WithDir("bar", 93 tfs.WithDir("tool", 94 tfs.WithFile("lex.txt", "..."), 95 ), 96 tfs.WithFile("1.txt", "..."), 97 ), 98 tfs.WithDir("tar", 99 tfs.WithFile("2.txt", "..."), 100 ), 101 ), 102 ) 103 104 require.NoError(t, err) 105 assert.Equal(t, fromSlashes( 106 "foo/bar/tool/lex.txt", "foo/tar/2.txt"), files) 107 } 108 109 func TestGomplateignore_Root(t *testing.T) { 110 files, err := execute(t, `.gomplateignore 111 /1.txt`, 112 tfs.WithDir("sub", 113 tfs.WithFile("1.txt", "..."), 114 tfs.WithFile("2.txt", "..."), 115 ), 116 tfs.WithFile("1.txt", "..."), 117 ) 118 119 require.NoError(t, err) 120 assert.Equal(t, fromSlashes( 121 "sub/1.txt", "sub/2.txt"), files) 122 } 123 124 func TestGomplateignore_Exclusion(t *testing.T) { 125 files, err := execute(t, `.gomplateignore 126 /e*.txt 127 !/e2.txt 128 en/e3.txt 129 !`, 130 tfs.WithFile("!", "xxx"), 131 tfs.WithFile("e1.txt", "xxx"), 132 tfs.WithFile("e2.txt", "xxx"), 133 tfs.WithFile("e3.txt", "xxx"), 134 tfs.WithDir("en", 135 tfs.WithFile("e1.txt", "xxx"), 136 tfs.WithFile("e2.txt", "xxx"), 137 tfs.WithFile("e3.txt", "xxx"), 138 ), 139 ) 140 141 require.NoError(t, err) 142 assert.Equal(t, fromSlashes( 143 "!", "e2.txt", "en/e1.txt", "en/e2.txt"), files) 144 } 145 146 func TestGomplateignore_Nested(t *testing.T) { 147 files, err := execute(t, `inner/foo.md`, 148 tfs.WithDir("inner", 149 tfs.WithDir("inner2", 150 tfs.WithFile(".gomplateignore", "moss.ini\n!/jess.ini"), 151 tfs.WithFile("jess.ini", "xxx"), 152 tfs.WithFile("moss.ini", "xxx")), 153 tfs.WithFile(".gomplateignore", "*.lst\njess.ini"), 154 tfs.WithFile("2.lst", "xxx"), 155 tfs.WithFile("foo.md", "xxx"), 156 ), 157 tfs.WithFile("1.txt", "xxx"), 158 ) 159 160 require.NoError(t, err) 161 assert.Equal(t, fromSlashes(".gomplateignore", "1.txt", 162 "inner/.gomplateignore", 163 "inner/inner2/.gomplateignore", 164 "inner/inner2/jess.ini"), files) 165 } 166 167 func TestGomplateignore_ByName(t *testing.T) { 168 files, err := execute(t, `.gomplateignore 169 world.txt`, 170 tfs.WithDir("aa", 171 tfs.WithDir("a1", 172 tfs.WithDir("a2", 173 tfs.WithFile("hello.txt", "..."), 174 tfs.WithFile("world.txt", "...")), 175 tfs.WithFile("hello.txt", "..."), 176 tfs.WithFile("world.txt", "...")), 177 tfs.WithFile("hello.txt", "..."), 178 tfs.WithFile("world.txt", "...")), 179 tfs.WithDir("bb", 180 tfs.WithFile("hello.txt", "..."), 181 tfs.WithFile("world.txt", "...")), 182 tfs.WithFile("hello.txt", "..."), 183 tfs.WithFile("world.txt", "..."), 184 ) 185 186 require.NoError(t, err) 187 assert.Equal(t, fromSlashes( 188 "aa/a1/a2/hello.txt", "aa/a1/hello.txt", 189 "aa/hello.txt", "bb/hello.txt", "hello.txt"), files) 190 } 191 192 func TestGomplateignore_BothName(t *testing.T) { 193 files, err := execute(t, `.gomplateignore 194 loss.txt 195 !2.log 196 `, 197 tfs.WithDir("loss.txt", 198 tfs.WithFile("1.log", "xxx"), 199 tfs.WithFile("2.log", "xxx")), 200 tfs.WithDir("foo", 201 tfs.WithFile("loss.txt", "xxx"), 202 tfs.WithFile("bare.txt", "xxx")), 203 ) 204 205 require.NoError(t, err) 206 assert.Equal(t, fromSlashes( 207 "foo/bare.txt", "loss.txt/2.log"), files) 208 } 209 210 func TestGomplateignore_LeadingSpace(t *testing.T) { 211 files, err := execute(t, `.gomplateignore 212 what.txt 213 !inner/ what.txt 214 *.log 215 ! dart.log 216 `, 217 tfs.WithDir("inner", 218 tfs.WithFile(" what.txt", "xxx"), 219 tfs.WithFile(" dart.log", "xxx")), 220 tfs.WithDir("inner2", 221 tfs.WithFile(" what.txt", "xxx")), 222 tfs.WithFile(" what.txt", "xxx"), 223 ) 224 225 require.NoError(t, err) 226 assert.Equal(t, fromSlashes( 227 "inner/ dart.log", "inner/ what.txt"), files) 228 } 229 230 func TestGomplateignore_WithExcludes(t *testing.T) { 231 files, err := executeOpts(t, `.gomplateignore 232 *.log 233 `, []string{ 234 "--exclude", "crash.bin", 235 "--exclude", "rules/*.txt", 236 "--exclude", "sprites/*.ini", 237 }, 238 tfs.WithDir("logs", 239 tfs.WithFile("archive.zip", "x"), 240 tfs.WithFile("engine.log", "x"), 241 tfs.WithFile("skills.log", "x")), 242 tfs.WithDir("rules", 243 tfs.WithFile("index.csv", "x"), 244 tfs.WithFile("fire.txt", "x"), 245 tfs.WithFile("earth.txt", "x")), 246 tfs.WithDir("sprites", 247 tfs.WithFile("human.csv", "x"), 248 tfs.WithFile("demon.xml", "x"), 249 tfs.WithFile("alien.ini", "x")), 250 tfs.WithFile("manifest.json", "x"), 251 tfs.WithFile("crash.bin", "x"), 252 ) 253 254 require.NoError(t, err) 255 assert.Equal(t, fromSlashes( 256 "logs/archive.zip", "manifest.json", "rules/index.csv", 257 "sprites/demon.xml", "sprites/human.csv"), files) 258 } 259 260 func TestGomplateignore_WithIncludes(t *testing.T) { 261 files, err := executeOpts(t, `.gomplateignore 262 *.log 263 `, []string{ 264 "--include", "rules/*", 265 "--exclude", "rules/*.txt", 266 }, 267 tfs.WithDir("logs", 268 tfs.WithFile("archive.zip", "x"), 269 tfs.WithFile("engine.log", "x"), 270 tfs.WithFile("skills.log", "x")), 271 tfs.WithDir("rules", 272 tfs.WithFile("index.csv", "x"), 273 tfs.WithFile("fire.txt", "x"), 274 tfs.WithFile("earth.txt", "x")), 275 tfs.WithFile("manifest.json", "x"), 276 tfs.WithFile("crash.bin", "x"), 277 ) 278 279 require.NoError(t, err) 280 assert.Equal(t, fromSlashes("rules/index.csv"), files) 281 } 282 283 func TestGomplateignore_WithExcludeProcessing(t *testing.T) { 284 files, err := executeOpts(t, `.gomplateignore 285 *.log 286 `, []string{ 287 "--exclude-processing", "crash.bin", 288 "--exclude-processing", "log/*.zip", 289 "--exclude", "rules/*.txt", 290 "--exclude", "sprites/*.ini", 291 }, 292 tfs.WithDir("logs", 293 tfs.WithFile("archive.zip", "xxx"), 294 tfs.WithFile("engine.log", "xxx"), 295 tfs.WithFile("skills.log", "xxx")), 296 tfs.WithDir("rules", 297 tfs.WithFile("index.csv", "xxx"), 298 tfs.WithFile("fire.txt", "xxx"), 299 tfs.WithFile("earth.txt", "xxx")), 300 tfs.WithDir("sprites", 301 tfs.WithFile("human.csv", "xxx"), 302 tfs.WithFile("demon.xml", "xxx"), 303 tfs.WithFile("alien.ini", "xxx")), 304 tfs.WithFile("manifest.json", "xxx"), 305 tfs.WithFile("crash.bin", "xxx"), 306 ) 307 308 require.NoError(t, err) 309 assert.Equal(t, fromSlashes( 310 "crash.bin", "logs/archive.zip", "manifest.json", "rules/index.csv", 311 "sprites/demon.xml", "sprites/human.csv"), files) 312 }