Delete some unnecessary parameter passing.

This commit is contained in:
Jeffrey Regan
2018-10-18 10:59:46 -07:00
parent 66bbae586f
commit 2b0e2725f9
24 changed files with 65 additions and 112 deletions

View File

@@ -23,7 +23,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -70,8 +69,8 @@ func (o *addBaseOptions) Complete(cmd *cobra.Command, args []string) error {
} }
// RunAddBase runs addBase command (do real work). // RunAddBase runs addBase command (do real work).
func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error { func (o *addBaseOptions) RunAddBase(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }
@@ -84,7 +83,7 @@ func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
// split directory paths // split directory paths
paths := strings.Split(o.baseDirectoryPaths, ",") paths := strings.Split(o.baseDirectoryPaths, ",")
for _, path := range paths { for _, path := range paths {
if !fsys.Exists(path) { if !fSys.Exists(path) {
return errors.New(path + " does not exist") return errors.New(path + " does not exist")
} }
if kustfile.StringInSlice(path, m.Bases) { if kustfile.StringInSlice(path, m.Bases) {

View File

@@ -21,7 +21,6 @@ import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -43,7 +42,7 @@ func TestAddBaseHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -92,7 +92,7 @@ func (o *addMetadataOptions) runE(
if err != nil { if err != nil {
return err return err
} }
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys) kf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -20,7 +20,6 @@ import (
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
"sigs.k8s.io/kustomize/pkg/validators" "sigs.k8s.io/kustomize/pkg/validators"
@@ -29,7 +28,7 @@ import (
func makeKustomization(t *testing.T) *types.Kustomization { func makeKustomization(t *testing.T) *types.Kustomization {
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomization() fakeFS.WriteTestKustomization()
kf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fakeFS) kf, err := kustfile.NewKustomizationFile(fakeFS)
if err != nil { if err != nil {
t.Errorf("unexpected new error %v", err) t.Errorf("unexpected new error %v", err)
} }

View File

@@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/patch"
) )
@@ -70,8 +69,8 @@ func (o *addPatchOptions) Complete(cmd *cobra.Command, args []string) error {
} }
// RunAddPatch runs addPatch command (do real work). // RunAddPatch runs addPatch command (do real work).
func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error { func (o *addPatchOptions) RunAddPatch(fSys fs.FileSystem) error {
patches, err := globPatterns(fsys, o.patchFilePaths) patches, err := globPatterns(fSys, o.patchFilePaths)
if err != nil { if err != nil {
return err return err
} }
@@ -79,7 +78,7 @@ func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
return nil return nil
} }
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -21,7 +21,6 @@ import (
"strings" "strings"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -45,7 +44,7 @@ func TestAddPatchHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -69,8 +68,8 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
} }
// RunAddResource runs addResource command (do real work). // RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error { func (o *addResourceOptions) RunAddResource(fSys fs.FileSystem) error {
resources, err := globPatterns(fsys, o.resourceFilePaths) resources, err := globPatterns(fSys, o.resourceFilePaths)
if err != nil { if err != nil {
return err return err
} }
@@ -78,7 +77,7 @@ func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
return nil return nil
} }
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -20,7 +20,6 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -44,7 +43,7 @@ func TestAddResourceHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
@@ -57,7 +56,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.
} }
// Load the kustomization file. // Load the kustomization file.
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fSys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -21,7 +21,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -73,8 +72,8 @@ func (o *setNamePrefixOptions) Complete(cmd *cobra.Command, args []string) error
} }
// RunSetNamePrefix runs setNamePrefix command (does real work). // RunSetNamePrefix runs setNamePrefix command (does real work).
func (o *setNamePrefixOptions) RunSetNamePrefix(fsys fs.FileSystem) error { func (o *setNamePrefixOptions) RunSetNamePrefix(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -20,7 +20,6 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -38,7 +37,7 @@ func TestSetNamePrefixHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -24,7 +24,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
@@ -99,8 +98,8 @@ func (o *setImageTagOptions) Validate(args []string) error {
} }
// RunSetImageTags runs setImageTags command (does real work). // RunSetImageTags runs setImageTags command (does real work).
func (o *setImageTagOptions) RunSetImageTags(fsys fs.FileSystem) error { func (o *setImageTagOptions) RunSetImageTags(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -20,7 +20,6 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
) )
@@ -35,7 +34,7 @@ func TestSetImageTagsHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }
@@ -70,7 +69,7 @@ func TestSetImageTagsOverride(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -23,7 +23,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
) )
@@ -72,8 +71,8 @@ func (o *setNamespaceOptions) Validate(args []string) error {
} }
// RunSetNamespace runs setNamespace command (does real work). // RunSetNamespace runs setNamespace command (does real work).
func (o *setNamespaceOptions) RunSetNamespace(fsys fs.FileSystem) error { func (o *setNamespaceOptions) RunSetNamespace(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -21,7 +21,6 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/validators" "sigs.k8s.io/kustomize/pkg/validators"
) )
@@ -40,7 +39,7 @@ func TestSetNamespaceHappyPath(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }
@@ -65,7 +64,7 @@ func TestSetNamespaceOverride(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected cmd error: %v", err) t.Errorf("unexpected cmd error: %v", err)
} }
content, err := fakeFS.ReadFile(constants.KustomizationFileName) content, err := fakeFS.ReadTestKustomization()
if err != nil { if err != nil {
t.Errorf("unexpected read error: %v", err) t.Errorf("unexpected read error: %v", err)
} }

