Convert TestAspects to MockRule.

It'd be nice to go further and break out a bunch of this code into more generally useful places (especially for callers that don't care about aspects). But that's a big mess and beyond the scope of what I'm aiming for here.

PiperOrigin-RevId: 177307854
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
index 76336fe..15bc2cc 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
@@ -29,11 +29,10 @@
 import com.google.devtools.build.lib.actions.util.ActionsTestUtil.NullAction;
 import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult;
 import com.google.devtools.build.lib.analysis.util.AnalysisTestCase;
+import com.google.devtools.build.lib.analysis.util.MockRule;
 import com.google.devtools.build.lib.analysis.util.TestAspects;
 import com.google.devtools.build.lib.analysis.util.TestAspects.AspectApplyingToFiles;
 import com.google.devtools.build.lib.analysis.util.TestAspects.AspectInfo;
-import com.google.devtools.build.lib.analysis.util.TestAspects.AspectRequiringRule;
-import com.google.devtools.build.lib.analysis.util.TestAspects.BaseRule;
 import com.google.devtools.build.lib.analysis.util.TestAspects.DummyRuleFactory;
 import com.google.devtools.build.lib.analysis.util.TestAspects.RuleInfo;
 import com.google.devtools.build.lib.cmdline.Label;
@@ -45,7 +44,6 @@
 import com.google.devtools.build.lib.packages.AspectParameters;
 import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault;
 import com.google.devtools.build.lib.packages.NativeAspectClass;
-import com.google.devtools.build.lib.packages.RuleClass;
 import com.google.devtools.build.lib.skyframe.AspectValue;
 import com.google.devtools.build.lib.testutil.TestConstants;
 import com.google.devtools.build.lib.vfs.ModifiedFileSet;
@@ -71,7 +69,7 @@
 
   @Test
   public void testAspectAppliedToAliasWithSelect() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new AspectRequiringRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE);
     pkg("a",
         "aspect(name='a', foo=[':b'])",
         "alias(name='b', actual=select({'//conditions:default': ':c'}))",
@@ -83,7 +81,7 @@
 
   @Test
   public void testAspectAppliedToChainedAliases() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new AspectRequiringRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE);
     pkg("a",
         "aspect(name='a', foo=[':b'])",
         "alias(name='b', actual=':c')",
@@ -98,7 +96,7 @@
 
   @Test
   public void testAspectAppliedToChainedAliasesAndSelect() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new AspectRequiringRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE);
     pkg("a",
         "aspect(name='a', foo=[':b'])",
         "alias(name='b', actual=select({'//conditions:default': ':c'}))",
@@ -111,7 +109,7 @@
 
   @Test
   public void providersOfAspectAreMergedIntoDependency() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new AspectRequiringRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE);
     pkg("a",
         "aspect(name='a', foo=[':b'])",
         "aspect(name='b', foo=[])");
@@ -123,8 +121,8 @@
 
   @Test
   public void aspectIsNotCreatedIfAdvertisedProviderIsNotPresent() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.LiarRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b'])",
@@ -136,8 +134,8 @@
 
   @Test
   public void aspectIsNotCreatedIfAdvertisedProviderIsNotPresentWithAlias() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.LiarRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b'])",
@@ -150,10 +148,8 @@
 
   @Test
   public void aspectIsNotPropagatedThroughLiars() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new TestAspects.LiarRule(),
-        new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE,
+        TestAspects.HONEST_RULE, TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b_alias'])",
@@ -168,8 +164,8 @@
 
   @Test
   public void aspectPropagatedThroughAliasRule() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b_alias'])",
@@ -183,8 +179,8 @@
 
   @Test
   public void aspectPropagatedThroughAliasRuleAndHonestRules() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b'])",
@@ -208,9 +204,8 @@
       // TODO(b/67651960): fix or justify disabling.
       return;
     }
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
-
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
     pkg("a",
         "aspect_requiring_provider(name='a', foo=['//external:b'])",
         "honest(name='b', foo=[])");
