Example #1
0
        public void ProjectBindingOperation_Commit_LegacyProjectSystem_DoesAddFile()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            this.ruleStore.RegisterConfigFilePath(Language.CSharp, @"c:\Solution\sln.ruleset");
            PropertyMock prop = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.DefaultProjectRuleSet);

            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");
        }
Example #2
0
        public void ProjectBindingOperation_Prepare_SameDefaultRuleSetsInProject()
        {
            // Arrange
            this.ruleStore.RegisterConfigFilePath(Language.VBNET, @"c:\Solution\sln.ruleset");
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock defaultRuleSetProperty1 = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.DefaultProjectRuleSet);
            PropertyMock defaultRuleSetProperty2 = CreateProperty(this.projectMock, "config2", ProjectBindingOperation.DefaultProjectRuleSet);

            testSubject.Initialize();

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


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

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

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

            // Assert that written
            fileSystem.GetFile(expectedRuleSetFileForPropertiesWithDefaultRulSets).Should().NotBe(null);
        }
        public void ProjectBindingOperation_GenerateNewProjectRuleSetPath_NoAvailableFileNames_AppendsGuid()
        {
            // Arrange
            const string            fileName        = "fileTaken";
            const string            ruleSetRootPath = @"X:\";
            ProjectBindingOperation 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);
        }
Example #4
0
        public void ProjectBindingOperation_Commit()
        {
            // Setup
            this.serviceProvider.RegisterService(typeof(IProjectSystemHelper), this.projectSystemHelper);
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            this.ruleStore.RegisterRuleSetPath(Language.CSharp, @"c:\Solution\sln.ruleset");
            PropertyMock prop = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.DefaultProjectRuleSet);

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

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

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

            Assert.AreEqual(Path.GetFileName(expectedFile), prop.Value.ToString(), "Should update the property value");
            Assert.IsTrue(this.projectMock.Files.ContainsKey(expectedFile), "Should be added to the project");
        }
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_RuleSetNotAlreadyWritten_WritesFile()
        {
            // Setup
            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);

            // Verify
            Assert.IsTrue(result, "Expected to return true when trying to update existing rule set");
            Assert.AreSame(existingRuleSet, rsOutput, "Same RuleSet instance expected");
            Assert.AreEqual(existingRuleSetFullPath, pathOutResult, "Unexpected rule set path was returned");
            this.sccFileSystem.AssertFileTimestamp(existingRuleSetFullPath, beforeTimestamp);
        }
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_RuleSetSaved(bool doesAlreadyExist)
        {
            // Arrange
            ProjectBindingOperation 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;
            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 is expected");
            pathOutResult.Should().Be(existingRuleSetFullPath, "Unexpected rule set path was returned");
        }
Example #7
0
        public void ProjectBindingOperation_Prepare_SameNonDefaultRuleSetsInProject()
        {
            // Setup
            this.ruleStore.RegisterRuleSetPath(Language.VBNET, @"c:\Solution\sln.ruleset");
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock customRuleSetProperty1 = CreateProperty(this.projectMock, "config1", "Custom.ruleset");
            PropertyMock customRuleSetProperty2 = CreateProperty(this.projectMock, "config2", "Custom.ruleset");

            testSubject.Initialize();

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

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

            this.sccFileSystem.AssertFileNotExists(expectedRuleSetFileForPropertiesWithDefaultRulSets);
            Assert.AreEqual(expectedRuleSetFileForPropertiesWithDefaultRulSets, testSubject.PropertyInformationMap[customRuleSetProperty1].NewRuleSetFilePath, "Expected different rule set path for properties with custom rulesets");
            Assert.AreEqual(expectedRuleSetFileForPropertiesWithDefaultRulSets, testSubject.PropertyInformationMap[customRuleSetProperty2].NewRuleSetFilePath, "Expected different rule set path for properties with custom rulesets");

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

            // Verify that written
            this.sccFileSystem.AssertFileExists(expectedRuleSetFileForPropertiesWithDefaultRulSets);
        }