View File

@@ -73,13 +73,13 @@ func squash(x [][]byte) []byte {
type kustomizationFile struct { type kustomizationFile struct {
path string path string
fsys fs.FileSystem fSys fs.FileSystem
originalFields []*commentedField originalFields []*commentedField
} }
// NewKustomizationFile returns a new instance. // NewKustomizationFile returns a new instance.
func NewKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile, error) { // nolint func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // nolint
mf := &kustomizationFile{path: mPath, fsys: fsys} mf := &kustomizationFile{path: constants.KustomizationFileName, fSys: fSys}
err := mf.validate() err := mf.validate()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -88,12 +88,12 @@ func NewKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile,
} }
func (mf *kustomizationFile) validate() error { func (mf *kustomizationFile) validate() error {
if !mf.fsys.Exists(mf.path) { if !mf.fSys.Exists(mf.path) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path) return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path)
} }
if mf.fsys.IsDir(mf.path) { if mf.fSys.IsDir(mf.path) {
mf.path = path.Join(mf.path, constants.KustomizationFileName) mf.path = path.Join(mf.path, constants.KustomizationFileName)
if !mf.fsys.Exists(mf.path) { if !mf.fSys.Exists(mf.path) {
return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path) return fmt.Errorf("Missing kustomization file '%s'.\n", mf.path)
} }
} else { } else {
@@ -106,7 +106,7 @@ func (mf *kustomizationFile) validate() error {
} }
func (mf *kustomizationFile) Read() (*types.Kustomization, error) { func (mf *kustomizationFile) Read() (*types.Kustomization, error) {
data, err := mf.fsys.ReadFile(mf.path) data, err := mf.fSys.ReadFile(mf.path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -130,7 +130,7 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
if err != nil { if err != nil {
return err return err
} }
return mf.fsys.WriteFile(mf.path, data) return mf.fSys.WriteFile(mf.path, data)
} }
// StringInSlice returns true if the string is in the slice. // StringInSlice returns true if the string is in the slice.

View File

@@ -21,7 +21,6 @@ import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
@@ -31,9 +30,9 @@ func TestWriteAndRead(t *testing.T) {
NamePrefix: "prefix", NamePrefix: "prefix",
} }
fsys := fs.MakeFakeFS() fSys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName) fSys.WriteTestKustomization()
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys) mf, err := NewKustomizationFile(fSys)
if err != nil { if err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
@@ -51,20 +50,9 @@ func TestWriteAndRead(t *testing.T) {
} }
} }
func TestEmptyFile(t *testing.T) {
fsys := fs.MakeFakeFS()
_, err := NewKustomizationFile("", fsys)
if err == nil {
t.Fatalf("Create kustomizationFile from empty filename should fail")
}
}
func TestNewNotExist(t *testing.T) { func TestNewNotExist(t *testing.T) {
badSuffix := "foo.bar"
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
fakeFS.Mkdir(".") _, err := NewKustomizationFile(fakeFS)
fakeFS.Create(badSuffix)
_, err := NewKustomizationFile(constants.KustomizationFileName, fakeFS)
if err == nil { if err == nil {
t.Fatalf("expect an error") t.Fatalf("expect an error")
} }
@@ -72,21 +60,13 @@ func TestNewNotExist(t *testing.T) {
if !strings.Contains(err.Error(), contained) { if !strings.Contains(err.Error(), contained) {
t.Fatalf("expect an error contains %q, but got %v", contained, err) t.Fatalf("expect an error contains %q, but got %v", contained, err)
} }
_, err = NewKustomizationFile(constants.KustomizationFileName, fakeFS) _, err = NewKustomizationFile(fakeFS)
if err == nil { if err == nil {
t.Fatalf("expect an error") t.Fatalf("expect an error")
} }
if !strings.Contains(err.Error(), contained) { if !strings.Contains(err.Error(), contained) {
t.Fatalf("expect an error contains %q, but got %v", contained, err) t.Fatalf("expect an error contains %q, but got %v", contained, err)
} }
_, err = NewKustomizationFile(badSuffix, fakeFS)
if err == nil {
t.Fatalf("expect an error")
}
contained = "should have .yaml suffix"
if !strings.Contains(err.Error(), contained) {
t.Fatalf("expect an error contains %q, but got %v", contained, err)
}
} }
func TestPreserveComments(t *testing.T) { func TestPreserveComments(t *testing.T) {
@@ -113,10 +93,9 @@ patchesStrategicMerge:
- service.yaml - service.yaml
- pod.yaml - pod.yaml
`) `)
fsys := fs.MakeFakeFS() fSys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName) fSys.WriteTestKustomizationWith(kustomizationContentWithComments)
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments) mf, err := NewKustomizationFile(fSys)
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil { if err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
@@ -127,7 +106,7 @@ patchesStrategicMerge:
if err = mf.Write(kustomization); err != nil { if err = mf.Write(kustomization); err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
bytes, _ := fsys.ReadFile(mf.path) bytes, _ := fSys.ReadFile(mf.path)
if !reflect.DeepEqual(kustomizationContentWithComments, bytes) { if !reflect.DeepEqual(kustomizationContentWithComments, bytes) {
t.Fatal("written kustomization with comments is not the same as original one") t.Fatal("written kustomization with comments is not the same as original one")
@@ -204,10 +183,9 @@ patchesStrategicMerge:
- service.yaml - service.yaml
- pod.yaml - pod.yaml
`) `)
fsys := fs.MakeFakeFS() fSys := fs.MakeFakeFS()
fsys.Create(constants.KustomizationFileName) fSys.WriteTestKustomizationWith(kustomizationContentWithComments)
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments) mf, err := NewKustomizationFile(fSys)
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil { if err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
@@ -219,7 +197,7 @@ patchesStrategicMerge:
if err = mf.Write(kustomization); err != nil { if err = mf.Write(kustomization); err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
bytes, _ := fsys.ReadFile(mf.path) bytes, _ := fSys.ReadFile(mf.path)
if !reflect.DeepEqual(expected, bytes) { if !reflect.DeepEqual(expected, bytes) {
t.Fatal("written kustomization with comments is not the same as original one\n", string(bytes)) t.Fatal("written kustomization with comments is not the same as original one\n", string(bytes))

View File

@@ -19,9 +19,8 @@ package fs
import ( import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"sort"
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sort"
) )
var _ FileSystem = &FakeFS{} var _ FileSystem = &FakeFS{}
@@ -54,13 +53,6 @@ configMapGenerator: []
secretGenerator: [] secretGenerator: []
` `
// WriteTestKustomization writes a standard test file.
func (fs *FakeFS) WriteTestKustomization() {
fs.WriteFile(
constants.KustomizationFileName,
[]byte(kustomizationContent))
}
// Create assures a fake file appears in the in-memory file system. // Create assures a fake file appears in the in-memory file system.
func (fs *FakeFS) Create(name string) (File, error) { func (fs *FakeFS) Create(name string) (File, error) {
f := &FakeFile{} f := &FakeFile{}
@@ -123,6 +115,10 @@ func (fs *FakeFS) ReadFile(name string) ([]byte, error) {
return nil, fmt.Errorf("cannot read file %q", name) return nil, fmt.Errorf("cannot read file %q", name)
} }
func (fs *FakeFS) ReadTestKustomization() ([]byte, error) {
return fs.ReadFile(constants.KustomizationFileName)
}
// WriteFile always succeeds and does nothing. // WriteFile always succeeds and does nothing.
func (fs *FakeFS) WriteFile(name string, c []byte) error { func (fs *FakeFS) WriteFile(name string, c []byte) error {
ff := &FakeFile{} ff := &FakeFile{}
@@ -131,6 +127,16 @@ func (fs *FakeFS) WriteFile(name string, c []byte) error {
return nil return nil
} }
// WriteTestKustomization writes a standard test file.
func (fs *FakeFS) WriteTestKustomization() {
fs.WriteTestKustomizationWith([]byte(kustomizationContent))
}
// WriteTestKustomizationWith writes a standard test file.
func (fs *FakeFS) WriteTestKustomizationWith(bytes []byte) {
fs.WriteFile(constants.KustomizationFileName, bytes)
}
func (fs *FakeFS) pathMatch(path, pattern string) bool { func (fs *FakeFS) pathMatch(path, pattern string) bool {
match, _ := filepath.Match(pattern, path) match, _ := filepath.Match(pattern, path)
return match return match

View File

@@ -19,12 +19,9 @@ package error
import ( import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
) )
func TestConfigmapError_Error(t *testing.T) { func TestConfigmapError_Error(t *testing.T) {
filepath := "/path/to/" + constants.KustomizationFileName
errorMsg := "configmap name is missing" errorMsg := "configmap name is missing"
me := ConfigmapError{Path: filepath, ErrorMsg: errorMsg} me := ConfigmapError{Path: filepath, ErrorMsg: errorMsg}

View File

@@ -20,12 +20,9 @@ import (
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
) )
func TestKustomizationError_Error(t *testing.T) { func TestKustomizationError_Error(t *testing.T) {
filepath := "/path/to/" + constants.KustomizationFileName
errorMsg := "Kustomization not found" errorMsg := "Kustomization not found"
me := KustomizationError{KustomizationPath: filepath, ErrorMsg: errorMsg} me := KustomizationError{KustomizationPath: filepath, ErrorMsg: errorMsg}
@@ -39,11 +36,9 @@ func TestKustomizationError_Error(t *testing.T) {
t.Errorf("Incorrect KustomizationError.Error() message \n") t.Errorf("Incorrect KustomizationError.Error() message \n")
t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg)
} }
} }
func TestKustomizationErrors_Error(t *testing.T) { func TestKustomizationErrors_Error(t *testing.T) {
filepath := "/path/to/kustomize"
me := KustomizationError{KustomizationPath: filepath, ErrorMsg: "Kustomization not found"} me := KustomizationError{KustomizationPath: filepath, ErrorMsg: "Kustomization not found"}
ce := ConfigmapError{Path: filepath, ErrorMsg: "can't find configmap name"} ce := ConfigmapError{Path: filepath, ErrorMsg: "can't find configmap name"}
pe := PatchError{KustomizationPath: filepath, PatchFilepath: filepath, ErrorMsg: "can't find patch file"} pe := PatchError{KustomizationPath: filepath, PatchFilepath: filepath, ErrorMsg: "can't find patch file"}

View File

@@ -19,12 +19,9 @@ package error
import ( import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
) )
func TestPatchError_Error(t *testing.T) { func TestPatchError_Error(t *testing.T) {
filepath := "/path/to/" + constants.KustomizationFileName
patchfilepath := "/path/to/patch/patch.yaml" patchfilepath := "/path/to/patch/patch.yaml"
errorMsg := "file not found" errorMsg := "file not found"
me := PatchError{KustomizationPath: filepath, PatchFilepath: patchfilepath, ErrorMsg: errorMsg} me := PatchError{KustomizationPath: filepath, PatchFilepath: patchfilepath, ErrorMsg: errorMsg}

View File

@@ -19,12 +19,9 @@ package error
import ( import (
"strings" "strings"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
) )
func TestResourceError_Error(t *testing.T) { func TestResourceError_Error(t *testing.T) {
filepath := "/path/to/" + constants.KustomizationFileName
resourcefilepath := "/path/to/resource/deployment.yaml" resourcefilepath := "/path/to/resource/deployment.yaml"
errorMsg := "file not found" errorMsg := "file not found"
me := ResourceError{KustomizationPath: filepath, ResourceFilepath: resourcefilepath, ErrorMsg: errorMsg} me := ResourceError{KustomizationPath: filepath, ResourceFilepath: resourcefilepath, ErrorMsg: errorMsg}

View File

@@ -22,7 +22,6 @@ import (
) )
func TestSecretError_Error(t *testing.T) { func TestSecretError_Error(t *testing.T) {
filepath := "/path/to/secret.yaml"
errorMsg := "missing a command" errorMsg := "missing a command"
me := SecretError{KustomizationPath: filepath, ErrorMsg: errorMsg} me := SecretError{KustomizationPath: filepath, ErrorMsg: errorMsg}
if !strings.Contains(me.Error(), filepath) { if !strings.Contains(me.Error(), filepath) {

View File

@@ -19,13 +19,11 @@ package error
import ( import (
"fmt" "fmt"
"testing" "testing"
"sigs.k8s.io/kustomize/pkg/constants"
) )
var ( const (
filepath = "/path/to/" + constants.KustomizationFileName filepath = "/path/to/whatever"
expected = "YAML file [/path/to/kustomization.yaml] encounters a format error.\n" + expected = "YAML file [/path/to/whatever] encounters a format error.\n" +
"error converting YAML to JSON: yaml: line 2: found character that cannot start any token\n" "error converting YAML to JSON: yaml: line 2: found character that cannot start any token\n"
) )