Skip to content

Commit

Permalink
Add dedicated integration tests for using DWYU inside a custom rule
Browse files Browse the repository at this point in the history
  • Loading branch information
martis42 committed Dec 28, 2023
1 parent 8a68934 commit a49b255
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 7 additions & 20 deletions test/aspect/recursion/BUILD
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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"],
)
2 changes: 0 additions & 2 deletions test/aspect/recursion/README.md
Original file line number Diff line number Diff line change
@@ -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.
51 changes: 51 additions & 0 deletions test/aspect/rule_using_aspect/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
1 change: 1 addition & 0 deletions test/aspect/rule_using_aspect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
One can create a rule which executes the DWYU aspect as part of a normal build ond specific targets.
6 changes: 6 additions & 0 deletions test/aspect/rule_using_aspect/a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef A_H
#define A_H

#include "test/aspect/rule_using_aspect/b.h"

#endif
4 changes: 4 additions & 0 deletions test/aspect/rule_using_aspect/aspect.bzl
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions test/aspect/rule_using_aspect/b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef B_H
#define B_H

// Do nothing

#endif
6 changes: 6 additions & 0 deletions test/aspect/rule_using_aspect/c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef C_H
#define C_H

// Do nothing

#endif
5 changes: 5 additions & 0 deletions test/aspect/rule_using_aspect/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test/aspect/rule_using_aspect/a.h"

int main() {
return 0;
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down
5 changes: 5 additions & 0 deletions test/aspect/rule_using_aspect/use_impl_deps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test/aspect/rule_using_aspect/a.h"

int doSth() {
return 42;
}
2 changes: 1 addition & 1 deletion test/aspect/test_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, _):
Expand Down

0 comments on commit a49b255

Please sign in to comment.