Skip to content

Commit

Permalink
Take into account TaskDefs with only TestSelectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Podsiadlo committed Dec 23, 2021
1 parent 320b678 commit b25db4b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
37 changes: 31 additions & 6 deletions src/main/java/com/novocode/junit/JUnitRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.runner.notification.RunListener;
import sbt.testing.Runner;
import sbt.testing.Task;
import sbt.testing.Selector;
import sbt.testing.TestSelector;
import sbt.testing.TaskDef;

import java.util.*;
Expand Down Expand Up @@ -77,15 +79,38 @@ else if(s.startsWith("-D") && s.contains("=")) {
@Override
public Task[] tasks(TaskDef[] taskDefs) {
used = true;
int length = taskDefs.length;
Task[] tasks = new Task[length];
for (int i = 0; i < length; i++) {
TaskDef taskDef = taskDefs[i];
tasks[i] = new JUnitTask(this, settings, taskDef);
}
Task[] tasks = Arrays
.stream(taskDefs)
.map(taskDef -> {
RunSettings alteredSettings = alterRunSettings(this.settings, taskDef.selectors());
return new JUnitTask(this, alteredSettings, taskDef);
})
.toArray(Task[]::new);
return tasks;
}

/**
* Alter default RunSettings depending on the passed selectors.
* If selectors contains only elements of type TestSelector, then default settings are altered to include only test
* names from these selectors. This allows to run particular test cases within given test class.
* testFilter is treated as a regular expression, hence joining is done via '|'.
*/
private RunSettings alterRunSettings(RunSettings defaultSettings, Selector[] selectors) {
boolean onlyTestSelectors = Arrays.stream(selectors).allMatch(selector -> selector instanceof TestSelector);
if (onlyTestSelectors) {
String testFilter = Arrays
.stream(selectors)
.map(selector -> ((TestSelector) selector).testName())
.collect(Collectors.joining("|"));
// if already provided testFilter is not empty add to it | (regex or operator)
String currentFilter = defaultSettings.testFilter.length() > 0 ? defaultSettings.testFilter + "|" : "";
String newFilter = currentFilter + testFilter;
return defaultSettings.withTestFilter(newFilter);
}

return defaultSettings;
}

private RunListener createRunListener(String runListenerClassName) {
if(runListenerClassName != null) {
try {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/novocode/junit/RunSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class RunSettings {
this.testFilter = testFilter;
}

public RunSettings withTestFilter(String newTestFilter) {
String ignoreRunners = String.join(",", this.ignoreRunners);
return new RunSettings(
this.color, this.decodeScalaNames, this.quiet, this.verbosity, this.summary, this.logAssert,
ignoreRunners, this.logExceptionClass, this.sysprops, this.globPatterns, this.includeCategories,
this.excludeCategories, newTestFilter
);
}

String decodeName(String name) {
return decodeScalaNames ? decodeScalaName(name) : name;
}
Expand Down Expand Up @@ -108,7 +117,7 @@ String buildErrorMessage(Throwable t) {

private String buildColoredName(Description desc, String c1, String c2, String c3) {
StringBuilder b = new StringBuilder();

String cn = decodeName(desc.getClassName());
int pos1 = cn.indexOf('$');
int pos2 = pos1 == -1 ? cn.lastIndexOf('.') : cn.lastIndexOf('.', pos1);
Expand Down

0 comments on commit b25db4b

Please sign in to comment.