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

feat: add more apis for CloverExecutor #997

Merged
merged 1 commit into from
Oct 1, 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 @@ -48,6 +48,7 @@
import static clover.com.google.common.collect.Maps.newHashMap;

/**
*
*/
public class RenderFileAction implements Callable {
protected static ThreadLocal<List<Column>> columnsTL;
Expand Down Expand Up @@ -331,7 +332,7 @@ private void buildCoverage(List<Map.Entry<TestCaseInfo, BlockMetrics>> sublist,
.getHitCount();
CloverReader.coverage.addCoverage(testClassName, testName, targetClassName, (Integer) line, hitCount);
} catch (Exception e) {
e.printStackTrace();
/*e.printStackTrace();*/
final int hitCount = this.fileInfo.getNamedClass(this.fileInfo.getName().split("\\.")[0])
.getAllMethods()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ public class CloverExecutor {

private static final String POM_FILE = "pom.xml";

public void instrument(String pathToRootOfProject) {
setMavenHome();
runGoals(
pathToRootOfProject,
"clean",
"org.openclover:clover-maven-plugin:4.4.1:setup",
"test",
"-DskipTests"
);
}

public void runInstrumentedTest(String pathToRootOfProject, Map<String, List<String>> tests) {
final String testsOptionsValue = tests.keySet()
.stream()
.map(key ->
key + "#" + String.join("+", tests.get(key))
).collect(Collectors.joining(","));
System.out.println(testsOptionsValue);
setMavenHome();
runGoals(
pathToRootOfProject,
"org.openclover:clover-maven-plugin:4.4.1:clean",
"test",
"-Dtest=" + testsOptionsValue
);
}

/**
* This class will execute, though maven goals, the instrumentation of Clover and the test of the project
*
Expand Down Expand Up @@ -75,6 +102,7 @@ private int runGoals(String pathToRootOfProject, String... goals) {
properties.setProperty("gpg.skip", "true");
properties.setProperty("jacoco.skip", "true");
properties.setProperty("animal.sniffer.skip", "true");
properties.setProperty("proguard.skip", "true");
request.setProperties(properties);

Invoker invoker = new DefaultInvoker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
*/
public class Coverage {

public void clear() {
this.testClassCoverage.clear();
}

public int size() {
return this.testClassCoverage.size();
}

public final Map<String, TestClassCoverage> testClassCoverage;

public Coverage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public synchronized void addCoverage(String className, int line, int hitCounts)
if (!this.classCoverageList.containsKey(className)) {
this.classCoverageList.put(className, new ClassCoverage(className));
}
System.out.println(this.classCoverageList.get(className));
this.classCoverageList.get(className).addCoverage(line, hitCounts);
}

Expand All @@ -36,7 +35,11 @@ public List<LineCoverage> getCoverageForClass(String className) {
}

public int getHitCountFromClassNameForLine(String className, int line) {
return this.classCoverageList.get(className).getHitCountForLine(line);
if (this.classCoverageList.containsKey(className)) {
return this.classCoverageList.get(className).getHitCountForLine(line);
} else {
return 0;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public Map<String, Set<String>> selectTests() {
this.addTestsThatHitGivenChanges(
selectTestsPerTestClasses,
modifiedLinesTool.getDeletionPerQualifiedName(),
this.coverageV1
this.coverageV1,
this.coverageV2
);
// t2 that hits the additions
this.addTestsThatHitGivenChanges(
selectTestsPerTestClasses,
modifiedLinesTool.getAdditionPerQualifiedName(),
this.coverageV2
this.coverageV2,
this.coverageV1
);
} else if (modifiedLinesTool.isTest()) {
final Map<String, Set<String>> currentModifiedTestsPerTestClass = modifiedLinesTool.getModifiedTestsPerTestClass();
Expand All @@ -71,7 +73,9 @@ public Map<String, Set<String>> selectTests() {
private void addTestsThatHitGivenChanges(
final Map<String, Set<String>> selectTestsPerTestClasses,
Map<String, List<Integer>> modificationPerQualifiedName,
Coverage coverage) {
Coverage coverage,
Coverage otherCoverage
) {
for (String modifiedClassFullQualifiedName : modificationPerQualifiedName.keySet()) {
final List<Integer> modifiedLines = modificationPerQualifiedName.get(modifiedClassFullQualifiedName);
this.coverage.addModifiedLines(modifiedClassFullQualifiedName, modifiedLines);
Expand All @@ -80,14 +84,19 @@ private void addTestsThatHitGivenChanges(
final Map<String, ClassCoverage> testMethodCoverageForClassName = coverage.getTestMethodCoverageForClassName(fullQualifiedNameOfTestClass, testMethodName);
if (testMethodCoverageForClassName.containsKey(modifiedClassFullQualifiedName)) {
if (modifiedLines.stream().anyMatch(line -> testMethodCoverageForClassName.get(modifiedClassFullQualifiedName).contains(line))) {
if (!selectTestsPerTestClasses.containsKey(fullQualifiedNameOfTestClass)) {
selectTestsPerTestClasses.put(fullQualifiedNameOfTestClass, new HashSet<>());
if (otherCoverage.getTestClasses().contains(fullQualifiedNameOfTestClass) &&
otherCoverage.getTestMethodsForTestClassName(fullQualifiedNameOfTestClass)
.contains(testMethodName)
) {
if (!selectTestsPerTestClasses.containsKey(fullQualifiedNameOfTestClass)) {
selectTestsPerTestClasses.put(fullQualifiedNameOfTestClass, new HashSet<>());
}
modifiedLines.stream()
.filter(line ->
testMethodCoverageForClassName.get(modifiedClassFullQualifiedName).contains(line))
.forEach(line -> this.coverage.covered(modifiedClassFullQualifiedName, line));
selectTestsPerTestClasses.get(fullQualifiedNameOfTestClass).add(testMethodName);
}
modifiedLines.stream()
.filter(line ->
testMethodCoverageForClassName.get(modifiedClassFullQualifiedName).contains(line))
.forEach(line -> this.coverage.covered(modifiedClassFullQualifiedName, line));
selectTestsPerTestClasses.get(fullQualifiedNameOfTestClass).add(testMethodName);
}
}
}
Expand Down