mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
All LocalPackageReader tests adapted, one fails
This commit is contained in:
@@ -214,11 +214,11 @@ func (r LocalPackageReader) Read() ([]*yaml.RNode, error) {
|
|||||||
ignoreFilesMatcher := &ignoreFilesMatcher{
|
ignoreFilesMatcher := &ignoreFilesMatcher{
|
||||||
fs: r.FileSystem,
|
fs: r.FileSystem,
|
||||||
}
|
}
|
||||||
dir, _, err := r.FileSystem.CleanedAbs(r.PackagePath)
|
dir, file, err := r.FileSystem.CleanedAbs(r.PackagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err)
|
return nil, errors.Wrap(err)
|
||||||
}
|
}
|
||||||
r.PackagePath = string(dir)
|
r.PackagePath = filepath.Join(string(dir), file)
|
||||||
err = r.FileSystem.Walk(r.PackagePath, func(
|
err = r.FileSystem.Walk(r.PackagePath, func(
|
||||||
path string, info os.FileInfo, err error) error {
|
path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
. "sigs.k8s.io/kustomize/kyaml/kio"
|
. "sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
@@ -44,10 +43,9 @@ var pkgFile = []byte(``)
|
|||||||
func TestLocalPackageReader_Read_empty(t *testing.T) {
|
func TestLocalPackageReader_Read_empty(t *testing.T) {
|
||||||
var r LocalPackageReader
|
var r LocalPackageReader
|
||||||
nodes, err := r.Read()
|
nodes, err := r.Read()
|
||||||
if assert.Error(t, err) {
|
require.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "must specify package path")
|
require.Contains(t, err.Error(), "must specify package path")
|
||||||
}
|
require.Nil(t, nodes)
|
||||||
assert.Nil(t, nodes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_pkg(t *testing.T) {
|
func TestLocalPackageReader_Read_pkg(t *testing.T) {
|
||||||
@@ -77,8 +75,10 @@ func TestLocalPackageReader_Read_pkg(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testLocalPackageReaderReadPkg(t *testing.T, path string, mockFS filesys.FileSystem) {
|
func testLocalPackageReaderReadPkg(t *testing.T, path string, mockFS filesys.FileSystem) {
|
||||||
rfr := LocalPackageReader{PackagePath: path}
|
rfr := LocalPackageReader{
|
||||||
rfr.FileSystem.Set(mockFS)
|
PackagePath: path,
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
|
}
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, nodes, 5)
|
require.Len(t, nodes, 5)
|
||||||
@@ -154,20 +154,13 @@ func TestLocalPackageReader_Read_pkgAndSkipFile(t *testing.T) {
|
|||||||
|
|
||||||
func testLocalPackageReaderReadPkgAndSkipFile(t *testing.T, path string, mockFS filesys.FileSystem) {
|
func testLocalPackageReaderReadPkgAndSkipFile(t *testing.T, path string, mockFS filesys.FileSystem) {
|
||||||
rfr := LocalPackageReader{
|
rfr := LocalPackageReader{
|
||||||
PackagePath: path,
|
PackagePath: path,
|
||||||
FileSkipFunc: func(relPath string) bool {
|
FileSkipFunc: func(relPath string) bool { return relPath == "d_test.yaml" },
|
||||||
return relPath == "d_test.yaml"
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
},
|
|
||||||
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
|
||||||
}
|
}
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
return
|
require.Len(t, nodes, 4)
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 4) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
expected := []string{
|
expected := []string{
|
||||||
`a: b #first
|
`a: b #first
|
||||||
metadata:
|
metadata:
|
||||||
@@ -240,7 +233,10 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testLocalPackageReaderReadJSON(t *testing.T, path string, mockFS filesys.FileSystem) {
|
func testLocalPackageReaderReadJSON(t *testing.T, path string, mockFS filesys.FileSystem) {
|
||||||
rfr := LocalPackageReader{PackagePath: path, MatchFilesGlob: []string{"*.json"}}
|
rfr := LocalPackageReader{
|
||||||
|
PackagePath: path,
|
||||||
|
MatchFilesGlob: []string{"*.json"},
|
||||||
|
}
|
||||||
rfr.FileSystem.Set(mockFS)
|
rfr.FileSystem.Set(mockFS)
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -310,92 +306,103 @@ metadata:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) {
|
func TestLocalPackageReader_Read_pkgOmitAnnotations(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||||
|
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||||
|
|
||||||
paths := []struct {
|
testLocalPackageReaderReadPkgOmitAnnotations(t, "./", nil)
|
||||||
path string
|
testLocalPackageReaderReadPkgOmitAnnotations(t, s.Root, nil)
|
||||||
}{
|
})
|
||||||
{path: "./"},
|
|
||||||
{path: s.Root},
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
OmitReaderAnnotations: true,
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
}
|
}
|
||||||
for _, p := range paths {
|
nodes, err := rfr.Read()
|
||||||
// empty path
|
require.NoError(t, err)
|
||||||
rfr := LocalPackageReader{PackagePath: p.path, OmitReaderAnnotations: true}
|
require.Len(t, nodes, 3)
|
||||||
nodes, err := rfr.Read()
|
expected := []string{
|
||||||
if !assert.NoError(t, err) {
|
`a: b #first
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 3) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
expected := []string{
|
|
||||||
`a: b #first
|
|
||||||
`,
|
`,
|
||||||
`c: d # second
|
`c: d # second
|
||||||
`,
|
`,
|
||||||
`# second thing
|
`# second thing
|
||||||
e: f
|
e: f
|
||||||
g:
|
g:
|
||||||
h:
|
h:
|
||||||
- i # has a list
|
- i # has a list
|
||||||
- j
|
- j
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
val, err := nodes[i].String()
|
val, err := nodes[i].String()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
return
|
require.Equal(t, expected[i], val)
|
||||||
}
|
|
||||||
if !assert.Equal(t, expected[i], val) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_PreserveSeqIndent(t *testing.T) {
|
func TestLocalPackageReader_Read_PreserveSeqIndent(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a_test.yaml"), readFileA)
|
||||||
|
s.WriteFile(t, filepath.Join("b_test.yaml"), readFileB)
|
||||||
|
|
||||||
paths := []struct {
|
testLocalPackageReaderReadPreserveSeqIndent(t, "./", nil)
|
||||||
path string
|
testLocalPackageReaderReadPreserveSeqIndent(t, s.Root, nil)
|
||||||
}{
|
})
|
||||||
{path: "./"},
|
|
||||||
{path: s.Root},
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
PreserveSeqIndent: true,
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
}
|
}
|
||||||
for _, p := range paths {
|
nodes, err := rfr.Read()
|
||||||
// empty path
|
require.NoError(t, err)
|
||||||
rfr := LocalPackageReader{PackagePath: p.path, PreserveSeqIndent: true}
|
require.Len(t, nodes, 3)
|
||||||
nodes, err := rfr.Read()
|
expected := []string{
|
||||||
if !assert.NoError(t, err) {
|
`a: b #first
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 3) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
expected := []string{
|
|
||||||
`a: b #first
|
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
config.kubernetes.io/path: 'a_test.yaml'
|
config.kubernetes.io/path: 'a_test.yaml'
|
||||||
internal.config.kubernetes.io/seqindent: 'compact'
|
internal.config.kubernetes.io/seqindent: 'compact'
|
||||||
`,
|
`,
|
||||||
`c: d # second
|
`c: d # second
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
config.kubernetes.io/path: 'a_test.yaml'
|
config.kubernetes.io/path: 'a_test.yaml'
|
||||||
internal.config.kubernetes.io/seqindent: 'compact'
|
internal.config.kubernetes.io/seqindent: 'compact'
|
||||||
`,
|
`,
|
||||||
`# second thing
|
`# second thing
|
||||||
e: f
|
e: f
|
||||||
g:
|
g:
|
||||||
h:
|
h:
|
||||||
@@ -407,56 +414,58 @@ metadata:
|
|||||||
config.kubernetes.io/path: 'b_test.yaml'
|
config.kubernetes.io/path: 'b_test.yaml'
|
||||||
internal.config.kubernetes.io/seqindent: 'compact'
|
internal.config.kubernetes.io/seqindent: 'compact'
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
val, err := nodes[i].String()
|
val, err := nodes[i].String()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
return
|
require.Equal(t, expected[i], val)
|
||||||
}
|
|
||||||
if !assert.Equal(t, expected[i], val) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
|
func TestLocalPackageReader_Read_nestedDirs(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||||
|
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||||
|
|
||||||
paths := []struct {
|
testLocalPackageReaderReadNestedDirs(t, "./", nil)
|
||||||
path string
|
testLocalPackageReaderReadNestedDirs(t, s.Root, nil)
|
||||||
}{
|
})
|
||||||
{path: "./"},
|
|
||||||
{path: s.Root},
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
}
|
}
|
||||||
for _, p := range paths {
|
nodes, err := rfr.Read()
|
||||||
// empty path
|
require.NoError(t, err)
|
||||||
rfr := LocalPackageReader{PackagePath: p.path}
|
require.Len(t, nodes, 3)
|
||||||
nodes, err := rfr.Read()
|
expected := []string{
|
||||||
if !assert.NoError(t, err) {
|
`a: b #first
|
||||||
assert.FailNow(t, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 3) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
expected := []string{
|
|
||||||
`a: b #first
|
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml'
|
config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml'
|
||||||
`,
|
`,
|
||||||
`c: d # second
|
`c: d # second
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
config.kubernetes.io/index: '1'
|
config.kubernetes.io/index: '1'
|
||||||
config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml'
|
config.kubernetes.io/path: 'a${SEP}b${SEP}a_test.yaml'
|
||||||
`,
|
`,
|
||||||
`# second thing
|
`# second thing
|
||||||
e: f
|
e: f
|
||||||
g:
|
g:
|
||||||
h:
|
h:
|
||||||
@@ -467,36 +476,46 @@ metadata:
|
|||||||
config.kubernetes.io/index: '0'
|
config.kubernetes.io/index: '0'
|
||||||
config.kubernetes.io/path: 'a${SEP}b${SEP}b_test.yaml'
|
config.kubernetes.io/path: 'a${SEP}b${SEP}b_test.yaml'
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
for i := range nodes {
|
for i := range nodes {
|
||||||
val, err := nodes[i].String()
|
val, err := nodes[i].String()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
return
|
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
||||||
}
|
require.Equal(t, want, val)
|
||||||
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
|
||||||
if !assert.Equal(t, want, val) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
|
func TestLocalPackageReader_Read_matchRegex(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||||
|
s.WriteFile(t, filepath.Join("a", "b", "b_test.yaml"), readFileB)
|
||||||
|
|
||||||
// empty path
|
testLocalPackageReaderReadMatchRegex(t, "./", nil)
|
||||||
rfr := LocalPackageReader{PackagePath: s.Root, MatchFilesGlob: []string{`a*.yaml`}}
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
MatchFilesGlob: []string{`a*.yaml`},
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
|
}
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
assert.FailNow(t, err.Error())
|
require.Len(t, nodes, 2)
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 2) {
|
|
||||||
assert.FailNow(t, "wrong number items")
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := []string{
|
expected := []string{
|
||||||
`a: b #first
|
`a: b #first
|
||||||
@@ -515,29 +534,45 @@ metadata:
|
|||||||
|
|
||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
val, err := node.String()
|
val, err := node.String()
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
||||||
assert.Equal(t, want, val)
|
require.Equal(t, want, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
|
func TestLocalPackageReader_Read_skipSubpackage(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||||
|
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||||
|
|
||||||
// empty path
|
testLocalPackageReaderReadSkipSubpackage(t, "./", nil)
|
||||||
rfr := LocalPackageReader{PackagePath: s.Root, PackageFileName: "pkgFile"}
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
PackageFileName: "pkgFile",
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
|
}
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
assert.FailNow(t, err.Error())
|
require.Len(t, nodes, 2)
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 2) {
|
|
||||||
assert.FailNow(t, "wrong number items")
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := []string{
|
expected := []string{
|
||||||
`a: b #first
|
`a: b #first
|
||||||
@@ -556,29 +591,46 @@ metadata:
|
|||||||
|
|
||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
val, err := node.String()
|
val, err := node.String()
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
||||||
assert.Equal(t, want, val)
|
require.Equal(t, want, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
|
func TestLocalPackageReader_Read_includeSubpackage(t *testing.T) {
|
||||||
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
t.Run("on_disk", func(t *testing.T) {
|
||||||
defer s.Clean()
|
s := SetupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
defer s.Clean()
|
||||||
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
s.WriteFile(t, filepath.Join("a", "b", "a_test.yaml"), readFileA)
|
||||||
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
s.WriteFile(t, filepath.Join("a", "c", "c_test.yaml"), readFileB)
|
||||||
|
s.WriteFile(t, filepath.Join("a", "c", "pkgFile"), pkgFile)
|
||||||
|
|
||||||
// empty path
|
testLocalPackageReaderReadIncludeSubpackage(t, "./", nil)
|
||||||
rfr := LocalPackageReader{PackagePath: s.Root, IncludeSubpackages: true, PackageFileName: "pkgFile"}
|
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{
|
||||||
|
PackagePath: path,
|
||||||
|
IncludeSubpackages: true,
|
||||||
|
PackageFileName: "pkgFile",
|
||||||
|
FileSystem: filesys.FileSystemOrOnDisk{FileSystem: mockFS},
|
||||||
|
}
|
||||||
nodes, err := rfr.Read()
|
nodes, err := rfr.Read()
|
||||||
if !assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
assert.FailNow(t, err.Error())
|
require.Len(t, nodes, 3)
|
||||||
}
|
|
||||||
|
|
||||||
if !assert.Len(t, nodes, 3) {
|
|
||||||
assert.FailNow(t, "wrong number items")
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := []string{
|
expected := []string{
|
||||||
`a: b #first
|
`a: b #first
|
||||||
@@ -608,9 +660,9 @@ metadata:
|
|||||||
|
|
||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
val, err := node.String()
|
val, err := node.String()
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
want := strings.ReplaceAll(expected[i], "${SEP}", string(filepath.Separator))
|
||||||
assert.Equal(t, want, val)
|
require.Equal(t, want, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user