Skip to content

Commit

Permalink
feat: remove modified test methods from the selected ones (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored Dec 28, 2020
1 parent 0f91352 commit 5de4962
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import org.slf4j.LoggerFactory;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public class ModifiedLinesTool {

Expand All @@ -29,6 +27,8 @@ public class ModifiedLinesTool {
private Map<String, List<Integer>> deletionPerQualifiedName;
private Map<String, List<Integer>> additionPerQualifiedName;

private Map<String, Set<String>> modifiedTestsPerTestClass;

public ModifiedLinesTool(final String pathToFirstVersion, final String pathToSecondVersion) {
this.pathToFirstVersion = pathToFirstVersion;
this.pathToSecondVersion = pathToSecondVersion;
Expand All @@ -42,10 +42,18 @@ public Map<String, List<Integer>> getAdditionPerQualifiedName() {
return additionPerQualifiedName;
}

public Map<String, Set<String>> getModifiedTestsPerTestClass() {
return modifiedTestsPerTestClass;
}

public boolean hasResult() {
return this.deletionPerQualifiedName != null && this.additionPerQualifiedName != null;
}

public boolean isTest() {
return this.modifiedTestsPerTestClass != null && !this.modifiedTestsPerTestClass.isEmpty();
}

public void compute(String currentLine, String secondLine) {
final File baseDir = new File(this.pathToFirstVersion);
final String file1 = ModifiedLinesUtils.getCorrectPathFile(currentLine);
Expand All @@ -59,13 +67,38 @@ public void compute(String currentLine, String secondLine) {
try {
LOGGER.info(f1.getAbsolutePath());
LOGGER.info(f2.getAbsolutePath());
this.buildMap(new AstComparator().compare(f1, f2));
final Diff diff = new AstComparator().compare(f1, f2);
if (f1.getAbsolutePath().contains("src/test/java/")) {
LOGGER.info("Build modified Tests {}", f2.getAbsolutePath());
this.buildModifiedTests(diff);
} else {
this.buildMap(diff);
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error when trying to compare " + f1 + " and " + f2);
}
}

private void buildModifiedTests(Diff compare) {
this.modifiedTestsPerTestClass = new HashMap<>();
final List<Operation> allOperations = compare.getAllOperations();
for (Operation operation : allOperations) {
final CtElement node = ModifiedLinesUtils.filterOperation(operation);
final CtMethod<?> testMethod;
if (node != null && (testMethod = node.getParent(CtMethod.class) ) != null) {
final String testClassQualifiedName = node.getPosition()
.getCompilationUnit()
.getMainType()
.getQualifiedName();
if (!this.modifiedTestsPerTestClass.containsKey(testClassQualifiedName)) {
this.modifiedTestsPerTestClass.put(testClassQualifiedName, new HashSet<>());
}
this.modifiedTestsPerTestClass.get(testClassQualifiedName).add(testMethod.getSimpleName());
}
}
}

private void buildMap(Diff compare) {
this.additionPerQualifiedName = new LinkedHashMap<>();// keeps the order
this.deletionPerQualifiedName = new LinkedHashMap<>();// keeps the order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public static boolean filterOperationFromNode(CtElement element) {
}

public static boolean shouldSkip(String pathToFirstVersion, String file1, String file2) {
if (file1.contains("src/test/java")) {
return true;
}
if (file2.endsWith(file1)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public EnhancedDiffTestSelection(

public Map<String, Set<String>> selectTests() {
final Map<String, Set<String>> selectTestsPerTestClasses = new LinkedHashMap<>();
final Map<String, Set<String>> modifiedTestsPerTestClass = new LinkedHashMap<>();
final String[] lines = this.diff.split(System.getProperty("line.separator"));
for (int i = 0; i < lines.length; i++) {
final String currentLine = lines[i];
Expand All @@ -43,9 +44,25 @@ public Map<String, Set<String>> selectTests() {
modifiedLinesTool.getAdditionPerQualifiedName(),
this.coverageV2
);
} else if (modifiedLinesTool.isTest()) {
final Map<String, Set<String>> currentModifiedTestsPerTestClass = modifiedLinesTool.getModifiedTestsPerTestClass();
for (String testClassName : currentModifiedTestsPerTestClass.keySet()) {
if (!modifiedTestsPerTestClass.containsKey(testClassName)) {
modifiedTestsPerTestClass.put(testClassName, new HashSet<>());
}
modifiedTestsPerTestClass.get(testClassName).addAll(currentModifiedTestsPerTestClass.get(testClassName));
}
}
}
}
for (String testClassName : modifiedTestsPerTestClass.keySet()) {
if (selectTestsPerTestClasses.containsKey(testClassName)) {
selectTestsPerTestClasses.get(testClassName).removeAll(modifiedTestsPerTestClass.get(testClassName));
}
if (selectTestsPerTestClasses.get(testClassName).isEmpty()) {
selectTestsPerTestClasses.remove(testClassName);
}
}
return selectTestsPerTestClasses;
}

Expand Down

0 comments on commit 5de4962

Please sign in to comment.