Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify code
Browse files Browse the repository at this point in the history
andrei-epure-sonarsource committed Dec 14, 2020
1 parent 9ccab5c commit 4869613
Showing 4 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@
@ScannerSide
public class ScannerFileService implements FileService {
private static final Logger LOG = Loggers.get(ScannerFileService.class);
private static final Pattern DETERMINISTIC_SOURCE_PATH_PREFIX = Pattern.compile("^(([a-zA-Z]:)?[\\\\][_][\\\\]|([/][_][/]))");
// the pattern is defensive and accepts both forward and back slash
private static final Pattern DETERMINISTIC_SOURCE_PATH_PREFIX = Pattern.compile("^([/\\\\]_\\d*[/\\\\])");
private FileSystem fileSystem;
private String languageKey;

@@ -65,7 +66,7 @@ public Optional<InputFile> getFileByRelativePath(String filePath) {
LOG.trace("Found indexed file '{}' for '{}' (normalized to '{}').", foundFile.uri().getPath(), filePath, normalizedRelativePath);
return Optional.of(foundFile);
} else {
LOG.debug("Found {} indexed files for '{}' (normalized to '{}'). Will skip this coverage entry.", count, filePath, normalizedRelativePath);
LOG.debug("Found {} indexed files for '{}' (normalized to '{}'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.", count, filePath, normalizedRelativePath);
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -118,12 +118,10 @@ private void handleSourceFileTag(XmlParserHelper xmlParserHelper) {
if (!fileService.isSupportedAbsolute(canonicalPath)) {
Optional<InputFile> foundFile = fileService.getFileByRelativePath(path);
if (foundFile.isPresent()) {
String newPath = foundFile.get().uri().getPath();
LOG.debug("Found indexed file '{}' for coverage entry '{}'.", newPath, path);
canonicalPath = newPath;
canonicalPath = foundFile.get().uri().getPath();
LOG.debug("Found indexed file '{}' for coverage entry '{}'.", canonicalPath, path);
} else {
LOG.debug("The path '{}' is not indexed by the scanner as an absolute or relative path. This file will be skipped." +
" Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.", path);
// debug logging should be done in the fileService
return;
}
}
Original file line number Diff line number Diff line change
@@ -112,12 +112,12 @@ public void getFileByRelativePath_when_no_indexed_files_returns_empty() {

// act
ScannerFileService sut = new ScannerFileService("key", fs);
Optional<InputFile> result = sut.getFileByRelativePath("C:\\_\\some\\path\\file.cs");
Optional<InputFile> result = sut.getFileByRelativePath("/_/some/path/file.cs");

// assert
assertThat(result).isEmpty();
assertThat(logTester.logs(LoggerLevel.TRACE)).containsExactly("It seems Deterministic Source Paths are used, replacing 'C:\\_\\some\\path\\file.cs' with 'some\\path\\file.cs'.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 0 indexed files for 'C:\\_\\some\\path\\file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry.");
assertThat(logTester.logs(LoggerLevel.TRACE)).containsExactly("It seems Deterministic Source Paths are used, replacing '/_/some/path/file.cs' with 'some/path/file.cs'.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 0 indexed files for '/_/some/path/file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
}

@Test
@@ -129,7 +129,7 @@ public void getFileByRelativePath_when_indexed_files_do_not_match_returns_empty(

assertThat(result).isEmpty();
assertThat(logTester.logs(LoggerLevel.TRACE)).containsExactly("It seems Deterministic Source Paths are used, replacing '/_/some/path/file.cs' with 'some/path/file.cs'.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 0 indexed files for '/_/some/path/file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 0 indexed files for '/_/some/path/file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
}

@Test
@@ -141,7 +141,7 @@ public void getFileByRelativePath_when_multiple_indexed_files_match_returns_empt

assertThat(result).isEmpty();
assertThat(logTester.logs(LoggerLevel.TRACE)).containsExactly("It seems Deterministic Source Paths are used, replacing '/_/some/path/file.cs' with 'some/path/file.cs'.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 2 indexed files for '/_/some/path/file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry.");
assertThat(logTester.logs(LoggerLevel.DEBUG)).containsExactly("Found 2 indexed files for '/_/some/path/file.cs' (normalized to 'some/path/file.cs'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
}

@Test
@@ -169,20 +169,20 @@ public void getFileByRelativePath_with_various_deterministic_source_path_when_ma
FileSystem fs = createFileSystemForInputFiles(Collections.singletonList(expectedResult));

ScannerFileService sut = new ScannerFileService("key", fs);
Optional<InputFile> result = sut.getFileByRelativePath("C:\\_\\some\\path\\file.cs");
Optional<InputFile> result = sut.getFileByRelativePath("\\_\\some\\path\\file.cs");

assertThat(result).hasValue(expectedResult);
assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(2);
assertThat(logTester.logs(LoggerLevel.TRACE).get(0))
.isEqualTo("It seems Deterministic Source Paths are used, replacing 'C:\\_\\some\\path\\file.cs' with 'some\\path\\file.cs'.");
.isEqualTo("It seems Deterministic Source Paths are used, replacing '\\_\\some\\path\\file.cs' with 'some\\path\\file.cs'.");
assertThat(logTester.logs(LoggerLevel.TRACE).get(1))
.startsWith("Found indexed file ")
.endsWith("root/some/path/file.cs' for 'C:\\_\\some\\path\\file.cs' (normalized to 'some/path/file.cs').");
.endsWith("root/some/path/file.cs' for '\\_\\some\\path\\file.cs' (normalized to 'some/path/file.cs').");

result = sut.getFileByRelativePath("D:\\_\\some\\path\\file.cs");
result = sut.getFileByRelativePath("\\_1\\some\\path\\file.cs");
assertThat(result).hasValue(expectedResult);

result = sut.getFileByRelativePath("\\_\\some\\path\\file.cs");
result = sut.getFileByRelativePath("\\_1234\\some\\path\\file.cs");
assertThat(result).hasValue(expectedResult);

result = sut.getFileByRelativePath("/_/some/path/file.cs");
@@ -191,10 +191,25 @@ public void getFileByRelativePath_with_various_deterministic_source_path_when_ma
result = sut.getFileByRelativePath("/_1/some/path/file.cs");
assertThat(result).hasValue(expectedResult);

result = sut.getFileByRelativePath("/_23/some/path/file.cs");
result = sut.getFileByRelativePath("\\_9999\\some/path/file.cs");
assertThat(result).hasValue(expectedResult);
}

@Test
public void getFileByRelativePath_with_with_windows_style_paths_returns_empty() {
InputFile expectedResult = mockInput("root/some/path/file.cs");
FileSystem fs = createFileSystemForInputFiles(Collections.singletonList(expectedResult));

ScannerFileService sut = new ScannerFileService("key", fs);
Optional<InputFile> result = sut.getFileByRelativePath("C:\\_\\some\\path\\file.cs");

assertThat(result).isEmpty();
assertThat(logTester.logs(LoggerLevel.TRACE)).isEmpty();
assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).isEqualTo("Found 0 indexed files for 'C:\\_\\some\\path\\file.cs'" +
" (normalized to 'C:/_/some/path/file.cs'). Will skip this coverage entry. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
}

@Test
public void getFileByRelativePath_with_no_deterministic_source_path_when_single_indexed_files_match_returns_file() {
InputFile expectedResult = mockInput("root/some/path/file.cs");
Original file line number Diff line number Diff line change
@@ -220,8 +220,6 @@ public void valid_with_no_absolute_path_no_relative_path() throws Exception {

assertThat(logTester.logs(LoggerLevel.INFO).get(0)).startsWith("Parsing the Visual Studio coverage XML report ");
assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("The current user dir is ");
assertThat(logTester.logs(LoggerLevel.DEBUG).get(1)).isEqualTo("The path 'CalcMultiplyTest\\MultiplyTest.cs'" +
" is not indexed by the scanner as an absolute or relative path. This file will be skipped. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
}

@Test
@@ -284,12 +282,9 @@ public void valid_with_deterministic_source_path_returns_found_path() {
Assertions.entry(13, 0));

assertThat(logTester.logs(LoggerLevel.INFO).get(0)).startsWith("Parsing the Visual Studio coverage XML report ");
assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(3);
assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(2);
assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("The current user dir is ");
assertThat(logTester.logs(LoggerLevel.DEBUG).get(1)).isEqualTo("The path " +
"'C:\\Workspace\\sonar-dotnet\\its\\projects\\CoverageWithDeterministicSourcePaths\\CoverageWithDeterministicSourcePaths.Tests\\FooTests.cs'" +
" is not indexed by the scanner as an absolute or relative path. This file will be skipped. Verify sonar.sources in .sonarqube\\out\\sonar-project.properties.");
assertThat(logTester.logs(LoggerLevel.DEBUG).get(2)).isEqualTo("Found indexed file " +
assertThat(logTester.logs(LoggerLevel.DEBUG).get(1)).isEqualTo("Found indexed file " +
"'/full/path/to/its/projects/CoverageWithDeterministicSourcePaths/CoverageWithDeterministicSourcePaths/Foo.cs'" +
" for coverage entry '/_/its/projects/CoverageWithDeterministicSourcePaths/CoverageWithDeterministicSourcePaths/Foo.cs'.");
}

0 comments on commit 4869613

Please sign in to comment.