Example #8
0
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_RuleSetAlreadyWritten_DoesNotWriteAgain()
        {
            // 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 = new RuleSet("test")
            {
                FilePath = existingRuleSetFullPath
            };

            testSubject.AlreadyUpdatedExistingRuleSetPaths.Add(existingRuleSet.FilePath, existingRuleSet);
            this.ruleSetFS.RegisterRuleSet(existingRuleSet);
            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 already updated existing rule set");
            rsOutput.Should().Be(existingRuleSet, "Same RuleSet instance is expected");
            pathOutResult.Should().Be(existingRuleSetFullPath, "Unexpected rule set path was returned");
            this.sccFileSystem.AssertFileTimestamp(existingRuleSetFullPath, beforeTimestamp);
        }
Example #9
0
        public void ProjectBindingOperation_TryUpdateExistingProjectRuleSet_ExistingRuleSetIsNotAtTheProjectLevel()
        {
            // Arrange
            ProjectBindingOperation 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",
                ProjectBindingOperation.DefaultProjectRuleSet,
                null,
                string.Empty
            };

            foreach (var currentRuleSet in cases)
            {
                // Act
                string  pathOutResult;
                RuleSet 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);
            }
        }
Example #10
0
        public void ProjectBindingOperation_Prepare_Cancellation()
        {
            // Arrange
            this.ruleStore.RegisterConfigFilePath(Language.CSharp, @"c:\Solution\sln.ruleset");
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            PropertyMock prop = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.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(ProjectBindingOperation.DefaultProjectRuleSet, "Should not update the property value");
            this.projectMock.Files.ContainsKey(expectedFile).Should().BeFalse("Should not be added to the project");
        }
Example #11
0
        public void ProjectBindingOperation_ArgChecks()
        {
            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(null, this.projectMock, this.ruleStore));
            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(this.serviceProvider, null, this.ruleStore));
            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(this.serviceProvider, this.projectMock, null));

            ProjectBindingOperation testSubject = this.CreateTestSubject();

            Assert.IsNotNull(testSubject, "Suppress warning that not used");
        }
Example #12
0
        public void ProjectBindingOperation_ArgChecks()
        {
            var logger = new TestLogger();

            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(null, this.projectMock, this.ruleStore));
            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(this.serviceProvider, null, this.ruleStore));
            Exceptions.Expect <ArgumentNullException>(() => new ProjectBindingOperation(this.serviceProvider, this.projectMock, null));

            ProjectBindingOperation testSubject = this.CreateTestSubject();

            testSubject.Should().NotBeNull("Suppress warning that not used");
        }
        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);
        }
Example #14
0
        public void ProjectBindingOperation_ShouldIgnoreConfigureRuleSetValue()
        {
            // Test case 1: not ignored
            ProjectBindingOperation.ShouldIgnoreConfigureRuleSetValue("My awesome rule set.ruleset").Should().BeFalse();

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

            // Test case 1: desired name exists
            // Arrange
            this.sccFileSystem.RegisterFile($"X:\\NameTaken.ruleset");

            // 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
            this.sccFileSystem.RegisterFile(@"X:\NameTaken-1.ruleset");
            this.sccFileSystem.RegisterFile(@"X:\NameTaken-2.ruleset");

            // 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");
        }
Example #16
0
        public void ProjectBindingOperation_GenerateNewProjectRuleSet()
        {
            // Arrange
            const string solutionIncludePath = @"..\..\solution.ruleset";
            const string currentRuleSetPath  = @"X:\MyOriginal.ruleset";
            var          expectedRuleSet     = new RuleSet(Constants.RuleSetName);

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

            // Act
            RuleSet actualRuleSet = ProjectBindingOperation.GenerateNewProjectRuleSet(solutionIncludePath, currentRuleSetPath, Constants.RuleSetName);

            // Assert
            RuleSetAssert.AreEqual(expectedRuleSet, actualRuleSet);
        }
        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);
            }
        }
Example #19
0
        public void ProjectBindingOperation_GenerateNewProjectRuleSetPath_FileNameHasDot_AppendsExtension()
        {
            // Arrange
            const string            ruleSetRootPath = @"X:\";
            const string            fileName        = "My.File.With.Dots";
            ProjectBindingOperation testSubject     = this.CreateTestSubject();
            string expected = $"X:\\My.File.With.Dots.ruleset";

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

            // Assert
            actual.Should().Be(expected);
        }
