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");
        }
        public void ProjectBindingOperation_Prepare_SameDefaultRuleSetsInProject()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock firstRuleSet  = CreateRuleSetProperty(this.projectMock, "config1", "MyCustomRuleSet.ruleset");
            PropertyMock secondRuleSet = CreateRuleSetProperty(this.projectMock, "config2", "MyCustomRuleSet.ruleset");

            testSubject.Initialize();

            // Act
            testSubject.Prepare(CancellationToken.None);


            // Assert
            string expectedRuleSetFileForPropertiesWithDefaultRuleSets = Path.Combine(Path.GetDirectoryName(this.projectMock.FilePath), Path.GetFileNameWithoutExtension(this.projectMock.FilePath) + ".ruleset");

            fileSystem.GetFile(expectedRuleSetFileForPropertiesWithDefaultRuleSets).Should().Be(null);
            testSubject.PropertyInformationMap[firstRuleSet].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRuleSets, "Expected different rule set path for properties with custom rulesets");
            testSubject.PropertyInformationMap[secondRuleSet].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRuleSets, "Expected different rule set path for properties with custom rulesets");

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

            // Assert that written
            fileSystem.GetFile(expectedRuleSetFileForPropertiesWithDefaultRuleSets).Should().NotBe(null);
        }
        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 ProjectBindingOperation_Prepare_Cancellation()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            PropertyMock prop = CreateRuleSetProperty(this.projectMock, "config1", CSharpVBBindingOperation.DefaultProjectRuleSet);

            testSubject.Initialize();
            using (CancellationTokenSource src = new CancellationTokenSource())
            {
                CancellationToken token = src.Token;
                src.Cancel();

                // Act
                testSubject.Prepare(token);
            }

            // Assert
            string expectedFile = Path.Combine(Path.GetDirectoryName(this.projectMock.FilePath), Path.GetFileNameWithoutExtension(this.projectMock.FilePath) + ".ruleset");

            testSubject.PropertyInformationMap[prop].NewRuleSetFilePath.Should().BeNull("Not expecting the new rule set path to be set when canceled");
            prop.Value.ToString().Should().Be(CSharpVBBindingOperation.DefaultProjectRuleSet, "Should not update the property value");
            this.projectMock.Files.ContainsKey(expectedFile).Should().BeFalse("Should not be added to the project");
        }
        public void ProjectBindingOperation_Commit_LegacyProjectSystem_DoesAddFile()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            PropertyMock prop = CreateRuleSetProperty(this.projectMock, "config1", "MyCustomRuleSet.ruleset");

            testSubject.Initialize();
            testSubject.Prepare(CancellationToken.None);

            this.projectSystemHelper.SetIsLegacyProjectSystem(true);

            // Act
            using (new AssertIgnoreScope()) // Ignore that the file is not on disk
            {
                testSubject.Commit();
            }

            // Assert
            string projectFile = Path.Combine(Path.GetDirectoryName(this.projectMock.FilePath), Path.GetFileNameWithoutExtension(this.projectMock.FilePath) + ".ruleset");

            prop.Value.ToString().Should().Be(Path.GetFileName(projectFile), "Should update the property value");
            this.projectMock.Files.ContainsKey(projectFile).Should().BeTrue("Should add the file to the project for the legacy project system");
        }
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_ExistingRuleSetIsNotAtTheProjectLevel()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

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

            string[] cases = new[]
            {
                "../relativeSolutionLevel.ruleset",
                @"..\..\relativeSolutionLevel.ruleset",
                @"X:\SolutionDir\Sonar\absolutionSolutionRooted.ruleset",
                @"c:\OtherPlaceEntirey\rules.ruleset",
                "MyCustomRuleSet.ruleset"
            };

            foreach (var currentRuleSet in cases)
            {
                // Act
                string    pathOutResult;
                VsRuleSet rsOutput;
                bool      result = testSubject.TryUpdateExistingProjectRuleSet(solutionRuleSetPath, projectRuleSetRoot, currentRuleSet, out pathOutResult, out rsOutput);

                // Assert
                string testCase = currentRuleSet ?? "NULL";
                pathOutResult.Should().BeNull("Unexpected rule set path was returned: {0}. Case: {1}", pathOutResult, testCase);
                rsOutput.Should().BeNull("Unexpected rule set was returned. Case: {0}", testCase);
                result.Should().BeFalse("Not expecting to update a non project rooted rulesets. Case: {0}", testCase);
            }
        }
        public void ProjectBindingOperation_GenerateNewProjectRuleSetPath_NoAvailableFileNames_AppendsGuid()
        {
            // Arrange
            const string             fileName        = "fileTaken";
            const string             ruleSetRootPath = @"X:\";
            CSharpVBBindingOperation testSubject     = this.CreateTestSubject();

            string[] existingFiles = Enumerable.Range(0, 10).Select(i => $@"X:\{fileName}-{i}.ruleset").ToArray();

            fileSystem.AddFile($@"X:\{fileName}.ruleset", new MockFileData(""));

            foreach (var existingFile in existingFiles)
            {
                fileSystem.AddFile(existingFile, new MockFileData(""));
            }

            // Act
            string actual = testSubject.GenerateNewProjectRuleSetPath
                            (
                ruleSetRootPath,
                fileName
                            );

            // Assert
            string actualFileName = Path.GetFileNameWithoutExtension(actual);

            actualFileName.Should().HaveLength(fileName.Length + 32 + 1, "Expected to append GUID to desired file name, actual: " + actualFileName);
        }
        public void EnsureItemTypeIsCorrect_CriticalException_IsNotCaught()
        {
            var projectItemMock = new Mock <ProjectItem>();

            projectItemMock.Setup(x => x.Properties).Throws <AccessViolationException>();

            Action act = () => CSharpVBBindingOperation.EnsureItemTypeIsCorrect(projectItemMock.Object);

            act.Should().ThrowExactly <AccessViolationException>();
        }
        public void ProjectBindingOperation_ArgChecks()
        {
            var logger = new TestLogger();

            Exceptions.Expect <ArgumentNullException>(() => new CSharpVBBindingOperation(null, projectMock, cSharpVBBindingConfig, logger));
            Exceptions.Expect <ArgumentNullException>(() => new CSharpVBBindingOperation(serviceProvider, null, cSharpVBBindingConfig, logger));
            Exceptions.Expect <ArgumentNullException>(() => new CSharpVBBindingOperation(serviceProvider, projectMock, null, logger));

            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            testSubject.Should().NotBeNull("Suppress warning that not used");
        }
        public void AddAdditional_CriticalException_IsNotCaught()
        {
            var projectMock = Mock.Of <EnvDTE.Project>();

            var projectSystemMock = new Mock <IProjectSystemHelper>();

            projectSystemMock.Setup(x => x.AddFileToProject(projectMock, It.IsAny <string>(), It.IsAny <string>()))
            .Throws <StackOverflowException>();

            Action act = () => CSharpVBBindingOperation.AddAdditionalFileToProject(projectSystemMock.Object, projectMock, "any file");

            act.Should().ThrowExactly <StackOverflowException>();
        }
        public void ProjectBindingOperation_IsDefaultMicrosoftRuleSet()
        {
            // Test case 1: not ignored
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet("My awesome rule set.ruleset").Should().BeFalse();

            // Test case 2: ignored
            // Act
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet(null).Should().BeTrue();
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet(" ").Should().BeTrue();
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet("\t").Should().BeTrue();
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet(CSharpVBBindingOperation.DefaultProjectRuleSet.ToLower(CultureInfo.CurrentCulture)).Should().BeTrue();
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet(CSharpVBBindingOperation.DefaultProjectRuleSet.ToUpper(CultureInfo.CurrentCulture)).Should().BeTrue();
            CSharpVBBindingOperation.IsDefaultMicrosoftRuleSet(CSharpVBBindingOperation.DefaultProjectRuleSet).Should().BeTrue();
        }
        public void ProjectBindingOperation_GenerateNewProjectRuleSetPath_DesiredFileNameExists_AppendsNumberToFileName()
        {
            // General setup
            const string             ruleSetRootPath = @"X:\";
            const string             fileName        = "NameTaken";
            CSharpVBBindingOperation testSubject     = this.CreateTestSubject();

            // Test case 1: desired name exists
            // Arrange
            fileSystem.AddFile($"X:\\NameTaken.ruleset", new MockFileData(""));

            // Act
            string actual = testSubject.GenerateNewProjectRuleSetPath
                            (
                ruleSetRootPath,
                fileName
                            );

            // Assert
            actual.Should().Be($"X:\\{fileName}-1.ruleset", "Expected to append running number to desired file name, skipping the default one since exists");

            // Test case 2: desired name + 1 + 2 exists
            // Arrange
            fileSystem.AddFile(@"X:\NameTaken-1.ruleset", new MockFileData(""));
            fileSystem.AddFile(@"X:\NameTaken-2.ruleset", new MockFileData(""));

            // Act
            actual = testSubject.GenerateNewProjectRuleSetPath
                     (
                ruleSetRootPath,
                fileName
                     );

            // Assert
            actual.Should().Be(@"X:\NameTaken-3.ruleset", "Expected to append running number to desired file name");

            // Test case 3: has a pending write (not exists yet)
            ((ISourceControlledFileSystem)this.sccFileSystem).QueueFileWrite(@"X:\NameTaken-3.ruleset", () => true);

            // Act
            actual = testSubject.GenerateNewProjectRuleSetPath
                     (
                ruleSetRootPath,
                fileName
                     );

            // Assert
            actual.Should().Be(@"X:\NameTaken-4.ruleset", "Expected to append running number to desired file name, skipping 3 since pending write");
        }
        public void ProjectBindingOperation_GenerateNewProjectRuleSet()
        {
            // Arrange
            const string solutionIncludePath = @"..\..\solution.ruleset";
            const string currentRuleSetPath  = @"X:\MyOriginal.ruleset";
            var          expectedRuleSet     = new VsRuleSet(Constants.RuleSetName);

            expectedRuleSet.RuleSetIncludes.Add(new RuleSetInclude(solutionIncludePath, RuleAction.Default));
            expectedRuleSet.RuleSetIncludes.Add(new RuleSetInclude(currentRuleSetPath, RuleAction.Default));

            // Act
            VsRuleSet actualRuleSet = CSharpVBBindingOperation.GenerateNewProjectRuleSet(solutionIncludePath, currentRuleSetPath, Constants.RuleSetName);

            // Assert
            RuleSetAssert.AreEqual(expectedRuleSet, actualRuleSet);
        }
        public void EnsureItemTypeIsCorrect_NonCriticalException_IsCaughtAndWrapped()
        {
            var projectMock    = new ProjectMock("myproject.vbproj");
            var innerException = new ArgumentException("aaa bbb");

            var projectItemMock = new Mock <ProjectItem>();

            projectItemMock.Setup(x => x.ContainingProject).Returns(projectMock);
            projectItemMock.Setup(x => x.Properties).Throws(innerException);

            Action act = () => CSharpVBBindingOperation.EnsureItemTypeIsCorrect(projectItemMock.Object);

            act.Should().ThrowExactly <SonarLintException>()
            .Where(x => x.Message.Contains(projectMock.FilePath) &&
                   x.Message.Contains(innerException.Message))
            .And.InnerException.Should().BeSameAs(innerException);
        }
        public void AddAdditional_NonCriticalException_IsCaughtAndWrapped()
        {
            var projectMock    = new ProjectMock("any.proj");
            var innerException = new InvalidCastException("inner exception message");

            var projectSystemMock = new Mock <IProjectSystemHelper>();

            projectSystemMock.Setup(x => x.AddFileToProject(projectMock, It.IsAny <string>(), Constants.AdditionalFilesItemTypeName))
            .Throws(innerException);

            Action act = () => CSharpVBBindingOperation.AddAdditionalFileToProject(projectSystemMock.Object, projectMock, "anyFile.txt");

            act.Should().ThrowExactly <SonarLintException>()
            .Where(x => x.Message.Contains(projectMock.FilePath) &&
                   x.Message.Contains(innerException.Message))
            .And.InnerException.Should().BeSameAs(innerException);
        }
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RelativePathRuleSetIsFound_ButNotUnderTheSProject()
        {
            // 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\myexistingproject.ruleset";

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

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

            VsRuleSet 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 csharpVbConfig = CreateCSharpVbBindingConfig(solutionRuleSetPath, ValidCoreRuleSet);

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetName, csharpVbConfig, 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_GenerateNewProjectRuleSetPath_FileNameHasDot_AppendsExtension()
        {
            // Arrange
            const string             ruleSetRootPath = @"X:\";
            const string             fileName        = "My.File.With.Dots";
            CSharpVBBindingOperation testSubject     = this.CreateTestSubject();
            string expected = $"X:\\My.File.With.Dots.ruleset";

            // Act
            string actual = testSubject.GenerateNewProjectRuleSetPath
                            (
                ruleSetRootPath,
                fileName
                            );

            // Assert
            actual.Should().Be(expected);
        }
        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_Prepare_VariousRuleSetsInProjects()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock customRuleSetProperty1  = CreateRuleSetProperty(this.projectMock, "config1", "Custom.ruleset");
            PropertyMock customRuleSetProperty2  = CreateRuleSetProperty(this.projectMock, "config2", "Custom.ruleset");
            PropertyMock defaultRuleSetProperty1 = CreateRuleSetProperty(this.projectMock, "config3", CSharpVBBindingOperation.DefaultProjectRuleSet);
            PropertyMock defaultRuleSetProperty2 = CreateRuleSetProperty(this.projectMock, "config4", CSharpVBBindingOperation.DefaultProjectRuleSet);

            testSubject.Initialize();

            // Act
            testSubject.Prepare(CancellationToken.None);

            // Assert
            string expectedRuleSetFileForPropertiesWithDefaultRuleSets = cSharpVBBindingConfig.RuleSet.Path;

            fileSystem.GetFile(expectedRuleSetFileForPropertiesWithDefaultRuleSets).Should().NotBe(null);
            testSubject.PropertyInformationMap[defaultRuleSetProperty1].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRuleSets, "Expected all the properties with default ruleset to have the same new ruleset");
            testSubject.PropertyInformationMap[defaultRuleSetProperty2].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRuleSets, "Expected all the properties with default ruleset to have the same new ruleset");

            string expectedRulesetFilePath = Path.Combine(Path.GetDirectoryName(this.projectMock.FilePath), Path.GetFileNameWithoutExtension(this.projectMock.FilePath) + ".ruleset");

            string expectedRuleSetForConfig1 = Path.ChangeExtension(expectedRulesetFilePath, "config1.ruleset");

            testSubject.PropertyInformationMap[customRuleSetProperty1].NewRuleSetFilePath.Should().Be(expectedRuleSetForConfig1, "Expected different rule set path for properties with custom rulesets");
            fileSystem.GetFile(expectedRuleSetForConfig1).Should().Be(null);

            string expectedRuleSetForConfig2 = Path.ChangeExtension(expectedRulesetFilePath, "config2.ruleset");

            testSubject.PropertyInformationMap[customRuleSetProperty2].NewRuleSetFilePath.Should().Be(expectedRuleSetForConfig2, "Expected different rule set path for properties with custom rulesets");
            fileSystem.GetFile(expectedRuleSetForConfig2).Should().Be(null);

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

            // Assert that written
            fileSystem.GetFile(expectedRuleSetForConfig1).Should().NotBe(null);
            fileSystem.GetFile(expectedRuleSetForConfig2).Should().NotBe(null);
        }
        public void ProjectBindingOperation_Initialize_ConfigurationPropertiesWithVariousValues()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            PropertyMock prop1 = CreateRuleSetProperty(this.projectMock, "config1", CSharpVBBindingOperation.DefaultProjectRuleSet);
            PropertyMock prop2 = CreateRuleSetProperty(this.projectMock, "config2", "NonDefualtRuleSet.ruleset");

            // Act
            testSubject.Initialize();

            // Assert
            testSubject.ProjectFullPath.Should().Be(@"c:\solution\Project\project.proj");
            testSubject.ProjectLanguage.Should().Be(Language.CSharp);
            CollectionAssert.AreEquivalent(new[] { prop1, prop2 }, testSubject.PropertyInformationMap.Keys.ToArray(), "Unexpected properties");

            testSubject.PropertyInformationMap[prop1].CurrentRuleSetFilePath.Should().Be(CSharpVBBindingOperation.DefaultProjectRuleSet);
            testSubject.PropertyInformationMap[prop1].TargetRuleSetFileName.Should().Be("project", "Default ruleset - expected project based name to be generated");
            testSubject.PropertyInformationMap[prop2].CurrentRuleSetFilePath.Should().Be("NonDefualtRuleSet.ruleset");
            testSubject.PropertyInformationMap[prop2].TargetRuleSetFileName.Should().Be("project.config2", "Non default ruleset - expected configuration based rule set name to be generated");
        }
        public void ProjectBindingOperation_Initialize_ConfigurationPropertyWithSameNonDefaultValues()
        {
            // Arrange
            CSharpVBBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock prop1 = CreateRuleSetProperty(this.projectMock, "config1", "Custom1.ruleset");
            PropertyMock prop2 = CreateRuleSetProperty(this.projectMock, "config2", "Custom1.ruleset");

            // Act
            testSubject.Initialize();

            // Assert
            testSubject.ProjectFullPath.Should().Be(@"c:\solution\Project\project.proj");
            testSubject.ProjectLanguage.Should().Be(Language.VBNET);
            CollectionAssert.AreEquivalent(new[] { prop1, prop2 }, testSubject.PropertyInformationMap.Keys.ToArray(), "Unexpected properties");

            foreach (var prop in new[] { prop1, prop2 })
            {
                testSubject.PropertyInformationMap[prop].CurrentRuleSetFilePath.Should().Be("Custom1.ruleset");
                testSubject.PropertyInformationMap[prop].TargetRuleSetFileName.Should().Be("project");
            }
        }
        public void ProjectBindingOperation_QueueWriteProjectLevelRuleSet_ProjectHasExistingRuleSet_RuleSetIsNotFound()
        {
            // Arrange
            CSharpVBBindingOperation 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);

            VsRuleSet expectedRuleSet = TestRuleSetHelper.CreateTestRuleSet
                                        (
                numRules: 0,
                includes: new[] { currentNonExistingRuleSet, newSolutionRuleSetInclude }
                                        );
            var csharpVbConfig = CreateCSharpVbBindingConfig(newSolutionRuleSetPath, ValidCoreRuleSet);

            // Act
            string actualPath = testSubject.QueueWriteProjectLevelRuleSet(projectFullPath, ruleSetFileName, csharpVbConfig, 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);
        }