From 7ab12b0a678f7351a8f0294e573446f11ddbd7f9 Mon Sep 17 00:00:00 2001 From: danglotb Date: Thu, 23 Sep 2021 10:55:30 +0200 Subject: [PATCH] feat: add more apis for CloverExecutor --- .../reporters/html/RenderFileAction.java | 3 +- .../clover/CloverExecutor.java | 28 ++++++++++++++++++ .../coverage/Coverage.java | 8 +++++ .../coverage/TestMethodCoverage.java | 7 +++-- .../selector/EnhancedDiffTestSelection.java | 29 ++++++++++++------- 5 files changed, 62 insertions(+), 13 deletions(-) diff --git a/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java b/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java index 9ecd137ed..356e3ebd5 100644 --- a/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java +++ b/dspot-diff-test-selection/src/main/java/com/atlassian/clover/reporters/html/RenderFileAction.java @@ -48,6 +48,7 @@ import static clover.com.google.common.collect.Maps.newHashMap; /** + * */ public class RenderFileAction implements Callable { protected static ThreadLocal> columnsTL; @@ -331,7 +332,7 @@ private void buildCoverage(List> 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() diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverExecutor.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverExecutor.java index 04a08f8a6..5944948d7 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverExecutor.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/clover/CloverExecutor.java @@ -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> 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 * @@ -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(); diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java index e2fb3d22f..2bffd4bf9 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/Coverage.java @@ -11,6 +11,14 @@ */ public class Coverage { + public void clear() { + this.testClassCoverage.clear(); + } + + public int size() { + return this.testClassCoverage.size(); + } + public final Map testClassCoverage; public Coverage() { diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java index bfbcf20ed..d6b5186f8 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/coverage/TestMethodCoverage.java @@ -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); } @@ -36,7 +35,11 @@ public List 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 diff --git a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/selector/EnhancedDiffTestSelection.java b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/selector/EnhancedDiffTestSelection.java index 0acc5a524..9685b33cb 100644 --- a/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/selector/EnhancedDiffTestSelection.java +++ b/dspot-diff-test-selection/src/main/java/eu/stamp_project/diff_test_selection/selector/EnhancedDiffTestSelection.java @@ -38,13 +38,15 @@ public Map> 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> currentModifiedTestsPerTestClass = modifiedLinesTool.getModifiedTestsPerTestClass(); @@ -71,7 +73,9 @@ public Map> selectTests() { private void addTestsThatHitGivenChanges( final Map> selectTestsPerTestClasses, Map> modificationPerQualifiedName, - Coverage coverage) { + Coverage coverage, + Coverage otherCoverage + ) { for (String modifiedClassFullQualifiedName : modificationPerQualifiedName.keySet()) { final List modifiedLines = modificationPerQualifiedName.get(modifiedClassFullQualifiedName); this.coverage.addModifiedLines(modifiedClassFullQualifiedName, modifiedLines); @@ -80,14 +84,19 @@ private void addTestsThatHitGivenChanges( final Map 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); } } }