-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61748: bazel: make lint pass unit tests runnable in the sandbox r=rickystewart a=rickystewart Some unit tests, these lint tests being an example, call into the `go` binary directly. This is a problem in sandboxed environments where there may be no globally installed `go` toolchain. A summary of the changes made here: 1. We depend on the `rules_go` Go library, which surfaces some utilities that are useful here as well as generally for porting tests to work in Bazel. 2. Add a helper library in `pkg/build/bazel` to surface that stuff. There's a stub version of the library as well for code built without Bazel; we use build tags to make sure the right version of the library is surfaced, and update `.bazelrc` accordingly. 3. Refactor `testutils.TestDataPath` to use the helper lib -- this was almost entirely duplicated logic that's outmoded by `rules_go`. 4. Update all the tests to a) depend on the Go SDK when building, and b) use the helper library to patch in the toolchain. 5. Finally, delete a bunch of stray `BUILD.bazel` files in the `pkg/testutils/lint/passes` directory -- these serve no purpose and an old Gazelle configuration put them there. We just forgot to clean them up until now. To test: dev test pkg/testutils/lint/passes/... -v Release justification: Non-production code changes Release note: None Co-authored-by: irfan sharif <[email protected]> Co-authored-by: Ricky Stewart <[email protected]>
- Loading branch information
Showing
49 changed files
with
377 additions
and
290 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# TODO(irfansharif): We should fold this into `dev` instead (#56965). | ||
|
||
build --ui_event_filters=-DEBUG | ||
build --ui_event_filters=-DEBUG --define gotags=bazel | ||
test --define gotags=bazel | ||
query --ui_event_filters=-DEBUG | ||
|
||
try-import %workspace%/.bazelrc.user |
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
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,9 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "bazel", | ||
srcs = ["bazel.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/build/bazel", | ||
visibility = ["//visibility:public"], | ||
deps = ["@io_bazel_rules_go//go/tools/bazel:go_default_library"], | ||
) |
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,91 @@ | ||
// Copyright 2015 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
// +build bazel | ||
|
||
package bazel | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
inner "github.com/bazelbuild/rules_go/go/tools/bazel" | ||
) | ||
|
||
// Return true iff this library was built with Bazel. | ||
func BuiltWithBazel() bool { | ||
return true | ||
} | ||
|
||
// FindBinary is a convenience wrapper around the rules_go variant. | ||
func FindBinary(pkg, name string) (string, bool) { | ||
return inner.FindBinary(pkg, name) | ||
} | ||
|
||
// Runfile is a convenience wrapper around the rules_go variant. | ||
func Runfile(path string) (string, error) { | ||
return inner.Runfile(path) | ||
} | ||
|
||
// RunfilePath is a convenience wrapper around the rules_go variant. | ||
func RunfilesPath() (string, error) { | ||
return inner.RunfilesPath() | ||
} | ||
|
||
// TestTmpDir is a convenience wrapper around the rules_go variant. | ||
func TestTmpDir() string { | ||
return inner.TestTmpDir() | ||
} | ||
|
||
// Updates the current environment to use the Go toolchain that Bazel built this | ||
// binary/test with (updates the `PATH`/`GOROOT`/`GOCACHE` environment | ||
// variables). | ||
// If you want to use this function, your binary/test target MUST have | ||
// `@go_sdk//:files` in its `data` -- this will make sure the whole toolchain | ||
// gets pulled into the sandbox as well. Generally, this function should only | ||
// be called in init(). | ||
func SetGoEnv() { | ||
gobin, err := Runfile("bin/go") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := os.Setenv("PATH", fmt.Sprintf("%s%c%s", filepath.Dir(gobin), os.PathListSeparator, os.Getenv("PATH"))); err != nil { | ||
panic(err) | ||
} | ||
if err := os.Setenv("GOROOT", filepath.Dir(filepath.Dir(gobin))); err != nil { | ||
panic(err) | ||
} | ||
if err := os.Setenv("GOCACHE", path.Join(inner.TestTmpDir(), ".gocache")); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Name of the environment variable containing the bazel target path | ||
// (//pkg/cmd/foo:bar). | ||
const testTargetEnv = "TEST_TARGET" | ||
|
||
// RelativeTestTargetPath returns relative path to the package | ||
// of the current test. | ||
func RelativeTestTargetPath() string { | ||
target := os.Getenv(testTargetEnv) | ||
if target == "" { | ||
return "" | ||
} | ||
|
||
// Drop target name. | ||
if last := strings.LastIndex(target, ":"); last > 0 { | ||
target = target[:last] | ||
} | ||
return strings.TrimPrefix(target, "//") | ||
} |
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,46 @@ | ||
// Copyright 2015 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
// +build !bazel | ||
|
||
package bazel | ||
|
||
// This file contains stub implementations for non-bazel builds. | ||
// See bazel.go for full documentation on the contracts of these functions. | ||
|
||
// BuiltWithBazel returns true iff this library was built with Bazel. | ||
func BuiltWithBazel() bool { | ||
return false | ||
} | ||
|
||
// Runfile is not implemented. | ||
func Runfile(string) (string, error) { | ||
panic("not built with Bazel") | ||
} | ||
|
||
// RunfilesPath is not implemented. | ||
func RunfilesPath() (string, error) { | ||
panic("not built with Bazel") | ||
} | ||
|
||
// TestTmpDir is not implemented. | ||
func TestTmpDir() string { | ||
panic("not built with Bazel") | ||
} | ||
|
||
// RelativeTestTargetPath is not implemented. | ||
func RelativeTestTargetPath() string { | ||
panic("not built with Bazel") | ||
} | ||
|
||
// SetGoEnv is not implemented. | ||
func SetGoEnv() { | ||
panic("not built with Bazel") | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.