public static string TryGetRoslynPluginPropertyPrefix(this SonarQubeRule rule) { // The SonarC# and Sonar VBNet repositories don't following the same prefix-naming rules // as third-party plugins - they have their own custom prefixes. if (CSharpRepositoryKey.Equals(rule.RepositoryKey)) { return(CSharpPropertyPrefix); } if (VBNetRepositoryKey.Equals(rule.RepositoryKey)) { return(VBNetPropertyPrefix); } // For plugins created by the SonarQube Roslyn SDK, the repository key is in the form // "roslyn.MyCustomAnalyzer", and the Sonar properties added by the custom plugin will // start with "MyCustomAnalyzer." e.g. // * MyCustomAnalyzer.analyzerId // * MyCustomAnalyzer.pluginVersion if (rule.RepositoryKey.StartsWith(ROSLYN_REPOSITORY_PREFIX)) { var propertyPrefix = rule.RepositoryKey.Substring(ROSLYN_REPOSITORY_PREFIX.Length); return(string.IsNullOrEmpty(propertyPrefix) ? null : propertyPrefix); } return(null); // not a Roslyn-based rule }
private Rule CreateRuleElement(SonarQubeRule sonarRule) { var actionText = (sonarRule.IsActive) ? GetActionText(GetVsSeverity(sonarRule.Severity)) : inactiveRuleActionText; return(new Rule(sonarRule.Key, actionText)); }
internal static /* for testing */ bool IsSupportedRule(SonarQubeRule rule) { // We don't want to generate configuration for taint-analysis rules or hotspots. // * taint-analysis rules: these are in a separate analyzer that doesn't ship in SLVS so there is no point in generating config // * hotspots: these are noisy so we don't want to run them in the IDE. There is special code in the Sonar hotspot analyzers to // control when they run; we are responsible for not generating configuration for them. return(IsSupportedIssueType(rule.IssueType) && !IsTaintAnalysisRule(rule)); }
public void Get_NoSonarProperties_NoPackageInfo() { var testSubject = new NuGetPackageInfoGenerator(); var rules = new SonarQubeRule[] { CreateRule("csharpsquid", "rule1"), CreateRule("csharpsquid", "rule2") }; var actual = testSubject.GetNuGetPackageInfos(rules, new Dictionary <string, string>()); actual.Should().BeEmpty(); }
private static string GetPartialRepoKey(SonarQubeRule rule, string language) { if (rule.RepositoryKey.StartsWith(RoslynRepositoryPrefix)) { return(rule.RepositoryKey.Substring(RoslynRepositoryPrefix.Length)); } else if ("csharpsquid".Equals(rule.RepositoryKey) || "vbnet".Equals(rule.RepositoryKey)) { return(string.Format(SonarAnalyzerRepositoryPrefix, language)); } else { return(null); } }
public void Get_MultiplePlugins_WithPropertiesAndMatchingRules_ExpectedPackageReturned() { var testSubject = new NuGetPackageInfoGenerator(); var properties = new Dictionary <string, string> { // Valid CSharp properties { "sonaranalyzer-cs.analyzerId", "analyzer.csharp" }, { "sonaranalyzer-cs.pluginVersion", "version.csharp" }, // Valid VBNet properties { "sonaranalyzer-vbnet.analyzerId", "analyzer.vb" }, { "sonaranalyzer-vbnet.pluginVersion", "version.vb" }, // First valid third-party analyzer properties { "myanalyzer1.analyzerId", "analyzer.myanalyzer1" }, { "myanalyzer1.pluginVersion", "version.myanalyzer1" }, // Second valid third-party analyzer properties { "wintellect.analyzerId", "analyzer.wintellect" }, { "wintellect.pluginVersion", "version.wintellect" }, // Invalid third-party analyzer properties - missing version { "invalid.analyzerId", "analyzer.invalid" } }; var rules = new SonarQubeRule[] { CreateRule("csharpsquid", "rule1"), CreateRule("vbnet", "rule2"), CreateRule("roslyn.myanalyzer1", "rule3"), // no wintellect rule, so it should not be invluded CreateRule("roslyn.invalid", "rule3"), }; var actual = testSubject.GetNuGetPackageInfos(rules, properties); actual.Count().Should().Be(3); var packages = actual.ToArray(); packages[0].Id.Should().Be("analyzer.csharp"); packages[0].Version.Should().Be("version.csharp"); packages[1].Id.Should().Be("analyzer.vb"); packages[1].Version.Should().Be("version.vb"); packages[2].Id.Should().Be("analyzer.myanalyzer1"); packages[2].Version.Should().Be("version.myanalyzer1"); }
private static SonarLintRule ToSonarLintRule(SonarQubeRule sqRule) { List <SonarLintKeyValuePair> slvsParameters = null; if (sqRule.Parameters != null && sqRule.Parameters.Count > 0) { slvsParameters = sqRule.Parameters .Select(ToSonarLintKeyValue) .OrderBy(p => p.Key) .ToList(); } return(new SonarLintRule() { Key = sqRule.Key, Parameters = slvsParameters }); }
private static RuleConfig ToRuleConfig(SonarQubeRule sonarQubeRule) { // Most rules don't have parameters, so to avoid creating objects unnecessarily // we'll leave the parameters as null unless there really are values. Dictionary <string, string> parameters = null; if ((sonarQubeRule.Parameters?.Count ?? 0) != 0) { parameters = sonarQubeRule.Parameters.ToDictionary(p => p.Key, p => p.Value); } var config = new RuleConfig() { Level = sonarQubeRule.IsActive ? RuleLevel.On : RuleLevel.Off, Parameters = parameters, Severity = Convert(sonarQubeRule.Severity) }; return(config); }
public void Get_SonarAnalyzer_WithPropertiesAndMatchingRules_ExpectedPackageReturned(string language, string repositoryKey) { var testSubject = new NuGetPackageInfoGenerator(); var properties = new Dictionary <string, string> { { $"sonaranalyzer-{language}.analyzerId", "AAA" }, { $"sonaranalyzer-{language}.pluginVersion", "1.2" } }; var rules = new SonarQubeRule[] { CreateRule(repositoryKey, "rule1") }; var actual = testSubject.GetNuGetPackageInfos(rules, properties); actual.Count().Should().Be(1); actual.First().Id.Should().Be("AAA"); actual.First().Version.Should().Be("1.2"); }
private static string ToRuleConfigKey(SonarQubeRule sonarQubeRule) => $"{sonarQubeRule.RepositoryKey}:{sonarQubeRule.Key}";
[DataRow("vbnet", "sonaranalyzer-vbnet")] // special case for SonarVBNet public void TryGetPrefix(string ruleKey, string expectedPrefix) { var rule = new SonarQubeRule("any", ruleKey, false, SonarQubeIssueSeverity.Unknown, null, SonarQubeIssueType.Unknown); rule.TryGetRoslynPluginPropertyPrefix().Should().Be(expectedPrefix); }
private static bool HasParameters(SonarQubeRule sqRule) => sqRule.Parameters.Count > 0;
private static bool IsTaintAnalysisRule(SonarQubeRule rule) => rule.RepositoryKey.StartsWith(TaintAnalyisRepoPrefix, StringComparison.OrdinalIgnoreCase);
public void IsSupportedRule_Severity(SonarQubeIssueType issueType, bool expected) { var rule = new SonarQubeRule("any", "any", true, SonarQubeIssueSeverity.Blocker, null, issueType); CSharpVBBindingConfigProvider.IsSupportedRule(rule).Should().Be(expected); }
public async Task GetConfig_HasActiveInactiveAndUnsupportedRules_ReturnsValidBindingConfig() { // Arrange const string expectedProjectName = "my project"; const string expectedServerUrl = "http://myhost:123/"; var properties = new SonarQubeProperty[] { new SonarQubeProperty("propertyAAA", "111"), new SonarQubeProperty("propertyBBB", "222") }; var activeRules = new SonarQubeRule[] { CreateRule("activeRuleKey", "repoKey1", true), ActiveTaintAnalysisRule, ActiveRuleWithUnsupportedSeverity }; var inactiveRules = new SonarQubeRule[] { CreateRule("inactiveRuleKey", "repoKey2", false), InactiveTaintAnalysisRule, InactiveRuleWithUnsupportedSeverity }; var builder = new TestEnvironmentBuilder(validQualityProfile, Language.CSharp, expectedProjectName, expectedServerUrl) { ActiveRulesResponse = activeRules, InactiveRulesResponse = inactiveRules, PropertiesResponse = properties, NuGetBindingOperationResponse = true, RuleSetGeneratorResponse = validRuleSet }; var testSubject = builder.CreateTestSubject(); // Act var result = await testSubject.GetConfigurationAsync(validQualityProfile, Language.CSharp, builder.BindingConfiguration, CancellationToken.None) .ConfigureAwait(false); // Assert result.Should().NotBeNull(); result.Should().BeOfType <CSharpVBBindingConfig>(); var dotNetResult = (CSharpVBBindingConfig)result; dotNetResult.RuleSet.Should().NotBeNull(); dotNetResult.RuleSet.Content.ToolsVersion.Should().Be(validRuleSet.ToolsVersion); var expectedName = string.Format(Strings.SonarQubeRuleSetNameFormat, expectedProjectName, validQualityProfile.Name); dotNetResult.RuleSet.Content.Name.Should().Be(expectedName); var expectedDescription = $"{OriginalValidRuleSetDescription} {string.Format(Strings.SonarQubeQualityProfilePageUrlFormat, expectedServerUrl, validQualityProfile.Key)}"; dotNetResult.RuleSet.Content.Description.Should().Be(expectedDescription); // Check properties passed to the ruleset generator builder.CapturedPropertiesPassedToRuleSetGenerator.Should().NotBeNull(); var capturedProperties = builder.CapturedPropertiesPassedToRuleSetGenerator.ToList(); capturedProperties.Count.Should().Be(2); capturedProperties[0].Key.Should().Be("propertyAAA"); capturedProperties[0].Value.Should().Be("111"); capturedProperties[1].Key.Should().Be("propertyBBB"); capturedProperties[1].Value.Should().Be("222"); // Check both active and inactive rules were passed to the ruleset generator. // The unsupported rules should have been removed. builder.CapturedRulesPassedToRuleSetGenerator.Should().NotBeNull(); var capturedRules = builder.CapturedRulesPassedToRuleSetGenerator.ToList(); capturedRules.Count.Should().Be(2); capturedRules[0].Key.Should().Be("activeRuleKey"); capturedRules[0].RepositoryKey.Should().Be("repoKey1"); capturedRules[1].Key.Should().Be("inactiveRuleKey"); capturedRules[1].RepositoryKey.Should().Be("repoKey2"); builder.AssertNuGetBindingOpWasCalled(); builder.Logger.AssertOutputStrings(0); // not expecting anything in the case of success }