1
- attr_aspects = [
2
- "deps" ,
3
- "runtime_deps" ,
4
- "_scalalib" ,
5
- "_scalareflect" ,
6
- "_scalatest_reporter" ,
7
- ]
1
+ """
2
+ This test makes sure that the implicit rule dependencies are discoverable by
3
+ an IDE. We stuff all dependencies into _scala_toolchain so we just need to make
4
+ sure the targets we expect are there.
5
+ """
6
+ attr_aspects = ["_scala_toolchain" , "deps" ]
8
7
9
8
def _aspect_impl (target , ctx ):
10
- visited = [target .label .name ]
11
- for name in attr_aspects :
12
- if hasattr (ctx .rule .attr , name ):
13
- attr = getattr (ctx .rule .attr , name )
14
- # Need to handle whether attribute is label or label list
15
- children = attr if type (attr ) == "list" else [attr ]
16
- for child in children :
17
- if hasattr (child , "visited" ):
18
- visited += child .visited
9
+ visited = [str (target .label )]
10
+ for attr_name in attr_aspects :
11
+ if hasattr (ctx .rule .attr , attr_name ):
12
+ for dep in getattr (ctx .rule .attr , attr_name ):
13
+ if hasattr (dep , "visited" ):
14
+ visited += dep .visited
19
15
return struct (visited = visited )
20
16
21
17
test_aspect = aspect (
@@ -24,26 +20,55 @@ test_aspect = aspect(
24
20
)
25
21
26
22
def _rule_impl (ctx ):
27
- expected = [
28
- "dummy" ,
29
- "jar" , # This is scalatest since @scalatest/jar
30
- "scala-library" ,
31
- "scala-reflect" ,
32
- "scala-xml" , # dependency of test_reporter
33
- "test_reporter" ,
34
- ]
35
- # Remove duplicates and sort so can do simple comparison
36
- visited = sorted (depset (ctx .attr .scala_rule .visited ).to_list ())
37
- if visited == expected :
38
- content = "true"
39
- else :
40
- content = """
41
- echo Expected these rules to be visited by the aspect: 1>&2
42
- echo %s, 1>&2
43
- echo but got these instead: 1>&2
44
- echo %s 1>&2
45
- false
46
- """ % (', ' .join (expected ), ', ' .join (visited ))
23
+ expected_deps = {
24
+ "scala_library" : [
25
+ "//test/aspect:scala_library" ,
26
+ "@scala//:scala-library" ,
27
+ ],
28
+ "scala_test" : [
29
+ "//test/aspect:scala_test" ,
30
+ "@scala//:scala-library" ,
31
+ "@scalatest//jar:jar" ,
32
+ ],
33
+ "scala_junit_test" : [
34
+ "//test/aspect:scala_junit_test" ,
35
+ "@scala//:scala-library" ,
36
+ "@io_bazel_rules_scala_junit_junit//jar:jar" ,
37
+ "@io_bazel_rules_scala_org_hamcrest_hamcrest_core//jar:jar" ,
38
+ ],
39
+ "scala_specs2_junit_test" : [
40
+ "//test/aspect:scala_specs2_junit_test" ,
41
+ "@scala//:scala-library" ,
42
+ "@io_bazel_rules_scala_junit_junit//jar:jar" ,
43
+ "@io_bazel_rules_scala_org_hamcrest_hamcrest_core//jar:jar" ,
44
+ # From specs2/specs2.bzl:specs2_dependencies()
45
+ "@io_bazel_rules_scala_org_specs2_specs2_core//jar:jar" ,
46
+ "@io_bazel_rules_scala_org_specs2_specs2_common//jar:jar" ,
47
+ "@io_bazel_rules_scala_org_specs2_specs2_matcher//jar:jar" ,
48
+ "@io_bazel_rules_scala_org_scalaz_scalaz_effect//jar:jar" ,
49
+ "@io_bazel_rules_scala_org_scalaz_scalaz_core//jar:jar" ,
50
+ "@scala//:scala-xml" ,
51
+ "@scala//:scala-parser-combinators" ,
52
+ "@scala//:scala-library" ,
53
+ "@scala//:scala-reflect" ,
54
+ # From specs2/specs2_junit.bzl:specs2_junit_dependencies()
55
+ "@io_bazel_rules_scala_org_specs2_specs2_junit_2_11//jar:jar" ,
56
+ ],
57
+ }
58
+ content = ""
59
+ for target in ctx .attr .targets :
60
+ visited = sorted (target .visited )
61
+ expected = sorted (expected_deps [target .label .name ])
62
+ if visited != expected :
63
+ content += """
64
+ echo Expected these deps from {name}: 1>&2
65
+ echo {expected}, 1>&2
66
+ echo but got these instead: 1>&2
67
+ echo {visited} 1>&2
68
+ false # test returns 1 (and fails) if this is the final line
69
+ """ .format (name = target .label .name ,
70
+ expected = ', ' .join (expected ),
71
+ visited = ', ' .join (visited ))
47
72
ctx .file_action (
48
73
output = ctx .outputs .executable ,
49
74
content = content ,
@@ -52,7 +77,10 @@ def _rule_impl(ctx):
52
77
53
78
aspect_test = rule (
54
79
implementation = _rule_impl ,
55
- attrs = { "scala_rule" : attr .label (aspects = [test_aspect ]) },
80
+ attrs = {
81
+ # The targets whose dependencies we want to verify.
82
+ "targets" : attr .label_list (aspects = [test_aspect ]),
83
+ },
56
84
test = True ,
57
85
)
58
86
0 commit comments