| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package kio |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 15 |
) |
| 16 |
|
| 17 |
func TestIgnoreFilesMatcher_readIgnoreFile(t *testing.T) { |
| 18 |
testCases := []struct { |
| 19 |
name string |
| 20 |
writeIgnoreFile bool |
| 21 |
isMatch bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "has .krmignore file", |
| 25 |
writeIgnoreFile: true, |
| 26 |
isMatch: true, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "no .krmignore file", |
| 30 |
writeIgnoreFile: false, |
| 31 |
isMatch: false, |
| 32 |
}, |
| 33 |
} |
| 34 |
|
| 35 |
const ( |
| 36 |
ignoreFileName = ".krmignore" |
| 37 |
testFileName = "testfile.yaml" |
| 38 |
ignoreFileBody = "\n" + testFileName + "\n" |
| 39 |
) |
| 40 |
|
| 41 |
fsMakers := map[string]func(bool) (string, filesys.FileSystem){ |
| 42 |
|
| 43 |
|
| 44 |
"onDisk": func(writeIgnoreFile bool) (string, filesys.FileSystem) { |
| 45 |
dir := t.TempDir() |
| 46 |
|
| 47 |
if writeIgnoreFile { |
| 48 |
ignoreFilePath := filepath.Join(dir, ignoreFileName) |
| 49 |
require.NoError(t, os.WriteFile(ignoreFilePath, []byte(ignoreFileBody), 0600)) |
| 50 |
} |
| 51 |
testFilePath := filepath.Join(dir, testFileName) |
| 52 |
require.NoError(t, os.WriteFile(testFilePath, []byte{}, 0600)) |
| 53 |
return dir, nil |
| 54 |
}, |
| 55 |
|
| 56 |
|
| 57 |
"inMem": func(writeIgnoreFile bool) (string, filesys.FileSystem) { |
| 58 |
fs := filesys.MakeEmptyDirInMemory() |
| 59 |
if writeIgnoreFile { |
| 60 |
require.NoError(t, fs.WriteFile(ignoreFileName, []byte(ignoreFileBody))) |
| 61 |
} |
| 62 |
require.NoError(t, fs.WriteFile(testFileName, nil)) |
| 63 |
return ".", fs |
| 64 |
}, |
| 65 |
} |
| 66 |
|
| 67 |
for name, fsMaker := range fsMakers { |
| 68 |
t.Run(name, func(t *testing.T) { |
| 69 |
fsMaker := fsMaker |
| 70 |
for i := range testCases { |
| 71 |
test := testCases[i] |
| 72 |
dir, fs := fsMaker(test.writeIgnoreFile) |
| 73 |
t.Run(test.name, func(t *testing.T) { |
| 74 |
m := ignoreFilesMatcher{} |
| 75 |
m.fs.Set(fs) |
| 76 |
require.NoError(t, m.readIgnoreFile(dir)) |
| 77 |
require.Equal(t, test.isMatch, m.matchFile(filepath.Join(dir, testFileName))) |
| 78 |
}) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
var ( |
| 85 |
readFileA = []byte(` |
| 86 |
a: a |
| 87 |
--- |
| 88 |
c: c |
| 89 |
`) |
| 90 |
readFileB = []byte(` |
| 91 |
b: b |
| 92 |
`) |
| 93 |
) |
| 94 |
|
| 95 |
func TestLocalPackageReader_Read_ignoreFile(t *testing.T) { |
| 96 |
testCases := []struct { |
| 97 |
name string |
| 98 |
directories []string |
| 99 |
files map[string][]byte |
| 100 |
expected []string |
| 101 |
}{ |
| 102 |
{ |
| 103 |
name: "ignore file", |
| 104 |
directories: []string{ |
| 105 |
filepath.Join("a", "b"), |
| 106 |
filepath.Join("a", "c"), |
| 107 |
}, |
| 108 |
files: map[string][]byte{ |
| 109 |
"pkgFile": {}, |
| 110 |
filepath.Join("a", "b", "a_test.yaml"): readFileA, |
| 111 |
filepath.Join("a", "c", "c_test.yaml"): readFileB, |
| 112 |
".krmignore": []byte(` |
| 113 |
a/c/c_test.yaml |
| 114 |
`, |
| 115 |
), |
| 116 |
}, |
| 117 |
expected: []string{ |
| 118 |
`a: a`, |
| 119 |
`c: c`, |
| 120 |
}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "ignore folder", |
| 124 |
directories: []string{ |
| 125 |
filepath.Join("a", "b"), |
| 126 |
filepath.Join("a", "c"), |
| 127 |
}, |
| 128 |
files: map[string][]byte{ |
| 129 |
"pkgFile": {}, |
| 130 |
filepath.Join("a", "b", "a_test.yaml"): readFileA, |
| 131 |
filepath.Join("a", "c", "c_test.yaml"): readFileB, |
| 132 |
".krmignore": []byte(` |
| 133 |
a/c |
| 134 |
`, |
| 135 |
), |
| 136 |
}, |
| 137 |
expected: []string{ |
| 138 |
`a: a`, |
| 139 |
`c: c`, |
| 140 |
}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "krmignore file in subpackage", |
| 144 |
directories: []string{ |
| 145 |
filepath.Join("a", "c"), |
| 146 |
}, |
| 147 |
files: map[string][]byte{ |
| 148 |
"pkgFile": {}, |
| 149 |
filepath.Join("a", "c", "a_test.yaml"): readFileA, |
| 150 |
filepath.Join("a", "c", "c_test.yaml"): readFileB, |
| 151 |
".krmignore": []byte(` |
| 152 |
d/e/f.yaml |
| 153 |
`, |
| 154 |
), |
| 155 |
filepath.Join("a", "c", "pkgFile"): {}, |
| 156 |
filepath.Join("a", "c", ".krmignore"): []byte(` |
| 157 |
a_test.yaml |
| 158 |
`), |
| 159 |
}, |
| 160 |
expected: []string{ |
| 161 |
`b: b`, |
| 162 |
}, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "krmignore files does not affect subpackages", |
| 166 |
directories: []string{ |
| 167 |
filepath.Join("a", "c"), |
| 168 |
}, |
| 169 |
files: map[string][]byte{ |
| 170 |
"pkgFile": {}, |
| 171 |
filepath.Join("a", "c", "a_test.yaml"): readFileA, |
| 172 |
filepath.Join("a", "c", "c_test.yaml"): readFileB, |
| 173 |
".krmignore": []byte(` |
| 174 |
a/c/c_test.yaml |
| 175 |
`, |
| 176 |
), |
| 177 |
filepath.Join("a", "c", "pkgFile"): {}, |
| 178 |
filepath.Join("a", "c", ".krmignore"): []byte(` |
| 179 |
a_test.yaml |
| 180 |
`), |
| 181 |
}, |
| 182 |
expected: []string{ |
| 183 |
`b: b`, |
| 184 |
}, |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "handles a combination of packages and directories", |
| 188 |
directories: []string{ |
| 189 |
"a", |
| 190 |
filepath.Join("d", "e"), |
| 191 |
"f", |
| 192 |
}, |
| 193 |
files: map[string][]byte{ |
| 194 |
"pkgFile": {}, |
| 195 |
filepath.Join("d", "pkgFile"): {}, |
| 196 |
filepath.Join("d", "e", "pkgFile"): {}, |
| 197 |
filepath.Join("f", "pkgFile"): {}, |
| 198 |
"manifest.yaml": []byte(`root: root`), |
| 199 |
filepath.Join("a", "manifest.yaml"): []byte(`a: a`), |
| 200 |
filepath.Join("d", "manifest.yaml"): []byte(`d: d`), |
| 201 |
filepath.Join("d", "e", "manifest.yaml"): []byte(`e: e`), |
| 202 |
filepath.Join("f", "manifest.yaml"): []byte(`f: f`), |
| 203 |
filepath.Join("d", ".krmignore"): []byte(` |
| 204 |
manifest.yaml |
| 205 |
`), |
| 206 |
}, |
| 207 |
expected: []string{ |
| 208 |
`a: a`, |
| 209 |
`e: e`, |
| 210 |
`f: f`, |
| 211 |
`root: root`, |
| 212 |
}, |
| 213 |
}, |
| 214 |
{ |
| 215 |
name: "ignore file can exclude subpackages", |
| 216 |
directories: []string{ |
| 217 |
"a", |
| 218 |
}, |
| 219 |
files: map[string][]byte{ |
| 220 |
"pkgFile": {}, |
| 221 |
filepath.Join("a", "pkgFile"): {}, |
| 222 |
"manifest.yaml": []byte(`root: root`), |
| 223 |
filepath.Join("a", "manifest.yaml"): []byte(`a: a`), |
| 224 |
".krmignore": []byte(` |
| 225 |
a |
| 226 |
`), |
| 227 |
}, |
| 228 |
expected: []string{ |
| 229 |
`root: root`, |
| 230 |
}, |
| 231 |
}, |
| 232 |
} |
| 233 |
|
| 234 |
for i := range testCases { |
| 235 |
test := testCases[i] |
| 236 |
t.Run(test.name, func(t *testing.T) { |
| 237 |
s := SetupDirectories(t, test.directories...) |
| 238 |
defer s.Clean() |
| 239 |
for path, content := range test.files { |
| 240 |
s.WriteFile(t, path, content) |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
rfr := LocalPackageReader{ |
| 245 |
PackagePath: s.Root, |
| 246 |
IncludeSubpackages: true, |
| 247 |
PackageFileName: "pkgFile", |
| 248 |
OmitReaderAnnotations: true, |
| 249 |
} |
| 250 |
nodes, err := rfr.Read() |
| 251 |
if !assert.NoError(t, err) { |
| 252 |
assert.FailNow(t, err.Error()) |
| 253 |
} |
| 254 |
|
| 255 |
if !assert.Len(t, nodes, len(test.expected)) { |
| 256 |
assert.FailNow(t, "wrong number items") |
| 257 |
} |
| 258 |
|
| 259 |
for i, node := range nodes { |
| 260 |
val, err := node.String() |
| 261 |
require.NoError(t, err) |
| 262 |
want := strings.ReplaceAll(test.expected[i], "${SEP}", string(filepath.Separator)) |
| 263 |
assert.Equal(t, strings.TrimSpace(want), strings.TrimSpace(val)) |
| 264 |
} |
| 265 |
}) |
| 266 |
} |
| 267 |
} |