Skip to content

Commit

Permalink
Fail early if there are no dotnet analyzers on the server (#1811)
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource authored Dec 1, 2023
1 parent 217589e commit 6e58e9b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
18 changes: 3 additions & 15 deletions Tests/SonarScanner.MSBuild.PreProcessor.Test/PreProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,18 @@ public async Task Execute_EndToEnd_SuccessCase_With_Organization()
}

[TestMethod]
public async Task Execute_NoPlugin_ReturnsTrue()
public async Task Execute_NoPlugin_ReturnsFalseAndLogsError()
{
using var scope = new TestScope(TestContext);
var factory = new MockObjectFactory();
factory.Server.Data.Languages.Clear();
factory.Server.Data.Languages.Add("invalid_plugin");
var settings = factory.ReadSettings();
var preProcessor = new PreProcessor(factory, factory.Logger);

var success = await preProcessor.Execute(CreateArgs());
success.Should().BeTrue("Expecting the pre-processing to complete successfully");

AssertDirectoriesCreated(settings);

factory.TargetsInstaller.Verify(x => x.InstallLoaderTargets(scope.WorkingDir), Times.Once());
factory.Server.AssertMethodCalled(nameof(ISonarWebServer.DownloadProperties), 1);
factory.Server.AssertMethodCalled(nameof(ISonarWebServer.DownloadAllLanguages), 1);
factory.Server.AssertMethodCalled(nameof(ISonarWebServer.DownloadQualityProfile), 0); // No valid plugin
factory.Server.AssertMethodCalled(nameof(ISonarWebServer.DownloadRules), 0); // No valid plugin

AssertAnalysisConfig(settings.AnalysisConfigFilePath, 0, factory.Logger);
success.Should().BeFalse("Expecting the pre-processing to fail");

// only contains SonarQubeAnalysisConfig (no rulesets or additional files)
AssertDirectoryContains(settings.SonarConfigDirectory, Path.GetFileName(settings.AnalysisConfigFilePath));
factory.Logger.AssertErrorLogged("Could not find any dotnet analyzer plugin on the server (SonarQube/SonarCloud)!");
}

[TestMethod]
Expand Down
9 changes: 8 additions & 1 deletion src/SonarScanner.MSBuild.PreProcessor/PreProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ private async Task<ArgumentsAndRuleSets> FetchArgumentsAndRuleSets(ISonarWebServ
args.TryGetSetting(SonarProperties.ProjectBranch, out var projectBranch);
argumentsAndRuleSets.ServerSettings = await server.DownloadProperties(args.ProjectKey, projectBranch);
var availableLanguages = await server.DownloadAllLanguages();
var knownLanguages = Languages.Where(availableLanguages.Contains).ToList();
if (knownLanguages.Count == 0)
{
logger.LogError(Resources.ERR_DotNetAnalyzersNotFound);
argumentsAndRuleSets.IsSuccess = false;
return argumentsAndRuleSets;
}

foreach (var language in Languages.Where(availableLanguages.Contains))
foreach (var language in knownLanguages)
{
var qualityProfile = await server.DownloadQualityProfile(args.ProjectKey, projectBranch, language);
if (qualityProfile is not { })
Expand Down
10 changes: 9 additions & 1 deletion src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/SonarScanner.MSBuild.PreProcessor/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,29 +26,29 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
Expand Down Expand Up @@ -357,4 +357,7 @@ Use '/?' or '/h' to see the help message.</value>
<data name="MSG_SonarCloudDetected_SkipVersionCheck" xml:space="preserve">
<value>SonarCloud detected, skipping server version check.</value>
</data>
<data name="ERR_DotNetAnalyzersNotFound" xml:space="preserve">
<value>Could not find any dotnet analyzer plugin on the server (SonarQube/SonarCloud)!</value>
</data>
</root>

0 comments on commit 6e58e9b

Please sign in to comment.