github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/gomplateignore_test.go (about)

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