Skip to content

Commit

Permalink
Update rules_go + other changes (#558)
Browse files Browse the repository at this point in the history
* Updated to rules_go at master to get compatibility fixes, new features for
minimal module compatibility, and go_bazel_test.
* Rewrote //internal:repository_rules_test to //internal:go_repository_test.
The old test was broken by incompatible changes in new versions of Bazel.
The new test uses a new integration testing framework. We should be able
to improve go_repository test coverage in the future.
* Added //internal/language/test_filegroup, an extension that generates
all_files filegroup targets in each package. This is needed for
go_bazel_test, since it explicitly depends files in repositories under test.
* Generated filegroups with the new extension.
  • Loading branch information
Jay Conrod authored Jun 23, 2019
1 parent 221457a commit 9baa0ab
Show file tree
Hide file tree
Showing 38 changed files with 611 additions and 391 deletions.
54 changes: 53 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
load("@io_bazel_rules_go//go:def.bzl", "nogo")
load("//:def.bzl", "gazelle")
load("//:def.bzl", "gazelle", "gazelle_binary")

# gazelle:prefix github.com/bazelbuild/bazel-gazelle
# gazelle:exclude vendor
# gazelle:exclude third_party
gazelle(
name = "gazelle",
command = "fix",
gazelle = ":gazelle_local",
)

gazelle_binary(
name = "gazelle_local",
languages = [
"//language/proto:go_default_library",
"//language/go:go_default_library",
"//internal/language/test_filegroup:go_default_library",
],
)

nogo(
Expand All @@ -14,3 +26,43 @@ nogo(
)

exports_files(["WORKSPACE"])

filegroup(
name = "all_files",
testonly = True,
# keep
srcs = glob(["vendor/**"]) + [
".bazelrc",
".gitignore",
".travis.yml",
"AUTHORS",
"BUILD.bazel",
"CODEOWNERS",
"CONTRIBUTING.md",
"CONTRIBUTORS",
"Design.rst",
"LICENSE",
"README.rst",
"WORKSPACE",
"def.bzl",
"deps.bzl",
"extend.rst",
"go.mod",
"go.sum",
"repository.rst",
"//cmd:all_files",
"//config:all_files",
"//flag:all_files",
"//internal:all_files",
"//label:all_files",
"//language:all_files",
"//merger:all_files",
"//pathtools:all_files",
"//repo:all_files",
"//resolve:all_files",
"//rule:all_files",
"//testtools:all_files",
"//walk:all_files",
],
visibility = ["//visibility:public"],
)
9 changes: 5 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
workspace(name = "bazel_gazelle")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
git_repository(
name = "io_bazel_rules_go",
sha256 = "6776d68ebb897625dead17ae510eac3d5f6342367327875210df44dbe2aeeb19",
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.17.1/rules_go-0.17.1.tar.gz"],
commit = "fabf03c1cd31bcf15fb945d932cef322b242be3a",
remote = "https://github.com/bazelbuild/rules_go",
shallow_since = "1561303606 -0400",
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
Expand Down
11 changes: 11 additions & 0 deletions cmd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
filegroup(
name = "all_files",
testonly = True,
srcs = [
"//cmd/autogazelle:all_files",
"//cmd/fetch_repo:all_files",
"//cmd/gazelle:all_files",
"//cmd/move_labels:all_files",
],
visibility = ["//visibility:public"],
)
14 changes: 14 additions & 0 deletions cmd/autogazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ go_binary(
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"README.rst",
"autogazelle.bash",
"autogazelle.go",
"client_unix.go",
"server_unix.go",
],
visibility = ["//visibility:public"],
)
13 changes: 13 additions & 0 deletions cmd/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ go_test(
embed = [":go_default_library"],
deps = ["@org_golang_x_tools//go/vcs:go_default_library"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"fetch_repo.go",
"fetch_repo_test.go",
"module.go",
"vcs.go",
],
visibility = ["//visibility:public"],
)
22 changes: 22 additions & 0 deletions cmd/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,25 @@ go_test(
"@com_github_bazelbuild_buildtools//build:go_default_library",
],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"diff.go",
"diff_test.go",
"fix.go",
"fix-update.go",
"fix_test.go",
"gazelle.go",
"integration_test.go",
"langs.go",
"metaresolver.go",
"print.go",
"update-repos.go",
"update_repos_test.go",
"version.go",
],
visibility = ["//visibility:public"],
)
11 changes: 11 additions & 0 deletions cmd/move_labels/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ go_test(
embed = [":go_default_library"],
deps = ["//testtools:go_default_library"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"move_labels.go",
"move_labels_test.go",
],
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ go_test(
embed = [":go_default_library"],
deps = ["//rule:go_default_library"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"config.go",
"config_test.go",
"constants.go",
],
visibility = ["//visibility:public"],
)
10 changes: 10 additions & 0 deletions flag/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ go_library(
importpath = "github.com/bazelbuild/bazel-gazelle/flag",
visibility = ["//visibility:public"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"flag.go",
],
visibility = ["//visibility:public"],
)
70 changes: 28 additions & 42 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test")

_REPOSITORY_RULES_TEST_WORKSPACE = """
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
# TODO(jayconrod): fix bazel_test so this isn't necessary
register_toolchains(
"@go_sdk//:go_darwin_amd64",
"@go_sdk//:go_linux_amd64",
"@go_sdk//:go_windows_amd64",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies(go_sdk = "go_sdk")
go_repository(
name = "errors_go_git",
importpath = "github.com/pkg/errors",
commit = "30136e27e2ac8d167177e8a583aa4c3fea5be833",
patches = ["@bazel_gazelle//internal:repository_rules_test_errors.patch"],
patch_args = ["-p1"],
)
go_repository(
name = "errors_go_mod",
importpath = "github.com/pkg/errors",
version = "v0.8.1",
sum ="h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
)
"""

bazel_test(
name = "repository_rules_test",
command = "build",
externals = ["@bazel_gazelle//:WORKSPACE"],
extra_files = ["repository_rules_test_errors.patch"],
targets = [
"@errors_go_git//:errors",
"@errors_go_mod//:go_default_library",
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

# gazelle:exclude go_repository_test.go
go_bazel_test(
name = "go_repository_test",
srcs = ["go_repository_test.go"],
rule_files = [
"@bazel_gazelle//:all_files",
"@io_bazel_rules_go//:all_files",
],
workspace = _REPOSITORY_RULES_TEST_WORKSPACE,
)

# TODO(jayconrod): test fetch_repo error cases.
Expand All @@ -52,3 +19,22 @@ exports_files(
],
visibility = ["//visibility:public"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"gazelle.bash.in",
"gazelle_binary.bzl",
"go_repository.bzl",
"list_repository_tools_srcs.go",
"overlay_repository.bzl",
"repository_rules_test_errors.patch",
"//internal/gazellebinarytest:all_files",
"//internal/language:all_files",
"//internal/version:all_files",
"//internal/wspace:all_files",
],
visibility = ["//visibility:public"],
)
11 changes: 11 additions & 0 deletions internal/gazellebinarytest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ go_test(
rundir = ".",
deps = ["//testtools:go_default_library"],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"gazellebinary_test.go",
"xlang.go",
],
visibility = ["//visibility:public"],
)
72 changes: 72 additions & 0 deletions internal/go_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright 2019 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 go_repository_test

import (
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

const mainWorkspace = `
-- WORKSPACE --
local_repository(
name = "io_bazel_rules_go",
path = "../io_bazel_rules_go",
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains(go_version = "host")
local_repository(
name = "bazel_gazelle",
path = "../bazel_gazelle",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()
go_repository(
name = "errors_go_git",
importpath = "github.com/pkg/errors",
commit = "30136e27e2ac8d167177e8a583aa4c3fea5be833",
patches = ["@bazel_gazelle//internal:repository_rules_test_errors.patch"],
patch_args = ["-p1"],
)
go_repository(
name = "errors_go_mod",
importpath = "github.com/pkg/errors",
version = "v0.8.1",
sum ="h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
)
-- BUILD.bazel --
`

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{Main: mainWorkspace})
}

func TestBuild(t *testing.T) {
if err := bazel_testing.RunBazel("build", "@errors_go_git//:errors", "@errors_go_mod//:go_default_library"); err != nil {
t.Fatal(err)
}
}
6 changes: 6 additions & 0 deletions internal/language/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
filegroup(
name = "all_files",
testonly = True,
srcs = ["//internal/language/test_filegroup:all_files"],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 9baa0ab

Please sign in to comment.