Example #20
0
        public void ProjectBindingOperation_Prepare_VariousRuleSetsInProjects()
        {
            // Arrange
            this.ruleStore.RegisterRuleSetPath(Language.VBNET, @"c:\Solution\sln.ruleset");
            ProjectBindingOperation testSubject = this.CreateTestSubject();

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

            testSubject.Initialize();

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

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

            this.sccFileSystem.files.Should().NotContainKey(expectedRuleSetFileForPropertiesWithDefaultRulSets);
            testSubject.PropertyInformationMap[defaultRuleSetProperty1].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRulSets, "Expected all the properties with default ruleset to have the same new ruleset");
            testSubject.PropertyInformationMap[defaultRuleSetProperty2].NewRuleSetFilePath.Should().Be(expectedRuleSetFileForPropertiesWithDefaultRulSets, "Expected all the properties with default ruleset to have the same new ruleset");

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

            testSubject.PropertyInformationMap[customRuleSetProperty1].NewRuleSetFilePath.Should().Be(expectedRuleSetForConfig1, "Expected different rule set path for properties with custom rulesets");
            this.sccFileSystem.files.Should().NotContainKey(expectedRuleSetForConfig1);

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

            testSubject.PropertyInformationMap[customRuleSetProperty2].NewRuleSetFilePath.Should().Be(expectedRuleSetForConfig2, "Expected different rule set path for properties with custom rulesets");
            this.sccFileSystem.files.Should().NotContainKey(expectedRuleSetForConfig2);

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

            // Assert that written
            this.sccFileSystem.files.Should().ContainKey(expectedRuleSetFileForPropertiesWithDefaultRulSets);
            this.sccFileSystem.files.Should().ContainKey(expectedRuleSetForConfig1);
            this.sccFileSystem.files.Should().ContainKey(expectedRuleSetForConfig2);
        }
        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);
        }
Example #22
0
        public void ProjectBindingOperation_Initialize_ConfigurationPropertiesWithVariousValues()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetCSProjectKind();
            PropertyMock prop1 = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.DefaultProjectRuleSet);
            PropertyMock prop2 = CreateProperty(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(ProjectBindingOperation.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");
        }
Example #23
0
        public void ProjectBindingOperation_Initialize_ConfigurationPropertyWithEmptyRuleSets()
        {
            // Setup
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock prop1 = CreateProperty(this.projectMock, "config1", null);
            PropertyMock prop2 = CreateProperty(this.projectMock, "config2", string.Empty);

            // Act
            testSubject.Initialize();

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

            foreach (var prop in new[] { prop1, prop2 })
            {
                Assert.IsTrue(string.IsNullOrEmpty(testSubject.PropertyInformationMap[prop].CurrentRuleSetFilePath));
                Assert.AreEqual("project", testSubject.PropertyInformationMap[prop].TargetRuleSetFileName);
            }
        }
Example #24
0
        public void ProjectBindingOperation_Initialize_ConfigurationPropertyWithDefaultValues()
        {
            // Arrange
            ProjectBindingOperation testSubject = this.CreateTestSubject();

            this.projectMock.SetVBProjectKind();
            PropertyMock prop1 = CreateProperty(this.projectMock, "config1", ProjectBindingOperation.DefaultProjectRuleSet);
            PropertyMock prop2 = CreateProperty(this.projectMock, "config2", ProjectBindingOperation.DefaultProjectRuleSet);

            // 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(ProjectBindingOperation.DefaultProjectRuleSet);
                testSubject.PropertyInformationMap[prop].TargetRuleSetFileName.Should().Be("project");
            }
        }
        public void ProjectBindingOperation_GenerateNewProjectRuleSetPath_NoAvailableFileNames_AppendsGuid()
        {
            // Setup
            const string            fileName        = "fileTaken";
            const string            ruleSetRootPath = @"X:\";
            ProjectBindingOperation testSubject     = this.CreateTestSubject();

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


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

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

            Assert.AreEqual(fileName.Length + 32 + 1, actualFileName.Length, "Expected to append GUID to desired file name, actual: " + actualFileName);
        }