Skip to content

Commit

Permalink
Merge pull request #44371 from geoand/#44118
Browse files Browse the repository at this point in the history
Make @QuarkusMainTest respect `quarkus.test.profile.tags`
  • Loading branch information
gsmet authored Nov 7, 2024
2 parents 54e17d8 + a82eba2 commit 5810fca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.Alternative;

import org.jboss.jandex.Index;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.quarkus.bootstrap.BootstrapConstants;
Expand All @@ -41,7 +44,8 @@
import io.quarkus.test.common.RestorableSystemProperties;
import io.quarkus.test.common.TestClassIndexer;

public class AbstractJvmQuarkusTestExtension extends AbstractQuarkusTestWithContextExtension {
public class AbstractJvmQuarkusTestExtension extends AbstractQuarkusTestWithContextExtension
implements ExecutionCondition {

protected static final String TEST_LOCATION = "test-location";
protected static final String TEST_CLASS = "test-class";
Expand Down Expand Up @@ -267,6 +271,42 @@ private Class<? extends QuarkusTestProfile> findTestProfileAnnotation(Class<?> c
return null;
}

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if (!context.getTestClass().isPresent()) {
return ConditionEvaluationResult.enabled("No test class specified");
}
if (context.getTestInstance().isPresent()) {
return ConditionEvaluationResult.enabled("Quarkus Test Profile tags only affect classes");
}
String tagsStr = System.getProperty("quarkus.test.profile.tags");
if ((tagsStr == null) || tagsStr.isEmpty()) {
return ConditionEvaluationResult.enabled("No Quarkus Test Profile tags");
}
Class<? extends QuarkusTestProfile> testProfile = getQuarkusTestProfile(context);
if (testProfile == null) {
return ConditionEvaluationResult.disabled("Test '" + context.getRequiredTestClass()
+ "' is not annotated with '@QuarkusTestProfile' but 'quarkus.profile.test.tags' was set");
}
QuarkusTestProfile profileInstance;
try {
profileInstance = testProfile.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
Set<String> testProfileTags = profileInstance.tags();
String[] tags = tagsStr.split(",");
for (String tag : tags) {
String trimmedTag = tag.trim();
if (testProfileTags.contains(trimmedTag)) {
return ConditionEvaluationResult.enabled("Tag '" + trimmedTag + "' is present on '" + testProfile
+ "' which is used on test '" + context.getRequiredTestClass());
}
}
return ConditionEvaluationResult.disabled("Test '" + context.getRequiredTestClass()
+ "' disabled because 'quarkus.profile.test.tags' don't match the tags of '" + testProfile + "'");
}

protected static class PrepareResult {
protected final AugmentAction augmentAction;
protected final QuarkusTestProfile profileInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ParameterContext;
Expand All @@ -38,7 +40,7 @@

public class QuarkusMainTestExtension extends AbstractJvmQuarkusTestExtension
implements InvocationInterceptor, BeforeEachCallback, AfterEachCallback, ParameterResolver, BeforeAllCallback,
AfterAllCallback {
AfterAllCallback, ExecutionCondition {

PrepareResult prepareResult;

Expand Down Expand Up @@ -321,4 +323,9 @@ public void afterAll(ExtensionContext context) throws Exception {
public void beforeAll(ExtensionContext context) throws Exception {
currentTestClassStack.push(context.getRequiredTestClass());
}

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return super.evaluateExecutionCondition(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -54,8 +53,6 @@
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ParameterContext;
Expand Down Expand Up @@ -105,7 +102,7 @@
public class QuarkusTestExtension extends AbstractJvmQuarkusTestExtension
implements BeforeEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, AfterEachCallback,
BeforeAllCallback, InvocationInterceptor, AfterAllCallback,
ParameterResolver, ExecutionCondition {
ParameterResolver {

private static final Logger log = Logger.getLogger(QuarkusTestExtension.class);

Expand Down Expand Up @@ -1136,42 +1133,6 @@ private boolean testMethodInvokerHandlesParamType(Object testMethodInvoker, Para
}
}

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
if (!context.getTestClass().isPresent()) {
return ConditionEvaluationResult.enabled("No test class specified");
}
if (context.getTestInstance().isPresent()) {
return ConditionEvaluationResult.enabled("Quarkus Test Profile tags only affect classes");
}
String tagsStr = System.getProperty("quarkus.test.profile.tags");
if ((tagsStr == null) || tagsStr.isEmpty()) {
return ConditionEvaluationResult.enabled("No Quarkus Test Profile tags");
}
Class<? extends QuarkusTestProfile> testProfile = getQuarkusTestProfile(context);
if (testProfile == null) {
return ConditionEvaluationResult.disabled("Test '" + context.getRequiredTestClass()
+ "' is not annotated with '@QuarkusTestProfile' but 'quarkus.profile.test.tags' was set");
}
QuarkusTestProfile profileInstance;
try {
profileInstance = testProfile.getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
Set<String> testProfileTags = profileInstance.tags();
String[] tags = tagsStr.split(",");
for (String tag : tags) {
String trimmedTag = tag.trim();
if (testProfileTags.contains(trimmedTag)) {
return ConditionEvaluationResult.enabled("Tag '" + trimmedTag + "' is present on '" + testProfile
+ "' which is used on test '" + context.getRequiredTestClass());
}
}
return ConditionEvaluationResult.disabled("Test '" + context.getRequiredTestClass()
+ "' disabled because 'quarkus.profile.test.tags' don't match the tags of '" + testProfile + "'");
}

public static class ExtensionState extends QuarkusTestExtensionState {

public ExtensionState(Closeable testResourceManager, Closeable resource, Runnable clearCallbacks) {
Expand Down

0 comments on commit 5810fca

Please sign in to comment.