Skip to content

Commit 05275a7

Browse files
committed
Extract repo name out of WORKSPACE and MODULE.bazel files
1 parent 27829e7 commit 05275a7

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

go/tools/bazel_testing/bazel_testing.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444

4545
// Set via x_defs.
4646
var goRootFile = ""
47-
var testedModuleName = ""
4847

4948
const (
5049
// Standard Bazel exit codes.
@@ -322,23 +321,34 @@ func setupWorkspace(args Args, files []string) (dir string, cleanup func() error
322321

323322
// Copy or link the files for the tested repository.
324323
testedRepoDir := filepath.Join(execDir, "tested_repo")
324+
singleRepoPrefix := ""
325325
for _, f := range files {
326-
if !strings.HasPrefix(f, "_main/") {
327-
return "", cleanup, fmt.Errorf("unexpected data file from a non-main repo: %s", f)
326+
if singleRepoPrefix == "" {
327+
singleRepoPrefix = f[:strings.Index(f, "/")+1]
328+
} else if !strings.HasPrefix(f, singleRepoPrefix) {
329+
return "", cleanup, fmt.Errorf("data files from more than one repo are unsupported, got %s and %s", singleRepoPrefix, f[:strings.Index(f, "/")+1])
328330
}
329331
srcPath, err := runfiles.Rlocation(f)
330332
if err != nil {
331333
return "", cleanup, fmt.Errorf("unknown runfile %s: %v", f, err)
332334
}
333-
dstPath := filepath.Join(testedRepoDir, strings.TrimPrefix(f, "_main/"))
335+
dstPath := filepath.Join(testedRepoDir, strings.TrimPrefix(f, singleRepoPrefix))
334336
if err := copyOrLink(dstPath, srcPath); err != nil {
335337
return "", cleanup, fmt.Errorf("copying %s to %s: %v", srcPath, dstPath, err)
336338
}
337339
}
340+
testedRepoModulePath := filepath.Join(testedRepoDir, "MODULE.bazel")
341+
testedModuleName := ""
342+
if _, err := os.Stat(testedRepoModulePath); err == nil {
343+
testedModuleName, err = loadName(testedRepoModulePath)
344+
if err != nil {
345+
return "", cleanup, fmt.Errorf("loading module name: %v", err)
346+
}
347+
}
338348
testedRepoWorkspacePath := filepath.Join(testedRepoDir, "WORKSPACE")
339349
testedModuleRepoName := testedModuleName
340350
if _, err = os.Stat(testedRepoWorkspacePath); err == nil {
341-
testedModuleRepoName, err = loadWorkspaceName(filepath.Join(testedRepoDir, "WORKSPACE"))
351+
testedModuleRepoName, err = loadName(testedRepoWorkspacePath)
342352
if err != nil {
343353
return "", cleanup, fmt.Errorf("loading workspace name: %v", err)
344354
}
@@ -451,19 +461,19 @@ func extractTxtar(dir, txt string) error {
451461
return nil
452462
}
453463

454-
func loadWorkspaceName(workspacePath string) (string, error) {
455-
workspaceData, err := os.ReadFile(workspacePath)
464+
func loadName(bazelFilePath string) (string, error) {
465+
content, err := os.ReadFile(bazelFilePath)
456466
if err != nil {
457467
return "", err
458468
}
459-
nameRe := regexp.MustCompile(`(?m)^workspace\(\s*name\s*=\s*("[^"]*"|'[^']*')\s*,?\s*\)\s*$`)
460-
match := nameRe.FindSubmatchIndex(workspaceData)
469+
nameRe := regexp.MustCompile(`(?m)^(?:\s*|workspace\()name\s*=\s*("[^"]*"|'[^']*')\s*,?\s*\)?\s*$`)
470+
match := nameRe.FindSubmatchIndex(content)
461471
if match == nil {
462-
return "", fmt.Errorf("%s: workspace name not set", workspacePath)
472+
return "", fmt.Errorf("%s: name not set", bazelFilePath)
463473
}
464-
name := string(workspaceData[match[2]+1 : match[3]-1])
474+
name := string(content[match[2]+1 : match[3]-1])
465475
if name == "" {
466-
return "", fmt.Errorf("%s: workspace name is empty", workspacePath)
476+
return "", fmt.Errorf("%s: name is empty", bazelFilePath)
467477
}
468478
return name, nil
469479
}

go/tools/bazel_testing/def.bzl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def go_bazel_test(rule_files = None, **kwargs):
2121
"""
2222

2323
if not rule_files:
24-
# Resolve the //:all_files target in the repo using this rule.
25-
rule_files = ["//:all_files"]
24+
rule_files = [Label("//:all_files")]
2625

2726
# Add dependency on bazel_testing library.
2827
kwargs.setdefault("deps", [])
@@ -60,8 +59,4 @@ def go_bazel_test(rule_files = None, **kwargs):
6059
if "exclusive" not in kwargs["tags"]:
6160
kwargs["tags"].append("exclusive")
6261

63-
kwargs.setdefault("x_defs", {}).update({
64-
"github.com/bazelbuild/rules_go/go/tools/bazel_testing.testedModuleName": native.module_name(),
65-
})
66-
6762
go_test(**kwargs)

0 commit comments

Comments
 (0)