-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: factor duplicated code into test helper
- Loading branch information
Showing
6 changed files
with
90 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package testpath | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"path/filepath" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func BrokerpakFile(parts ...string) string { | ||
GinkgoHelper() | ||
|
||
r := BrokerpakRoot() | ||
p := filepath.Join(append([]string{r}, parts...)...) | ||
Expect(p).To(BeAnExistingFile(), func() string { | ||
return fmt.Sprintf("could not find file %q in brokerpak %q", path.Join(parts...), r) | ||
}) | ||
|
||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package testpath | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
) | ||
|
||
// BrokerpakRoot searches upwards from the current working directory to find the root path of the brokerpak | ||
// Fails the test if not found | ||
func BrokerpakRoot() string { | ||
ginkgo.GinkgoHelper() | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
d, err := filepath.Abs(cwd) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
switch { | ||
case Exists(filepath.Join(d, "manifest.yml")) && Exists(filepath.Join(d, "acceptance-tests")): | ||
return d | ||
case d == "/": | ||
ginkgo.Fail(fmt.Sprintf("could not determine brokerpak root from %q", cwd)) | ||
default: | ||
d = filepath.Dir(d) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Package testpath provides path utilities for tests | ||
package testpath | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
) | ||
|
||
// Exists returns whether a path exists, failing the test on error | ||
func Exists(path string) bool { | ||
ginkgo.GinkgoHelper() | ||
|
||
_, err := os.Stat(path) | ||
switch { | ||
case err == nil: | ||
return true | ||
case strings.Contains(err.Error(), "NotFound"): | ||
return false | ||
default: | ||
ginkgo.Fail(err.Error()) | ||
} | ||
|
||
return false // unreachable | ||
} |