diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-11-05 16:56:58 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-11-05 17:04:57 +0100 |
commit | 79d0345b7a188a8d615ab8002dcc2036d6148cd1 (patch) | |
tree | 45ffdd4575e41ac0fab747e2ff96361341bc62f8 /sonar-batch | |
parent | 94f3dadb92b71533d8b84cdd4089a932c506d8e2 (diff) | |
download | sonarqube-79d0345b7a188a8d615ab8002dcc2036d6148cd1.tar.gz sonarqube-79d0345b7a188a8d615ab8002dcc2036d6148cd1.zip |
SONAR-6931 Speed up issues mode by scanning only changed files
Diffstat (limited to 'sonar-batch')
6 files changed, 70 insertions, 42 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java index 2e0d79cf9d1..31784aa1e80 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java +++ b/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java @@ -36,11 +36,11 @@ import java.util.Map; public class DefaultAnalysisMode extends AbstractAnalysisMode implements AnalysisMode { private static final Logger LOG = LoggerFactory.getLogger(DefaultAnalysisMode.class); - private static final String KEY_ONLY_ANALYZE_CHANGED = "sonar.scanChangedFilesOnly"; + private static final String KEY_SCAN_ALL = "sonar.scanAllFiles"; private boolean mediumTestMode; private boolean notAssociated; - private boolean onlyChanged; + private boolean scanAllFiles; public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props) { init(globalProps.properties(), props.properties()); @@ -54,8 +54,8 @@ public class DefaultAnalysisMode extends AbstractAnalysisMode implements Analysi return notAssociated; } - public boolean onlyAnalyzeChanged() { - return onlyChanged; + public boolean scanAllFiles() { + return scanAllFiles; } private void init(Map<String, String> globalProps, Map<String, String> analysisProps) { @@ -76,8 +76,8 @@ public class DefaultAnalysisMode extends AbstractAnalysisMode implements Analysi issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, FakePluginInstaller.MEDIUM_TEST_ENABLED)); notAssociated = issues && rootProjectKeyMissing(analysisProps); - String onlyChangedStr = getPropertyWithFallback(analysisProps, globalProps, KEY_ONLY_ANALYZE_CHANGED); - onlyChanged = issues && "true".equals(onlyChangedStr); + String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL); + scanAllFiles = !issues || "true".equals(scanAllStr); } public void printMode() { @@ -94,7 +94,7 @@ public class DefaultAnalysisMode extends AbstractAnalysisMode implements Analysi if (notAssociated) { LOG.info("Local analysis"); } - if (onlyChanged) { + if (!scanAllFiles) { LOG.info("Scanning only changed files"); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java index eaaf466228e..8c77079f5f6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java @@ -115,7 +115,7 @@ public class LocalIssueTracking { } private boolean shouldCopyServerIssues(BatchComponent component) { - if (mode.onlyAnalyzeChanged() && component.isFile()) { + if (!mode.scanAllFiles() && component.isFile()) { DefaultInputFile inputFile = (DefaultInputFile) component.inputComponent(); if (inputFile.status() == Status.SAME) { return true; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java index 17d6f8d4271..ad73100f467 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java @@ -78,7 +78,7 @@ public class DefaultModuleFileSystem extends DefaultFileSystem implements Module this.binaryDirs = initializer.binaryDirs(); // filter the files that sensors have access to (SONAR-6931) - if (mode.onlyAnalyzeChanged()) { + if (!mode.scanAllFiles()) { setDefaultPredicate(predicates.not(predicates.hasStatus(Status.SAME))); } } @@ -98,7 +98,7 @@ public class DefaultModuleFileSystem extends DefaultFileSystem implements Module this.binaryDirs = initializer.binaryDirs(); // filter the files sensors have access to - if (mode.onlyAnalyzeChanged()) { + if (!mode.scanAllFiles()) { setDefaultPredicate(predicates.not(predicates.hasStatus(Status.SAME))); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/analysis/DefaultAnalysisModeTest.java b/sonar-batch/src/test/java/org/sonar/batch/analysis/DefaultAnalysisModeTest.java index 9c9b294cbaf..70720595475 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/analysis/DefaultAnalysisModeTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/analysis/DefaultAnalysisModeTest.java @@ -84,29 +84,33 @@ public class DefaultAnalysisModeTest { } @Test - public void only_scan_changed() { + public void scan_all() { Map<String, String> props = new HashMap<>(); props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES); GlobalProperties globalProps = new GlobalProperties(props); - props.put("sonar.scanChangedFilesOnly", "true"); - AnalysisProperties analysisProps = new AnalysisProperties(props); - + AnalysisProperties analysisProps = new AnalysisProperties(new HashMap<String, String>()); DefaultAnalysisMode mode = new DefaultAnalysisMode(globalProps, analysisProps); - assertThat(mode.onlyAnalyzeChanged()).isTrue(); + assertThat(mode.scanAllFiles()).isFalse(); + + props.put("sonar.scanAllFiles", "true"); + analysisProps = new AnalysisProperties(props); + + mode = new DefaultAnalysisMode(globalProps, analysisProps); + assertThat(mode.scanAllFiles()).isTrue(); props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PUBLISH); analysisProps = new AnalysisProperties(props); mode = new DefaultAnalysisMode(globalProps, analysisProps); - assertThat(mode.onlyAnalyzeChanged()).isFalse(); + assertThat(mode.scanAllFiles()).isTrue(); } @Test public void default_publish_mode() { DefaultAnalysisMode mode = createMode(null); assertThat(mode.isPublish()).isTrue(); - assertThat(mode.onlyAnalyzeChanged()).isFalse(); + assertThat(mode.scanAllFiles()).isTrue(); } @Test @@ -114,7 +118,7 @@ public class DefaultAnalysisModeTest { DefaultAnalysisMode mode = createMode(CoreProperties.ANALYSIS_MODE_ISSUES); assertThat(mode.isIssues()).isTrue(); - assertThat(mode.onlyAnalyzeChanged()).isFalse(); + assertThat(mode.scanAllFiles()).isFalse(); } private static DefaultAnalysisMode createMode(@Nullable String mode) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issuesmode/ScanOnlyChangedTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issuesmode/ScanOnlyChangedTest.java index 05e68afb164..2141351e852 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issuesmode/ScanOnlyChangedTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issuesmode/ScanOnlyChangedTest.java @@ -20,7 +20,6 @@ package org.sonar.batch.mediumtest.issuesmode; import org.assertj.core.api.Condition; - import com.google.common.io.Resources; import org.sonar.batch.repository.FileData; import com.google.common.collect.ImmutableMap; @@ -139,28 +138,31 @@ public class ScanOnlyChangedTest { } @Test - public void testIssueTrackingChangedFiles() throws Exception { + public void testScanAll() throws Exception { + File projectDir = copyProject("/mediumtest/xoo/sample"); + + TaskResult result = tester + .newScanTask(new File(projectDir, "sonar-project.properties")) + .property("sonar.scanAllFiles", "true") + .start(); + + assertNumberIssues(result, 16, 3, 0); + + /* + * 8 new per line + * 1 manual + */ + assertNumberIssuesOnFile(result, "HelloJava.xoo", 9); + } + + @Test + public void testScanOnlyChangedFiles() throws Exception { File projectDir = copyProject("/mediumtest/xoo/sample"); TaskResult result = tester .newScanTask(new File(projectDir, "sonar-project.properties")) - .property("sonar.scanChangedFilesOnly", "true") .start(); - int newIssues = 0; - int openIssues = 0; - int resolvedIssue = 0; - for (Issue issue : result.trackedIssues()) { - System.out - .println(issue.message() + " " + issue.key() + " " + issue.ruleKey() + " " + issue.isNew() + " " + issue.resolution() + " " + issue.componentKey() + " " + issue.line()); - if (issue.isNew()) { - newIssues++; - } else if (issue.resolution() != null) { - resolvedIssue++; - } else { - openIssues++; - } - } /* * We have: * 6 new issues per line (open) in helloscala.xoo @@ -169,18 +171,40 @@ public class ScanOnlyChangedTest { * 1 manual issue (open, not new) in HelloJava.xoo * 1 existing issue on the project (open, not new) */ - System.out.println("new: " + newIssues + " open: " + openIssues + " resolved " + resolvedIssue); - assertThat(newIssues).isEqualTo(8); - assertThat(openIssues).isEqualTo(3); - assertThat(resolvedIssue).isEqualTo(0); + assertNumberIssues(result, 8, 3, 0); // should only have server issues (HelloJava.xoo should not have been analyzed) - assertThat(result.trackedIssues()).haveExactly(2, new Condition<Issue>() { + assertNumberIssuesOnFile(result, "HelloJava.xoo", 2); + } + + private static void assertNumberIssuesOnFile(TaskResult result, final String fileNameEndsWith, int issues) { + assertThat(result.trackedIssues()).haveExactly(issues, new Condition<Issue>() { @Override public boolean matches(Issue value) { - return value.componentKey().endsWith("HelloJava.xoo"); + return value.componentKey().endsWith(fileNameEndsWith); } }); } + private static void assertNumberIssues(TaskResult result, int expectedNew, int expectedOpen, int expectedResolved) { + int newIssues = 0; + int openIssues = 0; + int resolvedIssue = 0; + for (Issue issue : result.trackedIssues()) { + System.out + .println(issue.message() + " " + issue.key() + " " + issue.ruleKey() + " " + issue.isNew() + " " + issue.resolution() + " " + issue.componentKey() + " " + issue.line()); + if (issue.isNew()) { + newIssues++; + } else if (issue.resolution() != null) { + resolvedIssue++; + } else { + openIssues++; + } + } + + System.out.println("new: " + newIssues + " open: " + openIssues + " resolved " + resolvedIssue); + assertThat(newIssues).isEqualTo(expectedNew); + assertThat(openIssues).isEqualTo(expectedOpen); + assertThat(resolvedIssue).isEqualTo(expectedResolved); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java index ea087c8ebcb..b2035597af9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java @@ -116,7 +116,7 @@ public class DefaultModuleFileSystemTest { @Test public void default_predicate_scan_only_changed() throws IOException { - when(mode.onlyAnalyzeChanged()).thenReturn(true); + when(mode.scanAllFiles()).thenReturn(false); DefaultModuleFileSystem fs = new DefaultModuleFileSystem(moduleInputFileCache, new Project("foo"), settings, fileIndexer, initializer, componentIndexer, mode); |