Skip to content

Commit

Permalink
TestFramework: Fix code smells, bump coverage (#8608)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jan 25, 2024
1 parent 3491287 commit a597b59
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CSharpParseOptions>().Select(x => x.LanguageVersion.ToDisplayString());
Console.WriteLine(@"C# versions used for analysis: " + string.Join(", ", csVersions));
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<GeneratedCode>Public Class Sample : End Class", null, "File.vb");
TestGeneratedCodeRecognizer.Instance.IsGenerated(tree).Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -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.
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()}")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit a597b59

Please sign in to comment.