Skip to content

Commit

Permalink
feat: take into account TaskDefs with only TestSelectors.
Browse files Browse the repository at this point in the history
more information can be found at sbt/junit-interface#108
  • Loading branch information
Kamil Podsiadlo committed Mar 12, 2022
1 parent aa30ff9 commit b716c10
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

import sbt.testing.Runner;
import sbt.testing.Task;
import sbt.testing.Selector;
import sbt.testing.TestSelector;
import sbt.testing.TaskDef;

import java.util.stream.Collectors;

final class JUnitRunner implements Runner {
private final String[] args;
private final String[] remoteArgs;
Expand Down Expand Up @@ -127,7 +131,8 @@ public Task[] tasks(TaskDef[] taskDefs) {
for (int i = 0; i < length; i++) {
TaskDef taskDef = taskDefs[i];
if (shouldRun(computer, taskDef)) {
tasks.add(new JUnitTask(this, settings, taskDef, computer));
RunSettings alteredSettings = alterRunSettings(this.settings, taskDef.selectors());
tasks.add(new JUnitTask(this, alteredSettings, taskDef, computer));
}
}
return tasks.toArray(new Task[0]);
Expand All @@ -153,6 +158,30 @@ private RunListener createRunListener(String runListenerClassName) {
} else return null;
}

/**
* 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;
}

@Override
public String done() {
// Can't simply return the summary due to https://github.com/sbt/sbt/issues/3510
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ class RunSettings implements Settings {
this.trimStackTraces = trimStackTraces;
}

public RunSettings withTestFilter(String newTestFilter) {
String ignoreRunners = String.join(",", this.ignoreRunners);
return new RunSettings(
this.color,
this.decodeScalaNames,
this.verbose,
this.useSbtLoggers,
this.useBufferedLoggers,
this.trimStackTraces,
this.summary,
this.logAssert,
ignoreRunners,
this.logExceptionClass,
this.sysprops,
this.globPatterns,
this.includeCategories,
this.excludeCategories,
this.includeTags,
this.excludeTags,
newTestFilter);
}

String decodeName(String name) {
return decodeScalaNames ? decodeScalaName(name) : name;
}
Expand Down

0 comments on commit b716c10

Please sign in to comment.