Data-only class that returns conflict information between the SonarQube RuleSet and the configured RuleSet
Exemple #1
0
        private IReadOnlyList <ProjectRuleSetConflict> FindConflicts(RuleSetInformation[] aggregatedRuleSet)
        {
            List <ProjectRuleSetConflict> conflicts = new List <ProjectRuleSetConflict>();

            IRuleSetInspector inspector = this.serviceProvider.GetService <IRuleSetInspector>();

            inspector.AssertLocalServiceIsNotNull();

            // At the moment single threaded, if needed this could be done in parallel
            foreach (RuleSetInformation aggregate in aggregatedRuleSet)
            {
                try
                {
                    RuleConflictInfo conflict = inspector.FindConflictingRules(aggregate.BaselineFilePath, aggregate.RuleSetFilePath, aggregate.RuleSetDirectories);
                    Debug.Assert(conflict != null);

                    if (conflict?.HasConflicts ?? false)
                    {
                        conflicts.Add(new ProjectRuleSetConflict(conflict, aggregate));
                    }
                }
                catch (Exception ex)
                {
                    if (ErrorHandler.IsCriticalException(ex))
                    {
                        throw;
                    }

                    this.WriteWarning(Strings.ConflictsManagerFailedInFindConflicts, aggregate.RuleSetFilePath, aggregate.BaselineFilePath, ex.Message);
                    Debug.Fail("Failed to resolve conflict for " + aggregate.RuleSetFilePath, ex.ToString());
                }
            }

            return(conflicts);
        }
Exemple #2
0
        /// <summary>
        /// <see cref="IRuleSetInspector.FindConflictingRules(string, string)"/>
        /// </summary>
        public FixedRuleSetInfo FixConflictingRules(string baselineRuleSetPath, string targetRuleSetPath, params string[] ruleSetDirectories)
        {
            if (string.IsNullOrWhiteSpace(baselineRuleSetPath))
            {
                throw new ArgumentNullException(nameof(baselineRuleSetPath));
            }

            if (string.IsNullOrWhiteSpace(targetRuleSetPath))
            {
                throw new ArgumentNullException(nameof(targetRuleSetPath));
            }


            RuleSet baseline = RuleSet.LoadFromFile(baselineRuleSetPath);
            RuleSet target   = RuleSet.LoadFromFile(targetRuleSetPath);

            RuleConflictInfo     conflicts    = this.FindConflictsCore(baseline, target, ruleSetDirectories);
            List <RuleReference> deletedRules = null;

            string[] includeReset = null;
            if (conflicts.HasConflicts)
            {
                includeReset = new[] { baseline.FilePath };

                if (!this.TryResolveIncludeConflicts(baseline, target))
                {
                    deletedRules = this.DeleteConflictingRules(baseline, target);
                }
            }

            return(new FixedRuleSetInfo(target, includeReset, deletedRules?.Select(r => r.FullId)));
        }
Exemple #3
0
        /// <summary>
        /// Attempts to fix the conflicts by ensuring that the server ruleset is included with the expected Include Action
        /// </summary>
        /// <returns>Whether all conflicts were resolved</returns>
        private bool TryResolveIncludeConflicts(RuleSet baselineRuleSet, RuleSet targetRuleSet)
        {
            Debug.Assert(baselineRuleSet != null);
            Debug.Assert(targetRuleSet != null);
            Debug.Assert(!string.IsNullOrWhiteSpace(baselineRuleSet.FilePath));
            Debug.Assert(!string.IsNullOrWhiteSpace(targetRuleSet.FilePath));

            RuleSetHelper.UpdateExistingProjectRuleSet(targetRuleSet, baselineRuleSet.FilePath);
            RuleConflictInfo conflicts1stAttempt = this.FindConflictsCore(baselineRuleSet, targetRuleSet);

            return(!conflicts1stAttempt.HasConflicts);
        }
Exemple #4
0
        public ProjectRuleSetConflict(RuleConflictInfo conflict, RuleSetInformation aggregate)
        {
            if (conflict == null)
            {
                throw new ArgumentNullException(nameof(conflict));
            }

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }


            this.Conflict    = conflict;
            this.RuleSetInfo = aggregate;
        }
        public ProjectRuleSetConflict(RuleConflictInfo conflict, RuleSetInformation aggregate)
        {
            if (conflict == null)
            {
                throw new ArgumentNullException(nameof(conflict));
            }

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }


            this.Conflict = conflict;
            this.RuleSetInfo = aggregate;
        }
 private static void AssertNoWeakRules(RuleConflictInfo info)
 {
     Assert.AreEqual(0, info.WeakerActionRules.Count, "Actually weak: {0}", string.Join(", ", info.WeakerActionRules.Keys.Select(r => r.FullId)));
 }
 private static void AssertNoMissingRules(RuleConflictInfo info)
 {
     Assert.AreEqual(0, info.MissingRules.Count, "Actually missing: {0}", string.Join(", ", info.MissingRules.Select(r => r.FullId)));
 }
 private static void AssertNoConflicts(RuleConflictInfo info)
 {
     AssertNoMissingRules(info);
     AssertNoWeakRules(info);
     Assert.IsFalse(info.HasConflicts, "Not expecting conflicts");
 }
 private static void AssertMissingRulesByFullIds(RuleConflictInfo info, IEnumerable<RuleReference> expectedRules)
 {
     string[] expectedFullRuleIds = expectedRules.Select(r => r.FullId).ToArray();
     var actualFullIds = info.MissingRules.Select(r => r.FullId).ToArray();
     CollectionAssert.AreEquivalent(expectedFullRuleIds, actualFullIds, "Actually missing: {0}", string.Join(", ", actualFullIds));
     Assert.IsTrue(info.HasConflicts, "Expected missing rules");
 }
        private static void AssertWeakRulesByFullIds(RuleConflictInfo info, IEnumerable<RuleReference> expectedRules, RuleSet baseline)
        {
            var expectedFullRuleIds = new HashSet<string>(expectedRules.Select(r => r.FullId));
            List<string> found = new List<string>();
            foreach(var keyValue in info.WeakerActionRules)
            {
                string ruleFullId = keyValue.Key.FullId;
                found.Add(ruleFullId);

                Assert.IsTrue(expectedFullRuleIds.Contains(ruleFullId), "Unexpected weakened rule");
                RuleReference baselineRule;
                Assert.IsTrue(baseline.Rules.TryGetRule(ruleFullId, out baselineRule), "Test setup error: baseline doesn't contain the rule {0}", ruleFullId);
                Assert.AreEqual(baselineRule.Action, keyValue.Value, "Unexpected Action. Expecting the baseline rule action to be returned part of RuleConflictInfo");
            }

            Assert.AreEqual(expectedFullRuleIds.Count, info.WeakerActionRules.Count, "Not all the expected weakened rule were found. Missing: {0}", string.Join(", ", expectedFullRuleIds.Except(found)));
            Assert.IsTrue(info.HasConflicts, "Expected weakened rules");
        }