diff --git a/BUILD b/BUILD index c428cd66..2a53e126 100644 --- a/BUILD +++ b/BUILD @@ -1,5 +1,3 @@ -load("@bazel_gazelle//:def.bzl", "gazelle") - package_group( name = "internal", packages = ["//..."], @@ -15,11 +13,7 @@ filegroup( name = "all_bzl", srcs = [ ":root_bzl", - "//go:bzl", "//tools", ], visibility = ["//:internal"], ) - -# gazelle:prefix github.com/bazelbuild/bazel-integration-testing -gazelle(name = "gazelle") diff --git a/CODEOWNERS b/CODEOWNERS index 3a604f47..66349dae 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1,12 +1,8 @@ # Default owner * @ittaiz -* @achew22 # Java owners java/* @ittaiz javatests/* @ittaiz # Python owners - -# Go owners -go/* @jayconrod, @achew22 diff --git a/README.md b/README.md index a9f75063..15fbcf0a 100644 --- a/README.md +++ b/README.md @@ -49,25 +49,6 @@ load("@build_bazel_integration_testing//tools:repositories.bzl", "bazel_binaries #depend on the Bazel binaries, also accepts an array of versions bazel_binaries() -http_archive( - name = "io_bazel_rules_go", - sha256 = "f04d2373bcaf8aa09bccb08a98a57e721306c8f6043a2a0ee610fd6853dcde3d", - urls = [ - "https://github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz", - ], -) - -http_archive( - name = "bazel_gazelle", - sha256 = "7fc87f4170011201b1690326e8c16c5d802836e3a0d617d8f75c3af2b23180c4", - urls = [ - "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz", - ], -) - -load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies") - -go_rules_dependencies() ``` For JVM tests one would like to call the below instead of the above call to `bazel_binaries` since it does that plus more dependencies needed for `java_test`: ```python @@ -78,7 +59,18 @@ bazel_java_integration_test_deps() ## Usage ### [Jvm](java/README.md) ### Go -TODO + +[`rules_go`](https://github.com/bazelbuild/rules_go) already supports, +natively, an integration testing framework for validating your rules. If you +need to write integration tests, please refer to the +[godoc](https://godoc.org/github.com/bazelbuild/rules_go/go/tools/bazel_testing) +and associated +[`go_bazel_test` rule.](https://github.com/bazelbuild/rules_go/blob/master/go/tools/bazel_testing/def.bzl). +For example usage, `rules_go` itself is tested using this rule and many +packages inside of the +[`tests`](https://github.com/bazelbuild/rules_go/blob/master/tests/) package +can be used as examples. + ### Python TODO diff --git a/WORKSPACE b/WORKSPACE index 1157ee02..bc157d29 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -90,36 +90,6 @@ jvm_maven_import_external( ], ) -## golang - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_go", - sha256 = "f04d2373bcaf8aa09bccb08a98a57e721306c8f6043a2a0ee610fd6853dcde3d", - urls = [ - "https://github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz", - ], -) - -http_archive( - name = "bazel_gazelle", - sha256 = "d8c45ee70ec39a57e7a05e5027c32b1576cc7f16d9dd37135b0eddde45cf1b10", - urls = [ - "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.20.0/bazel-gazelle-v0.20.0.tar.gz", - ], -) - -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") - -go_rules_dependencies() - -go_register_toolchains() - -load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") - -gazelle_dependencies() - #### Use remote resources ## java diff --git a/bazel_integration_test.bzl b/bazel_integration_test.bzl index 1db545fd..9b158445 100644 --- a/bazel_integration_test.bzl +++ b/bazel_integration_test.bzl @@ -35,10 +35,6 @@ load( "//tools:import.bzl", bazel_external_dependency_archive_ = "bazel_external_dependency_archive", ) -load( - "//go:bazel_integration_test.bzl", - bazel_go_integration_test_ = "bazel_go_integration_test", -) print( "WARNING: bazel_integration_test.bzl is deprecated and will go away in " + @@ -59,5 +55,3 @@ bazel_java_integration_test_deps = bazel_java_integration_test_deps_ bazel_py_integration_test = bazel_py_integration_test_ bazel_external_dependency_archive = bazel_external_dependency_archive_ - -bazel_go_integration_test = bazel_go_integration_test_ diff --git a/go/BUILD b/go/BUILD deleted file mode 100644 index b02e08cc..00000000 --- a/go/BUILD +++ /dev/null @@ -1,34 +0,0 @@ -# gazelle:exclude bazel_test.go -# gazelle:ignore - -load("//:bazel_integration_test.bzl", "bazel_go_integration_test") -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -filegroup( - name = "bzl", - srcs = [ - "bazel_integration_test.bzl", - ], - visibility = ["//:internal"], -) - -bazel_go_integration_test( - name = "bazel_test", - srcs = [ - "bazel_test.go", # keep - ], - importpath = "github.com/bazelbuild/bazel-integration-testing/go_external_test", - deps = [ - ":go_default_library", - ], -) - -go_library( - name = "go_default_library", - srcs = ["bazel.go"], - importpath = "github.com/bazelbuild/bazel-integration-testing/go", - visibility = ["//visibility:public"], - deps = [ - "@io_bazel_rules_go//go/tools/bazel:go_default_library", # keep - ], -) diff --git a/go/bazel.go b/go/bazel.go deleted file mode 100644 index 791161bd..00000000 --- a/go/bazel.go +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Copyright 2017 The Bazel Authors. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package bazel_go - -import ( - "bytes" - "errors" - "fmt" - "io/ioutil" - "os" - "os/exec" - "runtime" - "strings" - "syscall" - - rules_go_bazel "github.com/bazelbuild/rules_go/go/tools/bazel" -) - -var _ = fmt.Printf - -var BazelVersion = "bazel_version was not set. This is an error." - -type TestingBazel struct { - // Path to the Bazel binary. - bazel string - - // Path to the testing temp directory. - tmpDir string -} - -func IsWindows() bool { - return runtime.GOOS == "windows" -} - -// New instance of a TestingBazel will be created and the program will cd into -// the appropriate working dir for that bazel client. -func New() (*TestingBazel, error) { - // Make a temporary dir in the OS specific tmpdir. - dir, err := ioutil.TempDir("", "testing") - if err != nil { - return nil, errors.New("Unable to make tempdir for bazel") - } - - err = os.Chdir(dir) - if err != nil { - return nil, errors.New("Unable to make tempdir for bazel") - } - - b := &TestingBazel{ - tmpDir: dir, - } - suffix := "" - if IsWindows() { - suffix = ".exe" - } - if !b.setAndUnpackBazel(fmt.Sprintf("build_bazel_bazel_%s/bazel%s", strings.Replace(BazelVersion, ".", "_", -1), suffix)) { - return nil, errors.New("Unable to find Bazel") - } - - return b, nil -} - -func (b *TestingBazel) setAndUnpackBazel(bazel string) bool { - bazel, err := b.Rlocation(bazel) - if err != nil { - return false - } - if _, err := os.Stat(bazel); !os.IsNotExist(err) { - b.bazel = bazel - // Unpack Bazel by running a command on it. - b.RunBazel([]string{"help"}) - return true - } - return false -} - -// ScratchFile creates a new file at the path you provided. -func (b *TestingBazel) ScratchDir(p string) error { - return os.MkdirAll(p, 0755) -} - -// ScratchFile creates a new file at the path you provided. If the file is in a -// directory that may not exist, use ScratchDir to make the directory. To set a -// mode on the file use ScratchFileWithMode. -func (b *TestingBazel) ScratchFile(p, content string) error { - return b.ScratchFileWithMode(p, content, 0644) -} - -// ScratchFile creates a new file at the path you provided with a specified -// mode (useful for setting executable bit). If the file is in a directory that -// may not exist, use ScratchDir to make the directory. -func (b *TestingBazel) ScratchFileWithMode(p, content string, mode os.FileMode) error { - if err := ioutil.WriteFile(p, []byte(content), mode); err != nil { - return err - } - return nil -} - -// GetTmpDir returns the path to the temp dir that will be used by this Bazel -// invocation. Prever using ScratchFile, ScratchDir, etc. if possible but if -// you're doing something extremely complicated, you can directly refer to the -// tmpDir. -func (b *TestingBazel) GetTmpDir() string { - return b.tmpDir -} - - -// GetBazel returns the path to the Bazel binary that is available. -func (b *TestingBazel) GetBazel() string { - return b.bazel -} - -func (b *TestingBazel) RunBazel(args []string) (int, string, string) { - cmd := exec.Command(b.bazel, args...) - - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - if err := cmd.Run(); err != nil { - if exitErr, ok := err.(*exec.ExitError); ok { - status := exitErr.Sys().(syscall.WaitStatus) - return status.ExitStatus(), stdout.String(), stderr.String() - } - } - - // When there isn't an error we know it exited successfully - return 0, stdout.String(), stderr.String() -} - -// Rlocation Returns the absolute path to a runfile. -func (b *TestingBazel) Rlocation(runfile string) (string, error) { - return rules_go_bazel.Runfile(runfile) -} diff --git a/go/bazel_integration_test.bzl b/go/bazel_integration_test.bzl deleted file mode 100644 index 9d25f7d7..00000000 --- a/go/bazel_integration_test.bzl +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2017 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Python integration test framework for using Bazel -load("//tools:common.bzl", "BAZEL_VERSIONS") -load("//tools:repositories.bzl", "bazel_binaries") -load("@io_bazel_rules_go//go:def.bzl", "go_test") - -def _make_compatible_version_list(): - toReturn = [] - for v in BAZEL_VERSIONS: - major, minor, patch = v.split(".") - if int(major) == 0 and int(minor) >= 7: - toReturn.append("%s.%s.%s" % (major, minor, patch)) - return toReturn - -RULES_GO_COMPATIBLE_BAZEL_VERSION = _make_compatible_version_list() - -def bazel_go_integration_test(name, - srcs, - deps = [], - data = [], - versions = RULES_GO_COMPATIBLE_BAZEL_VERSION, - **kwargs): - """A wrapper around go_test that create several go tests, one per version - of Bazel. - - Args: - versions: list of version of bazel to create a test for. Each test - will be named `/bazel`. - See go_test for the other arguments. - """ - for version in versions: - go_test( - name = "%s/bazel%s" % (name, version), - srcs = srcs, - deps = deps, - data = [ - "@build_bazel_bazel_%s//:bazel_binary" % version.replace(".", "_"), - ] + data, - x_defs = { - "github.com/bazelbuild/bazel-integration-testing/go.BazelVersion": version, - }, - **kwargs) diff --git a/go/bazel_test.go b/go/bazel_test.go deleted file mode 100644 index d88fcb0a..00000000 --- a/go/bazel_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package external_test - -import ( - "runtime/debug" - "strings" - "testing" - - bazel "github.com/bazelbuild/bazel-integration-testing/go" -) - -func must(t *testing.T, err error) { - if err != nil { - t.Errorf("Error: %s", err) - debug.PrintStack() - } -} - -func TestVersion(t *testing.T) { - if strings.Contains(bazel.BazelVersion, "error") { - t.Fatalf("Something is wrong with the compile and the BazelVersion variable wasn't set. This is a fatal error. Expected it to not contain error, but it was [%s]", bazel.BazelVersion) - } - - b, err := bazel.New() - if err != nil { - t.Fatal(err) - } - - must(t, b.ScratchFile("WORKSPACE", "")) - must(t, b.ScratchDir("demo/testing")) - exitCode, stdout, stderr := b.RunBazel([]string{"info", "release"}) - - if exitCode != 0 { - t.Errorf("Error code expected 0. Was: %d.\nStdout:\n%s\nStderr:\n%s", exitCode, stdout, stderr) - } - if !strings.Contains(stdout, "release "+bazel.BazelVersion) { - t.Errorf("Stdout didn't contain version (%s). Stdout:\n[%s]", bazel.BazelVersion, stdout) - } -} - -func TestSimpleShellscript(t *testing.T) { - b, err := bazel.New() - if err != nil { - t.Fatal(err) - } - - must(t, b.ScratchFile("WORKSPACE", "")) - must(t, b.ScratchDir("demo")) - must(t, b.ScratchDir("demo/testing")) - must(t, b.ScratchFile("demo/testing/BUILD", `sh_binary(name="testing", srcs=["test.sh"])`)) - must(t, b.ScratchFileWithMode("demo/testing/test.sh", `echo "Success!"`, 0755)) - - exitCode, stdout, stderr := b.RunBazel([]string{"run", "//demo/testing"}) - - if exitCode != 0 { - t.Errorf("Error code expected 0. Was: %d.\nStdout:\n%s\nStderr:\n%s", exitCode, stdout, stderr) - } - if !strings.Contains(stdout, "Success!") { - t.Errorf("Stdout didn't contain [Success!].\nStdout: [%s]", bazel.BazelVersion, stdout) - } -}