Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner should override RunAnalyzers/RunAnalyzersDuringBuild properties #993

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ internal static class TargetProperties

public const string AssemblyName = "AssemblyName";

public const string RunAnalyzers = "RunAnalyzers";
public const string RunAnalyzersDuringBuild = "RunAnalyzersDuringBuild";
public const string TreatWarningsAsErrors = "TreatWarningsAsErrors";
public const string WarningsAsErrors = "WarningsAsErrors";
public const string WarningLevel = "WarningLevel";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<Message Importance='high' Text='CAPTURE::PROPERTY::TreatWarningsAsErrors::$(TreatWarningsAsErrors)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::WarningsAsErrors::$(WarningsAsErrors)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::WarningLevel::$(WarningLevel)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::RunAnalyzers::$(RunAnalyzers)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::RunAnalyzersDuringBuild::$(RunAnalyzersDuringBuild)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::ProjectSpecificOutDir::$(ProjectSpecificOutDir)' />
<Message Importance='high' Text='CAPTURE::PROPERTY::ProjectSpecificConfDir::$(ProjectSpecificConfDir)' />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
<Compile Include="Infrastructure\BuildUtilities.cs" />
<Compile Include="E2ETests\E2EAnalysisTests.cs" />
<Compile Include="Infrastructure\TargetConstants.cs" />
<Compile Include="Infrastructure\WellKnownProjectProperties.cs" />
<Compile Include="MSBuildExecution\SimpleXmlLogger.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void Settings_ValidSetup_LegacyServer_Override_Analyzers()
AssertExpectedAdditionalFiles(result, "should.not.be.removed.additional1.txt", "should.not.be.removed.additional2.txt");
AssertExpectedAnalyzers(result, @"c:\data\new.analyzer1.dll", @"c:\new.analyzer2.dll");
AssertWarningsAreNotTreatedAsErrorsNorIgnored(result);
AssertRunAnalyzersIsEnabled(result);
}

[TestMethod]
Expand Down Expand Up @@ -246,6 +247,7 @@ public void Settings_ValidSetup_NonLegacyServer_MergeSettings()
@"original\should.be.preserved\analyzer3.dll");

AssertWarningsAreNotTreatedAsErrorsNorIgnored(result);
AssertRunAnalyzersIsEnabled(result);
}

[TestMethod]
Expand Down Expand Up @@ -356,6 +358,8 @@ public void Settings_TempFolderIsNotSet()
<ResolvedCodeAnalysisRuleset>pre-existing.ruleset</ResolvedCodeAnalysisRuleset>
<WarningsAsErrors>CS101</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RunAnalyzers>false</RunAnalyzers>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>

<!-- This will override the value that was set earlier in the project file -->
<SonarQubeTempPath />
Expand All @@ -377,8 +381,11 @@ public void Settings_TempFolderIsNotSet()
result.AssertExpectedItemGroupCount(TargetProperties.AnalyzerItemType, 0);
result.AssertExpectedItemGroupCount(TargetProperties.AdditionalFilesItemType, 0);

// Properties are not overriden
result.AssertExpectedCapturedPropertyValue(TargetProperties.TreatWarningsAsErrors, "true");
result.AssertExpectedCapturedPropertyValue(TargetProperties.WarningsAsErrors, "CS101");
result.AssertExpectedCapturedPropertyValue(TargetProperties.RunAnalyzers, "false");
result.AssertExpectedCapturedPropertyValue(TargetProperties.RunAnalyzersDuringBuild, "false");
}

[TestMethod]
Expand Down Expand Up @@ -661,6 +668,15 @@ private static void AssertWarningsAreNotTreatedAsErrorsNorIgnored(BuildLog actua
actualResult.AssertExpectedCapturedPropertyValue(TargetProperties.WarningLevel, "4");
}

/// <summary>
/// Checks that VS2019 properties are set to run the analysis.
/// </summary>
private static void AssertRunAnalyzersIsEnabled(BuildLog actualResult)
{
actualResult.AssertExpectedCapturedPropertyValue(TargetProperties.RunAnalyzers, "true");
actualResult.AssertExpectedCapturedPropertyValue(TargetProperties.RunAnalyzersDuringBuild, "true");
}

