Skip to content

Commit 82451fb

Browse files
committed
Switch to current directory in tests
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.
1 parent 612671c commit 82451fb

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

pkg/scaffold/scaffold.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type Scaffold struct {
5252
ProjectPath string
5353

5454
GetWriter func(path string) (io.Writer, error)
55+
56+
FileExists func(path string) bool
5557
}
5658

5759
func (s *Scaffold) setFieldsAndValidate(t input.File) error {
@@ -141,6 +143,12 @@ func (s *Scaffold) Execute(options input.Options, files ...input.File) error {
141143
if s.GetWriter == nil {
142144
s.GetWriter = (&FileWriter{}).WriteCloser
143145
}
146+
if s.FileExists == nil {
147+
s.FileExists = func(path string) bool {
148+
_, err := os.Stat(path)
149+
return err == nil
150+
}
151+
}
144152

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

183191
// Check if the file to write already exists
184-
if _, err := os.Stat(i.Path); err == nil {
192+
if s.FileExists(i.Path) {
185193
switch i.IfExistsAction {
186194
case input.Overwrite:
187195
case input.Skip:

pkg/scaffold/scaffoldtest/scaffoldtest.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ type TestResult struct {
4242
}
4343

4444
func getProjectRoot() string {
45-
gopath := os.Getenv("GOPATH")
46-
return path.Join(gopath, "src", "sigs.k8s.io", "kubebuilder")
45+
return path.Join(build.Default.GOPATH, "src", "sigs.k8s.io", "kubebuilder")
4746
}
4847

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

6766
// NewTestScaffold returns a new Scaffold and TestResult instance for testing
6867
func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestResult) {
68+
projRoot := getProjectRoot()
6969
r := &TestResult{}
7070
// Setup scaffold
7171
s := &scaffold.Scaffold{
@@ -74,12 +74,20 @@ func NewTestScaffold(writeToPath, goldenPath string) (*scaffold.Scaffold, *TestR
7474
gomega.Expect(path).To(gomega.Equal(writeToPath))
7575
return &r.Actual, nil
7676
},
77-
ProjectPath: filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project"),
77+
FileExists: func(path string) bool {
78+
return path != writeToPath
79+
},
80+
ProjectPath: filepath.Join(projRoot, "testdata", "gopath", "src", "project"),
81+
}
82+
oldGoPath := build.Default.GOPATH
83+
build.Default.GOPATH = filepath.Join(projRoot, "testdata", "gopath")
84+
defer func() { build.Default.GOPATH = oldGoPath }()
85+
if _, err := os.Stat(build.Default.GOPATH); err != nil {
86+
panic(err)
7887
}
79-
build.Default.GOPATH = filepath.Join(getProjectRoot(), "testdata", "gopath")
8088

8189
if len(goldenPath) > 0 {
82-
b, err := ioutil.ReadFile(filepath.Join(getProjectRoot(), "testdata", "gopath", "src", "project", goldenPath))
90+
b, err := ioutil.ReadFile(filepath.Join(projRoot, "testdata", "gopath", "src", "project", goldenPath))
8391
gomega.Expect(err).NotTo(gomega.HaveOccurred())
8492
r.Golden = string(b)
8593
}

0 commit comments

Comments
 (0)