aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java
diff options
context:
space:
mode:
authorGravatar Michael Staib <mstaib@google.com>2016-01-15 20:11:11 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-01-15 22:30:16 +0000
commitcd48cd56ab576f3ff1c5acaa749582c6bd7043b9 (patch)
treefea553f726bba8739dfa581281c684a2887229fc /src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java
parentcb5d741ab44c11809cb0cd666bfb20af5f6af114 (diff)
Enable Aspects to specify their configuration fragment dependencies.
Note: This specification currently does not have any effect, but soon... In the default mode, when an aspect does not call any of the configuration fragment methods on its AspectDefinition.Builder, the old behavior will persist; aspects can only access fragments their associated rule has access to, and have no guarantee as to what those fragments are. This mode will become deprecated with a future CL. If an aspect does call a configuration fragment method, it will have a configuration fragment policy. In a future CL, this will mean it will be restricted to accessing only those fragments, but will be understood as requiring access to them for the purposes of dynamic configuration, even if the rule it is attached to or created by does not otherwise require them. Eventually, all aspects will be required to declare their configuration fragments this way. Skylark aspects may also declare configuration fragments as of this CL. Two new parameters are added to the aspect() function, fragments and host_fragments, mirroring the similar parameters for rules. If both of these parameters are empty or unspecified, the default mode is used, as with normal aspects. Also in this CL: * Minor javadoc fixes for AspectDefinition. * Additional tests for AspectDefinition. -- MOS_MIGRATED_REVID=112271713
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java158
1 files changed, 149 insertions, 9 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java
index c413271186..32a63c8043 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AspectDefinitionTest.java
@@ -13,13 +13,23 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;
+import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static org.junit.Assert.fail;
+import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.AspectDefinition;
import com.google.devtools.build.lib.packages.AspectParameters;
+import com.google.devtools.build.lib.packages.Attribute;
+import com.google.devtools.build.lib.packages.Attribute.ConfigurationTransition;
+import com.google.devtools.build.lib.packages.Attribute.LateBoundLabel;
import com.google.devtools.build.lib.packages.BuildType;
+import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy;
+import com.google.devtools.build.lib.packages.NativeAspectClass;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
+import com.google.devtools.build.lib.util.FileTypeSet;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -59,23 +69,153 @@ public class AspectDefinitionTest {
}
@Test
- public void testSimpleAspect() throws Exception {
- new AspectDefinition.Builder("simple")
- .add(attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")))
- .attributeAspect("deps", TestAspectFactory.class)
+ public void testAspectWithImplicitOrLateboundAttribute_AddsToAttributeMap() throws Exception {
+ Attribute implicit = attr("$runtime", BuildType.LABEL)
+ .value(Label.parseAbsoluteUnchecked("//run:time"))
.build();
+ LateBoundLabel<String> latebound = new LateBoundLabel<String>() {
+ @Override
+ public Label getDefault(Rule rule, String configuration) {
+ return Label.parseAbsoluteUnchecked("//run:away");
+ }
+ };
+ AspectDefinition simple = new AspectDefinition.Builder("simple")
+ .add(implicit)
+ .add(attr(":latebound", BuildType.LABEL).value(latebound))
+ .build();
+ assertThat(simple.getAttributes()).containsEntry("$runtime", implicit);
+ assertThat(simple.getAttributes()).containsKey(":latebound");
+ assertThat(simple.getAttributes().get(":latebound").getLateBoundDefault())
+ .isEqualTo(latebound);
}
@Test
- public void testAspectWithUserVisibleAttribute() throws Exception {
+ public void testAspectWithDuplicateAttribute_FailsToAdd() throws Exception {
+ try {
+ new AspectDefinition.Builder("clash")
+ .add(attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")))
+ .add(attr("$runtime", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//oops")));
+ fail(); // expected IllegalArgumentException
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testAspectWithUserVisibleAttribute_FailsToAdd() throws Exception {
try {
new AspectDefinition.Builder("user_visible_attribute")
- .add(attr("invalid", BuildType.LABEL).value(Label.parseAbsoluteUnchecked("//run:time")))
- .attributeAspect("deps", TestAspectFactory.class)
+ .add(
+ attr("invalid", BuildType.LABEL)
+ .value(Label.parseAbsoluteUnchecked("//run:time"))
+ .allowedFileTypes(FileTypeSet.NO_FILE))
.build();
- fail(); // expected IllegalStateException
- } catch (IllegalStateException e) {
+ fail(); // expected IllegalArgumentException
+ } catch (IllegalArgumentException e) {
// expected
}
}
+
+ @Test
+ public void testAttributeAspect_WrapsAndAddsToMap() throws Exception {
+ AspectDefinition withAspects = new AspectDefinition.Builder("attribute_aspect")
+ .attributeAspect("srcs", TestAspectFactory.class)
+ .attributeAspect("deps", new NativeAspectClass<TestAspectFactory>(TestAspectFactory.class))
+ .build();
+ assertThat(withAspects.getAttributeAspects())
+ .containsEntry("srcs", new NativeAspectClass<TestAspectFactory>(TestAspectFactory.class));
+ assertThat(withAspects.getAttributeAspects())
+ .containsEntry("deps", new NativeAspectClass<TestAspectFactory>(TestAspectFactory.class));
+ }
+
+ @Test
+ public void testRequireProvider_AddsToSetOfRequiredProvidersAndNames() throws Exception {
+ AspectDefinition requiresProviders = new AspectDefinition.Builder("required_providers")
+ .requireProvider(String.class)
+ .requireProvider(Integer.class)
+ .build();
+ assertThat(requiresProviders.getRequiredProviders())
+ .containsExactly(String.class, Integer.class);
+ assertThat(requiresProviders.getRequiredProviderNames())
+ .containsExactly("java.lang.String", "java.lang.Integer");
+ }
+
+ @Test
+ public void testNoConfigurationFragmentPolicySetup_ReturnsNull() throws Exception {
+ AspectDefinition noPolicy = new AspectDefinition.Builder("no_policy")
+ .build();
+ assertThat(noPolicy.getConfigurationFragmentPolicy()).isNull();
+ }
+
+ @Test
+ public void testMissingFragmentPolicy_PropagatedToConfigurationFragmentPolicy() throws Exception {
+ AspectDefinition missingFragments = new AspectDefinition.Builder("missing_fragments")
+ .setMissingFragmentPolicy(MissingFragmentPolicy.IGNORE)
+ .build();
+ assertThat(missingFragments.getConfigurationFragmentPolicy()).isNotNull();
+ assertThat(missingFragments.getConfigurationFragmentPolicy().getMissingFragmentPolicy())
+ .isEqualTo(MissingFragmentPolicy.IGNORE);
+ }
+
+ @Test
+ public void testRequiresConfigurationFragments_PropagatedToConfigurationFragmentPolicy()
+ throws Exception {
+ AspectDefinition requiresFragments = new AspectDefinition.Builder("requires_fragments")
+ .requiresConfigurationFragments(Integer.class, String.class)
+ .build();
+ assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull();
+ assertThat(
+ requiresFragments.getConfigurationFragmentPolicy().getRequiredConfigurationFragments())
+ .containsExactly(Integer.class, String.class);
+ }
+
+ @Test
+ public void testRequiresHostConfigurationFragments_PropagatedToConfigurationFragmentPolicy()
+ throws Exception {
+ AspectDefinition requiresFragments = new AspectDefinition.Builder("requires_fragments")
+ .requiresHostConfigurationFragments(Integer.class, String.class)
+ .build();
+ assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull();
+ assertThat(
+ requiresFragments.getConfigurationFragmentPolicy().getRequiredConfigurationFragments())
+ .containsExactly(Integer.class, String.class);
+ }
+
+ @Test
+ public void testRequiresConfigurationFragmentNames_PropagatedToConfigurationFragmentPolicy()
+ throws Exception {
+ AspectDefinition requiresFragments = new AspectDefinition.Builder("requires_fragments")
+ .requiresConfigurationFragmentsBySkylarkModuleName(ImmutableList.of("test_fragment"))
+ .build();
+ assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull();
+ assertThat(
+ requiresFragments.getConfigurationFragmentPolicy()
+ .isLegalConfigurationFragment(TestFragment.class, ConfigurationTransition.NONE))
+ .isTrue();
+ }
+
+ @Test
+ public void testRequiresHostConfigurationFragmentNames_PropagatedToConfigurationFragmentPolicy()
+ throws Exception {
+ AspectDefinition requiresFragments = new AspectDefinition.Builder("requires_fragments")
+ .requiresHostConfigurationFragmentsBySkylarkModuleName(ImmutableList.of("test_fragment"))
+ .build();
+ assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull();
+ assertThat(
+ requiresFragments.getConfigurationFragmentPolicy()
+ .isLegalConfigurationFragment(TestFragment.class, ConfigurationTransition.HOST))
+ .isTrue();
+ }
+
+ @Test
+ public void testEmptySkylarkConfigurationFragmentPolicySetup_ReturnsNull() throws Exception {
+ AspectDefinition noPolicy = new AspectDefinition.Builder("no_policy")
+ .requiresConfigurationFragmentsBySkylarkModuleName(ImmutableList.<String>of())
+ .requiresHostConfigurationFragmentsBySkylarkModuleName(ImmutableList.<String>of())
+ .build();
+ assertThat(noPolicy.getConfigurationFragmentPolicy()).isNull();
+ }
+
+ @SkylarkModule(name = "test_fragment", doc = "test fragment")
+ private static final class TestFragment {}
}