diff --git a/edit/edit.go b/edit/edit.go index 2d394d8ca..75a9d4883 100644 --- a/edit/edit.go +++ b/edit/edit.go @@ -136,21 +136,29 @@ func InterpretLabelForWorkspaceLocation(root string, target string) (buildFile s } if strings.HasPrefix(target, "//") { - buildFile = path.Join(rootDir, pkg, "BUILD") + buildFile = filepath.Join(rootDir, pkg, "BUILD") + if !isFile(buildFile) { + // try it with the .bazel extension + buildFile += ".bazel" + } return } if isFile(pkg) { // allow operation on other files like WORKSPACE buildFile = pkg - pkg = path.Join(relativePath, filepath.Dir(pkg)) + pkg = filepath.Join(relativePath, filepath.Dir(pkg)) return } if pkg != "" { - buildFile = pkg + "/BUILD" + buildFile = filepath.Join(pkg, "/BUILD") } else { buildFile = "BUILD" } - pkg = path.Join(relativePath, pkg) + if !isFile(buildFile) { + // try it with the .bazel extension + buildFile += ".bazel" + } + pkg = filepath.Join(relativePath, pkg) return } diff --git a/edit/edit_test.go b/edit/edit_test.go index 1bb2a0e23..e8098a7eb 100644 --- a/edit/edit_test.go +++ b/edit/edit_test.go @@ -14,6 +14,9 @@ package edit import ( "fmt" + "io/ioutil" + "os" + "path/filepath" "reflect" "regexp" "strings" @@ -673,3 +676,49 @@ x = 2`, } } } + +type testCase struct { + inputRoot, inputTarget string + expectedBuildFile, expectedPkg, expectedRule string +} + +func runTestInterpretLabelForWorkspaceLocation(t *testing.T, buildFileName string) { + tmp, err := ioutil.TempDir("", "") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmp) + if err := os.MkdirAll(filepath.Join(tmp, "a", "b"), 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(tmp, "WORKSPACE"), nil, 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(tmp, buildFileName), nil, 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(tmp, "a", buildFileName), nil, 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(tmp, "a", "b", buildFileName), nil, 0755); err != nil { + t.Fatal(err) + } + + for _, tc := range []testCase{ + {tmp, "//", filepath.Join(tmp, buildFileName), "", "."}, + {tmp, "//a", filepath.Join(tmp, "a", buildFileName), "a", "a"}, + {tmp, "//a:a", filepath.Join(tmp, "a", buildFileName), "a", "a"}, + {tmp, "//a/b", filepath.Join(tmp, "a", "b", buildFileName), "a/b", "b"}, + {tmp, "//a/b:b", filepath.Join(tmp, "a", "b", buildFileName), "a/b", "b"}, + } { + buildFile, pkg, rule := InterpretLabelForWorkspaceLocation(tc.inputRoot, tc.inputTarget) + if buildFile != tc.expectedBuildFile || pkg != tc.expectedPkg || rule != tc.expectedRule { + t.Errorf("InterpretLabelForWorkspaceLocation(%q, %q) = %q, %q, %q; want %q, %q, %q", tc.inputRoot, tc.inputTarget, buildFile, pkg, rule, tc.expectedBuildFile, tc.expectedPkg, tc.expectedRule) + } + } +} + +func TestInterpretLabelForWorkspaceLocation(t *testing.T) { + runTestInterpretLabelForWorkspaceLocation(t, "BUILD") + runTestInterpretLabelForWorkspaceLocation(t, "BUILD.bazel") +}