Replace os.Stat with IsDir, simplifying FS abstraction.

This commit is contained in:
Jeffrey Regan
2018-07-18 11:33:30 -07:00
parent 8fda0f87ab
commit 9432671887
15 changed files with 83 additions and 69 deletions

View File

@@ -84,9 +84,8 @@ func (o *addBaseOptions) RunAddBase(fsys fs.FileSystem) error {
// split directory paths
paths := strings.Split(o.baseDirectoryPaths, ",")
for _, path := range paths {
_, err := fsys.Stat(path)
if err != nil {
return err
if !fsys.Exists(path) {
return errors.New(path + " does not exist")
}
if stringInSlice(path, m.Bases) {
return fmt.Errorf("base %s already in kustomization file", path)

View File

@@ -33,7 +33,7 @@ func TestAddBaseHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
bases := strings.Split(baseDirectoryPaths, ",")
for _, base := range bases {
fakeFS.Mkdir(base, 0777)
fakeFS.Mkdir(base)
}
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -60,7 +60,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
// Create fake directories
bases := strings.Split(baseDirectoryPaths, ",")
for _, base := range bases {
fakeFS.Mkdir(base, 0777)
fakeFS.Mkdir(base)
}
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -77,9 +77,9 @@ func TestAddBaseAlreadyThere(t *testing.T) {
}
var expectedErrors []string
for _, base := range bases {
error := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, error)
if !stringInSlice(error, expectedErrors) {
msg := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, msg)
if !stringInSlice(msg, expectedErrors) {
t.Errorf("unexpected error %v", err)
}
}

View File

@@ -70,9 +70,8 @@ func (o *addPatchOptions) Complete(cmd *cobra.Command, args []string) error {
// RunAddPatch runs addPatch command (do real work).
func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.patchFilePath)
if err != nil {
return err
if !fsys.Exists(o.patchFilePath) {
return errors.New(o.patchFilePath + " doesn't exist")
}
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)

View File

@@ -70,11 +70,9 @@ func (o *addResourceOptions) Complete(cmd *cobra.Command, args []string) error {
// RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.resourceFilePath)
if err != nil {
return err
if !fsys.Exists(o.resourceFilePath) {
return errors.New(o.resourceFilePath + " does not exist")
}
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil {
return err

View File

@@ -45,25 +45,23 @@ func newKustomizationFile(mPath string, fsys fs.FileSystem) (*kustomizationFile,
}
func (mf *kustomizationFile) validate() error {
f, err := mf.fsys.Stat(mf.path)
if err != nil {
if !mf.fsys.Exists(mf.path) {
errorMsg := fmt.Sprintf("Missing kustomization file '%s'.\n", mf.path)
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
return merr
}
if f.IsDir() {
if mf.fsys.IsDir(mf.path) {
mf.path = path.Join(mf.path, constants.KustomizationFileName)
_, err = mf.fsys.Stat(mf.path)
if err != nil {
if !mf.fsys.Exists(mf.path) {
errorMsg := fmt.Sprintf("Missing kustomization file '%s'.\n", mf.path)
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
return merr
}
} else {
if !strings.HasSuffix(mf.path, constants.KustomizationFileName) {
errorMsg := fmt.Sprintf("Kustomization file path (%s) should have %s suffix\n", mf.path, constants.KustomizationFileSuffix)
merr := interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
return merr
errorMsg := fmt.Sprintf("Kustomization file path (%s) should have %s suffix\n",
mf.path, constants.KustomizationFileSuffix)
return interror.KustomizationError{KustomizationPath: mf.path, ErrorMsg: errorMsg}
}
}
return nil

View File

@@ -62,7 +62,7 @@ func TestEmptyFile(t *testing.T) {
func TestNewNotExist(t *testing.T) {
badSuffix := "foo.bar"
fakeFS := fs.MakeFakeFS()
fakeFS.Mkdir(".", 0644)
fakeFS.Mkdir(".")
fakeFS.Create(badSuffix)
_, err := newKustomizationFile(constants.KustomizationFileName, fakeFS)
if err == nil {