Skip to content

Commit a597b59

Browse files
TestFramework: Fix code smells, bump coverage (#8608)
1 parent 3491287 commit a597b59

File tree

7 files changed

+112
-27
lines changed

7 files changed

+112
-27
lines changed

analyzers/tests/SonarAnalyzer.Test/TestSuiteInitialization.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void AssemblyInit(TestContext context)
3333
ConfigureFluentValidation();
3434

3535
Console.WriteLine(@"Running tests initialization...");
36-
Console.WriteLine(@$"Build reason: {Environment.GetEnvironmentVariable(TestContextHelper.BuildReason) ?? "Not set / Local build"}");
36+
Console.WriteLine(@$"Build reason: {TestContextHelper.BuildReason() ?? "Not set / Local build"}");
3737

3838
var csVersions = ParseOptionsHelper.Default(LanguageNames.CSharp).Cast<CSharpParseOptions>().Select(x => x.LanguageVersion.ToDisplayString());
3939
Console.WriteLine(@"C# versions used for analysis: " + string.Join(", ", csVersions));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SonarAnalyzer for .NET
3+
* Copyright (C) 2015-2024 SonarSource SA
4+
* mailto: contact AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
using Microsoft.CodeAnalysis.CSharp;
22+
using Microsoft.CodeAnalysis.VisualBasic;
23+
24+
namespace SonarAnalyzer.Test.TestFramework.Tests.MetadataReferences;
25+
26+
[TestClass]
27+
public class TestGeneratedCodeRecognizerTest
28+
{
29+
[TestMethod]
30+
public void IsGenerated_FromAttribute_CS()
31+
{
32+
var tree = CSharpSyntaxTree.ParseText("[GeneratedCode] public class Sample { }", null, "File.cs");
33+
TestGeneratedCodeRecognizer.Instance.IsGenerated(tree).Should().BeTrue();
34+
}
35+
36+
[TestMethod]
37+
public void IsGenerated_FromAttribute_VB()
38+
{
39+
var tree = VisualBasicSyntaxTree.ParseText("<GeneratedCode>Public Class Sample : End Class", null, "File.vb");
40+
TestGeneratedCodeRecognizer.Instance.IsGenerated(tree).Should().BeTrue();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SonarAnalyzer for .NET
3+
* Copyright (C) 2015-2024 SonarSource SA
4+
* mailto: contact AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
using System.IO;
22+
using SonarAnalyzer.Test.Common;
23+
24+
namespace SonarAnalyzer.Test.TestFramework.Tests.MetadataReferences;
25+
26+
[TestClass]
27+
public class NuGetMetadataFactoryTest
28+
{
29+
[TestMethod]
30+
public void EnsureInstalled_InstallsMissingPackage()
31+
{
32+
const string id = "PayBySquare.TextGenerator.NET"; // Small package that is not used for other UTs
33+
const string version = "1.0.0";
34+
var packagesFolder = Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? Path.Combine(Paths.AnalyzersRoot, "packages"); // Same as NuGetMetadataFactory.PackagesFolder
35+
var packageDir = Path.GetFullPath(Path.Combine(packagesFolder, id, "Sonar." + version, string.Empty));
36+
// 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.
37+
if (Directory.Exists(packageDir))
38+
{
39+
Directory.Delete(packageDir, true);
40+
}
41+
NuGetMetadataFactory.Create(id, version).Should().NotBeEmpty();
42+
Directory.Exists(packageDir).Should().BeTrue();
43+
// *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.
44+
}
45+
}

analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
2-
* SonarAnalyzer for .NET
3-
* Copyright (C) 2015-2024 SonarSource SA
4-
* mailto: contact AT sonarsource DOT com
5-
*
6-
* This program is free software; you can redistribute it and/or
7-
* modify it under the terms of the GNU Lesser General Public
8-
* License as published by the Free Software Foundation; either
9-
* version 3 of the License, or (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14-
* Lesser General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU Lesser General Public License
17-
* along with this program; if not, write to the Free Software Foundation,
18-
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19-
*/
2+
* SonarAnalyzer for .NET
3+
* Copyright (C) 2015-2024 SonarSource SA
4+
* mailto: contact AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
2020

2121
using Microsoft.CodeAnalysis.CSharp;
2222
using Microsoft.CodeAnalysis.CSharp.Syntax;

analyzers/tests/SonarAnalyzer.TestFramework/Common/TestContextHelper.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ namespace SonarAnalyzer.Test.Helpers
2222
{
2323
public static class TestContextHelper
2424
{
25-
public const string BuildReason = "BUILD_REASON";
26-
2725
public static bool IsAzureDevOpsContext =>
28-
Environment.GetEnvironmentVariable(BuildReason) != null;
26+
BuildReason() is not null;
2927

3028
public static bool IsPullRequestBuild =>
31-
Environment.GetEnvironmentVariable(BuildReason) == "PullRequest";
29+
BuildReason() == "PullRequest";
30+
31+
public static string BuildReason() =>
32+
Environment.GetEnvironmentVariable("BUILD_REASON");
3233
}
3334
}

analyzers/tests/SonarAnalyzer.TestFramework/Extensions/CompilationExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class CompilationExtensions
2828
public static string LanguageVersionString(this Compilation compilation) =>
2929
compilation switch
3030
{
31-
CSharpCompilation csharp => csharp.LanguageVersion.ToString(),
31+
CSharpCompilation cs => cs.LanguageVersion.ToString(),
3232
VisualBasicCompilation vb => vb.LanguageVersion.ToString(),
3333
_ => throw new NotSupportedException($"Not supported compilation: {compilation.GetType()}")
3434
};

analyzers/tests/SonarAnalyzer.TestFramework/MetadataReferences/NuGetMetadataFactory.Package.cs

-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
*/
2020

2121
using System.IO;
22-
using System.Threading.Tasks;
2322
using NuGet.Common;
24-
using NuGet.Configuration;
2523
using NuGet.Packaging;
2624
using NuGet.Protocol;
2725
using NuGet.Protocol.Core.Types;
2826
using NuGet.Versioning;
29-
using SonarAnalyzer.Test.Common;
3027

3128
namespace SonarAnalyzer.Test.MetadataReferences
3229
{

0 commit comments

Comments
 (0)