Assert functions for test harness
This CL adds various assert functions to the harness that let clients
assert on certain properties of the harness setup.
I'd like to note that repo_harness is intended to be fully generic and
not dependent on the structure of the CrOS project. In subsequent CLs I will
introduce something like cros_repo_harness, which will include cros-specific
assert functions.
TEST=run_tests.sh
BUG=chromium:980346
Change-Id: I0cf2cf63f034ee38df092a2064967c14a25672db
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/infra/go/+/1709529
Commit-Queue: Jack Neus <jackneus@google.com>
Tested-by: Jack Neus <jackneus@google.com>
Reviewed-by: Evan Hernandez <evanhernandez@chromium.org>
diff --git a/internal/git/git.go b/internal/git/git.go
index 0306f33..047452c 100644
--- a/internal/git/git.go
+++ b/internal/git/git.go
@@ -52,10 +52,19 @@
// MatchBranchName returns the names of branches who match the specified
// regular expression.
func MatchBranchName(gitRepo string, pattern *regexp.Regexp) ([]string, error) {
+ // MatchBranchWithNamespace trims the namespace off the branches it returns.
+ // Here, we need a namespace that matches every string but doesn't match any character
+ // (so that nothing is trimmed).
+ nullNamespace := regexp.MustCompile("")
+ return MatchBranchNameWithNamespace(gitRepo, pattern, nullNamespace)
+}
+
+// MatchBranchNameWithNamespace returns the names of branches who match the specified
+// pattern and start with the specified namespace.
+func MatchBranchNameWithNamespace(gitRepo string, pattern, namespace *regexp.Regexp) ([]string, error) {
// Regex should be case insensitive.
- if !strings.HasPrefix(pattern.String(), "(?i)") {
- pattern = regexp.MustCompile("(?i)" + pattern.String())
- }
+ namespace = regexp.MustCompile("(?i)^" + namespace.String())
+ pattern = regexp.MustCompile("(?i)" + pattern.String())
output, err := RunGit(gitRepo, []string{"ls-remote", gitRepo})
if err != nil {
@@ -71,6 +80,13 @@
continue
}
branch = strings.Fields(branch)[1]
+
+ // Only look at branches which match the namespace.
+ if !namespace.Match([]byte(branch)) {
+ continue
+ }
+ branch = namespace.ReplaceAllString(branch, "")
+
if pattern.Match([]byte(branch)) {
matchedBranches = append(matchedBranches, branch)
}
@@ -114,8 +130,12 @@
// CreateBranch creates a branch.
func CreateBranch(gitRepo, branch string) error {
output, err := RunGit(gitRepo, []string{"checkout", "-B", branch})
- if err != nil && strings.Contains(output.Stderr, "not a valid branch name") {
- return fmt.Errorf("%s is not a valid branch name", branch)
+ if err != nil {
+ if strings.Contains(output.Stderr, "not a valid branch name") {
+ return fmt.Errorf("%s is not a valid branch name", branch)
+ } else {
+ return fmt.Errorf(output.Stderr)
+ }
}
return err
}