Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Ward <matt.ward@microsoft.com>2020-01-17 18:43:53 +0300
committerGitHub <noreply@github.com>2020-01-17 18:43:53 +0300
commit7cd10e45004a2422b3a4eab51fae6a62d6442efb (patch)
treeab2c246cdf1742c88e76437af10cfbd01bf564b7
parent30ff79c2f58be039e7d3b9c430c1be50f6c30253 (diff)
parent0990511ae1b7a4bcfada1ea2a376e7e16bfd92b2 (diff)
Merge pull request #9502 from mono/project-code-analysis-ruleset
[C#] Support CodeAnalysisRuleSet file
-rw-r--r--main/src/addins/CSharpBinding/MonoDevelop.CSharp.Project/CSharpCompilerParameters.cs40
-rw-r--r--main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding.Tests.csproj1
-rw-r--r--main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding/CustomProjectRuleSetTests.cs88
-rw-r--r--main/tests/test-projects/ruleset/custom.ruleset10
-rw-r--r--main/tests/test-projects/ruleset/global.ruleset7
-rw-r--r--main/tests/test-projects/ruleset/ruleset.csproj35
-rw-r--r--main/tests/test-projects/ruleset/ruleset.sln17
7 files changed, 191 insertions, 7 deletions
diff --git a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Project/CSharpCompilerParameters.cs b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Project/CSharpCompilerParameters.cs
index ef40124eb1..ccedc00488 100644
--- a/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Project/CSharpCompilerParameters.cs
+++ b/main/src/addins/CSharpBinding/MonoDevelop.CSharp.Project/CSharpCompilerParameters.cs
@@ -28,7 +28,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
-using System.Globalization;
+using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@@ -46,7 +46,7 @@ namespace MonoDevelop.CSharp.Project
public class CSharpCompilerParameters : DotNetCompilerParameters
{
// Configuration parameters
-
+ FilePath codeAnalysisRuleSet;
int? warninglevel = 4;
[ItemProperty ("NoWarn", DefaultValue = "")]
@@ -113,6 +113,7 @@ namespace MonoDevelop.CSharp.Project
optimize = pset.GetValue ("Optimize", (bool?)null);
warninglevel = pset.GetValue<int?> ("WarningLevel", null);
outputType = pset.GetValue ("OutputType", "Library");
+ codeAnalysisRuleSet = pset.GetPathValue ("CodeAnalysisRuleSet");
}
static MetadataReferenceResolver CreateMetadataReferenceResolver (IMetadataService metadataService, string projectDirectory, string outputDirectory)
@@ -205,18 +206,43 @@ namespace MonoDevelop.CSharp.Project
Dictionary<string, ReportDiagnostic> GetSpecificDiagnosticOptions ()
{
var result = new Dictionary<string, ReportDiagnostic> ();
- foreach (var warning in GetSuppressedWarnings ())
- result [warning] = ReportDiagnostic.Suppress;
var globalRuleSet = IdeApp.TypeSystemService.RuleSetManager.GetGlobalRuleSet ();
if (globalRuleSet != null) {
- foreach (var kv in globalRuleSet.SpecificDiagnosticOptions) {
- result [kv.Key] = kv.Value;
- }
+ AddSpecificDiagnosticOptions (result, globalRuleSet);
+ }
+
+ var ruleSet = GetRuleSet (codeAnalysisRuleSet);
+ if (ruleSet != null) {
+ AddSpecificDiagnosticOptions (result, ruleSet);
}
+
+ foreach (var warning in GetSuppressedWarnings ()) {
+ result [warning] = ReportDiagnostic.Suppress;
+ }
+
return result;
}
+ static RuleSet GetRuleSet (FilePath ruleSetFileName)
+ {
+ try {
+ if (ruleSetFileName.IsNotNull && File.Exists (ruleSetFileName)) {
+ return RuleSet.LoadEffectiveRuleSetFromFile (ruleSetFileName);
+ }
+ } catch (Exception ex) {
+ LoggingService.LogError (string.Format ("Unable to load ruleset from file: {0}", ruleSetFileName), ex);
+ }
+ return null;
+ }
+
+ static void AddSpecificDiagnosticOptions (Dictionary<string, ReportDiagnostic> result, RuleSet ruleSet)
+ {
+ foreach (var kv in ruleSet.SpecificDiagnosticOptions) {
+ result [kv.Key] = kv.Value;
+ }
+ }
+
Microsoft.CodeAnalysis.Platform GetPlatform ()
{
Microsoft.CodeAnalysis.Platform platform;
diff --git a/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding.Tests.csproj b/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding.Tests.csproj
index b3678da717..b7dacf1b38 100644
--- a/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding.Tests.csproj
+++ b/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding.Tests.csproj
@@ -66,6 +66,7 @@
<Compile Include="MonoDevelop.CSharpBinding\AutoFormatIntegrationTests.cs" />
<Compile Include="MonoDevelop.CSharpBinding\CSharpDocumentOptionsProviderTests.cs" />
<Compile Include="MonoDevelop.CSharpBinding.Debugging\DebuggerCompletionControllerTests.cs" />
+ <Compile Include="MonoDevelop.CSharpBinding\CustomProjectRuleSetTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\addins\CSharpBinding\CSharpBinding.csproj">
diff --git a/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding/CustomProjectRuleSetTests.cs b/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding/CustomProjectRuleSetTests.cs
new file mode 100644
index 0000000000..263b5883d9
--- /dev/null
+++ b/main/tests/MonoDevelop.CSharpBinding.Tests/MonoDevelop.CSharpBinding/CustomProjectRuleSetTests.cs
@@ -0,0 +1,88 @@
+//
+// CustomProjectRuleSetTests.cs
+//
+// Author:
+// Matt Ward <matt.ward@microsoft.com>
+//
+// Copyright (c) 2020 Microsoft Corporation
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.CodeAnalysis;
+using MonoDevelop.Core;
+using MonoDevelop.Ide;
+using MonoDevelop.Projects;
+using NUnit.Framework;
+using UnitTests;
+
+namespace MonoDevelop.CSharpBinding.Tests
+{
+ [TestFixture]
+ class CustomProjectRuleSetTests : IdeTestBase
+ {
+ FilePath globalRuleSetFileNameBackup;
+
+ [TestFixtureSetUp]
+ public void SetUp ()
+ {
+ FilePath backupFileName = GlobalRuleSetFileName + "-test-backup";
+ File.Copy (GlobalRuleSetFileName, backupFileName, true);
+ globalRuleSetFileNameBackup = backupFileName;
+ }
+
+ static string GlobalRuleSetFileName {
+ get => IdeApp.TypeSystemService.RuleSetManager.GlobalRulesetFileName;
+ }
+
+ public override void TearDown ()
+ {
+ File.Copy (globalRuleSetFileNameBackup, IdeApp.TypeSystemService.RuleSetManager.GlobalRulesetFileName, true);
+ File.Delete (globalRuleSetFileNameBackup);
+ base.TearDown ();
+ }
+
+ [Test]
+ public async Task CustomCodeAnalysisRuleSetFile ()
+ {
+ FilePath solutionFileName = Util.GetSampleProject ("ruleset", "ruleset.sln");
+ File.Copy (solutionFileName.ParentDirectory.Combine ("global.ruleset"), GlobalRuleSetFileName, true);
+
+ using (var solution = (MonoDevelop.Projects.Solution)await Ide.Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName)) {
+ var project = solution.GetAllProjects ().OfType <DotNetProject> ().Single ();
+ var config = (DotNetProjectConfiguration)project.GetConfiguration (ConfigurationSelector.Default);
+ var compilationOptions = config.CompilationParameters.CreateCompilationOptions ();
+ var diagnosticOptions = compilationOptions.SpecificDiagnosticOptions;
+
+ // Project ruleset options.
+ Assert.AreEqual (ReportDiagnostic.Error, diagnosticOptions ["SA1000"]);
+ Assert.AreEqual (ReportDiagnostic.Warn, diagnosticOptions ["SA1001"]);
+ Assert.AreEqual (ReportDiagnostic.Suppress, diagnosticOptions ["SA1002"]);
+
+ // Global ruleset option which is not overridden by project ruleset.
+ Assert.AreEqual (ReportDiagnostic.Error, diagnosticOptions ["SA1003"]);
+
+ // NoWarn set in project file directly should override project ruleset.
+ Assert.AreEqual (ReportDiagnostic.Suppress, diagnosticOptions ["SA1600"]);
+ }
+ }
+ }
+}
diff --git a/main/tests/test-projects/ruleset/custom.ruleset b/main/tests/test-projects/ruleset/custom.ruleset
new file mode 100644
index 0000000000..0d7baaab51
--- /dev/null
+++ b/main/tests/test-projects/ruleset/custom.ruleset
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RuleSet Name="CustomTestRules" ToolsVersion="16.0">
+ <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
+ <Rule Id="SA1000" Action="Error" />
+ <Rule Id="SA1001" Action="Warning" />
+ <Rule Id="SA1002" Action="None" />
+ <!-- Rule SA1003 defined in global ruleset -->
+ <Rule Id="SA1600" Action="Error" />
+ </Rules>
+</RuleSet> \ No newline at end of file
diff --git a/main/tests/test-projects/ruleset/global.ruleset b/main/tests/test-projects/ruleset/global.ruleset
new file mode 100644
index 0000000000..b876fa315b
--- /dev/null
+++ b/main/tests/test-projects/ruleset/global.ruleset
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RuleSet Name="Global Rules" ToolsVersion="16.0">
+ <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
+ <Rule Id="SA1000" Action="None" />
+ <Rule Id="SA1003" Action="Error" />
+ </Rules>
+</RuleSet> \ No newline at end of file
diff --git a/main/tests/test-projects/ruleset/ruleset.csproj b/main/tests/test-projects/ruleset/ruleset.csproj
new file mode 100644
index 0000000000..b4779cd891
--- /dev/null
+++ b/main/tests/test-projects/ruleset/ruleset.csproj
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{2646A92D-26AB-4B3E-91A2-B73EC92FB064}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <RootNamespace>ruleset</RootNamespace>
+ <AssemblyName>ruleset</AssemblyName>
+ <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
+ <CodeAnalysisRuleSet>custom.ruleset</CodeAnalysisRuleSet>
+ <NoWarn>SA1600</NoWarn>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ConsolePause>false</ConsolePause>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project> \ No newline at end of file
diff --git a/main/tests/test-projects/ruleset/ruleset.sln b/main/tests/test-projects/ruleset/ruleset.sln
new file mode 100644
index 0000000000..f44fc00c8a
--- /dev/null
+++ b/main/tests/test-projects/ruleset/ruleset.sln
@@ -0,0 +1,17 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ruleset", "ruleset.csproj", "{2646A92D-26AB-4B3E-91A2-B73EC92FB064}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {2646A92D-26AB-4B3E-91A2-B73EC92FB064}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {2646A92D-26AB-4B3E-91A2-B73EC92FB064}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {2646A92D-26AB-4B3E-91A2-B73EC92FB064}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {2646A92D-26AB-4B3E-91A2-B73EC92FB064}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+EndGlobal