github.com/grahambrereton-form3/tilt@v0.10.18/internal/ignore/path_matcher_test.go (about)

     1  package ignore
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/windmilleng/tilt/internal/testutils/tempdir"
    11  	"github.com/windmilleng/tilt/pkg/model"
    12  )
    13  
    14  type FakeTarget struct {
    15  	path         string
    16  	dockerignore string
    17  }
    18  
    19  func (t FakeTarget) LocalRepos() []model.LocalGitRepo {
    20  	return []model.LocalGitRepo{
    21  		model.LocalGitRepo{LocalPath: t.path},
    22  	}
    23  }
    24  
    25  func (t FakeTarget) Dockerignores() []model.Dockerignore {
    26  	return []model.Dockerignore{
    27  		model.Dockerignore{
    28  			LocalPath: t.path,
    29  			Contents:  t.dockerignore,
    30  		},
    31  	}
    32  }
    33  
    34  func (t FakeTarget) TiltFilename() string {
    35  	return filepath.Join(t.path, "Tiltfile")
    36  }
    37  
    38  func (t FakeTarget) IgnoredLocalDirectories() []string {
    39  	return nil
    40  }
    41  
    42  type ignoreTestCase struct {
    43  	target               FakeTarget
    44  	change               string
    45  	ignoreInBuildContext bool
    46  	ignoreInFileChange   bool
    47  }
    48  
    49  func TestIgnores(t *testing.T) {
    50  	f := tempdir.NewTempDirFixture(t)
    51  	defer f.TearDown()
    52  
    53  	target := FakeTarget{
    54  		path: f.Path(),
    55  	}
    56  	targetWithIgnores := FakeTarget{
    57  		path:         f.Path(),
    58  		dockerignore: "**/ignored.txt",
    59  	}
    60  
    61  	cases := []ignoreTestCase{
    62  		{
    63  			target:               target,
    64  			change:               "x.txt",
    65  			ignoreInBuildContext: false,
    66  			ignoreInFileChange:   false,
    67  		},
    68  		{
    69  			target:               target,
    70  			change:               ".git/index",
    71  			ignoreInBuildContext: true,
    72  			ignoreInFileChange:   true,
    73  		},
    74  		{
    75  			target:               target,
    76  			change:               "ignored.txt",
    77  			ignoreInBuildContext: false,
    78  			ignoreInFileChange:   false,
    79  		},
    80  		{
    81  			target:               targetWithIgnores,
    82  			change:               "x.txt",
    83  			ignoreInBuildContext: false,
    84  			ignoreInFileChange:   false,
    85  		},
    86  		{
    87  			target:               targetWithIgnores,
    88  			change:               "ignored.txt",
    89  			ignoreInBuildContext: true,
    90  			ignoreInFileChange:   true,
    91  		},
    92  		{
    93  			target:               target,
    94  			change:               "dir/my-machine.yaml___jb_old___",
    95  			ignoreInBuildContext: false,
    96  			ignoreInFileChange:   true,
    97  		},
    98  		{
    99  			target:               target,
   100  			change:               "dir/.my-machine.yaml.swp",
   101  			ignoreInBuildContext: false,
   102  			ignoreInFileChange:   true,
   103  		},
   104  		{
   105  			target:               target,
   106  			change:               "dir/.my-machine.yaml.swn",
   107  			ignoreInBuildContext: false,
   108  			ignoreInFileChange:   true,
   109  		},
   110  		{
   111  			target:               target,
   112  			change:               "dir/.my-machine.yaml.swx",
   113  			ignoreInBuildContext: false,
   114  			ignoreInFileChange:   true,
   115  		},
   116  		{
   117  			target:               target,
   118  			change:               "dir/.#my-machine.yaml.swx",
   119  			ignoreInBuildContext: false,
   120  			ignoreInFileChange:   true,
   121  		},
   122  	}
   123  
   124  	for i, c := range cases {
   125  		t.Run(fmt.Sprintf("TestIgnores%d", i), func(t *testing.T) {
   126  			target := c.target
   127  			change := filepath.Join(f.Path(), c.change)
   128  
   129  			ctxFilter := CreateBuildContextFilter(target)
   130  			actual, err := ctxFilter.Matches(change)
   131  			if err != nil {
   132  				t.Fatal(err)
   133  			}
   134  
   135  			assert.Equal(t, c.ignoreInBuildContext, actual)
   136  
   137  			changeFilter, err := CreateFileChangeFilter(target)
   138  			if err != nil {
   139  				t.Fatal(err)
   140  			}
   141  
   142  			actual, err = changeFilter.Matches(change)
   143  			if err != nil {
   144  				t.Fatal(err)
   145  			}
   146  
   147  			assert.Equal(t, c.ignoreInFileChange, actual)
   148  		})
   149  	}
   150  }