Skip to content

Commit

Permalink
NET-833 Extract Helper analysis configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored and sonartech committed Jan 29, 2025
1 parent caa1608 commit 523faac
Show file tree
Hide file tree
Showing 51 changed files with 238 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/

using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Core.Analyzers;

public abstract class ParametrizedDiagnosticAnalyzer : SonarDiagnosticAnalyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System.Xml.Linq;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// Data class to describe an analysis configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.IO;
using System.Xml.Linq;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// This class reads and encapsulates <see cref="AnalysisConfig"/>, exposing only the configuration our analyzers need.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System.Xml.Linq;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// Data class to describe an analysis setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.IO;
using System.Text.RegularExpressions;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

public class FilesToAnalyzeProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Globalization;
using System.Reflection;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

internal static class ParameterLoader
{
Expand All @@ -44,7 +44,7 @@ internal static void SetParameterValues(ParametrizedDiagnosticAnalyzer parameter
.Select(x => new { Property = x, Descriptor = x.GetCustomAttributes<RuleParameterAttribute>().SingleOrDefault() })
.Where(x => x.Descriptor is not null);

var ids = new HashSet<string>(parameteredAnalyzer.SupportedDiagnostics.Select(diagnostic => diagnostic.Id));
var ids = new HashSet<string>(parameteredAnalyzer.SupportedDiagnostics.Select(x => x.Id));
foreach (var propertyParameterPair in propertyParameterPairs)
{
var parameter = sonarLintXml.ParametrizedRules.FirstOrDefault(x => ids.Contains(x.Key));
Expand All @@ -58,7 +58,7 @@ internal static void SetParameterValues(ParametrizedDiagnosticAnalyzer parameter

private static bool TryConvertToParameterType(string parameter, PropertyType type, out object result)
{
if (parameter == null)
if (parameter is null)
{
result = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System.Xml.Linq;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// Data class to describe a single project configuration for our analyzers.
Expand All @@ -27,7 +27,7 @@ namespace SonarAnalyzer.Helpers;
/// </remarks>
internal class ProjectConfig
{
public static readonly ProjectConfig Empty = new(nameof(Helpers.ProjectType.Unknown));
public static readonly ProjectConfig Empty = new(nameof(Configuration.ProjectType.Unknown));

/// <summary>
/// Full path to the SonarQubeAnalysisConfig.xml file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Text;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// This class reads and encapsulates <see cref="ProjectConfig"/>, exposing only the configuration our analyzers need.
Expand All @@ -43,10 +43,10 @@ public class ProjectConfigReader

public ProjectConfigReader(SourceText sonarProjectConfig)
{
projectConfig = sonarProjectConfig == null ? ProjectConfig.Empty : ReadContent(sonarProjectConfig);
projectConfig = sonarProjectConfig is null ? ProjectConfig.Empty : ReadContent(sonarProjectConfig);
projectType = new Lazy<ProjectType>(ParseProjectType);
filesToAnalyze = new Lazy<FilesToAnalyzeProvider>(() => new FilesToAnalyzeProvider(FilesToAnalyzePath));
analysisConfig = new(() => sonarProjectConfig == null ? null : new(AnalysisConfigPath));
analysisConfig = new(() => sonarProjectConfig is null ? null : new(AnalysisConfigPath));
}

private static ProjectConfig ReadContent(SourceText sonarProjectConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// Possible types of project. Note that we expect only the string format to be passed from the Scanner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

using System.Runtime.CompilerServices;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

internal static class ProjectTypeHelper
internal static class ProjectTypeCache
{
// This list is duplicated in sonar-scanner-msbuild and sonar-security and should be manually synchronized after each change.
public /* for testing */ static readonly ISet<string> TestAssemblyNames = new HashSet<string>
Expand Down Expand Up @@ -48,9 +48,9 @@ internal static class ProjectTypeHelper
// Should only be used by SonarAnalysisContext
public static bool IsTest(this Compilation compilation) =>
// We can't detect references => it's MAIN
compilation != null && Cache.GetValue(compilation, x => new IsTestWrapper(x)).Value;
compilation is not null && Cache.GetValue(compilation, x => new IsTestWrapper(x)).Value;

private class IsTestWrapper
private sealed class IsTestWrapper
{
public readonly bool Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using System.Xml.Serialization;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

/// <summary>
/// Data class to represent the SonarLint.xml for our analyzers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
using System.Xml.Serialization;
using Microsoft.CodeAnalysis.Text;

namespace SonarAnalyzer.Helpers;
namespace SonarAnalyzer.Core.Configuration;

public class SonarLintXmlReader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.IO;
using Roslyn.Utilities;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Core.Extensions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Roslyn.Utilities;
using SonarAnalyzer.Core.AnalysisContext;
using SonarAnalyzer.Core.Configuration;
using static SonarAnalyzer.Core.Analyzers.DiagnosticDescriptorFactory;

namespace SonarAnalyzer.Core.Extensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System.IO;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Core.Extensions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void FileScopeNamespace() =>
[TestMethod]
public void FileScopeNamespace_TestCode() =>
builder.AddPaths("FileScopeNamespace.cs")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, Helpers.ProjectType.Test))
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Test))
.Verify();

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
<Using Include="SonarAnalyzer.Core.Configuration" />
<Using Include="SonarAnalyzer.CSharp.Styling.Test.Common" />
<Using Include="SonarAnalyzer.CSharp.Styling.Rules" />
<Using Include="SonarAnalyzer.TestFramework.Build" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

using System.IO;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Test.Helpers;
namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class AnalysisConfigReaderTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

using System.IO;
using System.Text.RegularExpressions;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Test.Helpers;
namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class FilesToAnalyzeProviderTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

using System.IO;
using Microsoft.CodeAnalysis.Text;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Test.Helpers;
namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class ProjectConfigReaderTest
Expand Down Expand Up @@ -93,13 +94,14 @@ public void WhenInvalid_FilesToReturnPath_ThrowsException(string folder) =>
[TestMethod]
public void FilesToAnalyze_LoadsFileFromConfig()
{
var config = SourceText.From(@"
<SonarProjectConfig xmlns=""http://www.sonarsource.com/msbuild/analyzer/2021/1"">
<FilesToAnalyzePath>TestResources\FilesToAnalyze\FilesToAnalyze.txt</FilesToAnalyzePath>
</SonarProjectConfig>");
var config = SourceText.From("""
<SonarProjectConfig xmlns="http://www.sonarsource.com/msbuild/analyzer/2021/1">
<FilesToAnalyzePath>TestResources\FilesToAnalyze\FilesToAnalyze.txt</FilesToAnalyzePath>
</SonarProjectConfig>
""");
var files = new ProjectConfigReader(config).FilesToAnalyze.FindFiles("web.config", false);

files.Should().BeEquivalentTo(new[] { @"C:\Projects/DummyProj/wEB.config", @"C:\Projects/DummyProj/Views\Web.confiG" });
files.Should().BeEquivalentTo(@"C:\Projects/DummyProj/wEB.config", @"C:\Projects/DummyProj/Views\Web.confiG");
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/

namespace SonarAnalyzer.Test.Helpers;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class ProjectTypeHelperTest
public class ProjectTypeCacheTest
{
[TestMethod]
public void TestReference_ShouldBeSynchronized()
Expand All @@ -42,7 +44,7 @@ public void TestReference_ShouldBeSynchronized()
"Rhino.Mocks",
"Telerik.JustMock"
};
ProjectTypeHelper.TestAssemblyNames.OrderBy(x => x).Should().BeEquivalentTo(synchronizedSortedReferences);
ProjectTypeCache.TestAssemblyNames.OrderBy(x => x).Should().BeEquivalentTo(synchronizedSortedReferences);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

using System.IO;
using Microsoft.CodeAnalysis.Text;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Test.Helpers;
namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class SonarLintXmlReaderTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

using System.IO;
using System.Xml.Serialization;
using SonarAnalyzer.Core.Configuration;

namespace SonarAnalyzer.Test.Helpers;
namespace SonarAnalyzer.Core.Test.Configuration;

[TestClass]
public class SonarLintXmlTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
Expand Down Expand Up @@ -36,4 +36,10 @@
<Using Include="SonarAnalyzer.TestFramework.Verification" />
</ItemGroup>

<ItemGroup>
<Content Include="TestResources\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Loading

0 comments on commit 523faac

Please sign in to comment.