Automatic cleanup change.
PiperOrigin-RevId: 246137705
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
index efd0e84..16a19d5 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
@@ -16,7 +16,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -438,13 +438,10 @@
" mnemonics = ['Javac'],",
" extra_actions = [':extra'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//java/a:a");
- fail();
- } catch (ViewCreationFailedException e) {
- assertThat(e).hasMessageThat().contains("Analysis of target '//java/a:a' failed");
- assertContainsEvent("$(BUG) not defined");
- }
+ ViewCreationFailedException e =
+ assertThrows(ViewCreationFailedException.class, () -> update("//java/a:a"));
+ assertThat(e).hasMessageThat().contains("Analysis of target '//java/a:a' failed");
+ assertContainsEvent("$(BUG) not defined");
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java
index 4c6f2e4..ddac970 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java
@@ -16,7 +16,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.analysis.AnalysisUtils.checkProvider;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.auto.value.AutoValue;
import org.junit.Test;
@@ -33,12 +33,11 @@
@Test
public void checkProviderFailsOnClassGeneratedByAutoValue() {
- try {
- checkProvider(AutoValue_AnalysisUtilsTest_AutoValuedClass.class);
- fail("Expected IllegalArgumentException, but nothing was thrown.");
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessageThat().contains("generated by @AutoValue");
- }
+ IllegalArgumentException e =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> checkProvider(AutoValue_AnalysisUtilsTest_AutoValuedClass.class));
+ assertThat(e).hasMessageThat().contains("generated by @AutoValue");
}
// Note: this has to be defined outside of checkProviderFailsOnClassGeneratedByAutoValue() so it
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java
index dfb8401..e099990 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisWithIOExceptionsTest.java
@@ -13,7 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.devtools.build.lib.analysis.util.AnalysisTestCase;
import com.google.devtools.build.lib.clock.BlazeClock;
@@ -54,10 +54,6 @@
scratch.file("a/BUILD", "sh_library(name = 'a', srcs = glob(['a.sh']))");
crashMessage = path -> path.toString().contains("a.sh") ? "bork" : null;
reporter.removeHandler(failFastHandler);
- try {
- update("//b:b");
- fail("Expected failure");
- } catch (ViewCreationFailedException expected) {
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//b:b"));
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AspectAwareAttributeMapperTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AspectAwareAttributeMapperTest.java
index cc91b1e..b84493c 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AspectAwareAttributeMapperTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AspectAwareAttributeMapperTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
@@ -80,36 +80,27 @@
@Test
public void getAspectAttributeValue() throws Exception {
- try {
- mapper.get("fromaspect", BuildType.LABEL);
- fail("Expected failure because value queries aren't supported for aspect attributes");
- } catch (UnsupportedOperationException e) {
- // Expected.
- }
+ assertThrows(
+ UnsupportedOperationException.class, () -> mapper.get("fromaspect", BuildType.LABEL));
}
@Test
public void getAspectValueWrongType() throws Exception {
- try {
- mapper.get("fromaspect", BuildType.LABEL_LIST);
- fail("Expected failure on wrong-typed attribute");
- } catch (IllegalArgumentException e) {
- assertThat(e)
- .hasMessageThat()
- .isEqualTo("attribute fromaspect has type label, not expected type list(label)");
- }
+ IllegalArgumentException e =
+ assertThrows(
+ IllegalArgumentException.class, () -> mapper.get("fromaspect", BuildType.LABEL_LIST));
+ assertThat(e)
+ .hasMessageThat()
+ .isEqualTo("attribute fromaspect has type label, not expected type list(label)");
}
@Test
public void getMissingAttributeValue() throws Exception {
- try {
- mapper.get("noexist", BuildType.LABEL);
- fail("Expected failure on non-existent attribute");
- } catch (IllegalArgumentException e) {
- assertThat(e)
- .hasMessageThat()
- .isEqualTo("no attribute 'noexist' in either //foo:myrule or its aspects");
- }
+ IllegalArgumentException e =
+ assertThrows(IllegalArgumentException.class, () -> mapper.get("noexist", BuildType.LABEL));
+ assertThat(e)
+ .hasMessageThat()
+ .isEqualTo("no attribute 'noexist' in either //foo:myrule or its aspects");
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AspectCollectionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AspectCollectionTest.java
index 8083c0a..b6cd781 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AspectCollectionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AspectCollectionTest.java
@@ -15,7 +15,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
@@ -223,16 +223,14 @@
Aspect a1 = createAspect("a1");
Aspect a2 = createAspect("a2", "a1");
Aspect a3 = createAspect("a3", "a2", "a1");
- try {
- AspectCollection
- .create(
- ImmutableList.of(a2, a1, a2, a3),
- ImmutableSet.of(a3.getDescriptor()));
- fail();
- } catch (AspectCycleOnPathException e) {
- assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
- assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
- }
+ AspectCycleOnPathException e =
+ assertThrows(
+ AspectCycleOnPathException.class,
+ () ->
+ AspectCollection.create(
+ ImmutableList.of(a2, a1, a2, a3), ImmutableSet.of(a3.getDescriptor())));
+ assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
+ assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
}
/**
@@ -245,16 +243,14 @@
Aspect a1 = createAspect("a1");
Aspect a2 = createAspect("a2", "a1");
Aspect a3 = createAspect("a3", "a2");
- try {
- AspectCollection
- .create(
- ImmutableList.of(a2, a1, a2, a3),
- ImmutableSet.of(a3.getDescriptor()));
- fail();
- } catch (AspectCycleOnPathException e) {
- assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
- assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
- }
+ AspectCycleOnPathException e =
+ assertThrows(
+ AspectCycleOnPathException.class,
+ () ->
+ AspectCollection.create(
+ ImmutableList.of(a2, a1, a2, a3), ImmutableSet.of(a3.getDescriptor())));
+ assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
+ assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
}
/**
@@ -293,16 +289,14 @@
Aspect a1 = createAspect("a1", "a2");
Aspect a2 = createAspect("a2", "a1");
Aspect a3 = createAspect("a3", "a1", "a2");
- try {
- AspectCollection
- .create(
- ImmutableList.of(a2, a1, a2, a3),
- ImmutableSet.of(a3.getDescriptor()));
- fail();
- } catch (AspectCycleOnPathException e) {
- assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
- assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
- }
+ AspectCycleOnPathException e =
+ assertThrows(
+ AspectCycleOnPathException.class,
+ () ->
+ AspectCollection.create(
+ ImmutableList.of(a2, a1, a2, a3), ImmutableSet.of(a3.getDescriptor())));
+ assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
+ assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
}
/**
@@ -315,16 +309,14 @@
Aspect a1 = createAspect("a1", "a2");
Aspect a2 = createAspect("a2", "a1");
Aspect a3 = createAspect("a3", "a2");
- try {
- AspectCollection
- .create(
- ImmutableList.of(a2, a1, a2, a3),
- ImmutableSet.of(a3.getDescriptor()));
- fail();
- } catch (AspectCycleOnPathException e) {
- assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
- assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
- }
+ AspectCycleOnPathException e =
+ assertThrows(
+ AspectCycleOnPathException.class,
+ () ->
+ AspectCollection.create(
+ ImmutableList.of(a2, a1, a2, a3), ImmutableSet.of(a3.getDescriptor())));
+ assertThat(e.getAspect()).isEqualTo(a2.getDescriptor());
+ assertThat(e.getPreviousAspect()).isEqualTo(a1.getDescriptor());
}
/**
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 3e81a2a..e22f62e 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
@@ -15,7 +15,7 @@
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 static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@@ -94,29 +94,29 @@
@Test
public void testAspectWithDuplicateAttribute_FailsToAdd() throws Exception {
- try {
- new AspectDefinition.Builder(TEST_ASPECT_CLASS)
- .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
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ new AspectDefinition.Builder(TEST_ASPECT_CLASS)
+ .add(
+ attr("$runtime", BuildType.LABEL)
+ .value(Label.parseAbsoluteUnchecked("//run:time")))
+ .add(
+ attr("$runtime", BuildType.LABEL)
+ .value(Label.parseAbsoluteUnchecked("//oops"))));
}
@Test
public void testAspectWithUserVisibleAttribute_FailsToAdd() throws Exception {
- try {
- new AspectDefinition.Builder(TEST_ASPECT_CLASS)
- .add(
- attr("invalid", BuildType.LABEL)
- .value(Label.parseAbsoluteUnchecked("//run:time"))
- .allowedFileTypes(FileTypeSet.NO_FILE))
- .build();
- fail(); // expected IllegalArgumentException
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ new AspectDefinition.Builder(TEST_ASPECT_CLASS)
+ .add(
+ attr("invalid", BuildType.LABEL)
+ .value(Label.parseAbsoluteUnchecked("//run:time"))
+ .allowedFileTypes(FileTypeSet.NO_FILE))
+ .build());
}
@Test
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 08df7c2..2b80208 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
@@ -19,7 +19,7 @@
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
@@ -291,12 +291,7 @@
reporter.removeHandler(failFastHandler);
// getConfiguredTarget() uses a separate code path that does not hit
// SkyframeBuildView#configureTargets
- try {
- update("//a:a");
- fail();
- } catch (ViewCreationFailedException e) {
- // expected
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//a:a"));
assertContainsEvent("Aspect error");
}
@@ -314,12 +309,7 @@
reporter.removeHandler(failFastHandler);
// getConfiguredTarget() uses a separate code path that does not hit
// SkyframeBuildView#configureTargets
- try {
- update("//a:a");
- fail();
- } catch (ViewCreationFailedException e) {
- // expected
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//a:a"));
assertContainsEvent("Aspect error");
}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java b/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
index 0d6d771..561d3f0 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
@@ -18,6 +18,7 @@
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.testutil.MoreAsserts.assertEventCountAtLeast;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import static org.junit.Assert.fail;
import com.google.common.base.Function;
@@ -389,12 +390,10 @@
.that(options.asMap().get("output directory name"))
.isEqualTo("/home/wonkaw/wonka_chocolate/factory/out");
- try {
- useConfiguration("--output directory name=foo");
- fail();
- } catch (OptionsParsingException e) {
- assertThat(e).hasMessageThat().isEqualTo("Unrecognized option: --output directory name=foo");
- }
+ OptionsParsingException e =
+ assertThrows(
+ OptionsParsingException.class, () -> useConfiguration("--output directory name=foo"));
+ assertThat(e).hasMessageThat().isEqualTo("Unrecognized option: --output directory name=foo");
}
@Test
@@ -514,12 +513,7 @@
scratch.file("badbuild/BUILD", "");
reporter.removeHandler(failFastHandler);
injectGraphListenerForTesting(Listener.NULL_LISTENER, /*deterministic=*/ true);
- try {
- update("//foo:top");
- fail();
- } catch (ViewCreationFailedException e) {
- // Expected.
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//foo:top"));
assertContainsEvent("no such target '//badbuild:isweird': target 'isweird' not declared in "
+ "package 'badbuild'");
assertContainsEvent("and referenced by '//foo:bad'");
@@ -561,12 +555,7 @@
scratch.file("foo/BUILD",
"cc_binary(name = 'foo', linkshared = 1, srcs = ['foo.cc'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//foo:foo");
- fail(); // Expected ViewCreationFailedException.
- } catch (ViewCreationFailedException e) {
- // ok.
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//foo:foo"));
}
@Test
@@ -760,12 +749,8 @@
reporter.removeHandler(failFastHandler);
- try {
- update("//actions_not_registered:foo");
- fail("This build should fail because: 'linkshared' used in non-shared library");
- } catch (ViewCreationFailedException e) {
- assertThat(getActionGraph().getGeneratingAction(fooOut)).isNull();
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//actions_not_registered:foo"));
+ assertThat(getActionGraph().getGeneratingAction(fooOut)).isNull();
}
/**
@@ -870,22 +855,15 @@
"sh_library(name = 'b')",
"sh_library(name = 'z')");
reporter.removeHandler(failFastHandler);
- try {
- update("//foo:zquery");
- fail();
- } catch (ViewCreationFailedException e) {
- assertThat(e)
- .hasMessageThat()
- .contains("Analysis of target '//foo:zquery' failed; build aborted");
- }
- try {
- update("//foo:query");
- fail();
- } catch (ViewCreationFailedException e) {
- assertThat(e)
- .hasMessageThat()
- .contains("Analysis of target '//foo:query' failed; build aborted");
- }
+ ViewCreationFailedException e =
+ assertThrows(ViewCreationFailedException.class, () -> update("//foo:zquery"));
+ assertThat(e)
+ .hasMessageThat()
+ .contains("Analysis of target '//foo:zquery' failed; build aborted");
+ e = assertThrows(ViewCreationFailedException.class, () -> update("//foo:query"));
+ assertThat(e)
+ .hasMessageThat()
+ .contains("Analysis of target '//foo:query' failed; build aborted");
}
/**
@@ -936,14 +914,11 @@
// the cc_binary launcher.
useConfiguration("--java_launcher=//foo:cpp");
reporter.removeHandler(failFastHandler);
- try {
- update("//foo:java", "//foo:cpp");
- fail();
- } catch (ViewCreationFailedException expected) {
- assertThat(expected)
- .hasMessageThat()
- .matches("Analysis of target '//foo:(java|cpp)' failed; build aborted.*");
- }
+ ViewCreationFailedException expected =
+ assertThrows(ViewCreationFailedException.class, () -> update("//foo:java", "//foo:cpp"));
+ assertThat(expected)
+ .hasMessageThat()
+ .matches("Analysis of target '//foo:(java|cpp)' failed; build aborted.*");
assertContainsEvent("cycle in dependency graph");
}
@@ -954,14 +929,11 @@
scratch.file("bar/BUILD",
"BROKEN BROKEN BROKEN!!!");
reporter.removeHandler(failFastHandler);
- try {
- update("//foo:test");
- fail();
- } catch (ViewCreationFailedException expected) {
- assertThat(expected)
- .hasMessageThat()
- .matches("Analysis of target '//foo:test' failed; build aborted.*");
- }
+ ViewCreationFailedException expected =
+ assertThrows(ViewCreationFailedException.class, () -> update("//foo:test"));
+ assertThat(expected)
+ .hasMessageThat()
+ .matches("Analysis of target '//foo:test' failed; build aborted.*");
}
/**
@@ -978,15 +950,12 @@
"cc_library(name = 'foo', srcs = ['foo.cc'], deps = [':bar'])",
"cc_library(name = 'bar', srcs = ['bar.cc'], deps = [':foo'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//cycle:foo");
- fail();
- } catch (ViewCreationFailedException expected) {
- assertContainsEvent("in cc_library rule //cycle:foo: cycle in dependency graph:");
- assertThat(expected)
- .hasMessageThat()
- .contains("Analysis of target '//cycle:foo' failed; build aborted");
- }
+ ViewCreationFailedException expected =
+ assertThrows(ViewCreationFailedException.class, () -> update("//cycle:foo"));
+ assertContainsEvent("in cc_library rule //cycle:foo: cycle in dependency graph:");
+ assertThat(expected)
+ .hasMessageThat()
+ .contains("Analysis of target '//cycle:foo' failed; build aborted");
}
/**
@@ -1048,12 +1017,8 @@
scratch.file("z/b/BUILD",
"py_library(name='b', deps=['//z/a:a'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//z/b:b");
- fail();
- } catch (ViewCreationFailedException expected) {
- assertContainsEvent("no such package 'nonexistent'");
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//z/b:b"));
+ assertContainsEvent("no such package 'nonexistent'");
}
// regression test ("java.lang.IllegalStateException: cannot happen")
@@ -1065,12 +1030,8 @@
scratch.file("z/b/BUILD",
"py_library(name='b', deps=['//z/a:alib'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//z/b:b");
- fail();
- } catch (ViewCreationFailedException expected) {
- assertContainsEvent("no such package 'b'");
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//z/b:b"));
+ assertContainsEvent("no such package 'b'");
}
@Test
@@ -1085,11 +1046,7 @@
"sh_library(name = 'b')",
"undefined_symbol");
reporter.removeHandler(failFastHandler);
- try {
- update("//parent:a");
- fail();
- } catch (ViewCreationFailedException expected) {
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//parent:a"));
assertContainsEventWithFrequency("name 'undefined_symbol' is not defined", 1);
assertContainsEventWithFrequency(
"Target '//child:b' contains an error and its package is in error and referenced "
@@ -1130,12 +1087,7 @@
"sh_binary(name = 'okay', srcs = ['okay.sh'])");
useConfiguration("--experimental_action_listener=//parent:a");
reporter.removeHandler(failFastHandler);
- try {
- update("//okay");
- fail();
- } catch (ViewCreationFailedException e) {
- // Expected.
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//okay"));
assertContainsEventWithFrequency("name 'undefined_symbol' is not defined", 1);
assertContainsEventWithFrequency(
"Target '//child:b' contains an error and its package is in error and referenced "
@@ -1232,12 +1184,7 @@
"apple/BUILD",
"py_library(name='apple', visibility=['//non:existent'])");
reporter.removeHandler(failFastHandler);
- try {
- update("//apple");
- fail();
- } catch (ViewCreationFailedException e) {
- // Expected.
- }
+ assertThrows(ViewCreationFailedException.class, () -> update("//apple"));
assertDoesNotContainEvent("implicitly depends upon");
}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java b/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
index 31ad8d2..2cbb618 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
@@ -20,7 +20,7 @@
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.packages.BuildType.NODEP_LABEL;
import static com.google.devtools.build.lib.syntax.Type.STRING;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
@@ -112,12 +112,7 @@
Package pkg =
createScratchPackageForImplicitCycle(
"cycle", "java_library(name='jcyc',", " srcs = ['libjcyc.jar', 'foo.java'])");
- try {
- pkg.getTarget("jcyc");
- fail();
- } catch (NoSuchTargetException e) {
- /* ok */
- }
+ assertThrows(NoSuchTargetException.class, () -> pkg.getTarget("jcyc"));
assertThat(pkg.containsErrors()).isTrue();
assertContainsEvent("rule 'jcyc' has file 'libjcyc.jar' as both an" + " input and an output");
}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/CompilationHelperTest.java b/src/test/java/com/google/devtools/build/lib/analysis/CompilationHelperTest.java
index 10296cb..b65a0e5 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/CompilationHelperTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/CompilationHelperTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
@@ -114,15 +114,11 @@
ConfiguredTargetAndData ccRuleA = getConfiguredTargetAndData("//foo:liba.so");
List<Artifact> middleman1 = getAggregatingMiddleman(ccRuleA, true);
- try {
- ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so");
- getAggregatingMiddleman(ccRuleB, true);
- analysisEnvironment.registerWith(getMutableActionGraph());
- fail("Expected ActionConflictException due to same middleman artifact with different files");
- } catch (UncheckedActionConflictException e) {
- // Expected failure: same "purpose" and root directory sent to the middleman generator
- // (which results in the same output artifact), but different rules / middleman inputs.
- }
+ ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so");
+ getAggregatingMiddleman(ccRuleB, true);
+ assertThrows(
+ UncheckedActionConflictException.class,
+ () -> analysisEnvironment.registerWith(getMutableActionGraph()));
// This should succeed because the py_binary's middleman is under the Python configuration's
// internal directory, while the cc_binary's middleman is under the cc config's directory,
@@ -149,15 +145,11 @@
ConfiguredTargetAndData ccRuleA = getConfiguredTargetAndData("//foo:liba.so");
List<Artifact> middleman1 = getAggregatingMiddleman(ccRuleA, true);
- try {
- ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so");
- getAggregatingMiddleman(ccRuleB, true);
- analysisEnvironment.registerWith(getMutableActionGraph());
- fail("Expected ActionConflictException due to same middleman artifact with different files");
- } catch (UncheckedActionConflictException e) {
- // Expected failure: same "purpose" and root directory sent to the middleman generator
- // (which results in the same output artifact), but different rules / middleman inputs.
- }
+ ConfiguredTargetAndData ccRuleB = getConfiguredTargetAndData("//foo:libb.so");
+ getAggregatingMiddleman(ccRuleB, true);
+ assertThrows(
+ UncheckedActionConflictException.class,
+ () -> analysisEnvironment.registerWith(getMutableActionGraph()));
// This should succeed because the java_binary's middleman is under the Java configuration's
// internal directory, while the cc_binary's middleman is under the cc config's directory.
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java b/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
index 3ac9267..8b4a179 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
@@ -15,7 +15,7 @@
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 static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -866,12 +866,8 @@
")");
reporter.removeHandler(failFastHandler);
- try {
- getTarget("//java/foo:binary");
- fail();
- } catch (NoSuchTargetException e) {
- assertContainsEvent("'+' operator applied to incompatible types");
- }
+ assertThrows(NoSuchTargetException.class, () -> getTarget("//java/foo:binary"));
+ assertContainsEvent("'+' operator applied to incompatible types");
}
@Test
@@ -909,12 +905,8 @@
" }) + glob(['globbed.java']))");
reporter.removeHandler(failFastHandler);
- try {
- getTarget("//foo:binary");
- fail();
- } catch (NoSuchTargetException e) {
- assertContainsEvent("'+' operator applied to incompatible types");
- }
+ assertThrows(NoSuchTargetException.class, () -> getTarget("//foo:binary"));
+ assertContainsEvent("'+' operator applied to incompatible types");
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/DependencyTest.java b/src/test/java/com/google/devtools/build/lib/analysis/DependencyTest.java
index bf8f9ab..23a504f 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/DependencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/DependencyTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@@ -48,12 +48,7 @@
assertThat(nullDep.getConfiguration()).isNull();
assertThat(nullDep.getAspects().getAllAspects()).isEmpty();
- try {
- nullDep.getTransition();
- fail("withNullConfiguration-created Dependencies should throw ISE on getTransition()");
- } catch (IllegalStateException ex) {
- // good. expected.
- }
+ assertThrows(IllegalStateException.class, () -> nullDep.getTransition());
}
@Test
@@ -68,12 +63,7 @@
assertThat(targetDep.getConfiguration()).isEqualTo(getTargetConfiguration());
assertThat(targetDep.getAspects().getAllAspects()).isEmpty();
- try {
- targetDep.getTransition();
- fail("withConfiguration-created Dependencies should throw ISE on getTransition()");
- } catch (IllegalStateException ex) {
- // good. expected.
- }
+ assertThrows(IllegalStateException.class, () -> targetDep.getTransition());
}
@Test
@@ -95,12 +85,7 @@
assertThat(targetDep.getAspectConfiguration(attributeAspect))
.isEqualTo(getTargetConfiguration());
- try {
- targetDep.getTransition();
- fail("withConfigurationAndAspects-created Dependencies should throw ISE on getTransition()");
- } catch (IllegalStateException ex) {
- // good. that's what I WANTED to happen.
- }
+ assertThrows(IllegalStateException.class, () -> targetDep.getTransition());
}
@Test
@@ -111,13 +96,11 @@
AspectDescriptor attributeAspect = new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT);
AspectCollection twoAspects = AspectCollection.createForTests(simpleAspect, attributeAspect);
- try {
- Dependency.withConfigurationAndAspects(
- Label.parseAbsolute("//a", ImmutableMap.of()), null, twoAspects);
- fail("should not be allowed to create a dependency with a null configuration");
- } catch (NullPointerException expected) {
- // good. you fell rrrrright into my trap.
- }
+ assertThrows(
+ NullPointerException.class,
+ () ->
+ Dependency.withConfigurationAndAspects(
+ Label.parseAbsolute("//a", ImmutableMap.of()), null, twoAspects));
}
@Test
@@ -157,12 +140,7 @@
assertThat(targetDep.getAspectConfiguration(attributeAspect))
.isEqualTo(getHostConfiguration());
- try {
- targetDep.getTransition();
- fail("withConfiguredAspects-created Dependencies should throw ISE on getTransition()");
- } catch (IllegalStateException ex) {
- // good. all according to keikaku. (TL note: keikaku means plan)
- }
+ assertThrows(IllegalStateException.class, () -> targetDep.getTransition());
}
@@ -195,28 +173,12 @@
.containsExactlyElementsIn(twoAspects.getAllAspects());
assertThat(hostDep.getTransition().isHostTransition()).isTrue();
- try {
- hostDep.getConfiguration();
- fail("withTransitionAndAspects-created Dependencies should throw ISE on getConfiguration()");
- } catch (IllegalStateException ex) {
- // good. I knew you would do that.
- }
+ assertThrows(IllegalStateException.class, () -> hostDep.getConfiguration());
- try {
- hostDep.getAspectConfiguration(simpleAspect);
- fail("withTransitionAndAspects-created Dependencies should throw ISE on "
- + "getAspectConfiguration()");
- } catch (IllegalStateException ex) {
- // good. you're so predictable.
- }
+ assertThrows(IllegalStateException.class, () -> hostDep.getAspectConfiguration(simpleAspect));
- try {
- hostDep.getAspectConfiguration(attributeAspect);
- fail("withTransitionAndAspects-created Dependencies should throw ISE on "
- + "getAspectConfiguration()");
- } catch (IllegalStateException ex) {
- // good. you're so predictable.
- }
+ assertThrows(
+ IllegalStateException.class, () -> hostDep.getAspectConfiguration(attributeAspect));
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/InterruptedExceptionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/InterruptedExceptionTest.java
index fc1b707..bc113e3 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/InterruptedExceptionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/InterruptedExceptionTest.java
@@ -13,7 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.devtools.build.lib.analysis.util.AnalysisTestCase;
import com.google.devtools.build.lib.clock.BlazeClock;
@@ -56,11 +56,7 @@
scratch.file("a/causes_interrupt/bar.sh", "testfile");
reporter.removeHandler(failFastHandler);
- try {
- update("//a:a");
- fail("Expected interrupted exception");
- } catch (InterruptedException expected) {
- }
+ assertThrows(InterruptedException.class, () -> update("//a:a"));
}
@Test
@@ -76,10 +72,6 @@
scratch.file("a/causes_interrupt/bar.sh", "testfile");
reporter.removeHandler(failFastHandler);
- try {
- update("//a:a");
- fail("Expected interrupted exception");
- } catch (InterruptedException expected) {
- }
+ assertThrows(InterruptedException.class, () -> update("//a:a"));
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/LabelExpanderTest.java b/src/test/java/com/google/devtools/build/lib/analysis/LabelExpanderTest.java
index 0ae750d..590933a 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/LabelExpanderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/LabelExpanderTest.java
@@ -14,7 +14,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -169,24 +169,23 @@
@Test
public void testThrowsWhenMappingIsNotOneToOne() throws Exception {
setupDummy();
- try {
- LabelExpander.expand("x1", ImmutableMap.<Label, Iterable<Artifact>>of(
- labelFor("x1"), ImmutableList.<Artifact>of()), dummyTarget.getLabel());
+ assertThrows(
+ LabelExpander.NotUniqueExpansionException.class,
+ () ->
+ LabelExpander.expand(
+ "x1",
+ ImmutableMap.<Label, Iterable<Artifact>>of(
+ labelFor("x1"), ImmutableList.<Artifact>of()),
+ dummyTarget.getLabel()));
- fail("Expected an exception.");
- } catch (LabelExpander.NotUniqueExpansionException nuee) {
- // was expected
- }
-
- try {
- LabelExpander.expand("x1", ImmutableMap.<Label, Iterable<Artifact>>of(
- labelFor("x1"), ImmutableList.of(artifactFor("x1"), artifactFor("x2"))),
- dummyTarget.getLabel());
-
- fail("Expected an exception.");
- } catch (LabelExpander.NotUniqueExpansionException nuee) {
- // was expected
- }
+ assertThrows(
+ LabelExpander.NotUniqueExpansionException.class,
+ () ->
+ LabelExpander.expand(
+ "x1",
+ ImmutableMap.<Label, Iterable<Artifact>>of(
+ labelFor("x1"), ImmutableList.of(artifactFor("x1"), artifactFor("x2"))),
+ dummyTarget.getLabel()));
}
/**
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
index 258095f..bdc969c 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
@@ -15,7 +15,7 @@
package com.google.devtools.build.lib.analysis;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
@@ -59,57 +59,49 @@
@Test
public void noSuchLabel() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", false).build();
- try {
- func.apply("//bar", ImmutableMap.of());
- fail();
- } catch (IllegalStateException expected) {
- assertThat(expected).hasMessageThat()
- .isEqualTo(
- "label '//bar:bar' in $(location) expression is not a declared prerequisite of this "
- + "rule");
- }
+ IllegalStateException expected =
+ assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of()));
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo(
+ "label '//bar:bar' in $(location) expression is not a declared prerequisite of this "
+ + "rule");
}
@Test
public void emptyList() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", false).add("//foo").build();
- try {
- func.apply("//foo", ImmutableMap.of());
- fail();
- } catch (IllegalStateException expected) {
- assertThat(expected).hasMessageThat()
- .isEqualTo("label '//foo:foo' in $(location) expression expands to no files");
- }
+ IllegalStateException expected =
+ assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of()));
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo("label '//foo:foo' in $(location) expression expands to no files");
}
@Test
public void tooMany() throws Exception {
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/1", "/exec/2").build();
- try {
- func.apply("//foo", ImmutableMap.of());
- fail();
- } catch (IllegalStateException expected) {
- assertThat(expected).hasMessageThat()
- .isEqualTo(
- "label '//foo:foo' in $(location) expression expands to more than one file, "
- + "please use $(locations //foo:foo) instead. Files (at most 5 shown) are: "
- + "[./1, ./2]");
- }
+ IllegalStateException expected =
+ assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of()));
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo(
+ "label '//foo:foo' in $(location) expression expands to more than one file, "
+ + "please use $(locations //foo:foo) instead. Files (at most 5 shown) are: "
+ + "[./1, ./2]");
}
@Test
public void noSuchLabelMultiple() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", true).build();
- try {
- func.apply("//bar", ImmutableMap.of());
- fail();
- } catch (IllegalStateException expected) {
- assertThat(expected).hasMessageThat()
- .isEqualTo(
- "label '//bar:bar' in $(locations) expression is not a declared prerequisite of this "
- + "rule");
- }
+ IllegalStateException expected =
+ assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of()));
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo(
+ "label '//bar:bar' in $(locations) expression is not a declared prerequisite of this "
+ + "rule");
}
@Test