Simplified tests with helper func

This commit is contained in:
Francesc Campoy
2021-07-16 11:06:32 -07:00
parent 927568eea2
commit be4fe7540e

View File

@@ -49,32 +49,14 @@ func TestLocalPackageReader_Read_empty(t *testing.T) {
} }
func TestLocalPackageReader_Read_pkg(t *testing.T) { func TestLocalPackageReader_Read_pkg(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, "a_test.yaml", readFileA) {path: "a_test.yaml", content: readFileA},
s.WriteFile(t, "b_test.yaml", readFileB) {path: "b_test.yaml", content: readFileB},
s.WriteFile(t, "c_test.yaml", readFileC) {path: "c_test.yaml", content: readFileC},
s.WriteFile(t, "d_test.yaml", readFileD) {path: "d_test.yaml", content: readFileD},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadPkg(t, "./", nil)
testLocalPackageReaderReadPkg(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.yaml", readFileA))
require.NoError(t, fs.WriteFile("b_test.yaml", readFileB))
require.NoError(t, fs.WriteFile("c_test.yaml", readFileC))
require.NoError(t, fs.WriteFile("d_test.yaml", readFileD))
testLocalPackageReaderReadPkg(t, "/", fs)
})
}
func testLocalPackageReaderReadPkg(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS}, FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
@@ -124,35 +106,18 @@ metadata:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_pkgAndSkipFile(t *testing.T) { func TestLocalPackageReader_Read_pkgAndSkipFile(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, "a_test.yaml", readFileA) {path: "a_test.yaml", content: readFileA},
s.WriteFile(t, "b_test.yaml", readFileB) {path: "b_test.yaml", content: readFileB},
s.WriteFile(t, "c_test.yaml", readFileC) {path: "c_test.yaml", content: readFileC},
s.WriteFile(t, "d_test.yaml", readFileD) {path: "d_test.yaml", content: readFileD},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadPkgAndSkipFile(t, "./", nil)
testLocalPackageReaderReadPkgAndSkipFile(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.yaml", readFileA))
require.NoError(t, fs.WriteFile("b_test.yaml", readFileB))
require.NoError(t, fs.WriteFile("c_test.yaml", readFileC))
require.NoError(t, fs.WriteFile("d_test.yaml", readFileD))
testLocalPackageReaderReadPkgAndSkipFile(t, "/", fs)
})
}
func testLocalPackageReaderReadPkgAndSkipFile(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
FileSkipFunc: func(relPath string) bool { return relPath == "d_test.yaml" }, FileSkipFunc: func(relPath string) bool { return relPath == "d_test.yaml" },
@@ -197,47 +162,30 @@ metadata:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_JSON(t *testing.T) { func TestLocalPackageReader_Read_JSON(t *testing.T) {
aTestJSON := []byte(`{ testOnDiskAndOnMem(t, []mockFile{
{path: "a/b"},
{path: "a/c"},
{path: "a_test.json", content: []byte(`{
"a": "b" "a": "b"
}`) }`),
bTestJSON := []byte(`{ },
{path: "b_test.json", content: []byte(`{
"e": "f", "e": "f",
"g": { "g": {
"h": ["i", "j"] "h": ["i", "j"]
} }
}`) }`),
},
t.Run("on_disk", func(t *testing.T) { }, func(t *testing.T, path string, mockFS filesys.FileSystem) {
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
defer s.Clean()
s.WriteFile(t, "a_test.json", aTestJSON)
s.WriteFile(t, "b_test.json", bTestJSON)
testLocalPackageReaderReadJSON(t, "./", nil)
testLocalPackageReaderReadJSON(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.json", aTestJSON))
require.NoError(t, fs.WriteFile("b_test.json", bTestJSON))
testLocalPackageReaderReadJSON(t, "/", fs)
})
}
func testLocalPackageReaderReadJSON(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
MatchFilesGlob: []string{"*.json"}, MatchFilesGlob: []string{"*.json"},
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
} }
rfr.FileSystem.Set(mockFS)
nodes, err := rfr.Read() nodes, err := rfr.Read()
require.NoError(t, err) require.NoError(t, err)
require.Len(t, nodes, 2) require.Len(t, nodes, 2)
@@ -252,31 +200,16 @@ func testLocalPackageReaderReadJSON(t *testing.T, path string, mockFS filesys.Fi
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_file(t *testing.T) { func TestLocalPackageReader_Read_file(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA) {path: "a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB) {path: "b_test.yaml", content: readFileB},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadFile(t, "./", nil)
testLocalPackageReaderReadFile(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.yaml", readFileA))
require.NoError(t, fs.WriteFile("b_test.yaml", readFileB))
testLocalPackageReaderReadFile(t, "/", fs)
})
}
func testLocalPackageReaderReadFile(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: filepath.Join(path, "a_test.yaml"), PackagePath: filepath.Join(path, "a_test.yaml"),
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS}, FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
@@ -303,31 +236,16 @@ metadata:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) { func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA) {path: "a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB) {path: "b_test.yaml", content: readFileB},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadPkgOmitAnnotations(t, "./", nil)
testLocalPackageReaderReadPkgOmitAnnotations(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.yaml", readFileA))
require.NoError(t, fs.WriteFile("b_test.yaml", readFileB))
testLocalPackageReaderReadPkgOmitAnnotations(t, "/", fs)
})
}
func testLocalPackageReaderReadPkgOmitAnnotations(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
OmitReaderAnnotations: true, OmitReaderAnnotations: true,
@@ -354,31 +272,16 @@ g:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_PreserveSeqIndent(t *testing.T) { func TestLocalPackageReader_Read_PreserveSeqIndent(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA) {path: "a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB) {path: "b_test.yaml", content: readFileB},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadPreserveSeqIndent(t, "./", nil)
testLocalPackageReaderReadPreserveSeqIndent(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile("a_test.yaml", readFileA))
require.NoError(t, fs.WriteFile("b_test.yaml", readFileB))
testLocalPackageReaderReadPreserveSeqIndent(t, "/", fs)
})
}
func testLocalPackageReaderReadPreserveSeqIndent(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
PreserveSeqIndent: true, PreserveSeqIndent: true,
@@ -420,31 +323,16 @@ metadata:
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expected[i], val) require.Equal(t, expected[i], val)
} }
})
} }
func TestLocalPackageReader_Read_nestedDirs(t *testing.T) { func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA) {path: "a/b/a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB) {path: "a/b/b_test.yaml", content: readFileB},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadNestedDirs(t, "./", nil)
testLocalPackageReaderReadNestedDirs(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "a_test.yaml"), readFileA))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "b_test.yaml"), readFileB))
testLocalPackageReaderReadNestedDirs(t, "/", fs)
})
}
func testLocalPackageReaderReadNestedDirs(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS}, FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
@@ -483,31 +371,16 @@ metadata:
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
require.Equal(t, want, val) require.Equal(t, want, val)
} }
})
} }
func TestLocalPackageReader_Read_matchRegex(t *testing.T) { func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA) {path: "a/b/a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB) {path: "a/b/b_test.yaml", content: readFileB},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadMatchRegex(t, "./", nil)
testLocalPackageReaderReadMatchRegex(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "a_test.yaml"), readFileA))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "b_test.yaml"), readFileB))
testLocalPackageReaderReadMatchRegex(t, "/", fs)
})
}
func testLocalPackageReaderReadMatchRegex(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
MatchFilesGlob: []string{`a*.yaml`}, MatchFilesGlob: []string{`a*.yaml`},
@@ -538,33 +411,17 @@ metadata:
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
require.Equal(t, want, val) require.Equal(t, want, val)
} }
})
} }
func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) { func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA) {path: "a/b/a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB) {path: "a/c/c_test.yaml", content: readFileB},
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile) {path: "a/c/pkgFile", content: pkgFile},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadSkipSubpackage(t, "./", nil)
testLocalPackageReaderReadSkipSubpackage(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "a_test.yaml"), readFileA))
require.NoError(t, fs.WriteFile(filepath.Join("a", "c", "c_test.yaml"), readFileB))
require.NoError(t, fs.WriteFile(filepath.Join("a", "c", "pkgFile"), pkgFile))
testLocalPackageReaderReadSkipSubpackage(t, "/", fs)
})
}
func testLocalPackageReaderReadSkipSubpackage(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
PackageFileName: "pkgFile", PackageFileName: "pkgFile",
@@ -595,33 +452,17 @@ metadata:
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
require.Equal(t, want, val) require.Equal(t, want, val)
} }
})
} }
func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) { func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
t.Run("on_disk", func(t *testing.T) { testOnDiskAndOnMem(t, []mockFile{
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c")) {path: "a/b"},
defer s.Clean() {path: "a/c"},
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA) {path: "a/b/a_test.yaml", content: readFileA},
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB) {path: "a/c/c_test.yaml", content: readFileB},
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile) {path: "a/c/pkgFile", content: pkgFile},
}, func(t *testing.T, path string, mockFS filesys.FileSystem) {
testLocalPackageReaderReadIncludeSubpackage(t, "./", nil)
testLocalPackageReaderReadIncludeSubpackage(t, s.Root, nil)
})
t.Run("on_mem", func(t *testing.T) {
fs := filesys.MakeFsInMemory()
require.NoError(t, fs.MkdirAll(filepath.Join("a", "b")))
require.NoError(t, fs.MkdirAll(filepath.Join("a", "c")))
require.NoError(t, fs.WriteFile(filepath.Join("a", "b", "a_test.yaml"), readFileA))
require.NoError(t, fs.WriteFile(filepath.Join("a", "c", "c_test.yaml"), readFileB))
require.NoError(t, fs.WriteFile(filepath.Join("a", "c", "pkgFile"), pkgFile))
testLocalPackageReaderReadIncludeSubpackage(t, "/", fs)
})
}
func testLocalPackageReaderReadIncludeSubpackage(t *testing.T, path string, mockFS filesys.FileSystem) {
rfr := LocalPackageReader{ rfr := LocalPackageReader{
PackagePath: path, PackagePath: path,
IncludeSubpackages: true, IncludeSubpackages: true,
@@ -664,53 +505,47 @@ metadata:
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator)) want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
require.Equal(t, want, val) require.Equal(t, want, val)
} }
})
} }
// func TestLocalPackageReaderWriter_DeleteFiles(t *testing.T) { type mockFile struct {
// g, _, clean := testutil.SetupDefaultRepoAndWorkspace(t) path string
// defer clean() // nil content implies this is a directory
// if !assert.NoError(t, os.Chdir(g.RepoDirectory)) { content []byte
// return }
// }
// func testOnDiskAndOnMem(t *testing.T, files []mockFile, f func(t *testing.T, path string, fs filesys.FileSystem)) {
// rw := LocalPackageReadWriter{PackagePath: "."} t.Run("on_disk", func(t *testing.T) {
// nodes, err := rw.Read() var dirs []string
// if !assert.NoError(t, err) { for _, file := range files {
// t.FailNow() if file.content == nil {
// } dirs = append(dirs, filepath.FromSlash(file.path))
// _, err = os.Stat(filepath.Join("java", "java-deployment.resource.yaml")) }
// if !assert.NoError(t, err) { }
// t.FailNow()
// } s := SetupDirectories(t, dirs...)
// defer s.Clean()
// // delete one of the nodes for _, file := range files {
// var newNodes []*yaml.RNode if file.content != nil {
// for i := range nodes { s.WriteFile(t, filepath.FromSlash(file.path), file.content)
// meta, err := nodes[i].GetMeta() }
// if !assert.NoError(t, err) { }
// t.FailNow()
// } f(t, "./", nil)
// if meta.Name == "app" && meta.Kind == "Deployment" { f(t, s.Root, nil)
// continue })
// }
// newNodes = append(newNodes, nodes[i]) t.Run("on_mem", func(t *testing.T) {
// } fs := filesys.MakeFsInMemory()
// for _, file := range files {
// if !assert.NoError(t, rw.Write(newNodes)) { path := filepath.FromSlash(file.path)
// t.FailNow() if file.content == nil {
// } require.NoError(t, fs.MkdirAll(path))
// } else {
// _, err = os.Stat(filepath.Join("java", "java-deployment.resource.yaml")) require.NoError(t, fs.WriteFile(path, file.content))
// if !assert.Error(t, err) { }
// t.FailNow() }
// }
// f(t, "/", fs)
// diff, err := copyutil.Diff(filepath.Join(g.DatasetDirectory, testutil.Dataset1), ".") })
// if !assert.NoError(t, err) { }
// t.FailNow()
// }
//
// assert.ElementsMatch(t,
// diff.List(),
// []string{filepath.Join("java", "java-deployment.resource.yaml")})
// }