Skip to content

Commit

Permalink
Add regex based filters
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Apr 6, 2021
1 parent 6381135 commit 475dcaa
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TestConfig {
* started from the Dev UI, and console output will not be printed.
*/
@ConfigItem(defaultValue = "PAUSED")
public Mode enabled;
public Mode continuousTesting;

/**
* Tags that should be included for continuous testing.
Expand All @@ -44,6 +44,20 @@ public class TestConfig {
@ConfigItem(defaultValue = "slow")
public Optional<List<String>> excludeTags;

/**
* Tests that should be included for continuous testing. This is a regular expression.
*/
@ConfigItem
public Optional<String> includePattern;

/**
* Tests that should be excluded with continuous testing. This is a regular expression.
*
* This is ignored if include-pattern has been set.
*
*/
@ConfigItem
public Optional<String> excludePattern;
/**
* Disable the testing status/prompt message at the bottom of the console
* and log these messages to STDOUT instead.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package io.quarkus.deployment.dev;

import io.quarkus.bootstrap.runner.Timing;
import io.quarkus.changeagent.ClassChangeAgent;
import io.quarkus.deployment.dev.testing.TestSupport;
import io.quarkus.deployment.dev.testing.runner.TestRunner;
import io.quarkus.deployment.util.FSWatchUtil;
import io.quarkus.deployment.util.FileUtil;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.dev.spi.HotReplacementContext;
import io.quarkus.dev.spi.HotReplacementSetup;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
Expand Down Expand Up @@ -55,8 +42,22 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;

import io.quarkus.bootstrap.runner.Timing;
import io.quarkus.changeagent.ClassChangeAgent;
import io.quarkus.deployment.dev.testing.TestSupport;
import io.quarkus.deployment.dev.testing.runner.TestRunner;
import io.quarkus.deployment.util.FSWatchUtil;
import io.quarkus.deployment.util.FileUtil;
import io.quarkus.dev.spi.DevModeType;
import io.quarkus.dev.spi.HotReplacementContext;
import io.quarkus.dev.spi.HotReplacementSetup;

public class RuntimeUpdatesProcessor implements HotReplacementContext, Closeable {

Expand Down Expand Up @@ -109,10 +110,10 @@ public class RuntimeUpdatesProcessor implements HotReplacementContext, Closeable
private final TestSupport testSupport;

public RuntimeUpdatesProcessor(Path applicationRoot, DevModeContext context, QuarkusCompiler compiler,
DevModeType devModeType, BiConsumer<Set<String>, ClassScanResult> restartCallback,
BiConsumer<DevModeContext.ModuleInfo, String> copyResourceNotification,
BiFunction<String, byte[], byte[]> classTransformers,
TestSupport testSupport) {
DevModeType devModeType, BiConsumer<Set<String>, ClassScanResult> restartCallback,
BiConsumer<DevModeContext.ModuleInfo, String> copyResourceNotification,
BiFunction<String, byte[], byte[]> classTransformers,
TestSupport testSupport) {
this.applicationRoot = applicationRoot;
this.context = context;
this.compiler = compiler;
Expand Down Expand Up @@ -452,8 +453,8 @@ ClassScanResult checkForChangedTestClasses(boolean firstScan) {
* {@link RuntimeUpdatesProcessor#checkIfFileModified(Path, Map, boolean)} during the first scan.
*/
ClassScanResult checkForChangedClasses(QuarkusCompiler compiler,
Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf, boolean firstScan,
TimestampSet timestampSet) {
Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf, boolean firstScan,
TimestampSet timestampSet) {
ClassScanResult classScanResult = new ClassScanResult();
boolean ignoreFirstScanChanges = firstScan;

Expand Down Expand Up @@ -510,8 +511,8 @@ public Throwable getCompileProblem() {
}

private void checkForClassFilesChangesInModule(DevModeContext.ModuleInfo module, List<Path> moduleChangedSourceFiles,
boolean isInitialRun, ClassScanResult classScanResult,
Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf, TimestampSet timestampSet) {
boolean isInitialRun, ClassScanResult classScanResult,
Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf, TimestampSet timestampSet) {
if (cuf.apply(module).getClassesPath() == null) {
return;
}
Expand Down Expand Up @@ -567,8 +568,8 @@ private void checkForClassFilesChangesInModule(DevModeContext.ModuleInfo module,
}

private Path retrieveSourceFilePathForClassFile(Path classFilePath, List<Path> moduleChangedSourceFiles,
DevModeContext.ModuleInfo module, Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf,
TimestampSet timestampSet) {
DevModeContext.ModuleInfo module, Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf,
TimestampSet timestampSet) {
Path sourceFilePath = timestampSet.classFilePathToSourceFilePath.get(classFilePath);
if (sourceFilePath == null || moduleChangedSourceFiles.contains(sourceFilePath)) {
sourceFilePath = compiler.findSourcePath(classFilePath, cuf.apply(module).getSourcePaths(),
Expand Down Expand Up @@ -601,7 +602,7 @@ Set<String> checkForFileChange() {
}

Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContext.CompilationUnit> cuf,
TimestampSet timestampSet) {
TimestampSet timestampSet) {
Set<String> ret = new HashSet<>();
for (DevModeContext.ModuleInfo module : context.getAllModules()) {
final Set<Path> moduleResources = correspondingResources.computeIfAbsent(module.getName(),
Expand Down Expand Up @@ -705,12 +706,12 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
}

private boolean sourceFileWasRecentModified(final Path sourcePath, boolean ignoreFirstScanChanges,
TimestampSet timestampSet) {
TimestampSet timestampSet) {
return checkIfFileModified(sourcePath, sourceFileTimestamps, ignoreFirstScanChanges);
}

private boolean classFileWasRecentModified(final Path classFilePath, boolean ignoreFirstScanChanges,
TimestampSet timestampSet) {
TimestampSet timestampSet) {
return checkIfFileModified(classFilePath, timestampSet.classFileChangeTimeStamps, ignoreFirstScanChanges);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ public void start(boolean runTests) {
}

public void init() {
if (!context.getApplicationRoot().getTest().isPresent()) {
return;
}
if (testCuratedApplication == null) {
try {
testCuratedApplication = curatedApplication.getQuarkusBootstrap().clonedBuilder()
Expand Down Expand Up @@ -258,9 +261,19 @@ public TestRunResults getResults() {
}

public void setTags(List<String> includeTags, List<String> excludeTags) {
if (testRunner == null) {
return;
}
testRunner.setTags(includeTags, excludeTags);
}

public void setPatterns(String include, String exclude) {
if (testRunner == null) {
return;
}
testRunner.setPatterns(include, exclude);
}

public static class RunStatus {

final long lastRun;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.LogRecord;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -83,6 +84,8 @@ public class JunitTestRunner {
private final TestListener listener;
private final Set<String> includeTags;
private final Set<String> excludeTags;
private final Pattern include;
private final Pattern exclude;

private volatile boolean testsRunning = false;
private volatile boolean aborted;
Expand All @@ -98,6 +101,8 @@ public JunitTestRunner(Builder builder) {
this.testState = builder.testState;
this.includeTags = new HashSet<>(builder.includeTags);
this.excludeTags = new HashSet<>(builder.excludeTags);
this.include = builder.include;
this.exclude = builder.exclude;
}

public void runTests() {
Expand All @@ -122,6 +127,11 @@ public void runTests() {
} else if (!excludeTags.isEmpty()) {
launchBuilder.filters(new TagFilter(true, excludeTags));
}
if (include != null) {
launchBuilder.filters(new RegexFilter(false, include));
} else if (exclude != null) {
launchBuilder.filters(new RegexFilter(true, exclude));
}
LauncherDiscoveryRequest request = launchBuilder
.build();
TestPlan testPlan = launcher.discover(request);
Expand Down Expand Up @@ -482,6 +492,8 @@ static class Builder {
private TestListener listener;
private List<String> includeTags = Collections.emptyList();
private List<String> excludeTags = Collections.emptyList();
private Pattern include;
private Pattern exclude;

public Builder setRunId(long runId) {
this.runId = runId;
Expand Down Expand Up @@ -528,6 +540,16 @@ public Builder setTestState(TestState testState) {
return this;
}

public Builder setInclude(Pattern include) {
this.include = include;
return this;
}

public Builder setExclude(Pattern exclude) {
this.exclude = exclude;
return this;
}

public JunitTestRunner build() {
Objects.requireNonNull(devModeContext, "devModeContext");
Objects.requireNonNull(testClassUsages, "testClassUsages");
Expand All @@ -536,6 +558,7 @@ public JunitTestRunner build() {
Objects.requireNonNull(listener, "listener");
return new JunitTestRunner(this);
}

}

private static class TagFilter implements PostDiscoveryFilter {
Expand Down Expand Up @@ -565,7 +588,7 @@ public FilterResult apply(TestDescriptor testDescriptor) {
return FilterResult.includedIf(exclude);
}
}
return FilterResult.included("not a class");
return FilterResult.included("not a method");
}

public FilterResult filterTags(AnnotatedElement clz) {
Expand All @@ -587,4 +610,30 @@ public FilterResult filterTags(AnnotatedElement clz) {
return FilterResult.includedIf(exclude);
}
}

private static class RegexFilter implements PostDiscoveryFilter {

final boolean exclude;
final Pattern pattern;

private RegexFilter(boolean exclude, Pattern pattern) {
this.exclude = exclude;
this.pattern = pattern;
}

@Override
public FilterResult apply(TestDescriptor testDescriptor) {
if (testDescriptor.getSource().isPresent()) {
if (testDescriptor.getSource().get() instanceof MethodSource) {
MethodSource methodSource = (MethodSource) testDescriptor.getSource().get();
String name = methodSource.getJavaClass().getName();
if (pattern.matcher(name).matches()) {
return FilterResult.includedIf(!exclude);
}
return FilterResult.includedIf(exclude);
}
}
return FilterResult.included("not a method");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.regex.Pattern;

import org.jboss.logging.Logger;
import org.junit.platform.engine.TestExecutionResult;
Expand Down Expand Up @@ -51,6 +52,8 @@ public class TestRunner {
private volatile boolean firstRun = true;
volatile List<String> includeTags = Collections.emptyList();
volatile List<String> excludeTags = Collections.emptyList();
volatile Pattern include = null;
volatile Pattern exclude = null;
volatile InputHandler.ConsoleStatus promptHandler;
private JunitTestRunner runner;

Expand Down Expand Up @@ -80,7 +83,6 @@ public void handleInput(int[] keys) {
@Override
public void promptHandler(InputHandler.ConsoleStatus promptHandler) {
TestRunner.this.promptHandler = promptHandler;
promptHandler.setPrompt(DISABLED_PROMPT);
}
};

Expand Down Expand Up @@ -224,6 +226,8 @@ private void runInternal(ClassScanResult classScanResult) {
.setTestApplication(testApplication)
.setIncludeTags(includeTags)
.setExcludeTags(excludeTags)
.setInclude(include)
.setExclude(exclude)
.setListener(new JunitTestRunner.TestListener() {
@Override
public void runStarted(long toRun) {
Expand Down Expand Up @@ -350,4 +354,9 @@ public void setTags(List<String> includeTags, List<String> excludeTags) {
this.includeTags = includeTags;
this.excludeTags = excludeTags;
}

public void setPatterns(String include, String exclude) {
this.include = include == null ? null : Pattern.compile(include);
this.exclude = exclude == null ? null : Pattern.compile(exclude);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import java.util.List;
import java.util.function.BiFunction;

import io.quarkus.deployment.IsDevelopment;
import org.jboss.jandex.ClassInfo;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import io.quarkus.bootstrap.classloading.ClassPathElement;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.IsTest;
import io.quarkus.deployment.TestConfig;
Expand Down Expand Up @@ -42,9 +42,9 @@ LogCleanupFilterBuildItem handle() {
return new LogCleanupFilterBuildItem("org.junit.platform.launcher.core.EngineDiscoveryOrchestrator", "0 containers");
}

@BuildStep(onlyIf= IsDevelopment.class)
@BuildStep(onlyIf = IsDevelopment.class)
ServiceStartBuildItem setupConsole(TestConfig config) {
if (RuntimeUpdatesProcessor.INSTANCE == null || config.enabled == TestConfig.Mode.DISABLED) {
if (RuntimeUpdatesProcessor.INSTANCE == null || config.continuousTesting == TestConfig.Mode.DISABLED) {
return null;
}
QuarkusConsole.installConsole(config);
Expand All @@ -54,17 +54,19 @@ ServiceStartBuildItem setupConsole(TestConfig config) {
@BuildStep(onlyIfNot = IsNormal.class)
@Produce(LogHandlerBuildItem.class)
ServiceStartBuildItem startTesting(TestConfig config) {
if (RuntimeUpdatesProcessor.INSTANCE == null || config.enabled == TestConfig.Mode.DISABLED) {
if (RuntimeUpdatesProcessor.INSTANCE == null || config.continuousTesting == TestConfig.Mode.DISABLED) {
return null;
}
if (config.enabled == TestConfig.Mode.ENABLED) {
if (config.continuousTesting == TestConfig.Mode.ENABLED) {
RuntimeUpdatesProcessor.INSTANCE.getTestSupport().start();
} else if (config.enabled == TestConfig.Mode.PAUSED) {
} else if (config.continuousTesting == TestConfig.Mode.PAUSED) {
RuntimeUpdatesProcessor.INSTANCE.getTestSupport().init();
RuntimeUpdatesProcessor.INSTANCE.getTestSupport().stop();
}
RuntimeUpdatesProcessor.INSTANCE.getTestSupport().setTags(config.includeTags.orElse(Collections.emptyList()),
config.excludeTags.orElse(Collections.emptyList()));
RuntimeUpdatesProcessor.INSTANCE.getTestSupport().setPatterns(config.includePattern.orElse(null),
config.excludePattern.orElse(null));
return null;
}

Expand Down
Loading

0 comments on commit 475dcaa

Please sign in to comment.