Skip to content

Commit

Permalink
Switch to current directory in tests
Browse files Browse the repository at this point in the history
Lots of the module stuff (and more) depends on the "context" of the
module, so being in the right directory when we run the imports.Process
from the tests is important.
  • Loading branch information
DirectXMan12 committed May 10, 2019
1 parent 612671c commit 82451fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 9 additions & 1 deletion pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Scaffold struct {
ProjectPath string

GetWriter func(path string) (io.Writer, error)

FileExists func(path string) bool
}

func (s *Scaffold) setFieldsAndValidate(t input.File) error {
Expand Down Expand Up @@ -141,6 +143,12 @@ func (s *Scaffold) Execute(options input.Options, files ...input.File) error {
if s.GetWriter == nil {
s.GetWriter = (&FileWriter{}).WriteCloser
}
if s.FileExists == nil {
s.FileExists = func(path string) bool {
_, err := os.Stat(path)
return err == nil
}
}

if err := s.defaultOptions(&options); err != nil {
return err
Expand Down Expand Up @@ -181,7 +189,7 @@ func (s *Scaffold) doFile(e input.File) error {
}

// Check if the file to write already exists
if _, err := os.Stat(i.Path); err == nil {
if s.FileExists(i.Path) {
switch i.IfExistsAction {
case input.Overwrite:
case input.Skip:
Expand Down
18 changes: 13 additions & 5 deletions pkg/scaffold/scaffoldtest/scaffoldtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ type TestResult struct {
}

func getProjectRoot() string {
gopath := os.Getenv("GOPATH")
return path.Join(gopath, "src", "sigs.k8s.io", "kubebuilder")
return path.Join(build.Default.GOPATH, "src", "sigs.k8s.io", "kubebuilder")
}

// ProjectPath is the path to the controller-tools/testdata project file
Expand All @@ -66,6 +65,7 @@ func Options() input.Options {

// NewTestScaffold returns a new Scaffold and TestResult instance for testing
func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestResult) {
projRoot := getProjectRoot()
r := &TestResult{}
// Setup scaffold
s := &scaffold.Scaffold{
Expand All @@ -74,12 +74,20 @@ func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestR
gomega.Expect(path).To(gomega.Equal(writeToPath))
return &r.Actual, nil
},
ProjectPath: filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project"),
FileExists: func(path string) bool {
return path != writeToPath
},
ProjectPath: filepath.Join(projRoot, "testdata", "gopath", "src", "project"),
}
oldGoPath := build.Default.GOPATH
build.Default.GOPATH = filepath.Join(projRoot, "testdata", "gopath")
defer func() { build.Default.GOPATH = oldGoPath }()
if _, err := os.Stat(build.Default.GOPATH); err != nil {
panic(err)
}
build.Default.GOPATH = filepath.Join(getProjectRoot(), "testdata", "gopath")

if len(goldenPath) > 0 {
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project", goldenPath))
b, err := ioutil.ReadFile(filepath.Join(projRoot, "testdata", "gopath", "src", "project", goldenPath))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
r.Golden = string(b)
}
Expand Down

0 comments on commit 82451fb

Please sign in to comment.