Skip to content

Commit

Permalink
5.3.0 removes msbuild targets, but leaves projects configured to use …
Browse files Browse the repository at this point in the history
…sonarqube analysis (#1070)
  • Loading branch information
pavel-mikula-sonarsource authored Sep 1, 2021
1 parent bb3fffe commit 30d7d7a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void ImportsBefore_BuildingInsideVS_NotImported()
// 1. Pre-build
// Arrange
var dummySonarTargetsDir = EnsureDummyIntegrationTargetsFileExists();

var projectXml = $@"
<PropertyGroup>
<SonarQubeTempPath>{Path.GetTempPath()}</SonarQubeTempPath>
Expand Down Expand Up @@ -133,15 +133,16 @@ public void ImportsBefore_MissingAnalysisTargets()

AssertAnalysisTargetsAreNotImported(projectInstance); // Targets should not be imported

// 2. Now build -> fails with an error message
var result = BuildRunner.BuildTargets(TestContext, projectInstance.FullPath, buildShouldSucceed: false);
// 2. Now build -> logs a warning message
var result = BuildRunner.BuildTargets(TestContext, projectInstance.FullPath);

result.BuildSucceeded.Should().BeFalse();
result.BuildSucceeded.Should().BeTrue();
result.AssertTargetExecuted(TargetConstants.ImportBeforeInfo);
result.AssertExpectedErrorCount(1);
result.AssertExpectedErrorCount(0);

var projectName = Path.GetFileName(projectInstance.FullPath);
result.Errors[0].Contains(projectName).Should().BeTrue("Expecting the error message to contain the project file name");
result.Messages.Should().Contain($"Sonar: ({projectName}) SonarQube analysis targets imported: ");
result.Messages.Should().Contain($@"Sonar: ({projectName}) The analysis targets file not found: nonExistentPath\bin\targets\SonarQube.Integration.targets");
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,16 @@ public void testCSharpAllFlat() throws IOException {
assertThat(getComponent("CSharpAllFlat:Common.cs")).isNotNull();
}

@Test
public void testTargetUninstall() throws IOException {
Path projectDir = TestUtils.projectDir(temp, "CSharpAllFlat");
runBeginBuildAndEndForStandardProject(projectDir, "", true, false);
// Run the build for a second time - should not fail after uninstalling targets
TestUtils.runMSBuild(ORCHESTRATOR, projectDir, "/t:Rebuild", "CSharpAllFlat.sln");

assertThat(getComponent("CSharpAllFlat:Common.cs")).isNotNull();
}

@Test
public void testCSharpSharedFiles() throws IOException {
runBeginBuildAndEndForStandardProject("CSharpSharedFiles", "");
Expand Down Expand Up @@ -701,16 +711,16 @@ private void validateCSharpSdk(String folderName) throws IOException {
tuple("csharpsquid:S1134", folderName + ":AspNetCoreMvc/Program.cs"),
tuple("csharpsquid:S1134", folderName + ":Main/Common.cs"),
tuple("csharpsquid:S2699", folderName + ":UTs/CommonTest.cs"));
// The AspNetCoreMvc/Views/Home/Index.cshtml contains an external CS0219 issue
// which is currently not imported due to the fact that the generated code Index.cshtml.g.cs is in the object folder.
// The AspNetCoreMvc/Views/Home/Index.cshtml contains an external CS0219 issue
// which is currently not imported due to the fact that the generated code Index.cshtml.g.cs is in the object folder.
} else {
assertThat(issues).hasSize(2)
.extracting(Issue::getRule, Issue::getComponent)
.containsExactlyInAnyOrder(
tuple("csharpsquid:S1134", folderName + ":AspNetCoreMvc/Program.cs"),
tuple("csharpsquid:S1134", folderName + ":Main/Common.cs"));
// The AspNetCoreMvc/Views/Home/Index.cshtml contains an external CS0219 issue
// which is currently not imported due to the fact that the generated code Index.cshtml.g.cs is in the object folder.
// The AspNetCoreMvc/Views/Home/Index.cshtml contains an external CS0219 issue
// which is currently not imported due to the fact that the generated code Index.cshtml.g.cs is in the object folder.
}
}

