diff options
author | 2018-06-21 11:55:44 -0700 | |
---|---|---|
committer | 2018-06-21 11:57:27 -0700 | |
commit | f24479d495df540bae634b5ff2dde3177585c2a3 (patch) | |
tree | ab16e9bf0c46f1f2b1ad70d6502003de553abd81 /src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java | |
parent | 9594d5e8dc465bb4526a2f15a4b547de7e1324a7 (diff) |
Take into account repository mapping when processing labels inside BUILD files within external repositories.
For example:
a/BUILD
genrule(
name = "a",
srcs = ["@x//:x.txt"],
outs = ["result.txt"],
cmd = "echo hello > \$(location result.txt)"
)
If the main workspace file references that repository with a rule:
local_repository(
name = "other_repo",
path = "../a",
repo_mapping = {"@x" : "@y"}
)
Then when a/BUILD is evaluated, the string "@x//:x.txt" will be turned into a Label "@y//:x.txt"
RELNOTES: None
PiperOrigin-RevId: 201562148
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java index 0321498994..eccc5a5a35 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java @@ -23,7 +23,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; +import com.google.devtools.build.lib.cmdline.RepositoryName; import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.packages.BuildType.LabelConversionContext; import com.google.devtools.build.lib.packages.BuildType.Selector; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; @@ -47,10 +49,13 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class BuildTypeTest { private Label currentRule; + private LabelConversionContext labelConversionContext; @Before public final void setCurrentRule() throws Exception { this.currentRule = Label.parseAbsolute("//quux:baz"); + this.labelConversionContext = + new LabelConversionContext(currentRule, /* repositoryMapping= */ ImmutableMap.of()); } @Test @@ -64,8 +69,9 @@ public class BuildTypeTest { .put("d", "//d") .build(); - assertThat(BuildType.LABEL_DICT_UNARY.convert(input, null, null).keySet()) - .containsExactly("c", "b", "a", "f", "e", "d").inOrder(); + assertThat(BuildType.LABEL_DICT_UNARY.convert(input, null, labelConversionContext).keySet()) + .containsExactly("c", "b", "a", "f", "e", "d") + .inOrder(); } @Test @@ -284,6 +290,17 @@ public class BuildTypeTest { entry1Label, entry2Label); } + @Test + public void testLabelWithRemapping() throws Exception { + LabelConversionContext context = + new LabelConversionContext( + currentRule, + ImmutableMap.of( + RepositoryName.create("@orig_repo"), RepositoryName.create("@new_repo"))); + Label label = BuildType.LABEL.convert("@orig_repo//foo:bar", null, context); + assertThat(label).isEquivalentAccordingToCompareTo(Label.parseAbsolute("@new_repo//foo:bar")); + } + /** * Tests basic {@link Selector} functionality. */ @@ -293,7 +310,7 @@ public class BuildTypeTest { "//conditions:a", "//a:a", "//conditions:b", "//b:b", Selector.DEFAULT_CONDITION_KEY, "//d:d"); - Selector<Label> selector = new Selector<>(input, null, currentRule, BuildType.LABEL); + Selector<Label> selector = new Selector<>(input, null, labelConversionContext, BuildType.LABEL); assertThat(selector.getOriginalType()).isEqualTo(BuildType.LABEL); Map<Label, Label> expectedMap = ImmutableMap.of( @@ -313,7 +330,7 @@ public class BuildTypeTest { "//conditions:a", "not a/../label", "//conditions:b", "also not a/../label", BuildType.Selector.DEFAULT_CONDITION_KEY, "whatever"); try { - new Selector<>(input, null, currentRule, BuildType.LABEL); + new Selector<>(input, null, labelConversionContext, BuildType.LABEL); fail("Expected Selector instantiation to fail since the input isn't a selection of labels"); } catch (ConversionException e) { assertThat(e).hasMessageThat().contains("invalid label 'not a/../label'"); @@ -329,7 +346,7 @@ public class BuildTypeTest { "not a/../label", "//a:a", BuildType.Selector.DEFAULT_CONDITION_KEY, "whatever"); try { - new Selector<>(input, null, currentRule, BuildType.LABEL); + new Selector<>(input, null, labelConversionContext, BuildType.LABEL); fail("Expected Selector instantiation to fail since the key isn't a label"); } catch (ConversionException e) { assertThat(e).hasMessageThat().contains("invalid label 'not a/../label'"); @@ -345,7 +362,7 @@ public class BuildTypeTest { "//conditions:a", "//a:a", "//conditions:b", "//b:b", BuildType.Selector.DEFAULT_CONDITION_KEY, "//d:d"); - assertThat(new Selector<>(input, null, currentRule, BuildType.LABEL).getDefault()) + assertThat(new Selector<>(input, null, labelConversionContext, BuildType.LABEL).getDefault()) .isEqualTo(Label.create("@//d", "d")); } @@ -355,8 +372,12 @@ public class BuildTypeTest { ImmutableList.of("//a:a"), "//conditions:b", ImmutableList.of("//b:b")), ""); Object selector2 = new SelectorValue(ImmutableMap.of("//conditions:c", ImmutableList.of("//c:c"), "//conditions:d", ImmutableList.of("//d:d")), ""); - BuildType.SelectorList<List<Label>> selectorList = new BuildType.SelectorList<>( - ImmutableList.of(selector1, selector2), null, currentRule, BuildType.LABEL_LIST); + BuildType.SelectorList<List<Label>> selectorList = + new BuildType.SelectorList<>( + ImmutableList.of(selector1, selector2), + null, + labelConversionContext, + BuildType.LABEL_LIST); assertThat(selectorList.getOriginalType()).isEqualTo(BuildType.LABEL_LIST); assertThat(selectorList.getKeyLabels()) @@ -388,7 +409,10 @@ public class BuildTypeTest { Object selector2 = new SelectorValue(ImmutableMap.of("//conditions:b", "//b:b"), ""); try { - new BuildType.SelectorList<>(ImmutableList.of(selector1, selector2), null, currentRule, + new BuildType.SelectorList<>( + ImmutableList.of(selector1, selector2), + null, + labelConversionContext, BuildType.LABEL_LIST); fail("Expected SelectorList initialization to fail on mixed element types"); } catch (ConversionException e) { @@ -457,14 +481,16 @@ public class BuildTypeTest { ImmutableList.of(Label.create("@//a", "a1"), Label.create("@//a", "a2")); // Conversion to direct type: - Object converted = BuildType - .selectableConvert(BuildType.LABEL_LIST, nativeInput, null, currentRule); + Object converted = + BuildType.selectableConvert( + BuildType.LABEL_LIST, nativeInput, null, labelConversionContext); assertThat(converted instanceof List<?>).isTrue(); assertThat((List<Label>) converted).containsExactlyElementsIn(expectedLabels); // Conversion to selectable type: - converted = BuildType - .selectableConvert(BuildType.LABEL_LIST, selectableInput, null, currentRule); + converted = + BuildType.selectableConvert( + BuildType.LABEL_LIST, selectableInput, null, labelConversionContext); BuildType.SelectorList<?> selectorList = (BuildType.SelectorList<?>) converted; assertThat(((Selector<Label>) selectorList.getSelectors().get(0)).getEntries().entrySet()) .containsExactlyElementsIn( @@ -508,7 +534,10 @@ public class BuildTypeTest { public void testUnconditionalSelects() throws Exception { assertThat( new Selector<>( - ImmutableMap.of("//conditions:a", "//a:a"), null, currentRule, BuildType.LABEL) + ImmutableMap.of("//conditions:a", "//a:a"), + null, + labelConversionContext, + BuildType.LABEL) .isUnconditional()) .isFalse(); assertThat( @@ -519,7 +548,7 @@ public class BuildTypeTest { BuildType.Selector.DEFAULT_CONDITION_KEY, "//b:b"), null, - currentRule, + labelConversionContext, BuildType.LABEL) .isUnconditional()) .isFalse(); @@ -527,7 +556,7 @@ public class BuildTypeTest { new Selector<>( ImmutableMap.of(BuildType.Selector.DEFAULT_CONDITION_KEY, "//b:b"), null, - currentRule, + labelConversionContext, BuildType.LABEL) .isUnconditional()) .isTrue(); |