@@ -232,8 +227,8 @@
 
   @Test
   public void aspectCreatedIfAdvertisedProviderIsPresent() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
 
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b'])",
@@ -246,8 +241,8 @@
 
   @Test
   public void aspectCreatedIfAtLeastOneSetOfAdvertisedProvidersArePresent() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.HonestRule2(), new TestAspects.AspectRequiringProviderSetsRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.HONEST_RULE_2, TestAspects.ASPECT_REQUIRING_PROVIDER_SETS_RULE);
 
     pkg("a",
         "aspect_requiring_provider_sets(name='a', foo=[':b', ':c'])",
@@ -262,9 +257,9 @@
   @Test
   public void aspectWithParametrizedDefinition() throws Exception {
     setRulesAvailableInTests(
-        new TestAspects.BaseRule(),
-        new TestAspects.HonestRule(),
-        new TestAspects.ParametrizedDefinitionAspectRule());
+        TestAspects.BASE_RULE,
+        TestAspects.HONEST_RULE,
+        TestAspects.PARAMETERIZED_DEFINITION_ASPECT_RULE);
 
     pkg(
         "a",
@@ -283,8 +278,8 @@
 
   @Test
   public void aspectInError() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.ErrorAspectRule(),
-        new TestAspects.SimpleRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ERROR_ASPECT_RULE,
+        TestAspects.SIMPLE_RULE);
 
     pkg("a",
         "simple(name='a', foo=[':b'])",
@@ -305,8 +300,8 @@
 
   @Test
   public void transitiveAspectInError() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.ErrorAspectRule(),
-        new TestAspects.SimpleRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ERROR_ASPECT_RULE,
+        TestAspects.SIMPLE_RULE);
 
     pkg("a",
         "error_aspect(name='a', foo=[':b'])",
@@ -328,8 +323,7 @@
 
   @Test
   public void aspectDependenciesDontShowDeprecationWarnings() throws Exception {
-    setRulesAvailableInTests(
-        new TestAspects.BaseRule(), new TestAspects.ExtraAttributeAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.EXTRA_ATTRIBUTE_ASPECT_RULE);
 
     pkg("extra", "base(name='extra', deprecation='bad aspect')");
 
@@ -343,7 +337,7 @@
 
   @Test
   public void ruleDependencyDeprecationWarningsAbsentDuringAspectEvaluations() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.AspectRequiringRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE);
 
     pkg("a", "aspect(name='a', foo=['//b:b'])");
     pkg("b", "aspect(name='b', bar=['//d:d'])");
@@ -359,7 +353,7 @@
       // TODO(b/67651960): fix or justify disabling.
       return;
     }
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.WarningAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.WARNING_ASPECT_RULE);
     pkg("a", "warning_aspect(name='a', foo=['//b:b', '//c:c'])");
     pkg("b", "base(name='b')");
     pkg("c", "base(name='c')");
@@ -373,8 +367,8 @@
 
   @Test
   public void sameTargetInDifferentAttributes() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.AspectRequiringRule(),
-        new TestAspects.SimpleRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE,
+        TestAspects.SIMPLE_RULE);
     pkg("a",
         "aspect(name='a', foo=[':b'], bar=[':b'])",
         "aspect(name='b', foo=[])");
@@ -386,8 +380,8 @@
 
   @Test
   public void sameTargetInDifferentAttributesWithDifferentAspects() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.MultiAspectRule(),
-        new TestAspects.SimpleRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.MULTI_ASPECT_RULE,
+        TestAspects.SIMPLE_RULE);
     pkg("a",
         "multi_aspect(name='a', foo=':b', bar=':b')",
         "simple(name='b')");
@@ -398,9 +392,8 @@
 
   @Test
   public void informationFromBaseRulePassedToAspect() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.HonestRule(),
-        new TestAspects.AspectRequiringProviderRule());
-
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE,
+        TestAspects.ASPECT_REQUIRING_PROVIDER_RULE);
     pkg("a",
         "aspect_requiring_provider(name='a', foo=[':b'], baz='hello')",
         "honest(name='b', foo=[])");
@@ -414,21 +407,13 @@
    * Rule definitions to be used in emptyAspectAttributesAreAvailableInRuleContext().
    */
   public static class EmptyAspectAttributesAreAvailableInRuleContext {
-    public static class TestRule implements RuleDefinition {
-      @Override
-      public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
-        return builder
-            .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType()
-                .aspect(ASPECT_WITH_EMPTY_LATE_BOUND_ATTRIBUTE))
-            .build();
-      }
-
-      @Override
-      public Metadata getMetadata() {
-        return RuleDefinition.Metadata.builder().name("testrule")
-            .factoryClass(DummyRuleFactory.class).ancestors(BaseRule.class).build();
-      }
-    }
+    public static final MockRule TEST_RULE = () ->
+        MockRule.ancestor(TestAspects.BASE_RULE.getClass()).factory(DummyRuleFactory.class).define(
+            "testrule",
+            (builder, env) ->
+                builder
+                    .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType()
+                        .aspect(new AspectWithEmptyLateBoundAttribute())));
 
     public static class AspectWithEmptyLateBoundAttribute extends NativeAspectClass
       implements ConfiguredAspectFactory {
@@ -453,8 +438,6 @@
             .build();
       }
     }