Expand Down Expand Up @@ -765,7 +775,12 @@ private BuildResult runBeginBuildAndEndForStandardProject(String folderName, Str

private BuildResult runBeginBuildAndEndForStandardProject(String folderName, String projectName, Boolean setProjectBaseDirExplicitly, Boolean useNuGet) throws IOException {
Path projectDir = TestUtils.projectDir(temp, folderName);
return runBeginBuildAndEndForStandardProject(projectDir, projectName, setProjectBaseDirExplicitly, useNuGet);
}

private BuildResult runBeginBuildAndEndForStandardProject(Path projectDir, String projectName, Boolean setProjectBaseDirExplicitly, Boolean useNuGet) throws IOException {
String token = TestUtils.getNewToken(ORCHESTRATOR);
String folderName = projectDir.getFileName().toString();
ScannerForMSBuild scanner = TestUtils.newScanner(ORCHESTRATOR, projectDir)
.addArgument("begin")
.setProjectKey(folderName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
<SonarQubeBuildDirectory Condition=" $(SonarQubeBuildDirectory) == '' ">$(AGENT_BUILDDIRECTORY)</SonarQubeBuildDirectory>
<!-- Command line -->
<SonarQubeBuildDirectory Condition=" $(SonarQubeBuildDirectory) == '' ">$(MSBuildStartupDirectory)</SonarQubeBuildDirectory>

<SonarQubeTempPath Condition=" $(SonarQubeBuildDirectory) != '' AND Exists('$(SonarQubeBuildDirectory)\.sonarqube\conf\SonarQubeAnalysisConfig.xml') ">$(SonarQubeBuildDirectory)\.sonarqube</SonarQubeTempPath>

</PropertyGroup>

<!-- **************************************************************************** -->
Expand All @@ -31,36 +29,18 @@
<Import Condition=" $(SonarQubeTargetsImported) != 'true' AND $(BuildingInsideVisualStudio) != 'true' AND Exists('$(SonarQubeTargetFilePath)') " Project="$(SonarQubeTargetFilePath)" />

<!-- **************************************************************************** -->
<!-- Diagnostic/error-checking target
Causes the build to fail if the analysis properties are not correctly configured
i.e. if the user has requested a SonarQube analysis run, but the analysis targets
cannot be found.
Writes out diagnostic information to help with troubleshooting. -->
<!-- Write out diagnostic information to help with troubleshooting. -->
<!-- **************************************************************************** -->
<Target Name="SonarQubeImportBeforeInfo" Condition=" $(SonarQubeTempPath) != '' AND $(BuildingInsideVisualStudio) != 'true' " BeforeTargets="CoreCompile">

<PropertyGroup>
<AnalysisTargetsFileFound Condition=" Exists('$(SonarQubeTargetFilePath)') " >true</AnalysisTargetsFileFound>
<ReportAnalysisTargetsError Condition=" $(AnalysisTargetsFileFound) != 'true'">true</ReportAnalysisTargetsError>
</PropertyGroup>

<!-- Diagnostic messages for troubleshooting -->
<Message Importance="normal" Text="Sonar: ($(MSBuildProjectName)) SonarQube.Integration.ImportBefore.targets was loaded" />
<Message Importance="low" Text="Sonar: ($(MSBuildProjectFile)) SonarQube analysis targets file found: $(AnalysisTargetsFileFound)" />
<Message Importance="low" Text="Sonar: ($(MSBuildProjectFile)) SonarQube analysis targets imported: $(SonarQubeTargetsImported)" />

<!-- Conditionally raise an error that will fail the build -->
<Message Condition=" $(ReportAnalysisTargetsError) == 'true'"
Importance="high"
Text="Sonar: ($(MSBuildProjectFile)) Calculated location for the analysis targets file: $(SonarQubeTargetFilePath)" />

<!-- We include name of the project being built in the error message as
it helps provide some context in TeamBuild scenarios when the error message
appears on the build summary page. -->
<Error Condition=" $(ReportAnalysisTargetsError) == 'true'"
Text="The build is configured to run SonarQube analysis but the SonarQube analysis targets could not be located. Project: $(MSBuildProjectFile)" />
<Message Condition=" $(AnalysisTargetsFileFound) != 'true'" Importance="high" Text="Sonar: ($(MSBuildProjectFile)) The analysis targets file not found: $(SonarQubeTargetFilePath)" />

</Target>
</Project>

0 comments on commit 30d7d7a

Please sign in to comment.