Skip to content

Commit

Permalink
allow deps to include test targets (#13)
Browse files Browse the repository at this point in the history
Change-Id: I228e14a393bc1b3e95b82a07ac91425cf679591e
  • Loading branch information
oliverlee authored Aug 23, 2024
1 parent f84de51 commit 0c9f94f
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 6 deletions.
38 changes: 32 additions & 6 deletions apply_fixes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ multiple times to a header when running on multiple targets.
load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@local_bazel_version//:bazel_version.bzl", "BAZEL_VERSION")
load("//private:functional.bzl", fn = "functional")
load(":aspects.bzl", "export_fixes")

def _do_verify_deps(ctx, out):
Expand Down Expand Up @@ -103,6 +104,17 @@ cd "$BUILD_WORKSPACE_DIRECTORY"
},
)

def _deduced_label(s):
label = str(Label(s))

def _implicit_workspace(x):
idx = x.find("//")
return idx < 1 or fn.empty(x[:idx].strip("@"))

return (
fn.add(*(label.rpartition("//")[-2:])) if _implicit_workspace(s) else label
)

ctx.actions.run_shell(
inputs = depset(direct = [query]),
outputs = [out],
Expand Down Expand Up @@ -138,7 +150,7 @@ touch {outfile}
outfile = out.path,
query = query.path,
actual_deps = " ".join([
shell.quote(str(d.label).strip("@"))
shell.quote(_deduced_label(d))
for d in ctx.attr.deps
]),
pattern = ctx.attr.desired_deps,
Expand Down Expand Up @@ -173,9 +185,8 @@ def _verify_deps_impl(ctx):
_verify_deps = rule(
implementation = _verify_deps_impl,
attrs = {
"deps": attr.label_list(
providers = [CcInfo],
),
# avoid using a label_list in case some deps are test targets
"deps": attr.string_list(),
"desired_deps": attr.string(),
"bazel_bin": attr.string(
default = "bazel",
Expand Down Expand Up @@ -282,8 +293,8 @@ def apply_fixes(
`bazel_bin`), which may not have the same configuration as the
parent process.
Note that the following issue is may still present. `.bazelignore`
can be defined ignore convenience symlinks.
Note that the following issue may still present. `.bazelignore`
can be defined to ignore convenience symlinks.
https://github.com/bazelbuild/bazel/issues/10653
bazel_bin: `string`; default is `bazel`
Expand All @@ -304,6 +315,21 @@ def apply_fixes(
)
```
"""

# Create a test target that depends on deps. We need to use a test target
# in case any deps are tests. We use this to check that all deps are valid
# targets, which otherwise will not happen at build time. The <name>.verify
# target takes deps as a string_list and the <name> target is tagged manual.
#
#
# https://github.com/bazelbuild/bazel/issues/6842
# https://github.com/bazelbuild/bazel/issues/14294
native.sh_test(
name = name + ".dummy",
srcs = [Label("//private:true.bash")],
data = deps,
)

_verify_deps(
name = name + ".verify",
deps = deps,
Expand Down
1 change: 1 addition & 0 deletions example/dep-cc_test/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-dep-cc_test
8 changes: 8 additions & 0 deletions example/dep-cc_test/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
common --enable_bzlmod=false

build:clang-tidy --aspects=@rules_clang_tidy//:aspects.bzl%check
build:clang-tidy --output_groups=report

build:clang-tidy-export-fixes --aspects=@rules_clang_tidy//:aspects.bzl%export_fixes
build:clang-tidy-export-fixes --output_groups=report

17 changes: 17 additions & 0 deletions example/dep-cc_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cc_binary(
name = "foo",
srcs = ["foo.cpp"],
visibility = [
"//fail:__pkg__",
"//pass:__pkg__",
],
)

cc_test(
name = "bar",
srcs = ["bar.cpp"],
visibility = [
"//fail:__pkg__",
"//pass:__pkg__",
],
)
8 changes: 8 additions & 0 deletions example/dep-cc_test/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local_repository(
name = "rules_clang_tidy",
path = "../..",
)

load("@rules_clang_tidy//:dependencies.bzl", "rules_clang_tidy_dependencies")

rules_clang_tidy_dependencies()
4 changes: 4 additions & 0 deletions example/dep-cc_test/bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto main() -> int
{
return 1;
}
10 changes: 10 additions & 0 deletions example/dep-cc_test/fail/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_clang_tidy//:defs.bzl", "apply_fixes")

apply_fixes(
name = "build_fail",
deps = [
"//:bar",
"//:baz", # doesn't exist
"//:foo",
],
)
4 changes: 4 additions & 0 deletions example/dep-cc_test/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
auto main() -> int
{
return 1;
}
9 changes: 9 additions & 0 deletions example/dep-cc_test/pass/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_clang_tidy//:defs.bzl", "apply_fixes")

apply_fixes(
name = "build_pass",
deps = [
"//:bar",
"//:foo",
],
)
4 changes: 4 additions & 0 deletions private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports_files(
["true.bash"],
visibility = ["//visibility:public"],
)
4 changes: 4 additions & 0 deletions private/functional.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def _add(head, *tail):
def _bind_front(f, *front):
return lambda *back: f(*(tuple(front) + tuple(back)))

def _empty(x):
return not len(x)

functional = struct(
map = _map,
filter = _filter,
reduce = _reduce,
left_fold = _left_fold,
add = _add,
bind_front = _bind_front,
empty = _empty,
)
3 changes: 3 additions & 0 deletions private/true.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

true
2 changes: 2 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tidy_test(name = "check_misc-unused_test")

tidy_test(name = "check_extra-options_test")

tidy_test(name = "check_cc_test-deps_test")

tidy_test(name = "fix_script_test")

tidy_test(name = "fix_rule_test")
Expand Down
22 changes: 22 additions & 0 deletions test/check_cc_test-deps_test.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euxo pipefail

source test/prelude.bash

check_setup example/dep-cc_test

# This sets a 15 second idle timeout
# https://github.com/bazelbuild/bazel/issues/11062
unset TEST_TMPDIR

bazel \
--bazelrc="$test_bazelrc" \
build \
//pass/...

bazel \
--bazelrc="$test_bazelrc" \
build \
//fail/... 2>&1 | tee "$log" || true

grep "no such target '//:baz'" "$log"

0 comments on commit 0c9f94f

Please sign in to comment.