/// <summary>
/// Checks that a SonarQubeSetting does not exist
/// </summary>
Expand Down Expand Up @@ -750,6 +766,9 @@ private BuildLog Execute_Settings_ValidSetup(string msBuildLanguage, bool isTest
<Language>{msBuildLanguage}</Language>
<SonarQubeTestProject>{isTestProject}</SonarQubeTestProject>
<ResolvedCodeAnalysisRuleset>c:\should.be.overridden.ruleset</ResolvedCodeAnalysisRuleset>
<!-- These should be overriden by the targets file -->
<RunAnalyzers>false</RunAnalyzers>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
</PropertyGroup>

<ItemGroup>
Expand All @@ -773,6 +792,7 @@ private BuildLog Execute_Settings_ValidSetup(string msBuildLanguage, bool isTest

AssertErrorLogIsSetBySonarQubeTargets(result);
AssertWarningsAreNotTreatedAsErrorsNorIgnored(result);
AssertRunAnalyzersIsEnabled(result);

var capturedProjectSpecificConfDir = result.GetCapturedPropertyValue(TargetProperties.ProjectSpecificConfDir);
result.MessageLog.Should().Contain($@"Sonar: ({Path.GetFileName(filePath)}) Analysis configured successfully with {capturedProjectSpecificConfDir}\SonarProjectConfig.xml.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,46 @@
<!--
This file contains targets to integrate SonarQube and MSBuild.

The targets will produce an output folder structure containing information required
by the sonar-scanner.
The targets will produce an output folder structure containing information required by the sonar-scanner.

A subdirectory will be created for each MSBuild project. Information about the project
will be written to a file called "ProjectInfo.xml". Additional files may also be written
containing information required by different analyzers. In particular, a file will be written
containing the list of files in the project that should be analyzed by the sonar-scanner.

The output will only be produced if the property $(SonarQubeTempPath) and
is specified. $(SonarQubeTempPath) is the root folder under which per-analysis output
should be written.
The output will only be produced if the property $(SonarQubeTempPath) and is specified.
$(SonarQubeTempPath) is the root folder under which per-analysis output should be written.

Excluding projects and files from SonarQube analysis
****************************************************
Individual projects can excluded from SonarQube analysis by setting
the property $(SonarQubeExclude) e.g.
Individual projects can excluded from SonarQube analysis by setting the property $(SonarQubeExclude) e.g.

<SonarQubeExclude>true</SonarQubeExclude>

Individual files can be excluded from analysis by setting the
<SonarQubeExclude> metadata item e.g.
Individual files can be excluded from analysis by setting the <SonarQubeExclude> metadata item e.g.

<Compile Include="Logger.cs">
<SonarQubeExclude>true</SonarQubeExclude>
</Compile>

Project is also excluded if it's recognized as a Fakes or a Temp project. These projects are auto-generated.

Test projects
*************
The analysis performed by SonarQube varies depending on whether a project
is a test project or not.
The analysis performed by SonarQube varies depending on whether a project is a test project or not.

A project can be explicitly marked as being a test or product project by setting
the property $(SonarQubeTestProject) e.g.
A project can be explicitly marked as being a test or product project by setting the property $(SonarQubeTestProject) e.g.

<SonarQubeTestProject>true</SonarQubeTestProject>

If $(SonarQubeTestProject) is not set explicitly then the targets will determine the project
type as follows:

1) Projects whose full project file name matches the regular expression specified in
the analysis setting "sonar.msbuild.testProjectPattern" (case sensitive)
will be treated as a test projects.

Note that the regular expression uses the .Net Regex format i.e. "+" is the
single character wildcard and "*" is the multi-character wildcard (zero to many).

This property was deprecated. Use SonarQubeTestProject property instead.
If $(SonarQubeTestProject) is not set explicitly then the project is categorized as a Test project if:
* Is a Fakes or a Temp project explicitly marked as SonarQubeExclude=false
* Has one of the expected test project GUIDs ("3AC096D0-A1C2-E12C-1390-A8335801FDAB" or legacy "82A7F48D-3B50-4B1E-B82E-3ADA8210C358").
* Has a TestContainer project capability.
* References a known unit test related assembly.
* Deprecated: Project name matches "sonar.msbuild.testProjectPattern" parameter. Use SonarQubeTestProject=true instead.

2) MSTest projects will be treated as test projects.
The $(ProjectTypeGuids) property of MS Test projects contains a specific guid
("3AC096D0-A1C2-E12C-1390-A8335801FDAB" or legacy "82A7F48D-3B50-4B1E-B82E-3ADA8210C358")

3) SDK-style projects with ProjectCapability TestContainer will be treated as test projects.
Otherwise it is a Product project.

Supplying analysis setting in a project file
********************************************
Expand All @@ -71,7 +58,6 @@

Settings specified in the project file will override those specified on the SonarQube server.


Specifying additional types of file to be analysed
**************************************************

Expand Down Expand Up @@ -169,19 +155,7 @@
<!-- **************************************************************************** -->
<!-- Categorize as a product or test project -->
<!-- **************************************************************************** -->
<!-- Project is excluded if it's recognized as a Fakes or a Temp project. These projects are auto-generated.
Project is categorized as a Test project if:
* Is explicitly marked with SonarQubeTestProject=true
* Is a Fakes or a Temp project explicitly marked as SonarQubeExclude=false
* Has one of the expected test project GUIDs.
* Has a TestContainer project capability.
* References a known unit test related assembly.
* Deprecated: Project name matches sonar.msbuild.testProjectPattern parameter. Use SonarQubeTestProject=true instead.

Otherwise it is a Product project.

This target is a utility target: it won't be executed unless a target that depends on it is executed.
-->
<!-- This target is a utility target: it won't be executed unless a target that depends on it is executed. -->
<Target Name="SonarQubeCategoriseProject"
DependsOnTargets="SonarResolveReferencesMsBuild14;SonarResolveReferences"
Condition=" $(SonarQubeTempPath) != '' ">
Expand Down Expand Up @@ -440,6 +414,10 @@
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<WarningsAsErrors></WarningsAsErrors>

<!-- Override VS2019 properties to make sure analysis runs in the scanner build. -->
<RunAnalyzers>true</RunAnalyzers>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>

<!-- The Roslyn analyzers output messages with different warning levels. This is tied to the diagnostic severity and currently
is translated as follows - error: 0, warning: 1, info: 4. To avoid issues being missed override the warning level by setting it to max tolerance.-->
<WarningLevel>4</WarningLevel>
Expand Down