Skip to content

Commit

Permalink
Throw an exception on extension loading if a build step doesn't produ…
Browse files Browse the repository at this point in the history
…ce anything
  • Loading branch information
yrodiere committed Aug 1, 2022
1 parent 95c52f7 commit 9a4a383
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ public BuildStepBuilder consumes(Class<? extends BuildItem> type, ConsumeFlags f
*/
public BuildChainBuilder build() {
final BuildChainBuilder chainBuilder = this.buildChainBuilder;
if (produces.isEmpty()) {
throw new IllegalArgumentException(
"Build step '" + buildStep.getId()
+ "' does not produce any build item and thus will never get executed."
+ " Either change the return type of the method to a build item type,"
+ " add a parameter of type BuildProducer<[some build item type]>/Consumer<[some build item type]>,"
+ " or annotate the method with @Produces."
+ " Use @Produce(EmptyBuildItem.class) if you want to always execute this step.");
}
if (BuildChainBuilder.LOG_CONFLICT_CAUSING) {
chainBuilder.addStep(this, new Exception().getStackTrace());
} else {
Expand Down
27 changes: 27 additions & 0 deletions core/builder/src/test/java/io/quarkus/builder/BasicTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.builder;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -176,6 +177,32 @@ public void execute(final BuildContext context) {
assertFalse(ran.get());
}

@Test
public void testMissingProduces() {
final BuildChainBuilder builder = BuildChain.builder();
BuildStepBuilder stepBuilder = builder.addBuildStep(new BuildStep() {
@Override
public void execute(final BuildContext context) {
context.produce(new DummyItem());
}

@Override
public String getId() {
return "myBuildStepId";
}
});
stepBuilder.consumes(DummyItem.class);
assertThatThrownBy(stepBuilder::build)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContainingAll(
"Build step 'myBuildStepId'",
"does not produce any build item and thus will never get executed",
"change the return type of the method to a build item type",
"add a parameter of type BuildProducer<[some build item type]>/Consumer<[some build item type]>",
"annotate the method with @Produces",
"Use @Produce(EmptyBuildItem.class) if you want to always execute this step");
}

@Test
public void testCircular() {
final BuildChainBuilder builder = BuildChain.builder();
Expand Down

0 comments on commit 9a4a383

Please sign in to comment.