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

2
Gopkg.lock generated
View File

@@ -296,6 +296,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "586d4cb9094e9b5c0731f16e931b953e9c0f709b7125a7537ae3625ada179eee" inputs-digest = "74d444cd05ac6f803960180ec8ccfd5a4358077f7c79a5218a243554cb599274"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@@ -18,7 +18,6 @@ package app
import ( import (
"encoding/base64" "encoding/base64"
"os"
"reflect" "reflect"
"testing" "testing"
@@ -264,7 +263,7 @@ func makeLoader2(t *testing.T) loader.Loader {
if err != nil { if err != nil {
t.Fatalf("Failed to setup fake loader.") t.Fatalf("Failed to setup fake loader.")
} }
err = loader.AddDirectory("/testpath/base", os.ModeDir) err = loader.AddDirectory("/testpath/base")
if err != nil { if err != nil {
t.Fatalf("Failed to setup fake loader.") t.Fatalf("Failed to setup fake loader.")
} }

View File

@@ -84,9 +84,8 @@ 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 {
_, err := fsys.Stat(path) if !fsys.Exists(path) {
if err != nil { return errors.New(path + " does not exist")
return err
} }
if stringInSlice(path, m.Bases) { if stringInSlice(path, m.Bases) {
return fmt.Errorf("base %s already in kustomization file", path) return fmt.Errorf("base %s already in kustomization file", path)

View File

@@ -33,7 +33,7 @@ func TestAddBaseHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS() fakeFS := fs.MakeFakeFS()
bases := strings.Split(baseDirectoryPaths, ",") bases := strings.Split(baseDirectoryPaths, ",")
for _, base := range bases { for _, base := range bases {
fakeFS.Mkdir(base, 0777) fakeFS.Mkdir(base)
} }
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -60,7 +60,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
// Create fake directories // Create fake directories
bases := strings.Split(baseDirectoryPaths, ",") bases := strings.Split(baseDirectoryPaths, ",")
for _, base := range bases { for _, base := range bases {
fakeFS.Mkdir(base, 0777) fakeFS.Mkdir(base)
} }
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
@@ -77,9 +77,9 @@ func TestAddBaseAlreadyThere(t *testing.T) {
} }
var expectedErrors []string var expectedErrors []string
for _, base := range bases { for _, base := range bases {
error := "base " + base + " already in kustomization file" msg := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, error) expectedErrors = append(expectedErrors, msg)
if !stringInSlice(error, expectedErrors) { if !stringInSlice(msg, expectedErrors) {
t.Errorf("unexpected error %v", err) 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). // RunAddPatch runs addPatch command (do real work).
func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error { func (o *addPatchOptions) RunAddPatch(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.patchFilePath) if !fsys.Exists(o.patchFilePath) {
if err != nil { return errors.New(o.patchFilePath + " doesn't exist")
return err
} }
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) 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). // RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error { func (o *addResourceOptions) RunAddResource(fsys fs.FileSystem) error {
_, err := fsys.Stat(o.resourceFilePath) if !fsys.Exists(o.resourceFilePath) {
if err != nil { return errors.New(o.resourceFilePath + " does not exist")
return err
} }
mf, err := newKustomizationFile(constants.KustomizationFileName, fsys) mf, err := newKustomizationFile(constants.KustomizationFileName, fsys)
if err != nil { if err != nil {
return err return err

View File

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

View File

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

View File

@@ -18,7 +18,6 @@ package fs
import ( import (
"fmt" "fmt"
"os"
) )
var _ FileSystem = &FakeFS{} var _ FileSystem = &FakeFS{}
@@ -42,7 +41,7 @@ func (fs *FakeFS) Create(name string) (File, error) {
} }
// Mkdir assures a fake directory appears in the in-memory file system. // Mkdir assures a fake directory appears in the in-memory file system.
func (fs *FakeFS) Mkdir(name string, perm os.FileMode) error { func (fs *FakeFS) Mkdir(name string) error {
fs.m[name] = makeDir(name) fs.m[name] = makeDir(name)
return nil return nil
} }
@@ -55,12 +54,19 @@ func (fs *FakeFS) Open(name string) (File, error) {
return fs.m[name], nil return fs.m[name], nil
} }
// Stat always returns nil FileInfo, and returns an error if file does not exist. // Exists returns true if file is known.
func (fs *FakeFS) Stat(name string) (os.FileInfo, error) { func (fs *FakeFS) Exists(name string) bool {
if f, found := fs.m[name]; found { _, found := fs.m[name]
return &Fakefileinfo{f}, nil return found
} }
return nil, fmt.Errorf("file %q does not exist", name)
// IsDir returns true if the file exists and is a directory.
func (fs *FakeFS) IsDir(name string) bool {
f, found := fs.m[name]
if !found {
return false
}
return f.dir
} }
// ReadFile always returns an empty bytes and error depending on content of m. // ReadFile always returns an empty bytes and error depending on content of m.

View File

@@ -21,34 +21,25 @@ import (
"testing" "testing"
) )
func TestStatNotExist(t *testing.T) { func TestExists(t *testing.T) {
x := MakeFakeFS() x := MakeFakeFS()
info, err := x.Stat("foo") if x.Exists("foo") {
if info != nil { t.Fatalf("expected no foo")
t.Fatalf("expected nil info")
}
if err == nil {
t.Fatalf("expected error")
} }
} }
func TestStat(t *testing.T) { func TestIsDir(t *testing.T) {
x := MakeFakeFS() x := MakeFakeFS()
expectedName := "my-dir" expectedName := "my-dir"
err := x.Mkdir(expectedName, 0666) err := x.Mkdir(expectedName)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
info, err := x.Stat(expectedName) if !x.Exists(expectedName) {
if err != nil { t.Fatalf(expectedName + " should exist")
t.Fatalf("unexpected error: %v", err)
} }
name := info.Name() if !x.IsDir(expectedName) {
if name != expectedName { t.Fatalf(expectedName + " should be a dir")
t.Fatalf("expected %v but got %v", expectedName, name)
}
if !info.IsDir() {
t.Fatalf("expected IsDir() return true")
} }
} }
@@ -61,12 +52,8 @@ func TestCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error") t.Fatalf("unexpected error")
} }
info, err := x.Stat("foo") if !x.Exists("foo") {
if info == nil { t.Fatalf("expected foo to exist")
t.Fatalf("expected non-nil info")
}
if err != nil {
t.Fatalf("expected no error")
} }
} }

View File

@@ -25,9 +25,10 @@ import (
// FileSystem groups basic os filesystem methods. // FileSystem groups basic os filesystem methods.
type FileSystem interface { type FileSystem interface {
Create(name string) (File, error) Create(name string) (File, error)
Mkdir(name string, perm os.FileMode) error Mkdir(name string) error
Open(name string) (File, error) Open(name string) (File, error)
Stat(name string) (os.FileInfo, error) IsDir(name string) bool
Exists(name string) bool
ReadFile(name string) ([]byte, error) ReadFile(name string) ([]byte, error)
ReadFiles(name string) (map[string][]byte, error) ReadFiles(name string) (map[string][]byte, error)
WriteFile(name string, data []byte) error WriteFile(name string, data []byte) error

View File

@@ -36,13 +36,27 @@ func MakeRealFS() FileSystem {
func (realFS) Create(name string) (File, error) { return os.Create(name) } func (realFS) Create(name string) (File, error) { return os.Create(name) }
// Mkdir delegates to os.Mkdir. // Mkdir delegates to os.Mkdir.
func (realFS) Mkdir(name string, perm os.FileMode) error { return os.Mkdir(name, perm) } func (realFS) Mkdir(name string) error {
return os.Mkdir(name, 0777|os.ModeDir)
}
// Open delegates to os.Open. // Open delegates to os.Open.
func (realFS) Open(name string) (File, error) { return os.Open(name) } func (realFS) Open(name string) (File, error) { return os.Open(name) }
// Stat delegates to os.Stat. // Exists returns true if os.Stat succeeds.
func (realFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) } func (realFS) Exists(name string) bool {
_, err := os.Stat(name)
return err == nil
}
// IsDir delegates to os.Stat and FileInfo.IsDir
func (realFS) IsDir(name string) bool {
info, err := os.Stat(name)
if err != nil {
return false
}
return info.IsDir()
}
// ReadFile delegates to ioutil.ReadFile. // ReadFile delegates to ioutil.ReadFile.
func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) } func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) }

