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;
        }
        public void ProjectRuleSetConflict_ArgChecks()
        {
            var info     = new RuleSetInformation("projectFullName", "baselineRuleSet", "projectRuleSet", new string[0]);
            var conflict = new RuleConflictInfo();

            Exceptions.Expect <ArgumentNullException>(() => new ProjectRuleSetConflict(null, info));
            Exceptions.Expect <ArgumentNullException>(() => new ProjectRuleSetConflict(conflict, null));

            new ProjectRuleSetConflict(conflict, info).Should().NotBeNull("Not expecting this to fail, just to make the static analyzer happy");
        }
Example #3
0
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RelativePathRuleSetIsFound_ButNotUnderTheSProject()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            const string ruleSetName = "Happy";

            const string projectFullPath            = @"X:\SolutionDir\ProjectDir\My Project.proj";
            const string solutionRuleSetPath        = @"X:\SolutionDir\RuleSets\sonar1.ruleset";
            const string existingProjectRuleSetPath = @"x:\SolutionDir\myexistingproject.ruleset";

            this.ruleSetFS.RegisterRuleSet(new RuleSet("NotOurRuleSet")
            {
                FilePath = existingProjectRuleSetPath
            });
            this.ruleSetFS.RegisterRuleSet(new RuleSet("SolutionRuleSet")
            {
                FilePath = solutionRuleSetPath
            });

            string relativePathToExistingProjectRuleSet = PathHelper.CalculateRelativePath(existingProjectRuleSetPath, projectFullPath);

            RuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                      (
                numRules: 0,
                includes: new[]
            {
                relativePathToExistingProjectRuleSet,     // The project exists, but not ours so we should keep it as it was previously specified
                PathHelper.CalculateRelativePath(projectFullPath, solutionRuleSetPath)
            }
                                      );

            var ruleSetInfo = new RuleSetInformation(Language.CSharp, expectedRuleSet)
            {
                NewRuleSetFilePath = solutionRuleSetPath
            };

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetName, ruleSetInfo, relativePathToExistingProjectRuleSet);

            // Assert
            this.ruleSetFS.AssertRuleSetNotExists(actualPath);
            actualPath.Should().NotBe(existingProjectRuleSetPath, "Expecting a new rule set to be created once written pending");

            // Act (write pending)
            this.sccFileSystem.WritePendingNoErrorsExpected();

            // Assert
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
        }
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RelativePathRuleSetIsFound_UnderTheProject()
        {
            // Setup
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            const string ruleSetName                = "Happy";
            const string projectFullPath            = @"X:\SolutionDir\ProjectDir\My Project.proj";
            const string solutionRuleSetPath        = @"X:\SolutionDir\RuleSets\sonar1.ruleset";
            const string existingProjectRuleSetPath = @"X:\SolutionDir\ProjectDir\ExistingRuleSet.ruleset";

            RuleSet existingRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                      (
                numRules: 0,
                includes: new[] { PathHelper.CalculateRelativePath(projectFullPath, solutionRuleSetPath) }
                                      );

            existingRuleSet.FilePath = existingProjectRuleSetPath;

            this.ruleSetFS.RegisterRuleSet(existingRuleSet);
            this.ruleSetFS.RegisterRuleSet(new RuleSet("SolutionRuleSet")
            {
                FilePath = solutionRuleSetPath
            });


            string newSolutionRuleSetPath    = Path.Combine(Path.GetDirectoryName(solutionRuleSetPath), "sonar2.ruleset");
            string newSolutionRuleSetInclude = PathHelper.CalculateRelativePath(projectFullPath, newSolutionRuleSetPath);

            RuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                      (
                numRules: 0,
                includes: new[] { newSolutionRuleSetInclude }
                                      );

            var ruleSetInfo = new RuleSetInformation(Language.CSharp, expectedRuleSet)
            {
                NewRuleSetFilePath = newSolutionRuleSetPath
            };

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetName, ruleSetInfo, PathHelper.CalculateRelativePath(projectFullPath, existingProjectRuleSetPath));

            // Verify
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
            Assert.AreEqual(existingProjectRuleSetPath, actualPath, "Expecting the rule set to be updated");
        }
