Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow multiple test engines #900

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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