-    public static final AspectWithEmptyLateBoundAttribute ASPECT_WITH_EMPTY_LATE_BOUND_ATTRIBUTE =
-        new AspectWithEmptyLateBoundAttribute();
   }
 
   /**
@@ -464,8 +447,8 @@
    */
   @Test
   public void emptyAspectAttributesAreAvailableInRuleContext() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new EmptyAspectAttributesAreAvailableInRuleContext.TestRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE,
+        EmptyAspectAttributesAreAvailableInRuleContext.TEST_RULE);
     pkg("a",
         "testrule(name='a', foo=[':b'])",
         "testrule(name='b')");
@@ -477,22 +460,14 @@
    * Rule definitions to be used in extraActionsAreEmitted().
    */
   public static class ExtraActionsAreEmitted {
-    public static class TestRule implements RuleDefinition {
-      @Override
-      public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
-        return builder
-            .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType()
-                .aspect(ASPECT_THAT_REGISTERS_ACTION))
-            .add(attr(":action_listener", LABEL_LIST).cfg(HOST).value(ACTION_LISTENER))
-            .build();
-      }
-
-      @Override
-      public Metadata getMetadata() {
-        return RuleDefinition.Metadata.builder().name("testrule")
-            .factoryClass(DummyRuleFactory.class).ancestors(BaseRule.class).build();
-      }
-    }
+    public static final MockRule TEST_RULE = () ->
+        MockRule.ancestor(TestAspects.BASE_RULE.getClass()).factory(DummyRuleFactory.class).define(
+            "testrule",
+            (builder, env) ->
+                builder
+                    .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType()
+                        .aspect(new AspectThatRegistersAction()))
+                    .add(attr(":action_listener", LABEL_LIST).cfg(HOST).value(ACTION_LISTENER)));
 
     public static class AspectThatRegistersAction extends NativeAspectClass
       implements ConfiguredAspectFactory {
@@ -511,8 +486,6 @@
         return new ConfiguredAspect.Builder(this, parameters, ruleContext).build();
       }
     }
-    private static final AspectThatRegistersAction ASPECT_THAT_REGISTERS_ACTION =
-        new AspectThatRegistersAction();
   }
 
   /**
@@ -525,8 +498,7 @@
    */
   @Test
   public void extraActionsAreEmitted() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new ExtraActionsAreEmitted.TestRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, ExtraActionsAreEmitted.TEST_RULE);
     useConfiguration("--experimental_action_listener=//extra_actions:listener");
     scratch.file(
         "extra_actions/BUILD",
@@ -547,9 +519,8 @@
 
   @Test
   public void aspectPropagatesToAllAttributes() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.AllAttributesAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.ALL_ATTRIBUTES_ASPECT_RULE);
     pkg("a",
         "simple(name='a', foo=[':b'], foo1=':c', txt='some text')",
         "simple(name='b', foo=[], txt='some text')",
@@ -682,11 +653,8 @@
 
   @Test
   public void aspectPropagatesToAllAttributesImplicit() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.ImplicitDepRule(),
-        new TestAspects.AllAttributesAspectRule());
-
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.IMPLICIT_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE);
     scratch.file(
         "extra/BUILD",
         "simple(name ='extra')"
@@ -711,10 +679,8 @@
 
   @Test
   public void aspectPropagatesToAllAttributesLateBound() throws Exception {
-    setRulesAvailableInTests(new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.LateBoundDepRule(),
-        new TestAspects.AllAttributesAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.LATE_BOUND_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE);
 
     scratch.file(
         "extra/BUILD",
@@ -744,10 +710,8 @@
    */
   @Test
   public void aspectWithAllAttributesDoesNotPropagateToOwnImplicitAttributes() throws Exception {
-    setRulesAvailableInTests(
-        new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.AllAttributesWithToolAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE);
     pkg(
         "a",
         "simple(name='tool')",
@@ -765,10 +729,8 @@
    */
   @Test
   public void aspectWithAllAttributesPropagatesToItsToolIfThereIsPath() throws Exception {
-    setRulesAvailableInTests(
-        new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.AllAttributesWithToolAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE);
     pkg(
         "a",
         "simple(name='tool')",
@@ -786,10 +748,8 @@
   @Test
   public void aspectTruthInAdvertisement() throws Exception {
     reporter.removeHandler(failFastHandler); // expect errors
-    setRulesAvailableInTests(
-        new TestAspects.BaseRule(),
-        new TestAspects.SimpleRule(),
-        new TestAspects.FalseAdvertisementAspectRule());
+    setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE,
+        TestAspects.FALSE_ADVERTISEMENT_ASPECT_RULE);
     pkg(
         "a",
         "simple(name = 's')",