public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_RuleSetSaved(bool doesAlreadyExist)
        {
            // Arrange
            CSharpVBBindingOperation testSubject = CreateTestSubject();

            string solutionRuleSetPath      = @"X:\SolutionDir\Sonar\Sonar1.ruleset";
            string projectRuleSetRoot       = @"X:\SolutionDir\Project\";
            string existingRuleSetFullPath  = @"X:\SolutionDir\Project\ExistingSharedRuleSet.ruleset";
            string existingRuleSetPropValue = PathHelper.CalculateRelativePath(projectRuleSetRoot, existingRuleSetFullPath);

            var existingRuleSet = TestRuleSetHelper.CreateTestRuleSet(existingRuleSetFullPath);

            if (doesAlreadyExist)
            {
                testSubject.AlreadyUpdatedExistingRuleSetPaths.Add(existingRuleSet.FilePath, existingRuleSet);
            }
            this.ruleSetFS.RegisterRuleSet(existingRuleSet);

            // Act
            string    pathOutResult;
            VsRuleSet rsOutput;
            bool      result = testSubject.TryUpdateExistingProjectRuleSet(solutionRuleSetPath, projectRuleSetRoot, existingRuleSetPropValue, out pathOutResult, out rsOutput);

            // Assert
            result.Should().BeTrue("Expected to return true when trying to update existing rule set");
            rsOutput.Should().Be(existingRuleSet, "Same RuleSet instance is expected");
            pathOutResult.Should().Be(existingRuleSetFullPath, "Unexpected rule set path was returned");
        }
Exemple #2
0
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_RuleSetNotAlreadyWritten_WritesFile()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            string solutionRuleSetPath     = @"X:\SolutionDir\Sonar\Sonar1.ruleset";
            string projectRuleSetRoot      = @"X:\SolutionDir\Project\";
            string existingRuleSetFullPath = @"X:\SolutionDir\Project\ExistingSharedRuleSet.ruleset";

            string existingRuleSetPropValue = PathHelper.CalculateRelativePath(projectRuleSetRoot, existingRuleSetFullPath);

            var existingRuleSet = TestRuleSetHelper.CreateTestRuleSet(existingRuleSetFullPath);

            this.ruleSetFS.RegisterRuleSet(existingRuleSet, existingRuleSetFullPath);
            long beforeTimestamp = this.sccFileSystem.GetFileTimestamp(existingRuleSetFullPath);

            // Act
            string  pathOutResult;
            RuleSet rsOutput;
            bool    result = testSubject.TryUpdateExistingProjectRuleSet(solutionRuleSetPath, projectRuleSetRoot, existingRuleSetPropValue, out pathOutResult, out rsOutput);

            // Assert
            result.Should().BeTrue("Expected to return true when trying to update existing rule set");
            rsOutput.Should().Be(existingRuleSet, "Same RuleSet instance expected");
            pathOutResult.Should().Be(existingRuleSetFullPath, "Unexpected rule set path was returned");
            this.sccFileSystem.AssertFileTimestamp(existingRuleSetFullPath, beforeTimestamp);
        }
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_NewBinding()
        {
            // Arrange
            CSharpVBBindingOperation 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);
            VsRuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                        (
                numRules: 0,
                includes: new[] { expectedSolutionRuleSetInclude, "MyCustomRuleSet.ruleset" }
                                        );
            var csharpVbConfig = CreateCSharpVbBindingConfig(solutionRuleSetPath, ValidCoreRuleSet);

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetFileName, csharpVbConfig, "MyCustomRuleSet.ruleset");

            // 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
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
        }
        public void IsReferencedByAllDeclarations_NoIncludes()
        {
            // Arrange
            var unreferencedRuleSet = TestRuleSetHelper.CreateTestRuleSet(@"c:\unreferenced.ruleset");

            // Act No includes at all
            var project      = CreateMockProjectWithRuleSet(unreferencedRuleSet);
            var isReferenced = testSubject.IsReferencedByAllDeclarations(project, solutionRuleSetFilePath);

            // Assert
            isReferenced.Should().BeFalse();
        }
        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 dotNetRuleSet = new DotNetBindingConfigFile(expectedRuleSet);

            var ruleSetInfo = new ConfigFileInformation(dotNetRuleSet)
            {
                NewFilePath = 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 IsReferencedByAllDeclarations_SourceIsTarget_ReturnsTrue()
        {
            // Covers the case where the ruleset is included directly in the project, rather
            // than indirectly as a RuleSetInclude in another ruleset.

            // Arrange
            var targetRuleSetFilePath = @"c:\Solution\SomeFolder\fullFilePath.ruleset";
            var projectRuleSet        = TestRuleSetHelper.CreateTestRuleSet(@"C:/SOLUTION\./SomeFolder\fullFilePath.ruleset");

            // Act
            var project      = CreateMockProjectWithRuleSet(projectRuleSet);
            var isReferenced = testSubject.IsReferencedByAllDeclarations(project, targetRuleSetFilePath);

            // Assert
            isReferenced.Should().BeTrue();
        }
        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");
        }
        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 dotNetRuleSet = new DotNetBindingConfigFile(expectedRuleSet);

            var ruleSetInfo = new ConfigFileInformation(dotNetRuleSet)
            {
                NewFilePath = 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);
            }
        }
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_AbsolutePathRuleSetIsFound_UnderTheProject()
        {
            // Arrange
            CSharpVBBindingOperation 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";

            VsRuleSet existingRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                        (
                numRules: 0,
                includes: new[] { solutionRuleSetPath }
                                        );

            existingRuleSet.FilePath = existingProjectRuleSetPath;

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

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

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

            var csharpVbConfig = CreateCSharpVbBindingConfig(newSolutionRuleSetPath, ValidCoreRuleSet);

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetName, csharpVbConfig, existingProjectRuleSetPath);

            // Assert
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
            actualPath.Should().Be(existingProjectRuleSetPath, "Expecting the rule set to be updated");
        }
        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 dotNetRuleSet = new DotNetBindingConfigFile(expectedRuleSet);

            var ruleSetInfo = new ConfigFileInformation(dotNetRuleSet)
            {
                NewFilePath = 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);
        }
        public void TestInitialize()
        {
            solutionRuleSetsInformationProviderMock = new Mock <ISolutionRuleSetsInformationProvider>();
            ruleSetSerializerMock = new Mock <IRuleSetSerializer>();

            var serviceProviderMock = new Mock <IServiceProvider>();

            serviceProviderMock
            .Setup(x => x.GetService(typeof(ISolutionRuleSetsInformationProvider)))
            .Returns(solutionRuleSetsInformationProviderMock.Object);

            serviceProviderMock
            .Setup(x => x.GetService(typeof(IRuleSetSerializer)))
            .Returns(ruleSetSerializerMock.Object);

            testSubject = new RuleSetReferenceChecker(serviceProviderMock.Object, Mock.Of <ILogger>());

            solutionRuleSetFilePath = @"c:\aaa\Solution\SomeFolder\fullFilePath.ruleset";
            projectRuleSetThatDoesNotIncludeSolutionRuleSet = TestRuleSetHelper.CreateTestRuleSet(@"c:\foo\dummy.ruleset");;

            var relativeInclude = @"Solution\SomeFolder\fullFilePath.ruleset".ToLowerInvariant(); // Catch casing errors

            projectRuleSetThatIncludesSolutionRuleSet = TestRuleSetHelper.CreateTestRuleSetWithIncludes(@"c:\aaa\fullFilePath.ruleset", relativeInclude, "otherInclude.ruleset");
        }