diff --git a/analyzers/tests/SonarAnalyzer.Test/TestSuiteInitialization.cs b/analyzers/tests/SonarAnalyzer.Test/TestSuiteInitialization.cs index fa93d209ec8..7d219ae8ec3 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestSuiteInitialization.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestSuiteInitialization.cs @@ -33,7 +33,7 @@ public static void AssemblyInit(TestContext context) ConfigureFluentValidation(); Console.WriteLine(@"Running tests initialization..."); - Console.WriteLine(@$"Build reason: {Environment.GetEnvironmentVariable(TestContextHelper.BuildReason) ?? "Not set / Local build"}"); + Console.WriteLine(@$"Build reason: {TestContextHelper.BuildReason() ?? "Not set / Local build"}"); var csVersions = ParseOptionsHelper.Default(LanguageNames.CSharp).Cast().Select(x => x.LanguageVersion.ToDisplayString()); Console.WriteLine(@"C# versions used for analysis: " + string.Join(", ", csVersions)); diff --git a/analyzers/tests/SonarAnalyzer.TestFramework.Test/Analyzers/TestGeneratedCodeRecognizerTest.cs b/analyzers/tests/SonarAnalyzer.TestFramework.Test/Analyzers/TestGeneratedCodeRecognizerTest.cs new file mode 100644 index 00000000000..f9615c25875 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.TestFramework.Test/Analyzers/TestGeneratedCodeRecognizerTest.cs @@ -0,0 +1,42 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2024 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.VisualBasic; + +namespace SonarAnalyzer.Test.TestFramework.Tests.MetadataReferences; + +[TestClass] +public class TestGeneratedCodeRecognizerTest +{ + [TestMethod] + public void IsGenerated_FromAttribute_CS() + { + var tree = CSharpSyntaxTree.ParseText("[GeneratedCode] public class Sample { }", null, "File.cs"); + TestGeneratedCodeRecognizer.Instance.IsGenerated(tree).Should().BeTrue(); + } + + [TestMethod] + public void IsGenerated_FromAttribute_VB() + { + var tree = VisualBasicSyntaxTree.ParseText("Public Class Sample : End Class", null, "File.vb"); + TestGeneratedCodeRecognizer.Instance.IsGenerated(tree).Should().BeTrue(); + } +} diff --git a/analyzers/tests/SonarAnalyzer.TestFramework.Test/MetadataReferences/NuGetMetadataFactoryTest.cs b/analyzers/tests/SonarAnalyzer.TestFramework.Test/MetadataReferences/NuGetMetadataFactoryTest.cs new file mode 100644 index 00000000000..c979c7458c5 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.TestFramework.Test/MetadataReferences/NuGetMetadataFactoryTest.cs @@ -0,0 +1,45 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2024 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +using System.IO; +using SonarAnalyzer.Test.Common; + +namespace SonarAnalyzer.Test.TestFramework.Tests.MetadataReferences; + +[TestClass] +public class NuGetMetadataFactoryTest +{ + [TestMethod] + public void EnsureInstalled_InstallsMissingPackage() + { + const string id = "PayBySquare.TextGenerator.NET"; // Small package that is not used for other UTs + const string version = "1.0.0"; + var packagesFolder = Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? Path.Combine(Paths.AnalyzersRoot, "packages"); // Same as NuGetMetadataFactory.PackagesFolder + var packageDir = Path.GetFullPath(Path.Combine(packagesFolder, id, "Sonar." + version, string.Empty)); + // We need to delete the package from local cache to force the factory to always download it. Otherwise, the code would (almost*) never be covered on CI runs. + if (Directory.Exists(packageDir)) + { + Directory.Delete(packageDir, true); + } + NuGetMetadataFactory.Create(id, version).Should().NotBeEmpty(); + Directory.Exists(packageDir).Should().BeTrue(); + // *Almost, because first run on of the day on a new VM after a release of any LATEST package would randomly mark it as covered. + } +} diff --git a/analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs b/analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs index a6d868350c4..0e8609ad45e 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs @@ -1,22 +1,22 @@ /* -* SonarAnalyzer for .NET -* Copyright (C) 2015-2024 SonarSource SA -* mailto: contact AT sonarsource DOT com -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 3 of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this program; if not, write to the Free Software Foundation, -* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ + * SonarAnalyzer for .NET + * Copyright (C) 2015-2024 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/analyzers/tests/SonarAnalyzer.TestFramework/Common/TestContextHelper.cs b/analyzers/tests/SonarAnalyzer.TestFramework/Common/TestContextHelper.cs index c8bdb6cb6d0..a2e3d98a6d9 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework/Common/TestContextHelper.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework/Common/TestContextHelper.cs @@ -22,12 +22,13 @@ namespace SonarAnalyzer.Test.Helpers { public static class TestContextHelper { - public const string BuildReason = "BUILD_REASON"; - public static bool IsAzureDevOpsContext => - Environment.GetEnvironmentVariable(BuildReason) != null; + BuildReason() is not null; public static bool IsPullRequestBuild => - Environment.GetEnvironmentVariable(BuildReason) == "PullRequest"; + BuildReason() == "PullRequest"; + + public static string BuildReason() => + Environment.GetEnvironmentVariable("BUILD_REASON"); } } diff --git a/analyzers/tests/SonarAnalyzer.TestFramework/Extensions/CompilationExtensions.cs b/analyzers/tests/SonarAnalyzer.TestFramework/Extensions/CompilationExtensions.cs index cd0b99cd013..2534d24dc05 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework/Extensions/CompilationExtensions.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework/Extensions/CompilationExtensions.cs @@ -28,7 +28,7 @@ public static class CompilationExtensions public static string LanguageVersionString(this Compilation compilation) => compilation switch { - CSharpCompilation csharp => csharp.LanguageVersion.ToString(), + CSharpCompilation cs => cs.LanguageVersion.ToString(), VisualBasicCompilation vb => vb.LanguageVersion.ToString(), _ => throw new NotSupportedException($"Not supported compilation: {compilation.GetType()}") }; diff --git a/analyzers/tests/SonarAnalyzer.TestFramework/MetadataReferences/NuGetMetadataFactory.Package.cs b/analyzers/tests/SonarAnalyzer.TestFramework/MetadataReferences/NuGetMetadataFactory.Package.cs index 6ffac366d9f..318b36cc3bd 100644 --- a/analyzers/tests/SonarAnalyzer.TestFramework/MetadataReferences/NuGetMetadataFactory.Package.cs +++ b/analyzers/tests/SonarAnalyzer.TestFramework/MetadataReferences/NuGetMetadataFactory.Package.cs @@ -19,14 +19,11 @@ */ using System.IO; -using System.Threading.Tasks; using NuGet.Common; -using NuGet.Configuration; using NuGet.Packaging; using NuGet.Protocol; using NuGet.Protocol.Core.Types; using NuGet.Versioning; -using SonarAnalyzer.Test.Common; namespace SonarAnalyzer.Test.MetadataReferences {