Skip to content

Commit

Permalink
Multi-file analysis: Fix Analysis Warnings (#2145)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource authored Aug 5, 2024
1 parent aa42ca0 commit cadcd69
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public void AnalysisWarningProcessor_Process_MultipleWarnings()
fileWrapper.Received(1).WriteAllText(string.Empty, """
[
{
"Text": "exploding"
"text": "exploding"
},
{
"Text": "whale"
"text": "whale"
}
]
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void PostProc_NoProjectsToAnalyze_NoExecutionTriggered()
"""
[
{
"Text": "Multi-Language analysis is enabled. If this was not intended, please set \"/d:sonar.scanner.scanAll=false\" in the begin step."
"text": "Multi-Language analysis is enabled. If this was not intended, please set \"/d:sonar.scanner.scanAll=false\" in the begin step."
}
]
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,26 @@ void testCSharpSharedFileWithOneProjectUsingProjectBaseDirAbsolute() throws IOEx

@Test
void testCSharpFramework48() throws IOException {
validateCSharpFramework("CSharp.Framework.4.8");
var folderName = "CSharp.Framework.4.8";
assumeFalse(TestUtils.getMsBuildPath(ORCHESTRATOR).toString().contains("2017")); // We can't run .NET Core SDK under VS 2017 CI context
BuildResult buildResult = runBeginBuildAndEndForStandardProject(folderName, "", true, true);

assertOnlyMultiFileAnalysisWarning(buildResult);
List<Issue> issues = TestUtils.allIssues(ORCHESTRATOR);
if (isTestProjectSupported()) {
assertThat(issues).hasSize(3)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
tuple(SONAR_RULES_PREFIX + "S1134", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2094", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2699", folderName + ":UTs/CommonTest.cs"));
} else {
assertThat(issues).hasSize(2)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
tuple(SONAR_RULES_PREFIX + "S1134", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2094", folderName + ":Main/Common.cs"));
}
}

@Test
Expand All @@ -749,7 +768,7 @@ void testScannerNetCore31NoAnalysisWarning() throws IOException {
BuildResult buildResult = runNetCoreBeginBuildAndEnd(projectDir, ScannerClassifier.NET);

assertThat(buildResult.getLogs()).doesNotContain("Failed to parse properties from the environment variable 'SONARQUBE_SCANNER_PARAMS'");
assertNoAnalysisWarnings(buildResult);
assertOnlyMultiFileAnalysisWarning(buildResult);
}

@Test
Expand All @@ -764,7 +783,7 @@ void testScannerNet5NoAnalysisWarnings() throws IOException {
BuildResult buildResult = runNetCoreBeginBuildAndEnd(projectDir, ScannerClassifier.NET);

assertThat(buildResult.getLogs()).doesNotContain("Failed to parse properties from the environment variable 'SONARQUBE_SCANNER_PARAMS'");
assertNoAnalysisWarnings(buildResult);
assertOnlyMultiFileAnalysisWarning(buildResult);
}

@Test
Expand All @@ -784,7 +803,7 @@ void testScannerNet7NoAnalysisWarnings() throws IOException {
BuildResult buildResult = runBeginBuildAndEndForStandardProject("CSharp.SDK.7.0", "");

assertThat(buildResult.getLogs()).doesNotContain("Failed to parse properties from the environment variable 'SONARQUBE_SCANNER_PARAMS'");
assertNoAnalysisWarnings(buildResult);
assertOnlyMultiFileAnalysisWarning(buildResult);
}

@Test
Expand Down Expand Up @@ -1229,40 +1248,11 @@ private void validateCSharpSdk(String folderName) throws IOException {
}
}

private void validateCSharpFramework(String folderName) throws IOException {
assumeFalse(TestUtils.getMsBuildPath(ORCHESTRATOR).toString().contains("2017")); // We can't run .NET Core SDK under VS 2017 CI context
BuildResult buildResult = runBeginBuildAndEndForStandardProject(folderName, "", true, true);

assertNoAnalysisWarnings(buildResult);

List<Issue> issues = TestUtils.allIssues(ORCHESTRATOR);
if (isTestProjectSupported()) {
assertThat(issues).hasSize(3)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
tuple(SONAR_RULES_PREFIX + "S1134", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2094", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2699", folderName + ":UTs/CommonTest.cs"));
} else {
assertThat(issues).hasSize(2)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
tuple(SONAR_RULES_PREFIX + "S1134", folderName + ":Main/Common.cs"),
tuple(SONAR_RULES_PREFIX + "S2094", folderName + ":Main/Common.cs"));
}
}

private void assertNoAnalysisWarnings(BuildResult buildResult) {
Ce.Task task = TestUtils.getAnalysisWarningsTask(ORCHESTRATOR, buildResult);
assertThat(task.getStatus()).isEqualTo(Ce.TaskStatus.SUCCESS);
assertThat(task.getWarningsList()).isEmpty();
}

// Verify an AnalysisWarning is raised inside the SQ GUI (on the project dashboard)
private void assertAnalysisWarning(BuildResult buildResult, String message) {
Ce.Task task = TestUtils.getAnalysisWarningsTask(ORCHESTRATOR, buildResult);
assertThat(task.getStatus()).isEqualTo(Ce.TaskStatus.SUCCESS);
assertThat(task.getWarningsList()).contains(message);
private void assertOnlyMultiFileAnalysisWarning(BuildResult buildResult) {
var warnings = TestUtils.getAnalysisWarningsTask(ORCHESTRATOR, buildResult);
assertThat(warnings.getStatus()).isEqualTo(Ce.TaskStatus.SUCCESS);
assertThat(warnings.getWarningsList()).containsExactly(
"Multi-Language analysis is enabled. If this was not intended, please set \"/d:sonar.scanner.scanAll=false\" in the begin step.");
}

private void runCSharpSharedFileWithOneProjectUsingProjectBaseDir(Function<Path, String> getProjectBaseDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void Process(string[] warnings, string outputPath, IFileWrapper fi
{
logger.LogWarning(warning);
}
var warningsJson = JsonConvert.SerializeObject(warnings.Select(x => new { Text = x }).ToArray(), Formatting.Indented);
var warningsJson = JsonConvert.SerializeObject(warnings.Select(x => new { text = x }).ToArray(), Formatting.Indented);
fileWrapper.WriteAllText(outputPath, warningsJson);
}
}
Expand Down

0 comments on commit cadcd69

Please sign in to comment.