View File

@@ -26,17 +26,30 @@ import (
func TestReadFilesRealFS(t *testing.T) { func TestReadFilesRealFS(t *testing.T) {
x := MakeRealFS() x := MakeRealFS()
testDir := "kustomize_testing_dir" testDir := "kustomize_testing_dir"
err := x.Mkdir(testDir, 0777) err := x.Mkdir(testDir)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
if err != nil { if err != nil {
t.Fatalf("unexpected error %s", err) t.Fatalf("unexpected error %s", err)
} }
if !x.Exists(testDir) {
t.Fatalf("expected existence")
}
if !x.IsDir(testDir) {
t.Fatalf("expected directory")
}
err = x.WriteFile(path.Join(testDir, "foo"), []byte(`foo`)) err = x.WriteFile(path.Join(testDir, "foo"), []byte(`foo`))
if err != nil { if err != nil {
t.Fatalf("unexpected error %s", err) t.Fatalf("unexpected error %s", err)
} }
if !x.Exists(path.Join(testDir, "foo")) {
t.Fatalf("expected foo")
}
if x.IsDir(path.Join(testDir, "foo")) {
t.Fatalf("expected foo not to be a directory")
}
err = x.WriteFile(path.Join(testDir, "bar"), []byte(`bar`)) err = x.WriteFile(path.Join(testDir, "bar"), []byte(`bar`))
if err != nil { if err != nil {
t.Fatalf("unexpected error %s", err) t.Fatalf("unexpected error %s", err)

View File

@@ -18,8 +18,6 @@ limitations under the License.
package loadertest package loadertest
import ( import (
"os"
"github.com/kubernetes-sigs/kustomize/pkg/fs" "github.com/kubernetes-sigs/kustomize/pkg/fs"
"github.com/kubernetes-sigs/kustomize/pkg/loader" "github.com/kubernetes-sigs/kustomize/pkg/loader"
) )
@@ -49,8 +47,8 @@ func (f FakeLoader) AddFile(fullFilePath string, content []byte) error {
} }
// AddDirectory adds a fake directory to the file system. // AddDirectory adds a fake directory to the file system.
func (f FakeLoader) AddDirectory(fullDirPath string, mode os.FileMode) error { func (f FakeLoader) AddDirectory(fullDirPath string) error {
return f.fs.Mkdir(fullDirPath, mode) return f.fs.Mkdir(fullDirPath)
} }
// Root returns root. // Root returns root.

View File

@@ -18,11 +18,12 @@ package resmap
import ( import (
"context" "context"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"time" "time"
"os"
"github.com/kubernetes-sigs/kustomize/pkg/resource" "github.com/kubernetes-sigs/kustomize/pkg/resource"
"github.com/kubernetes-sigs/kustomize/pkg/types" "github.com/kubernetes-sigs/kustomize/pkg/types"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -62,6 +63,7 @@ func makeSecret(p string, sArgs types.SecretArgs) (*corev1.Secret, error) {
return s, nil return s, nil
} }
// Run a command, return its output as the secret.
func createSecretKey(wd string, command string) ([]byte, error) { func createSecretKey(wd string, command string) ([]byte, error) {
fi, err := os.Stat(wd) fi, err := os.Stat(wd)
if err != nil || !fi.IsDir() { if err != nil || !fi.IsDir() {