Skip to content

Commit

Permalink
Added unit tests for clippy_aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 22, 2021
1 parent ce8005c commit e26a20f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/clippy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ load(
"rust_test",
)

package(default_visibility = ["//test:__subpackages__"])

# Declaration of passing targets.

rust_binary(
Expand Down
5 changes: 5 additions & 0 deletions test/unit/clippy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":clippy_test.bzl", "clippy_test_suite")

clippy_test_suite(
name = "clippy_test_suite",
)
112 changes: 112 additions & 0 deletions test/unit/clippy/clippy_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""Unittest to verify compile_data (attribute) propagation"""

load("@bazel_skylib//lib:unittest.bzl", "analysistest")
load("//rust:defs.bzl", "rust_clippy_aspect")
load("//test/unit:common.bzl", "assert_argv_contains", "assert_list_contains_adjacent_elements")

def _find_clippy_action(actions):
for action in actions:
if action.mnemonic == "Clippy":
return action
fail("Failed to find Clippy action")

def _clippy_action_has_flag_impl(ctx, flags):
env = analysistest.begin(ctx)

# TODO: Replace with `analysistest.target_under_test(env)` upstream fix
# https://github.com/bazelbuild/bazel-skylib/pull/299
target = env.ctx.attr.target_under_test_with_aspect

clippy_action = _find_clippy_action(target.actions)

# Ensure each flag is present in the clippy action
for flag in flags:
assert_argv_contains(
env,
clippy_action,
flag,
)

clippy_checks = target[OutputGroupInfo].clippy_checks.to_list()
if len(clippy_checks) > 1:
fail("clippy_checks is only expected to contain 1 file")

# Ensure the arguments to generate the marker file are present in
# the clippy action
assert_list_contains_adjacent_elements(
env,
clippy_action.argv,
[
"--touch-file",
clippy_checks[0].path,
],
)

return analysistest.end(env)

def _binary_clippy_action_has_warnings_flag_test_impl(ctx):
return _clippy_action_has_flag_impl(
ctx,
["-Dwarnings"],
)

def _library_clippy_action_has_warnings_flag_test_impl(ctx):
return _clippy_action_has_flag_impl(
ctx,
["-Dwarnings"],
)

def _test_clippy_action_has_warnings_flag_test_impl(ctx):
return _clippy_action_has_flag_impl(
ctx,
[
"-Dwarnings",
"--test",
],
)

def make_clippy_aspect_unittest(impl):
return analysistest.make(
impl,
attrs = {
# We can't just use target_under_test because we need to add our own aspect to the attribute.
# See https://github.com/bazelbuild/bazel-skylib/pull/299
"target_under_test_with_aspect": attr.label(aspects = [rust_clippy_aspect], mandatory = True),
},
)

binary_clippy_action_has_warnings_flag_test = make_clippy_aspect_unittest(_binary_clippy_action_has_warnings_flag_test_impl)
library_clippy_action_has_warnings_flag_test = make_clippy_aspect_unittest(_library_clippy_action_has_warnings_flag_test_impl)
test_clippy_action_has_warnings_flag_test = make_clippy_aspect_unittest(_test_clippy_action_has_warnings_flag_test_impl)

def clippy_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name (str): Name of the macro.
"""

binary_clippy_action_has_warnings_flag_test(
name = "binary_clippy_action_has_warnings_flag_test",
target_under_test = Label("//test/clippy:ok_binary"),
target_under_test_with_aspect = Label("//test/clippy:ok_binary"),
)
library_clippy_action_has_warnings_flag_test(
name = "library_clippy_action_has_warnings_flag_test",
target_under_test = Label("//test/clippy:ok_library"),
target_under_test_with_aspect = Label("//test/clippy:ok_library"),
)
test_clippy_action_has_warnings_flag_test(
name = "test_clippy_action_has_warnings_flag_test",
target_under_test = Label("//test/clippy:ok_test"),
target_under_test_with_aspect = Label("//test/clippy:ok_test"),
)

native.test_suite(
name = name,
tests = [
":binary_clippy_action_has_warnings_flag_test",
":library_clippy_action_has_warnings_flag_test",
":test_clippy_action_has_warnings_flag_test",
],
)

0 comments on commit e26a20f

Please sign in to comment.