-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestFramework: Fix code smells, bump coverage (#8608)
- Loading branch information
1 parent
3491287
commit a597b59
Showing
7 changed files
with
112 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...yzers/tests/SonarAnalyzer.TestFramework.Test/Analyzers/TestGeneratedCodeRecognizerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ers/tests/SonarAnalyzer.TestFramework.Test/MetadataReferences/NuGetMetadataFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
36 changes: 18 additions & 18 deletions
36
analyzers/tests/SonarAnalyzer.TestFramework/Analyzers/DummyAnalyzerWithLocation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters