diff options
author | 2018-06-27 15:29:02 -0700 | |
---|---|---|
committer | 2018-06-27 15:30:32 -0700 | |
commit | d790ce4027a4361e2ea821f56d236d4fe0e732c3 (patch) | |
tree | 63c199df18f81d493b11e4f1fa6655ffb70de337 /src/test/java/com/google | |
parent | 57974d489d613f452d4bc9f4324262479e13400d (diff) |
Inspect post-evaluation exported variables to obtain rule names.
This is a much cleaner, more elegant approach than previous regex matching.
This still leaves room for unknown-name rule definitions, in case, for example, a user namespaces their rule definition not at the top level.
For example: "foo.bar = rule(...)"
RELNOTES: None.
PiperOrigin-RevId: 202380975
Diffstat (limited to 'src/test/java/com/google')
6 files changed, 109 insertions, 5 deletions
diff --git a/src/test/java/com/google/devtools/build/skydoc/BUILD b/src/test/java/com/google/devtools/build/skydoc/BUILD index d6daaa3398..e5a80fd54d 100644 --- a/src/test/java/com/google/devtools/build/skydoc/BUILD +++ b/src/test/java/com/google/devtools/build/skydoc/BUILD @@ -44,3 +44,10 @@ skydoc_test( input_file = "unknown_name_test/input.txt", skydoc = "//src/main/java/com/google/devtools/build/skydoc", ) + +skydoc_test( + name = "multiple_rules_test", + golden_file = "multiple_rules_test/golden.txt", + input_file = "multiple_rules_test/input.txt", + skydoc = "//src/main/java/com/google/devtools/build/skydoc", +) diff --git a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java index d80fe4ed35..ad524c6df5 100644 --- a/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java +++ b/src/test/java/com/google/devtools/build/skydoc/SkydocTest.java @@ -16,13 +16,17 @@ package com.google.devtools.build.skydoc; import static com.google.common.truth.Truth.assertThat; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.skylark.util.SkylarkTestCase; import com.google.devtools.build.lib.syntax.ParserInputSource; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.skydoc.rendering.RuleInfo; -import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -56,12 +60,63 @@ public final class SkydocTest extends SkylarkTestCase { ParserInputSource parserInputSource = ParserInputSource.create(bytes, file.asFragment()); - List<RuleInfo> ruleInfos = new SkydocMain().eval(parserInputSource); + ImmutableMap.Builder<String, RuleInfo> ruleInfoMap = ImmutableMap.builder(); + ImmutableList.Builder<RuleInfo> unexportedRuleInfos = ImmutableList.builder(); + + new SkydocMain().eval(parserInputSource, ruleInfoMap, unexportedRuleInfos); + Map<String, RuleInfo> ruleInfos = ruleInfoMap.build(); assertThat(ruleInfos).hasSize(1); - RuleInfo ruleInfo = Iterables.getOnlyElement(ruleInfos); - assertThat(ruleInfo.getDocString()).isEqualTo("This is my rule. It does stuff."); - assertThat(ruleInfo.getAttrNames()).containsExactly( + Entry<String, RuleInfo> ruleInfo = Iterables.getOnlyElement(ruleInfos.entrySet()); + assertThat(ruleInfo.getKey()).isEqualTo("my_rule"); + assertThat(ruleInfo.getValue().getDocString()).isEqualTo("This is my rule. It does stuff."); + assertThat(ruleInfo.getValue().getAttrNames()).containsExactly( "first", "second", "third", "fourth"); + assertThat(unexportedRuleInfos.build()).isEmpty(); + } + + @Test + public void testMultipleRuleNames() throws Exception { + Path file = + scratch.file( + "/test/test.bzl", + "def rule_impl(ctx):", + " return struct()", + "", + "rule_one = rule(", + " doc = 'Rule one',", + " implementation = rule_impl,", + ")", + "", + "rule(", + " doc = 'This rule is not named',", + " implementation = rule_impl,", + ")", + "", + "rule(", + " doc = 'This rule also is not named',", + " implementation = rule_impl,", + ")", + "", + "rule_two = rule(", + " doc = 'Rule two',", + " implementation = rule_impl,", + ")"); + byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize()); + + ParserInputSource parserInputSource = + ParserInputSource.create(bytes, file.asFragment()); + + ImmutableMap.Builder<String, RuleInfo> ruleInfoMap = ImmutableMap.builder(); + ImmutableList.Builder<RuleInfo> unexportedRuleInfos = ImmutableList.builder(); + + new SkydocMain().eval(parserInputSource, ruleInfoMap, unexportedRuleInfos); + + assertThat(ruleInfoMap.build().keySet()).containsExactly("rule_one", "rule_two"); + + assertThat(unexportedRuleInfos.build().stream() + .map(ruleInfo -> ruleInfo.getDocString()) + .collect(Collectors.toList())) + .containsExactly("This rule is not named", "This rule also is not named"); } } diff --git a/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/golden.txt b/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/golden.txt new file mode 100644 index 0000000000..b1a4a66325 --- /dev/null +++ b/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/golden.txt @@ -0,0 +1,12 @@ +my_rule +This is my rule. It does stuff. +first,second + +other_rule +This is another rule. +third,fourth + +yet_another_rule +This is yet another rule +fifth + diff --git a/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/input.txt b/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/input.txt new file mode 100644 index 0000000000..b1c0538231 --- /dev/null +++ b/src/test/java/com/google/devtools/build/skydoc/multiple_rules_test/input.txt @@ -0,0 +1,28 @@ +def my_rule_impl(ctx): + return struct() + +my_rule = rule( + implementation = my_rule_impl, + doc = "This is my rule. It does stuff.", + attrs = { + "first": attr.label(mandatory = True, allow_files = True, single_file = True), + "second": attr.string_dict(mandatory = True), + }, +) + +other_rule = rule( + implementation = my_rule_impl, + doc = "This is another rule.", + attrs = { + "third": attr.label(mandatory = True, allow_files = True, single_file = True), + "fourth": attr.string_dict(mandatory = True), + }, +) + +yet_another_rule = rule( + implementation = my_rule_impl, + doc = "This is yet another rule", + attrs = { + "fifth": attr.label(mandatory = True, allow_files = True, single_file = True), + }, +) diff --git a/src/test/java/com/google/devtools/build/skydoc/simple_test/golden.txt b/src/test/java/com/google/devtools/build/skydoc/simple_test/golden.txt index 0bf3a8c96a..50b71f076b 100644 --- a/src/test/java/com/google/devtools/build/skydoc/simple_test/golden.txt +++ b/src/test/java/com/google/devtools/build/skydoc/simple_test/golden.txt @@ -1,3 +1,4 @@ my_rule This is my rule. It does stuff. first,second,third,fourth + diff --git a/src/test/java/com/google/devtools/build/skydoc/unknown_name_test/golden.txt b/src/test/java/com/google/devtools/build/skydoc/unknown_name_test/golden.txt index 9b49c77015..ab79bcf5d6 100644 --- a/src/test/java/com/google/devtools/build/skydoc/unknown_name_test/golden.txt +++ b/src/test/java/com/google/devtools/build/skydoc/unknown_name_test/golden.txt @@ -1,2 +1,3 @@ <unknown name> first,second,third,fourth + |