Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for rust_clippy_aspect #851

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 properties of clippy rules"""

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_aspect_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:
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
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_aspect_action_has_warnings_flag_test_impl(ctx):
return _clippy_aspect_action_has_flag_impl(
ctx,
["-Dwarnings"],
)

def _library_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
return _clippy_aspect_action_has_flag_impl(
ctx,
["-Dwarnings"],
)

def _test_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
return _clippy_aspect_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_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_binary_clippy_aspect_action_has_warnings_flag_test_impl)
library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_library_clippy_aspect_action_has_warnings_flag_test_impl)
test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_test_clippy_aspect_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_aspect_action_has_warnings_flag_test(
name = "binary_clippy_aspect_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_aspect_action_has_warnings_flag_test(
name = "library_clippy_aspect_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_aspect_action_has_warnings_flag_test(
name = "test_clippy_aspect_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_aspect_action_has_warnings_flag_test",
":library_clippy_aspect_action_has_warnings_flag_test",
":test_clippy_aspect_action_has_warnings_flag_test",
],
)