Skip to content

Commit

Permalink
Merge pull request #900 from hcoles/multiple-test-engines
Browse files Browse the repository at this point in the history
allow multiple test engines
  • Loading branch information
hcoles authored May 24, 2021
2 parents cba9290 + 283f387 commit d51eb95
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.coverage.execute.samples.simple.TesteeWithMultipleLines;
import com.example.coverage.execute.samples.simple.Tests;
import com.example.coverage.execute.samples.simple.TestsForMultiBlockCoverage;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.pitest.SystemTest;
Expand Down Expand Up @@ -285,6 +286,7 @@ public void shouldNotCorruptedTheSystemNewLineProperty() throws Exception {
}

@Test
@Ignore("we have testng on the classpath")
public void shouldFailWithExitCode() throws Exception {
final Consumer<CoverageResult> noOpHandler = a -> {
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public JUnitCompatibleConfiguration(TestGroupConfig config, Collection<String> e
this.includedTestMethods = includedTestMethods;
}

@Override
public int priority() {
// make sure we are used after any test plugins added to the classpath
return DEFAULT_PRIORITY + 1;
}

@Override
public TestUnitFinder testUnitFinder() {
return new CompoundTestUnitFinder(Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.mutationtest.MutationEngineFactory;
import org.pitest.testapi.Configuration;
import org.pitest.testapi.TestPluginFactory;
import org.pitest.util.PitError;

import java.util.List;
import java.util.stream.Collectors;

public class MinionSettings {

private final ClientPluginServices plugins;
Expand All @@ -26,16 +28,15 @@ public MutationEngineFactory createEngine(String engine) {


public Configuration getTestFrameworkPlugin(TestPluginArguments options, ClassByteArraySource source) {
for (final TestPluginFactory each : this.plugins.findTestFrameworkPlugins()) {
if (each.name().equals(options.getTestPlugin())) {
return each.createTestFrameworkConfiguration(options.getGroupConfig(),
source,
options.getExcludedRunners(),
options.getIncludedTestMethods());
}
}
throw new PitError("Could not load requested test plugin "
+ options.getTestPlugin());
List<Configuration> configurations = this.plugins.findTestFrameworkPlugins().stream()
.map(p -> p.createTestFrameworkConfiguration(options.getGroupConfig(),
source,
options.getExcludedRunners(),
options.getIncludedTestMethods()))
.collect(Collectors.toList());

return new PrioritisingTestConfiguration(configurations);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.pitest.mutationtest.config;

import org.pitest.help.PitHelpError;
import org.pitest.testapi.Configuration;
import org.pitest.testapi.TestSuiteFinder;
import org.pitest.testapi.TestUnitFinder;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

class PrioritisingTestConfiguration implements Configuration {
private final List<Configuration> children;
private final TestUnitFinder finder;
private final TestSuiteFinder suiteFinder;

PrioritisingTestConfiguration(List<Configuration> children) {
this.children = pickChildren(children);
this.finder = makeFinder(this.children);
this.suiteFinder = makeSuiteFinder(this.children);
}

@Override
public TestUnitFinder testUnitFinder() {
return finder;
}

@Override
public TestSuiteFinder testSuiteFinder() {
return suiteFinder;
}

@Override
public Optional<PitHelpError> verifyEnvironment() {
return children.stream()
.map(Configuration::verifyEnvironment)
.findFirst()
.get();
}

private static List<Configuration> pickChildren(List<Configuration> configs) {
List<Configuration> working = configs.stream()
.filter(c -> !c.verifyEnvironment().isPresent())
.sorted(byPriority())
.collect(Collectors.toList());
// We don't have a working config, let it report errors later
if (working.isEmpty()) {
return configs;
}
return working;
}

private static Comparator<Configuration> byPriority() {
return Comparator.comparingInt(Configuration::priority);
}

private TestUnitFinder makeFinder(List<Configuration> children) {
List<TestUnitFinder> finders = children.stream()
.map(Configuration::testUnitFinder)
.collect(Collectors.toList());
return new PrioritisingTestUnitFinder(finders);
}

private TestSuiteFinder makeSuiteFinder(List<Configuration> children) {
List<TestSuiteFinder> finders = children.stream()
.map(Configuration::testSuiteFinder)
.collect(Collectors.toList());
return new PrioritisingTestSuiteFinder(finders);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.pitest.mutationtest.config;

import org.pitest.testapi.TestSuiteFinder;

import java.util.Collections;
import java.util.List;

class PrioritisingTestSuiteFinder implements TestSuiteFinder {
private final List<TestSuiteFinder> orderedChildren;

PrioritisingTestSuiteFinder(List<TestSuiteFinder> orderedChildren) {
this.orderedChildren = orderedChildren;
}

@Override
public List<Class<?>> apply(Class<?> clazz) {
for (TestSuiteFinder each : orderedChildren) {
List<Class<?>> found = each.apply(clazz);
if (!found.isEmpty()) {
return found;
}
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.pitest.mutationtest.config;

import org.pitest.testapi.TestUnit;
import org.pitest.testapi.TestUnitFinder;

import java.util.Collections;
import java.util.List;

class PrioritisingTestUnitFinder implements TestUnitFinder {
private final List<TestUnitFinder> orderedChildren;

PrioritisingTestUnitFinder(List<TestUnitFinder> orderedChildren) {
this.orderedChildren = orderedChildren;
}

@Override
public List<TestUnit> findTestUnits(Class<?> clazz) {
for (TestUnitFinder each : orderedChildren) {
List<TestUnit> found = each.findTestUnits(clazz);
if (!found.isEmpty()) {
return found;
}
}
return Collections.emptyList();
}
}
6 changes: 6 additions & 0 deletions pitest/src/main/java/org/pitest/testapi/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

public interface Configuration {

int DEFAULT_PRIORITY = 10;

default int priority() {
return DEFAULT_PRIORITY;
}

TestUnitFinder testUnitFinder();

TestSuiteFinder testSuiteFinder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.pitest.extension.common.NoTestSuiteFinder;
import java.util.Optional;

import org.pitest.help.Help;
import org.pitest.help.PitHelpError;
import org.pitest.testapi.Configuration;
import org.pitest.testapi.TestGroupConfig;
Expand Down Expand Up @@ -46,6 +48,12 @@ public TestSuiteFinder testSuiteFinder() {

@Override
public Optional<PitHelpError> verifyEnvironment() {
try {
Class.forName("org.testng.annotations.Test");
} catch (NoClassDefFoundError | ClassNotFoundException er) {
return Optional.ofNullable(new PitHelpError(Help.NO_TEST_LIBRARY));
}

return Optional.empty();
}

Expand Down
Loading

0 comments on commit d51eb95

Please sign in to comment.