From 17e98c7c183a35e927d7054061620d414c81de2f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 7 May 2019 12:02:55 +0200 Subject: [PATCH] SONARCLOUD-633 Fix analysis of projects using 'sonar.branch' --- .../scan/filesystem/InputComponentStore.java | 13 +-- .../scanner/mediumtest/scm/ScmMediumTest.java | 91 +++++++++++++------ 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java index 5e5b7e83087..e4cb0672f7a 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java @@ -52,7 +52,6 @@ public class InputComponentStore extends DefaultFileSystem.Cache { private final Map globalInputFileCache = new HashMap<>(); private final Table inputFileByModuleCache = TreeBasedTable.create(); private final Map inputModuleKeyByFileCache = new HashMap<>(); - // indexed by key with branch private final Map inputModuleCache = new HashMap<>(); private final Map inputComponents = new HashMap<>(); private final SetMultimap filesByNameCache = LinkedHashMultimap.create(); @@ -126,24 +125,18 @@ public class InputComponentStore extends DefaultFileSystem.Cache { return globalInputFileCache.get(relativePath); } - @CheckForNull - public DefaultInputModule getModule(String moduleKeyWithBranch) { - return inputModuleCache.get(moduleKeyWithBranch); - } - public DefaultInputModule findModule(DefaultInputFile file) { - return Optional.ofNullable(inputModuleKeyByFileCache.get(file)).map(this::getModule) + return Optional.ofNullable(inputModuleKeyByFileCache.get(file)).map(inputModuleCache::get) .orElseThrow(() -> new IllegalStateException("No modules for file '" + file.toString() + "'")); } public void put(DefaultInputModule inputModule) { String key = inputModule.key(); - String keyWithBranch = inputModule.getKeyWithBranch(); Preconditions.checkNotNull(inputModule); Preconditions.checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key); - Preconditions.checkState(!inputModuleCache.containsKey(keyWithBranch), "Module '%s' already indexed", keyWithBranch); + Preconditions.checkState(!inputModuleCache.containsKey(key), "Module '%s' already indexed", key); inputComponents.put(key, inputModule); - inputModuleCache.put(keyWithBranch, inputModule); + inputModuleCache.put(key, inputModule); } @Override diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java index 62ea71126ae..b317ac1c4cb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java @@ -152,7 +152,7 @@ public class ScmMediumTest { File baseDir = prepareProject(); File xooFileWithoutBlame = new File(baseDir, "src/sample_no_blame.xoo"); - FileUtils.write(xooFileWithoutBlame, "Sample xoo\ncontent\n3\n4\n5"); + FileUtils.write(xooFileWithoutBlame, "Sample xoo\ncontent\n3\n4\n5", StandardCharsets.UTF_8); tester.newAnalysis() .properties(ImmutableMap.builder() @@ -183,29 +183,29 @@ public class ScmMediumTest { File baseDir = prepareProject(); File changedContentScmOnServer = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO); - FileUtils.write(changedContentScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged"); + FileUtils.write(changedContentScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8); File xooScmFile = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO + ".scm"); FileUtils.write(xooScmFile, // revision,author,dateTime "1,foo,2013-01-04\n" + "1,bar,2013-01-04\n" + - "2,biz,2014-01-04\n"); + "2,biz,2014-01-04\n", StandardCharsets.UTF_8); File sameContentScmOnServer = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO); - FileUtils.write(sameContentScmOnServer, SAMPLE_XOO_CONTENT); + FileUtils.write(sameContentScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8); // No need to write .scm file since this file should not be blamed File noBlameScmOnServer = new File(baseDir, NO_BLAME_SCM_ON_SERVER_XOO); - FileUtils.write(noBlameScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged"); + FileUtils.write(noBlameScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8); // No .scm file to emulate a failure during blame File sameContentNoScmOnServer = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO); - FileUtils.write(sameContentNoScmOnServer, SAMPLE_XOO_CONTENT); + FileUtils.write(sameContentNoScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8); xooScmFile = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO + ".scm"); FileUtils.write(xooScmFile, // revision,author,dateTime "1,foo,2013-01-04\n" + - "1,bar,2013-01-04\n"); + "1,bar,2013-01-04\n", StandardCharsets.UTF_8); tester.newAnalysis() .properties(ImmutableMap.builder() @@ -236,26 +236,77 @@ public class ScmMediumTest { assertThat(logTester.logs()).containsSubsequence(MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES, " * src/no_blame_scm_on_server.xoo"); } + @Test + public void optimize_blame_for_deprecated_branch() throws IOException, URISyntaxException { + + File baseDir = prepareProject(); + File changedContentScmOnServer = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO); + FileUtils.write(changedContentScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8); + File xooScmFile = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO + ".scm"); + FileUtils.write(xooScmFile, + // revision,author,dateTime + "1,foo,2013-01-04\n" + + "1,bar,2013-01-04\n" + + "2,biz,2014-01-04\n", StandardCharsets.UTF_8); + + File sameContentScmOnServer = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO); + FileUtils.write(sameContentScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8); + // No need to write .scm file since this file should not be blamed + + File noBlameScmOnServer = new File(baseDir, NO_BLAME_SCM_ON_SERVER_XOO); + FileUtils.write(noBlameScmOnServer, SAMPLE_XOO_CONTENT + "\nchanged", StandardCharsets.UTF_8); + // No .scm file to emulate a failure during blame + + File sameContentNoScmOnServer = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO); + FileUtils.write(sameContentNoScmOnServer, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8); + xooScmFile = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO + ".scm"); + FileUtils.write(xooScmFile, + // revision,author,dateTime + "1,foo,2013-01-04\n" + + "1,bar,2013-01-04\n", StandardCharsets.UTF_8); + + tester.newAnalysis() + .properties(ImmutableMap.builder() + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.branch", "mybranch") + .put("sonar.sources", "src") + .put("sonar.scm.provider", "xoo") + .build()) + .execute(); + + assertThat(getChangesets(baseDir, "src/sample.xoo")).isNotNull(); + + assertThat(getChangesets(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse(); + + assertThat(getChangesets(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isTrue(); + + assertThat(getChangesets(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO).getCopyFromPrevious()).isFalse(); + + assertThat(getChangesets(baseDir, NO_BLAME_SCM_ON_SERVER_XOO)).isNull(); + + // 5 .xoo files + 3 .scm files, but only 4 marked for publishing. 1 file is SAME so not included in the total + assertThat(logTester.logs()).containsSubsequence("8 files indexed"); + assertThat(logTester.logs()).containsSubsequence("4 files to be analyzed", "3/4 files analyzed"); + assertThat(logTester.logs()).containsSubsequence(MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES, " * src/no_blame_scm_on_server.xoo"); + } + @Test public void forceReload() throws IOException, URISyntaxException { File baseDir = prepareProject(); File xooFileNoScm = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO); - FileUtils.write(xooFileNoScm, SAMPLE_XOO_CONTENT); + FileUtils.write(xooFileNoScm, SAMPLE_XOO_CONTENT, StandardCharsets.UTF_8); File xooScmFile = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO + ".scm"); FileUtils.write(xooScmFile, // revision,author,dateTime "1,foo,2013-01-04\n" + - "1,bar,2013-01-04\n"); + "1,bar,2013-01-04\n", StandardCharsets.UTF_8); AnalysisBuilder analysisBuilder = tester.newAnalysis() .properties(ImmutableMap.builder() - .put("sonar.task", "scan") .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project") .put("sonar.sources", "src") .put("sonar.scm.provider", "xoo") // Force reload @@ -278,12 +329,8 @@ public class ScmMediumTest { tester.newAnalysis() .properties(ImmutableMap.builder() - .put("sonar.task", "scan") .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project") .put("sonar.sources", "src") .put("sonar.links.scm_dev", "scm:xoo:foobar") .build()) @@ -301,12 +348,8 @@ public class ScmMediumTest { tester.newAnalysis() .properties(ImmutableMap.builder() - .put("sonar.task", "scan") .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project") .put("sonar.sources", "src") .build()) .execute(); @@ -326,7 +369,7 @@ public class ScmMediumTest { srcDir.mkdir(); File xooFile1 = new File(srcDir, "sample.xoo"); - FileUtils.write(xooFile1, "Sample xoo\ncontent\n3\n4\n5"); + FileUtils.write(xooFile1, "Sample xoo\ncontent\n3\n4\n5", StandardCharsets.UTF_8); File xooScmFile1 = new File(srcDir, "sample.xoo.scm"); FileUtils.write(xooScmFile1, // revision,author,dateTime @@ -347,12 +390,8 @@ public class ScmMediumTest { tester.newAnalysis() .properties(ImmutableMap.builder() - .put("sonar.task", "scan") .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectName", "Foo Project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project") .put("sonar.sources", "src") .put("sonar.scm.disabled", "true") .put("sonar.scm.provider", "xoo") -- 2.39.5