From a49b2555c61b9acbba490b0d683b4ef5612f4db8 Mon Sep 17 00:00:00 2001 From: Martin Medler Date: Thu, 28 Dec 2023 11:52:55 +0100 Subject: [PATCH] Add dedicated integration tests for using DWYU inside a custom rule --- README.md | 2 +- test/aspect/recursion/BUILD | 27 +++------- test/aspect/recursion/README.md | 2 - test/aspect/rule_using_aspect/BUILD | 51 +++++++++++++++++++ test/aspect/rule_using_aspect/README.md | 1 + test/aspect/rule_using_aspect/a.h | 6 +++ test/aspect/rule_using_aspect/aspect.bzl | 4 ++ test/aspect/rule_using_aspect/b.h | 6 +++ test/aspect/rule_using_aspect/c.h | 6 +++ test/aspect/rule_using_aspect/main.cpp | 5 ++ .../{recursion => rule_using_aspect}/rule.bzl | 2 +- .../test_dwyu_rule.py} | 2 +- .../test_recursive_dwyu_rule.py} | 4 +- ...est_recursive_dwyu_rule_with_impl_deps.py} | 4 +- .../rule_using_aspect/use_impl_deps.cpp | 5 ++ test/aspect/test_case_test.py | 2 +- 16 files changed, 99 insertions(+), 30 deletions(-) create mode 100644 test/aspect/rule_using_aspect/BUILD create mode 100644 test/aspect/rule_using_aspect/README.md create mode 100644 test/aspect/rule_using_aspect/a.h create mode 100644 test/aspect/rule_using_aspect/aspect.bzl create mode 100644 test/aspect/rule_using_aspect/b.h create mode 100644 test/aspect/rule_using_aspect/c.h create mode 100644 test/aspect/rule_using_aspect/main.cpp rename test/aspect/{recursion => rule_using_aspect}/rule.bzl (88%) rename test/aspect/{recursion/test_succeeding_dwyu_rule.py => rule_using_aspect/test_dwyu_rule.py} (73%) rename test/aspect/{recursion/test_failing_dwyu_rule.py => rule_using_aspect/test_recursive_dwyu_rule.py} (66%) rename test/aspect/{recursion/test_dwyu_rule_with_impl_deps.py => rule_using_aspect/test_recursive_dwyu_rule_with_impl_deps.py} (75%) create mode 100644 test/aspect/rule_using_aspect/use_impl_deps.cpp diff --git a/README.md b/README.md index a959d8c8..d88777ee 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ You can invoke the aspect from within a rule. This can be useful to make the execution part of a bazel build without having to manually execute the longish aspect build command. The Bazel documentation for invoking an aspect from within a rule can be found [here](https://bazel.build/rules/aspects#invoking_the_aspect_from_a_rule). -A concrete example for doing so for the DWYU aspect can be found in [a rule in the recursion test cases](test/aspect/recursion/rule.bzl). +A concrete example for doing so for the DWYU aspect can be found in [aspect integration tests](test/aspect/rule_using_aspect). # Features diff --git a/test/aspect/recursion/BUILD b/test/aspect/recursion/BUILD index b7c29434..d7b91765 100644 --- a/test/aspect/recursion/BUILD +++ b/test/aspect/recursion/BUILD @@ -1,5 +1,3 @@ -load("//test/aspect/recursion:rule.bzl", "dwyu_rule_direct", "dwyu_rule_recursive", "dwyu_rule_recursive_with_impl_deps") - # Has a diamond dependency towards C through A and B # main specifies its dependencies correctly, but C has an error cc_binary( @@ -11,15 +9,15 @@ cc_binary( ], ) -dwyu_rule_direct( - name = "dwyu_direct_main", - deps = [":main"], +cc_library( + name = "use_impl_deps", + srcs = ["use_impl_deps.cpp"], + implementation_deps = [":a"], ) -dwyu_rule_recursive( - name = "dwyu_recursive_main", - deps = [":main"], -) +## +## Support Targets +## cc_library( name = "a", @@ -51,14 +49,3 @@ cc_library( name = "e", hdrs = ["e.h"], ) - -cc_library( - name = "use_impl_deps", - srcs = ["use_impl_deps.cpp"], - implementation_deps = [":a"], -) - -dwyu_rule_recursive_with_impl_deps( - name = "dwyu_recursive_use_impl_deps", - deps = [":use_impl_deps"], -) diff --git a/test/aspect/recursion/README.md b/test/aspect/recursion/README.md index 1c2c360c..9afa9f50 100644 --- a/test/aspect/recursion/README.md +++ b/test/aspect/recursion/README.md @@ -1,3 +1 @@ The aspect can analyze either solely the targets is being executed on or it can analyze the whole dependency tree. - -One can use a rule to perform the aspect analysis as part of a normal build. diff --git a/test/aspect/rule_using_aspect/BUILD b/test/aspect/rule_using_aspect/BUILD new file mode 100644 index 00000000..3da3f05c --- /dev/null +++ b/test/aspect/rule_using_aspect/BUILD @@ -0,0 +1,51 @@ +load("//test/aspect/rule_using_aspect:rule.bzl", "dwyu_rule_direct", "dwyu_rule_recursive", "dwyu_rule_recursive_with_impl_deps") + +dwyu_rule_direct( + name = "dwyu_direct_main", + deps = [":main"], +) + +dwyu_rule_recursive( + name = "dwyu_recursive_main", + deps = [":main"], +) + +dwyu_rule_recursive_with_impl_deps( + name = "dwyu_recursive_with_impl_deps", + deps = [":use_impl_deps"], +) + +## +## Support Targets +## + +cc_binary( + name = "main", + srcs = ["main.cpp"], + deps = [":a"], +) + +cc_library( + name = "a", + hdrs = ["a.h"], + deps = [":b"], +) + +cc_library( + name = "b", + hdrs = ["b.h"], + deps = [ + ":c", # unused dependency + ], +) + +cc_library( + name = "c", + hdrs = ["c.h"], +) + +cc_library( + name = "use_impl_deps", + srcs = ["use_impl_deps.cpp"], + implementation_deps = [":a"], +) diff --git a/test/aspect/rule_using_aspect/README.md b/test/aspect/rule_using_aspect/README.md new file mode 100644 index 00000000..204a361b --- /dev/null +++ b/test/aspect/rule_using_aspect/README.md @@ -0,0 +1 @@ +One can create a rule which executes the DWYU aspect as part of a normal build ond specific targets. diff --git a/test/aspect/rule_using_aspect/a.h b/test/aspect/rule_using_aspect/a.h new file mode 100644 index 00000000..5f4cda2e --- /dev/null +++ b/test/aspect/rule_using_aspect/a.h @@ -0,0 +1,6 @@ +#ifndef A_H +#define A_H + +#include "test/aspect/rule_using_aspect/b.h" + +#endif diff --git a/test/aspect/rule_using_aspect/aspect.bzl b/test/aspect/rule_using_aspect/aspect.bzl new file mode 100644 index 00000000..b6df64c1 --- /dev/null +++ b/test/aspect/rule_using_aspect/aspect.bzl @@ -0,0 +1,4 @@ +load("//:defs.bzl", "dwyu_aspect_factory") + +dwyu_recursive = dwyu_aspect_factory(recursive = True) +dwyu_recursive_impl_deps = dwyu_aspect_factory(recursive = True, use_implementation_deps = True) diff --git a/test/aspect/rule_using_aspect/b.h b/test/aspect/rule_using_aspect/b.h new file mode 100644 index 00000000..827afc87 --- /dev/null +++ b/test/aspect/rule_using_aspect/b.h @@ -0,0 +1,6 @@ +#ifndef B_H +#define B_H + +// Do nothing + +#endif diff --git a/test/aspect/rule_using_aspect/c.h b/test/aspect/rule_using_aspect/c.h new file mode 100644 index 00000000..2265c63c --- /dev/null +++ b/test/aspect/rule_using_aspect/c.h @@ -0,0 +1,6 @@ +#ifndef C_H +#define C_H + +// Do nothing + +#endif diff --git a/test/aspect/rule_using_aspect/main.cpp b/test/aspect/rule_using_aspect/main.cpp new file mode 100644 index 00000000..b26a3be6 --- /dev/null +++ b/test/aspect/rule_using_aspect/main.cpp @@ -0,0 +1,5 @@ +#include "test/aspect/rule_using_aspect/a.h" + +int main() { + return 0; +} diff --git a/test/aspect/recursion/rule.bzl b/test/aspect/rule_using_aspect/rule.bzl similarity index 88% rename from test/aspect/recursion/rule.bzl rename to test/aspect/rule_using_aspect/rule.bzl index 8bdae77f..06ed6322 100644 --- a/test/aspect/recursion/rule.bzl +++ b/test/aspect/rule_using_aspect/rule.bzl @@ -1,5 +1,5 @@ load("//test/aspect:aspect.bzl", "dwyu") -load("//test/aspect/recursion:aspect.bzl", "dwyu_recursive", "dwyu_recursive_impl_deps") +load("//test/aspect/rule_using_aspect:aspect.bzl", "dwyu_recursive", "dwyu_recursive_impl_deps") def _dwyu_rule_impl(ctx): # gather artifacts to make sure the aspect is executed diff --git a/test/aspect/recursion/test_succeeding_dwyu_rule.py b/test/aspect/rule_using_aspect/test_dwyu_rule.py similarity index 73% rename from test/aspect/recursion/test_succeeding_dwyu_rule.py rename to test/aspect/rule_using_aspect/test_dwyu_rule.py index e0260d36..fdd0e46b 100644 --- a/test/aspect/recursion/test_succeeding_dwyu_rule.py +++ b/test/aspect/rule_using_aspect/test_dwyu_rule.py @@ -5,6 +5,6 @@ class TestCase(TestCaseBase): def execute_test_logic(self) -> Result: expected = ExpectedResult(success=True) - actual = self._run_bazel_build(target="//test/aspect/recursion:dwyu_direct_main") + actual = self._run_bazel_build(target="//test/aspect/rule_using_aspect:dwyu_direct_main") return self._check_result(actual=actual, expected=expected) diff --git a/test/aspect/recursion/test_failing_dwyu_rule.py b/test/aspect/rule_using_aspect/test_recursive_dwyu_rule.py similarity index 66% rename from test/aspect/recursion/test_failing_dwyu_rule.py rename to test/aspect/rule_using_aspect/test_recursive_dwyu_rule.py index c4ec10f3..bbcbfe4f 100644 --- a/test/aspect/recursion/test_failing_dwyu_rule.py +++ b/test/aspect/rule_using_aspect/test_recursive_dwyu_rule.py @@ -4,7 +4,7 @@ class TestCase(TestCaseBase): def execute_test_logic(self) -> Result: - expected = ExpectedResult(success=False, unused_public_deps=["//test/aspect/recursion:e"]) - actual = self._run_bazel_build(target="//test/aspect/recursion:dwyu_recursive_main") + expected = ExpectedResult(success=False, unused_public_deps=["//test/aspect/rule_using_aspect:c"]) + actual = self._run_bazel_build(target="//test/aspect/rule_using_aspect:dwyu_recursive_main") return self._check_result(actual=actual, expected=expected) diff --git a/test/aspect/recursion/test_dwyu_rule_with_impl_deps.py b/test/aspect/rule_using_aspect/test_recursive_dwyu_rule_with_impl_deps.py similarity index 75% rename from test/aspect/recursion/test_dwyu_rule_with_impl_deps.py rename to test/aspect/rule_using_aspect/test_recursive_dwyu_rule_with_impl_deps.py index 91f5073d..0d4f9849 100644 --- a/test/aspect/recursion/test_dwyu_rule_with_impl_deps.py +++ b/test/aspect/rule_using_aspect/test_recursive_dwyu_rule_with_impl_deps.py @@ -4,9 +4,9 @@ class TestCase(TestCaseBase): def execute_test_logic(self) -> Result: - expected = ExpectedResult(success=False, unused_public_deps=["//test/aspect/recursion:e"]) + expected = ExpectedResult(success=False, unused_public_deps=["//test/aspect/rule_using_aspect:c"]) actual = self._run_bazel_build( - target="//test/aspect/recursion:dwyu_recursive_use_impl_deps", + target="//test/aspect/rule_using_aspect:dwyu_recursive_with_impl_deps", extra_args=["--experimental_cc_implementation_deps"], ) diff --git a/test/aspect/rule_using_aspect/use_impl_deps.cpp b/test/aspect/rule_using_aspect/use_impl_deps.cpp new file mode 100644 index 00000000..7b608f75 --- /dev/null +++ b/test/aspect/rule_using_aspect/use_impl_deps.cpp @@ -0,0 +1,5 @@ +#include "test/aspect/rule_using_aspect/a.h" + +int doSth() { + return 42; +} diff --git a/test/aspect/test_case_test.py b/test/aspect/test_case_test.py index eeab6954..0c925004 100644 --- a/test/aspect/test_case_test.py +++ b/test/aspect/test_case_test.py @@ -45,7 +45,7 @@ def test_get_name(self): self.assertEqual(self.unit.name, "foo") def test_get_default_aspect(self): - self.assertEqual(self.unit.default_aspect, "//test/aspect:aspect.bzl%dwyu_default_aspect") + self.assertEqual(self.unit.default_aspect, "//test/aspect:aspect.bzl%dwyu") @patch("subprocess.run") def test_get_success(self, _):