public void RoslynRuleSet_ActiveRuleAction_Unsupported() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider()); // Act & Assert var action = new Action(() => generator.ActiveRuleAction = (RuleAction)5); action.Should().ThrowExactly <NotSupportedException>().And .Message.Should().Be("5 is not a supported RuleAction."); }
public void RoslynRuleSet_GeneratorArgumentChecks() { IDictionary <string, string> dict = new Dictionary <string, string>(); var generator = new RoslynRuleSetGenerator(dict); IEnumerable <ActiveRule> activeRules = new List <ActiveRule>(); IEnumerable <string> inactiveRules = new List <string>(); var language = "cs"; AssertException.Expects <ArgumentNullException>(() => generator.Generate(activeRules, inactiveRules, null)); AssertException.Expects <ArgumentNullException>(() => generator.Generate(activeRules, null, language)); AssertException.Expects <ArgumentNullException>(() => generator.Generate(null, inactiveRules, language)); }
public void RoslynRuleSet_Common_Parameters() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider()); // Act var ruleSet = generator.Generate("cs", Enumerable.Empty <SonarRule>()); // Assert ruleSet.Description.Should().Be("This rule set was automatically generated from SonarQube"); ruleSet.ToolsVersion.Should().Be("14.0"); ruleSet.Name.Should().Be("Rules for SonarQube"); }
public void RoslynRuleSet_GeneratorArgumentChecks() { var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider()); var rules = Enumerable.Empty <SonarRule>(); var language = "cs"; Action act1 = () => generator.Generate(null, rules); act1.Should().ThrowExactly <ArgumentNullException>(); Action act2 = () => generator.Generate(language, null); act2.Should().ThrowExactly <ArgumentNullException>(); }
private string CreateRuleSet(string language, IEnumerable <SonarRule> rules, bool deactivateAll) { var ruleSetGenerator = new RoslynRuleSetGenerator(this.sonarProperties, deactivateAll); var ruleSet = ruleSetGenerator.Generate(language, rules); Debug.Assert(ruleSet != null, "Expecting the RuleSet to be created."); Debug.Assert(ruleSet.Rules != null, "Expecting the RuleSet.Rules to be initialized."); var rulesetFilePath = Path.Combine(this.teamBuildSettings.SonarConfigDirectory, string.Format(deactivateAll ? RulesetFileNameNone : RulesetFileNameNormal, language)); this.logger.LogDebug(Resources.RAP_UnpackingRuleset, rulesetFilePath); ruleSet.Save(rulesetFilePath); return(rulesetFilePath); }
public void RoslynRuleSet_Unsupported_Rules_Ignored() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider()); var rules = new[] { new SonarRule("other.repo", "other.rule1", true), new SonarRule("other.repo", "other.rule2", false), }; // Act var ruleSet = generator.Generate("cs", rules); // Assert ruleSet.Rules.Should().BeEmpty(); }
public void RoslynRuleSet_CSharp() { IDictionary <string, string> dict = new Dictionary <string, string> { { "sonaranalyzer-cs.analyzerId", "SonarAnalyzer.CSharp" }, { "sonaranalyzer-cs.ruleNamespace", "SonarAnalyzer.CSharp" }, { "sonaranalyzer-cs.pluginKey", "csharp" }, { "sonaranalyzer-cs.staticResourceName", "SonarAnalyzer.zip" }, { "sonaranalyzer-cs.nuget.packageId", "SonarAnalyzer.CSharp" }, { "sonaranalyzer-cs.pluginVersion", "1.13.0" }, { "sonaranalyzer-cs.nuget.packageVersion", "1.13.0" }, { "custom.analyzerId", "SonarAnalyzer.Custom" }, { "custom.ruleNamespace", "SonarAnalyzer.Custom" } }; var generator = new RoslynRuleSetGenerator(dict); var activeRules = new List <ActiveRule>(); var inactiveRules = new List <string>(); var language = "cs"; activeRules.Add(new ActiveRule("csharpsquid", "S1000")); activeRules.Add(new ActiveRule("csharpsquid", "S1001")); activeRules.Add(new ActiveRule("roslyn.custom", "custom", "custom.internal")); activeRules.Add(new ActiveRule("other.repo", "other.rule")); inactiveRules.Add("csharpsquid:S1002"); inactiveRules.Add("roslyn.custom:S1005"); var ruleSet = generator.Generate(activeRules, inactiveRules, language); string[] activatedCSharp = { "S1000", "S1001" }; string[] activatedCustom = { "custom" }; string[] inactivatedCSharp = { "S1002" }; string[] inactivatedCustom = { "S1005" }; Assert.AreEqual(2, ruleSet.Rules.Count()); AssertAnalyzerRules(ruleSet, "SonarAnalyzer.Custom", activatedCustom, inactivatedCustom); AssertAnalyzerRules(ruleSet, "SonarAnalyzer.CSharp", activatedCSharp, inactivatedCSharp); Assert.AreEqual(ruleSet.Description, "This rule set was automatically generated from SonarQube"); Assert.AreEqual(ruleSet.ToolsVersion, "14.0"); Assert.AreEqual(ruleSet.Name, "Rules for SonarQube"); }
public void RoslynRuleSet_Empty() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider()); var rules = new List <SonarRule> { new SonarRule("repo", "key"), }; // Act var ruleSet = generator.Generate("cs", rules); // Assert ruleSet.Rules.Should().BeEmpty(); // No analyzers ruleSet.Description.Should().Be("This rule set was automatically generated from SonarQube"); ruleSet.ToolsVersion.Should().Be("14.0"); ruleSet.Name.Should().Be("Rules for SonarQube"); }
public void RoslynRuleSet_Empty() { IDictionary <string, string> dict = new Dictionary <string, string>(); var generator = new RoslynRuleSetGenerator(dict); var activeRules = new List <ActiveRule>(); IEnumerable <string> inactiveRules = new List <string>(); var language = "cs"; activeRules.Add(new ActiveRule("repo", "key")); var ruleSet = generator.Generate(activeRules, inactiveRules, language); // No analyzer Assert.IsFalse(ruleSet.Rules.Any()); Assert.AreEqual(ruleSet.Description, "This rule set was automatically generated from SonarQube"); Assert.AreEqual(ruleSet.ToolsVersion, "14.0"); Assert.AreEqual(ruleSet.Name, "Rules for SonarQube"); }
public void RoslynRuleSet_Empty() { IDictionary <string, string> dict = new Dictionary <string, string>(); var generator = new RoslynRuleSetGenerator(dict); var activeRules = new List <SonarRule>(); IEnumerable <SonarRule> inactiveRules = new List <SonarRule>(); var language = "cs"; activeRules.Add(new SonarRule("repo", "key")); var ruleSet = generator.Generate(activeRules, inactiveRules, language); // No analyzer ruleSet.Rules.Any().Should().BeFalse(); ruleSet.Description.Should().Be("This rule set was automatically generated from SonarQube"); ruleSet.ToolsVersion.Should().Be("14.0"); ruleSet.Name.Should().Be("Rules for SonarQube"); }
public void RoslynRuleSet_GeneratorArgumentChecks() { IDictionary <string, string> dict = new Dictionary <string, string>(); var generator = new RoslynRuleSetGenerator(dict); IEnumerable <SonarRule> activeRules = new List <SonarRule>(); IEnumerable <SonarRule> inactiveRules = new List <SonarRule>(); var language = "cs"; Action act1 = () => generator.Generate(null, activeRules, inactiveRules); act1.Should().ThrowExactly <ArgumentNullException>(); Action act2 = () => generator.Generate(language, activeRules, null); act2.Should().ThrowExactly <ArgumentNullException>(); Action act3 = () => generator.Generate(language, null, inactiveRules); act3.Should().ThrowExactly <ArgumentNullException>(); }
public void RoslynRuleSet_Unsupported_Rules_Ignored() { // Arrange var generator = new RoslynRuleSetGenerator(new Dictionary <string, string>()); var activeRules = new[] { new SonarRule("other.repo", "other.rule1", true), }; var inactiveRules = new[] { new SonarRule("other.repo", "other.rule2", false), }; // Act var ruleSet = generator.Generate("cs", activeRules, inactiveRules); // Assert ruleSet.Rules.Should().BeEmpty(); }
private AnalyzerSettings ConfigureAnalyzer(string language, IEnumerable <ActiveRule> activeRules, IEnumerable <string> inactiveRules) { RoslynRuleSetGenerator ruleSetGenerator = new RoslynRuleSetGenerator(sqServerSettings); RuleSet ruleSet = ruleSetGenerator.generate(activeRules, inactiveRules, language); string rulesetFilePath = this.WriteRuleset(ruleSet, language); if (rulesetFilePath == null) { return(null); } IEnumerable <string> additionalFiles = this.WriteAdditionalFiles(language, activeRules); IEnumerable <string> analyzersAssemblies = this.FetchAnalyzerAssemblies(activeRules, language); AnalyzerSettings compilerConfig = new AnalyzerSettings(language, rulesetFilePath, analyzersAssemblies ?? Enumerable.Empty <string>(), additionalFiles ?? Enumerable.Empty <string>()); return(compilerConfig); }
public void RoslynRuleSet_RuleNamespace_Proprety_Missing() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider(new Dictionary <string, string> { { "sonaranalyzer-cs.analyzerId", "SonarAnalyzer.CSharp" }, })); var rules = new[] { new SonarRule("csharpsquid", "active1", true), }; // Act & Assert var action = new Action(() => generator.Generate("cs", rules)); action.Should().ThrowExactly <AnalysisException>().And .Message.Should().StartWith( "Property does not exist: sonaranalyzer-cs.ruleNamespace. This property should be set by the plugin in SonarQube."); }
/// <summary> /// Generates several files related to rulesets and roslyn analyzer assemblies. /// Active rules should never be empty, but depending on the server settings of repo keys, we might have no rules in the ruleset. /// In that case, this method returns null. /// </summary> private AnalyzerSettings ConfigureAnalyzer(string language, IEnumerable <ActiveRule> activeRules, IEnumerable <string> inactiveRules) { var ruleSetGenerator = new RoslynRuleSetGenerator(sqServerSettings); var ruleSet = ruleSetGenerator.Generate(activeRules, inactiveRules, language); var rulesetFilePath = WriteRuleset(ruleSet, language); if (rulesetFilePath == null) { // no ruleset, nothing was written in disk return(null); } var additionalFiles = WriteAdditionalFiles(language, activeRules); var analyzersAssemblies = FetchAnalyzerAssemblies(activeRules, language); var compilerConfig = new AnalyzerSettings(language, rulesetFilePath, analyzersAssemblies ?? Enumerable.Empty <string>(), additionalFiles ?? Enumerable.Empty <string>()); return(compilerConfig); }
public void RoslynRuleSet_RoslynSDK_Rules_Added() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider(new Dictionary <string, string> { ["custom1.analyzerId"] = "CustomAnalyzer1", ["custom1.ruleNamespace"] = "CustomNamespace1", ["custom2.analyzerId"] = "CustomAnalyzer2", ["custom2.ruleNamespace"] = "CustomNamespace2", })); var activeRules = new[] { new SonarRule("roslyn.custom1", "active1", true), new SonarRule("roslyn.custom2", "active2", true), }; var inactiveRules = new[] { new SonarRule("roslyn.custom1", "inactive1", false), new SonarRule("roslyn.custom2", "inactive2", false), }; // Act var ruleSet = generator.Generate("cs", activeRules, inactiveRules); // Assert ruleSet.Rules.Should().HaveCount(2); ruleSet.Rules[0].RuleNamespace.Should().Be("CustomNamespace1"); ruleSet.Rules[0].AnalyzerId.Should().Be("CustomAnalyzer1"); ruleSet.Rules[0].RuleList.Should().HaveCount(2); ruleSet.Rules[0].RuleList.Select(r => r.Id).Should().BeEquivalentTo("active1", "inactive1"); ruleSet.Rules[0].RuleList.Select(r => r.Action).Should().BeEquivalentTo("Warning", "None"); ruleSet.Rules[1].RuleNamespace.Should().Be("CustomNamespace2"); ruleSet.Rules[1].AnalyzerId.Should().Be("CustomAnalyzer2"); ruleSet.Rules[1].RuleList.Should().HaveCount(2); ruleSet.Rules[1].RuleList.Select(r => r.Id).Should().BeEquivalentTo("active2", "inactive2"); ruleSet.Rules[1].RuleList.Select(r => r.Action).Should().BeEquivalentTo("Warning", "None"); }
public void RoslynRuleSet_Sonar_Rules_Added() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider(new Dictionary <string, string> { { "sonaranalyzer-cs.analyzerId", "SonarAnalyzer.CSharp" }, { "sonaranalyzer-cs.ruleNamespace", "SonarAnalyzer.CSharp" }, })); var activeRules = new[] { new SonarRule("csharpsquid", "active1", true), // Even though this rule is for VB it will be added as C#, see NOTE below new SonarRule("vbnet", "active2", true), }; var inactiveRules = new[] { new SonarRule("csharpsquid", "inactive1", false), // Even though this rule is for VB it will be added as C#, see NOTE below new SonarRule("vbnet", "inactive2", false), }; // Act var ruleSet = generator.Generate("cs", activeRules, inactiveRules); // Assert ruleSet.Rules.Should().HaveCount(1); // NOTE: The RuleNamespace and AnalyzerId are taken from the language parameter of the // Generate method. The FetchArgumentsAndRulesets method will retrieve active/inactive // rules from SonarQube per language/quality profile and mixture of VB-C# rules is not // expected. ruleSet.Rules[0].RuleNamespace.Should().Be("SonarAnalyzer.CSharp"); ruleSet.Rules[0].AnalyzerId.Should().Be("SonarAnalyzer.CSharp"); ruleSet.Rules[0].RuleList.Should().HaveCount(4); ruleSet.Rules[0].RuleList.Select(r => r.Id).Should().BeEquivalentTo( "active1", "active2", "inactive1", "inactive2"); ruleSet.Rules[0].RuleList.Select(r => r.Action).Should().BeEquivalentTo( "Warning", "Warning", "None", "None"); }
public void RoslynRuleSet_ActiveRuleAction_OverrideToNone() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider(new Dictionary <string, string> { ["sonaranalyzer-cs.analyzerId"] = "SonarAnalyzer.CSharp", ["sonaranalyzer-cs.ruleNamespace"] = "SonarAnalyzer.CSharp", }), deactivateAll: true); var rules = new List <SonarRule> { new SonarRule("csharpsquid", "rule1", isActive: true), new SonarRule("csharpsquid", "rule2", isActive: true), }; // Act var ruleSet = generator.Generate("cs", rules); // Assert ruleSet.Rules.Should().HaveCount(1); ruleSet.Rules[0].RuleList.Select(r => r.Action).Should().BeEquivalentTo("None", "None"); }
public void RoslynRuleSet_InactiveRules_Always_None() { // Arrange var generator = new RoslynRuleSetGenerator(new ListPropertiesProvider(new Dictionary <string, string> { ["sonaranalyzer-cs.analyzerId"] = "SonarAnalyzer.CSharp", ["sonaranalyzer-cs.ruleNamespace"] = "SonarAnalyzer.CSharp", })); var inactiveRules = new List <SonarRule> { new SonarRule("csharpsquid", "rule1", isActive: false), new SonarRule("csharpsquid", "rule2", isActive: false), }; // Act generator.ActiveRuleAction = RuleAction.Error; var ruleSet = generator.Generate("cs", Enumerable.Empty <SonarRule>(), inactiveRules); // Assert ruleSet.Rules.Should().HaveCount(1); ruleSet.Rules[0].RuleList.Select(r => r.Action).Should().BeEquivalentTo("None", "None"); }
public void RoslynRuleSet_ActiveRuleAction_Hidden() { // Arrange var generator = new RoslynRuleSetGenerator(new Dictionary <string, string> { ["sonaranalyzer-cs.analyzerId"] = "SonarAnalyzer.CSharp", ["sonaranalyzer-cs.ruleNamespace"] = "SonarAnalyzer.CSharp", }); var activeRules = new List <SonarRule> { new SonarRule("csharpsquid", "rule1", isActive: true), new SonarRule("csharpsquid", "rule2", isActive: true), }; // Act generator.ActiveRuleAction = RuleAction.Hidden; var ruleSet = generator.Generate("cs", activeRules, Enumerable.Empty <SonarRule>()); // Assert ruleSet.Rules.Should().HaveCount(1); ruleSet.Rules[0].RuleList.Select(r => r.Action).Should().BeEquivalentTo("Hidden", "Hidden"); }
private string CreateRuleSet(string language, IEnumerable <SonarRule> activeRules, IEnumerable <SonarRule> inactiveRules, ProjectType projectType) { var ruleSetGenerator = new RoslynRuleSetGenerator(this.sonarProperties); if (projectType == ProjectType.Test) { ruleSetGenerator.ActiveRuleAction = RuleAction.None; } var ruleSet = ruleSetGenerator.Generate(language, activeRules, inactiveRules); Debug.Assert(ruleSet != null, "Expecting the RuleSet to be created."); Debug.Assert(ruleSet.Rules != null, "Expecting the RuleSet.Rules to be initialized."); var rulesetFilePath = Path.Combine( this.teamBuildSettings.SonarConfigDirectory, GetRoslynRulesetFileName(language, projectType)); this.logger.LogDebug(Resources.RAP_UnpackingRuleset, rulesetFilePath); ruleSet.Save(rulesetFilePath); return(rulesetFilePath); }