github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/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/tilt-dev/tilt/internal/testutils/tempdir" 11 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1" 12 ) 13 14 type FakeTarget struct { 15 path string 16 dockerignorePatterns []string 17 } 18 19 func (t FakeTarget) GetIgnores() []v1alpha1.IgnoreDef { 20 result := []v1alpha1.IgnoreDef{ 21 {BasePath: filepath.Join(t.path, "Tiltfile")}, 22 {BasePath: filepath.Join(t.path, ".git")}, 23 } 24 25 if len(t.dockerignorePatterns) != 0 { 26 result = append(result, v1alpha1.IgnoreDef{BasePath: t.path, Patterns: t.dockerignorePatterns}) 27 } 28 return result 29 } 30 31 type ignoreTestCase struct { 32 target FakeTarget 33 change string 34 ignoreInBuildContext bool 35 ignoreInFileChange bool 36 } 37 38 func TestIgnores(t *testing.T) { 39 f := tempdir.NewTempDirFixture(t) 40 41 target := FakeTarget{ 42 path: f.Path(), 43 } 44 targetWithIgnores := FakeTarget{ 45 path: f.Path(), 46 dockerignorePatterns: []string{"**/ignored.txt"}, 47 } 48 49 cases := []ignoreTestCase{ 50 { 51 target: target, 52 change: "x.txt", 53 ignoreInBuildContext: false, 54 ignoreInFileChange: false, 55 }, 56 { 57 target: target, 58 change: ".git/index", 59 ignoreInBuildContext: true, 60 ignoreInFileChange: true, 61 }, 62 { 63 target: target, 64 change: "ignored.txt", 65 ignoreInBuildContext: false, 66 ignoreInFileChange: false, 67 }, 68 { 69 target: targetWithIgnores, 70 change: "x.txt", 71 ignoreInBuildContext: false, 72 ignoreInFileChange: false, 73 }, 74 { 75 target: targetWithIgnores, 76 change: "ignored.txt", 77 ignoreInBuildContext: true, 78 ignoreInFileChange: true, 79 }, 80 { 81 target: target, 82 change: "dir/my-machine.yaml___jb_old___", 83 ignoreInBuildContext: false, 84 ignoreInFileChange: true, 85 }, 86 { 87 target: target, 88 change: "dir/.my-machine.yaml.swp", 89 ignoreInBuildContext: false, 90 ignoreInFileChange: true, 91 }, 92 { 93 target: target, 94 change: "dir/.my-machine.yaml.swn", 95 ignoreInBuildContext: false, 96 ignoreInFileChange: true, 97 }, 98 { 99 target: target, 100 change: "dir/.my-machine.yaml.swx", 101 ignoreInBuildContext: false, 102 ignoreInFileChange: true, 103 }, 104 { 105 target: target, 106 change: "dir/.#my-machine.yaml.swx", 107 ignoreInBuildContext: false, 108 ignoreInFileChange: true, 109 }, 110 { 111 target: target, 112 change: "dir/#my-machine#", 113 ignoreInBuildContext: false, 114 ignoreInFileChange: true, 115 }, 116 } 117 118 for i, c := range cases { 119 t.Run(fmt.Sprintf("TestIgnores%d", i), func(t *testing.T) { 120 target := c.target 121 change := filepath.Join(f.Path(), c.change) 122 123 ctxFilter := CreateBuildContextFilter(target.GetIgnores()) 124 actual, err := ctxFilter.Matches(change) 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 assert.Equal(t, c.ignoreInBuildContext, actual) 130 131 changeFilter := CreateFileChangeFilter(target.GetIgnores()) 132 actual, err = changeFilter.Matches(change) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 assert.Equal(t, c.ignoreInFileChange, actual) 138 }) 139 } 140 }