Pārlūkot izejas kodu

SONARCLOUD-633 Fix analysis of projects using 'sonar.branch'

tags/7.8
Julien HENRY pirms 5 gadiem
vecāks
revīzija
17e98c7c18

+ 3
- 10
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java Parādīt failu

private final Map<String, InputFile> globalInputFileCache = new HashMap<>(); private final Map<String, InputFile> globalInputFileCache = new HashMap<>();
private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create(); private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create();
private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>(); private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>();
// indexed by key with branch
private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>(); private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>();
private final Map<String, InputComponent> inputComponents = new HashMap<>(); private final Map<String, InputComponent> inputComponents = new HashMap<>();
private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create(); private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create();
return globalInputFileCache.get(relativePath); return globalInputFileCache.get(relativePath);
} }


@CheckForNull
public DefaultInputModule getModule(String moduleKeyWithBranch) {
return inputModuleCache.get(moduleKeyWithBranch);
}

public DefaultInputModule findModule(DefaultInputFile file) { 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() + "'")); .orElseThrow(() -> new IllegalStateException("No modules for file '" + file.toString() + "'"));
} }


public void put(DefaultInputModule inputModule) { public void put(DefaultInputModule inputModule) {
String key = inputModule.key(); String key = inputModule.key();
String keyWithBranch = inputModule.getKeyWithBranch();
Preconditions.checkNotNull(inputModule); Preconditions.checkNotNull(inputModule);
Preconditions.checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key); 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); inputComponents.put(key, inputModule);
inputModuleCache.put(keyWithBranch, inputModule);
inputModuleCache.put(key, inputModule);
} }


@Override @Override

+ 65
- 26
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java Parādīt failu



File baseDir = prepareProject(); File baseDir = prepareProject();
File xooFileWithoutBlame = new File(baseDir, "src/sample_no_blame.xoo"); 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() tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()


File baseDir = prepareProject(); File baseDir = prepareProject();
File changedContentScmOnServer = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO); 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"); File xooScmFile = new File(baseDir, CHANGED_CONTENT_SCM_ON_SERVER_XOO + ".scm");
FileUtils.write(xooScmFile, FileUtils.write(xooScmFile,
// revision,author,dateTime // revision,author,dateTime
"1,foo,2013-01-04\n" + "1,foo,2013-01-04\n" +
"1,bar,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); 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 // No need to write .scm file since this file should not be blamed


File noBlameScmOnServer = new File(baseDir, NO_BLAME_SCM_ON_SERVER_XOO); 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 // No .scm file to emulate a failure during blame


File sameContentNoScmOnServer = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO); 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"); xooScmFile = new File(baseDir, SAME_CONTENT_NO_SCM_ON_SERVER_XOO + ".scm");
FileUtils.write(xooScmFile, FileUtils.write(xooScmFile,
// revision,author,dateTime // revision,author,dateTime
"1,foo,2013-01-04\n" + "1,foo,2013-01-04\n" +
"1,bar,2013-01-04\n");
"1,bar,2013-01-04\n", StandardCharsets.UTF_8);


tester.newAnalysis() tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()
assertThat(logTester.logs()).containsSubsequence(MISSING_BLAME_INFORMATION_FOR_THE_FOLLOWING_FILES, " * src/no_blame_scm_on_server.xoo"); 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.<String, String>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 @Test
public void forceReload() throws IOException, URISyntaxException { public void forceReload() throws IOException, URISyntaxException {


File baseDir = prepareProject(); File baseDir = prepareProject();
File xooFileNoScm = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO); 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"); File xooScmFile = new File(baseDir, SAME_CONTENT_SCM_ON_SERVER_XOO + ".scm");
FileUtils.write(xooScmFile, FileUtils.write(xooScmFile,
// revision,author,dateTime // revision,author,dateTime
"1,foo,2013-01-04\n" + "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() AnalysisBuilder analysisBuilder = tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "scan")
.put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
.put("sonar.projectKey", "com.foo.project") .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.sources", "src")
.put("sonar.scm.provider", "xoo") .put("sonar.scm.provider", "xoo")
// Force reload // Force reload


tester.newAnalysis() tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "scan")
.put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
.put("sonar.projectKey", "com.foo.project") .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.sources", "src")
.put("sonar.links.scm_dev", "scm:xoo:foobar") .put("sonar.links.scm_dev", "scm:xoo:foobar")
.build()) .build())


tester.newAnalysis() tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "scan")
.put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
.put("sonar.projectKey", "com.foo.project") .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.sources", "src")
.build()) .build())
.execute(); .execute();
srcDir.mkdir(); srcDir.mkdir();


File xooFile1 = new File(srcDir, "sample.xoo"); 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"); File xooScmFile1 = new File(srcDir, "sample.xoo.scm");
FileUtils.write(xooScmFile1, FileUtils.write(xooScmFile1,
// revision,author,dateTime // revision,author,dateTime


tester.newAnalysis() tester.newAnalysis()
.properties(ImmutableMap.<String, String>builder() .properties(ImmutableMap.<String, String>builder()
.put("sonar.task", "scan")
.put("sonar.projectBaseDir", baseDir.getAbsolutePath()) .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
.put("sonar.projectKey", "com.foo.project") .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.sources", "src")
.put("sonar.scm.disabled", "true") .put("sonar.scm.disabled", "true")
.put("sonar.scm.provider", "xoo") .put("sonar.scm.provider", "xoo")

Notiek ielāde…
Atcelt
Saglabāt