From 6e58e9b26dc628af50098d6621f20272d3d9594c Mon Sep 17 00:00:00 2001 From: Costin Zaharia <56015273+costin-zaharia-sonarsource@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:35:27 +0100 Subject: [PATCH] Fail early if there are no dotnet analyzers on the server (#1811) --- .../PreProcessorTests.cs | 18 +++------------ .../PreProcessor.cs | 9 +++++++- .../Resources.Designer.cs | 10 +++++++- .../Resources.resx | 23 +++++++++++-------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Tests/SonarScanner.MSBuild.PreProcessor.Test/PreProcessorTests.cs b/Tests/SonarScanner.MSBuild.PreProcessor.Test/PreProcessorTests.cs index 999dfe2ea..9473567af 100644 --- a/Tests/SonarScanner.MSBuild.PreProcessor.Test/PreProcessorTests.cs +++ b/Tests/SonarScanner.MSBuild.PreProcessor.Test/PreProcessorTests.cs @@ -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] diff --git a/src/SonarScanner.MSBuild.PreProcessor/PreProcessor.cs b/src/SonarScanner.MSBuild.PreProcessor/PreProcessor.cs index c15c4905b..f73f98a7a 100644 --- a/src/SonarScanner.MSBuild.PreProcessor/PreProcessor.cs +++ b/src/SonarScanner.MSBuild.PreProcessor/PreProcessor.cs @@ -137,8 +137,15 @@ private async Task 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 { }) diff --git a/src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs b/src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs index 281888201..a030ae123 100644 --- a/src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs +++ b/src/SonarScanner.MSBuild.PreProcessor/Resources.Designer.cs @@ -1,7 +1,6 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -114,6 +113,15 @@ internal static string CmdLine_ArgDescription_ProjectVersion { } } + /// + /// Looks up a localized string similar to Could not find any dotnet analyzer plugin on the server (SonarQube/SonarCloud)!. + /// + internal static string ERR_DotNetAnalyzersNotFound { + get { + return ResourceManager.GetString("ERR_DotNetAnalyzersNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to An error occured while querying the server version! Please check if the server is running and if the address is correct.. /// diff --git a/src/SonarScanner.MSBuild.PreProcessor/Resources.resx b/src/SonarScanner.MSBuild.PreProcessor/Resources.resx index 4268c9841..83cf5d889 100644 --- a/src/SonarScanner.MSBuild.PreProcessor/Resources.resx +++ b/src/SonarScanner.MSBuild.PreProcessor/Resources.resx @@ -2,16 +2,16 @@