Example #5
0
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_NewBinding()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            const string ruleSetFileName     = "Happy";
            const string projectFullPath     = @"X:\SolutionDir\ProjectDir\My Project.proj";
            const string solutionRuleSetPath = @"X:\SolutionDir\RuleSets\sonar1.ruleset";

            string  expectedSolutionRuleSetInclude = PathHelper.CalculateRelativePath(projectFullPath, solutionRuleSetPath);
            RuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                      (
                numRules: 0,
                includes: new[] { expectedSolutionRuleSetInclude }
                                      );

            var ruleSetInfo = new RuleSetInformation(Language.CSharp, expectedRuleSet)
            {
                NewRuleSetFilePath = solutionRuleSetPath
            };

            List <string> filesPending = new List <string>();

            foreach (var currentRuleSet in new[] { null, string.Empty, ProjectBindingOperation.DefaultProjectRuleSet })
            {
                // Act
                string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetFileName, ruleSetInfo, currentRuleSet);
                filesPending.Add(actualPath);

                // Assert
                this.ruleSetFS.AssertRuleSetNotExists(actualPath);
                actualPath.Should().NotBe(solutionRuleSetPath, "Expecting a new rule set to be created once pending were written");
            }

            // Act (write pending)
            this.sccFileSystem.WritePendingNoErrorsExpected();

            // Assert
            foreach (var pending in filesPending)
            {
                // Assert
                this.ruleSetFS.AssertRuleSetsAreEqual(pending, expectedRuleSet);
            }
        }
Example #6
0
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RuleSetIsNotFound()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            const string projectName     = "My Project";
            const string ruleSetFileName = "Happy";

            const string solutionRoot              = @"X:\SolutionDir";
            string       projectRoot               = Path.Combine(solutionRoot, "ProjectDir");
            string       projectFullPath           = Path.Combine(projectRoot, $"{projectName}.proj");
            string       currentNonExistingRuleSet = "my-non-existingproject.ruleset";

            string newSolutionRuleSetPath    = Path.Combine(solutionRoot, "RuleSets", "sonar2.ruleset");
            string newSolutionRuleSetInclude = PathHelper.CalculateRelativePath(projectFullPath, newSolutionRuleSetPath);

            RuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                      (
                numRules: 0,
                includes: new[] { currentNonExistingRuleSet, newSolutionRuleSetInclude }
                                      );

            var ruleSetInfo = new RuleSetInformation(Language.CSharp, expectedRuleSet)
            {
                NewRuleSetFilePath = newSolutionRuleSetPath
            };

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetFileName, ruleSetInfo, currentNonExistingRuleSet);

            // Assert
            this.ruleSetFS.AssertRuleSetNotExists(actualPath);
            actualPath.Should().NotBe(currentNonExistingRuleSet, "Expecting a new rule set to be created once written pending");

            // Act (write pending)
            this.sccFileSystem.WritePendingNoErrorsExpected();

            // Assert
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
        }
        private void AddOrUpdateAggregatedRuleSetInformation(Dictionary<string, RuleSetInformation> projectRuleSetAggregation, string baselineRuleSet, RuleSetDeclaration declaration, string projectRuleSet)
        {
            RuleSetInformation aggregate;
            if (projectRuleSetAggregation.TryGetValue(projectRuleSet, out aggregate))
            {
                aggregate.ConfigurationContexts.Add(declaration.ConfigurationContext);

                if (!aggregate.RuleSetDirectories.SequenceEqual(declaration.RuleSetDirectories))
                {
                    this.WriteWarning(Strings.InconsistentRuleSetDirectoriesWarning,
                                    CombineDirectories(declaration.RuleSetDirectories),
                                    projectRuleSet,
                                    CombineDirectories(aggregate.RuleSetDirectories));
                }
            }
            else
            {
                aggregate = new RuleSetInformation(declaration.RuleSetProjectFullName, baselineRuleSet, projectRuleSet, declaration.RuleSetDirectories);
                aggregate.ConfigurationContexts.Add(declaration.ConfigurationContext);
                projectRuleSetAggregation[projectRuleSet] = aggregate;
            }
        }