Skip to content

Commit

Permalink
feat: compute top package name on the fly (#531)
Browse files Browse the repository at this point in the history
* feat: compute top package name on the fly

* feat: add a way to specify the path to the second version

* fix: substring the path if it is absolute
  • Loading branch information
danglotb authored Aug 29, 2018
1 parent e38027d commit 8fe41b3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
25 changes: 19 additions & 6 deletions dspot-maven/src/main/java/eu/stamp_project/DSpotMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ public class DSpotMojo extends AbstractMojo {
@Parameter(defaultValue = "", property = "path-to-test-list-csv")
private String pathToTestListCsv = "";

/**
* Allows to specify the path to the second version through command line, rather than using properties file.
* This parameter is the same than {@link eu.stamp_project.program.ConstantsProperties#PATH_TO_SECOND_VERSION}
* If this parameter is used, DSpot will ignore the value used in the properties file.
* It is recommended to use an absolute path
*/
@Parameter(defaultValue = "", property = "path-to-second-version")
private String pathToSecondVersion = "";

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.help) {
Expand All @@ -204,7 +213,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

Properties properties = initializeProperties();
try {
final InputConfiguration configuration = InputConfiguration.initialize(properties)
InputConfiguration.initialize(properties)
.setAmplifiers(AmplifierEnum.buildAmplifiersFromString(this.amplifiers.toArray(new String[this.amplifiers.size()])))
.setNbIteration(this.iteration)
.setTestClasses(this.test)
Expand All @@ -223,23 +232,27 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.setGenerateAmplifiedTestClass(this.generateNewTestClass)
.setOutputDirectory(this.outputPath);

if (!this.pathToSecondVersion.isEmpty()) {
InputConfiguration.get().setAbsolutePathToSecondVersionProjectRoot(this.pathToSecondVersion);
}

if (!this.pathToTestListCsv.isEmpty()) {
// clear both list of test classes and test cases
configuration.getTestCases().clear();
configuration.getTestClasses().clear();
InputConfiguration.get().getTestCases().clear();
InputConfiguration.get().getTestClasses().clear();
// add all test classes and test cases from the csv file
try (BufferedReader buffer = new BufferedReader(new FileReader(this.pathToTestListCsv))) {
buffer.lines().forEach(line -> {
final String[] splittedLine = line.split(";");
configuration.addTestClasses(splittedLine[0]);
InputConfiguration.get().addTestClasses(splittedLine[0]);
for (int i = 1 ; i < splittedLine.length ; i++) {
configuration.addTestCase(splittedLine[i]);
InputConfiguration.get().addTestCase(splittedLine[i]);
}
}
);
}
}
Main.run(configuration);
Main.run(InputConfiguration.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;

import java.io.BufferedReader;
Expand Down Expand Up @@ -154,7 +155,7 @@ public void runPit(String pathToRootOfProject, CtType<?>... testClasses) {
try {

if (InputConfiguration.get().getFilter().isEmpty()) {
LOGGER.warn(MESSAGE_WARN_PIT_NO_FILTER);
initializeFilterInputConfiguration();
}

String[] phases = new String[]{CMD_PIT_MUTATION_COVERAGE + ":" +
Expand Down Expand Up @@ -196,7 +197,7 @@ public void runPit(String pathToRootOfProject) {
}

if (InputConfiguration.get().getFilter().isEmpty()) {
LOGGER.warn(MESSAGE_WARN_PIT_NO_FILTER);
initializeFilterInputConfiguration();
}

try {
Expand Down Expand Up @@ -224,6 +225,16 @@ public void runPit(String pathToRootOfProject) {
}
}

private void initializeFilterInputConfiguration() {
LOGGER.warn(MESSAGE_WARN_PIT_NO_FILTER);
LOGGER.warn("Trying to compute the top package of the project...");
CtPackage currentPackage = InputConfiguration.get().getFactory().Package().getRootPackage();
while (currentPackage.getPackages().size() == 1) {
currentPackage = (CtPackage) currentPackage.getPackages().toArray()[0];
}
InputConfiguration.get().setFilter(currentPackage.getQualifiedName());
}

private int runGoals(String pathToRootOfProject, String... goals) {
InvocationRequest request = new DefaultInvocationRequest();
request.setGoals(Arrays.asList(goals));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public String getAbsolutePathToSourceCode() {
}

public InputConfiguration setPathToSourceCode(String pathToSourceCode) {
if (new File(pathToSourceCode).isAbsolute()) {
pathToSourceCode = pathToSourceCode.substring(this.absolutePathToProjectRoot.length());
}
this.pathToSourceCode = DSpotUtils.shouldAddSeparator.apply(pathToSourceCode);
return this;
}
Expand All @@ -279,6 +282,9 @@ public String getPathToTestSourceCode() {
}

public InputConfiguration setPathToTestSourceCode(String pathToTestSourceCode) {
if (new File(pathToTestSourceCode).isAbsolute()) {
pathToTestSourceCode = pathToTestSourceCode.substring(this.absolutePathToProjectRoot.length());
}
this.pathToTestSourceCode = DSpotUtils.shouldAddSeparator.apply(pathToTestSourceCode);
return this;
}
Expand All @@ -298,6 +304,9 @@ public String getPathToClasses() {
}

public InputConfiguration setPathToClasses(String pathToClasses) {
if (new File(pathToClasses).isAbsolute()) {
pathToClasses = pathToClasses.substring(this.absolutePathToProjectRoot.length());
}
this.pathToClasses = DSpotUtils.shouldAddSeparator.apply(pathToClasses);
return this;
}
Expand All @@ -317,6 +326,9 @@ public String getAbsolutePathToTestClasses() {
}

public InputConfiguration setPathToTestClasses(String pathToTestClasses) {
if (new File(pathToTestClasses).isAbsolute()) {
pathToTestClasses = pathToTestClasses.substring(this.absolutePathToProjectRoot.length());
}
this.pathToTestClasses = DSpotUtils.shouldAddSeparator.apply(pathToTestClasses);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
public class DSpotCompiler extends JDTBasedSpoonCompiler {



public static DSpotCompiler createDSpotCompiler(InputConfiguration configuration, String pathToDependencies) {
String pathToSources = configuration.getAbsolutePathToSourceCode()
+ PATH_SEPARATOR +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import eu.stamp_project.mutant.pit.PitResultParser;
import eu.stamp_project.program.InputConfiguration;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -25,16 +23,6 @@
*/
public class MavenAutomaticBuilderTest {

@Before
public void setUp() throws Exception {
Utils.getInputConfiguration().setVerbose(true);
}

@After
public void tearDown() throws Exception {
Utils.getInputConfiguration().setVerbose(false);
}

@Test
public void testGetDependenciesOf() throws Exception {

Expand All @@ -60,6 +48,7 @@ public void testGetDependenciesOf() throws Exception {
public void testRunPit() throws Exception {

Utils.init("src/test/resources/test-projects/test-projects.properties");
InputConfiguration.get().setFilter("");

Utils.getBuilder().runPit(Utils.getInputConfiguration().getAbsolutePathToProjectRoot());
final List<PitResult> pitResults = PitResultParser.parseAndDelete(Utils.getInputConfiguration().getAbsolutePathToProjectRoot() + Utils.getBuilder().getOutputDirectoryPit());
Expand Down

0 comments on commit 8fe41b3

Please sign in to comment.