Skip to content

Commit

Permalink
Make root directory platform-agnostic
Browse files Browse the repository at this point in the history
Support windows root directory. Dogfood own error package.
  • Loading branch information
annasong20 committed Jun 20, 2022
1 parent 4e46337 commit 3967eaa
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 76 deletions.
6 changes: 3 additions & 3 deletions api/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/internal/git"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand Down Expand Up @@ -168,7 +168,7 @@ func (fl *fileLoader) New(path string) (ifc.Loader, error) {
}
root, err := filesys.DemandDir(fl.fSys, fl.root.Join(path))
if err != nil {
return nil, errors.Wrapf(err, ErrLdrDir.Error())
return nil, errors.WrapPrefixf(err, ErrLdrDir.Error())
}
if err = fl.errIfGitContainmentViolation(root); err != nil {
return nil, err
Expand Down Expand Up @@ -298,7 +298,7 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
if resp.StatusCode < 200 || resp.StatusCode > 299 {
_, err := git.NewRepoSpecFromURL(path)
if err == nil {
return nil, errors.New("URL is a git repository")
return nil, errors.Errorf("URL is a git repository")
}
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
}
Expand Down
4 changes: 2 additions & 2 deletions api/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package loader

import (
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/internal/git"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

Expand All @@ -28,7 +28,7 @@ func NewLoader(
}
root, err := filesys.DemandDir(fSys, target)
if err != nil {
return nil, errors.Wrapf(err, ErrLdrDir.Error())
return nil, errors.WrapPrefixf(err, ErrLdrDir.Error())
}
return newLoaderAtConfirmedDir(
lr, root, fSys, nil, git.ClonerUsingGitExec), nil
Expand Down
4 changes: 2 additions & 2 deletions kyaml/filesys/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

package filesys

import "github.com/pkg/errors"
import "sigs.k8s.io/kustomize/kyaml/errors"

var ErrNotDir = errors.New("invalid directory")
var ErrNotDir = errors.Errorf("invalid directory")
12 changes: 6 additions & 6 deletions kyaml/filesys/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"path/filepath"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/kyaml/errors"
)

const (
Expand Down Expand Up @@ -66,20 +66,20 @@ type FileSystem interface {
Walk(path string, walkFn filepath.WalkFunc) error
}

// DemandDir returns an error if path is not an existing directory on fSys.
// DemandDir returns an error if the user-specified path is not an existing directory on fSys.
// Otherwise, DemandDir returns path, which can be relative, as a ConfirmedDir and all that implies.
func DemandDir(fSys FileSystem, path string) (ConfirmedDir, error) {
if path == "" {
return "", errors.New("directory path cannot be empty")
return "", errors.Errorf("directory path cannot be empty")
}

d, f, err := fSys.CleanedAbs(path)
if err != nil {
return "", errors.Wrapf(err, ErrNotDir.Error())
return "", errors.WrapPrefixf(err, ErrNotDir.Error())
}
if f != "" {
return "", errors.Wrapf(
errors.Wrapf(errors.New("file is not directory"), fmt.Sprintf("'%s'", path)),
return "", errors.WrapPrefixf(
errors.WrapPrefixf(errors.Errorf("file is not directory"), fmt.Sprintf("'%s'", path)),
ErrNotDir.Error())
}
return d, nil
Expand Down
45 changes: 21 additions & 24 deletions kyaml/filesys/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,32 @@ func testNotExistErr(t *testing.T, fs FileSystem) {
}

// setupFileSys returns file system, default directory to test in, and working directory
func setupFileSys(t *testing.T, req *require.Assertions, name string, bldr func() FileSystem) (FileSystem, string, string) {
func setupFileSys(t *testing.T, name string) (FileSystem, string, string) {
t.Helper()

switch name {
case disk:
fSys, testDir := makeTestDir(t, req)
return fSys, testDir, string(cleanWd(req, fSys))
fSys, testDir := makeTestDir(t)
return fSys, testDir, cleanWd(t)
case memoryAbs:
return bldr(), Separator, Separator
return filesysBuilders[name](), Separator, Separator
default:
req.Equal(memoryEmpty, name)
return bldr(), "", ""
require.Equal(t, memoryEmpty, name)
return filesysBuilders[name](), "", ""
}
}

func TestDemandDir(t *testing.T) {
for name, builder := range filesysBuilders {
for name := range filesysBuilders {
thisName := name
thisBldr := builder
t.Run(thisName, func(t *testing.T) {
req := require.New(t)
fSys, prefixPath, wd := setupFileSys(t, req, thisName, thisBldr)
fSys, prefixPath, wd := setupFileSys(t, thisName)

d1Path := filepath.Join(prefixPath, "d1")
d2Path := filepath.Join(d1Path, ".d2")
err := fSys.MkdirAll(d2Path)
req.NoError(err)
req.Truef(fSys.Exists(d2Path), existMsg, d2Path)
require.NoError(t, err)
require.Truef(t, fSys.Exists(d2Path), existMsg, d2Path)

tests := map[string]*struct {
dir string
Expand All @@ -104,35 +103,33 @@ func TestDemandDir(t *testing.T) {
t.Run(subName, func(t *testing.T) {
cleanDir, err := DemandDir(fSys, tCase.dir)
require.NoError(t, err)
require.Equal(t, ConfirmedDir(tCase.cleanDir), cleanDir)
require.Equal(t, tCase.cleanDir, cleanDir.String())
})
}
})
}
}

func TestDemandDirErr(t *testing.T) {
for name, builder := range filesysBuilders {
for name := range filesysBuilders {
thisName := name
thisBldr := builder
t.Run(thisName, func(t *testing.T) {
req := require.New(t)
fSys, prefixPath, _ := setupFileSys(t, req, thisName, thisBldr)
fSys, prefixPath, _ := setupFileSys(t, thisName)

fPath := filepath.Join(prefixPath, "foo")
err := fSys.WriteFile(fPath, []byte(`foo`))
req.NoError(err)
req.Truef(fSys.Exists(fPath), existMsg, fPath)
require.NoError(t, err)
require.Truef(t, fSys.Exists(fPath), existMsg, fPath)

tests := map[string]string{
"Empty": "",
"File": fPath,
"Does not exist": filepath.Join(prefixPath, "bar"),
"Empty": "",
"File": fPath,
"Non-existent": filepath.Join(prefixPath, "bar"),
}
for subName, invalidPath := range tests {
path := invalidPath
invalid := invalidPath
t.Run(subName, func(t *testing.T) {
_, err := DemandDir(fSys, path)
_, err := DemandDir(fSys, invalid)
require.Error(t, err)
})
}
Expand Down
4 changes: 2 additions & 2 deletions kyaml/filesys/fsnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sort"
"strings"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/kyaml/errors"
)

var _ File = &fsNode{}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (n *fsNode) AddDir(path string) (result *fsNode, err error) {
func (n *fsNode) CleanedAbs(path string) (ConfirmedDir, string, error) {
node, err := n.Find(path)
if err != nil {
return "", "", errors.Wrap(err, "unable to clean")
return "", "", errors.WrapPrefixf(err, "unable to clean")
}
if node == nil {
return "", "", notExistError(path)
Expand Down
2 changes: 1 addition & 1 deletion kyaml/filesys/fsnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ func TestDemandDirRelativeNode(t *testing.T) {

actual, err := DemandDir(fSysSub, "c")
req.NoError(err)
req.Equal(ConfirmedDir(expected), actual)
req.Equal(expected, actual.String())
}

func TestFileOps(t *testing.T) {
Expand Down
Loading

0 comments on commit 3967eaa

Please sign in to comment.