public void Save_InvalidArgs()
        {
            var testSubject = new DotNetBindingConfigFile(new RuleSet("dummy"));

            // 1. Null -> throw
            Action act = () => testSubject.Save(null);

            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("fullFilePath");

            // 2. Empty -> throw
            act = () => testSubject.Save("");
            act.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("fullFilePath");
        }
        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 ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RelativePathRuleSetIsFound_UnderTheProject()
        {
            // 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\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 dotNetRuleSet = new DotNetBindingConfigFile(expectedRuleSet);

            var ruleSetInfo = new ConfigFileInformation(dotNetRuleSet)
            {
                NewFilePath = newSolutionRuleSetPath
            };

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

            // Assert
            this.ruleSetFS.AssertRuleSetsAreEqual(actualPath, expectedRuleSet);
            actualPath.Should().Be(existingProjectRuleSetPath, "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 Save_ValidFilePath_SaveCalled()
        {
            // We can't mock the RuleSet class so we're testing Save by actually
            // writing to disk.
            // Arrange
            var testDir = Path.Combine(TestContext.DeploymentDirectory, TestContext.TestName);

            Directory.CreateDirectory(testDir);
            var fullPath = Path.Combine(testDir, "savedRuleSet.txt");

            var testSubject = new DotNetBindingConfigFile(new RuleSet("dummy"));

            // Act
            testSubject.Save(fullPath);

            // Assert
            File.Exists(fullPath).Should().BeTrue();
        }
        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);
        }