コード例 #1
0
        [WorkItem(50)] // Regression test for Bug 50:MSBuild projects with missing ProjectGuids cause the build to fail
        public void WriteProjectInfoFile_MissingProjectGuid()
        {
            // Arrange
            var testFolder = TestUtils.CreateTestSpecificFolder(TestContext);

            var task = new WriteProjectInfoFile
            {
                FullProjectPath = "c:\\fullPath\\project.proj",
                IsTest          = true,
                OutputFolder    = testFolder,
                ProjectName     = "ProjectWithoutProjectGuid",
                ProjectLanguage = "C#"
            };
            // No analysis results are supplied

            // Act
            var engine = new DummyBuildEngine();

            task.BuildEngine = engine;
            var success = task.Execute();

            // Assert
            success.Should().BeTrue("Not expecting the task to fail as this would fail the build");
            engine.AssertNoErrors();
            engine.Warnings.Should().HaveCount(1, "Expecting a build warning as the ProjectGuid is missing");

            var firstWarning = engine.Warnings[0];

            firstWarning.Message.Should().NotBeNull("Warning message should not be null");

            var projectInfoFilePath = Path.Combine(testFolder, ExpectedProjectInfoFileName);

            File.Exists(projectInfoFilePath).Should().BeTrue("Expecting the project info file to have been created");
        }
コード例 #2
0
        private static void ExecuteAndCheckSuccess(Task task)
        {
            var dummyEngine = new DummyBuildEngine();

            task.BuildEngine = dummyEngine;

            var taskSucess = task.Execute();

            taskSucess.Should().BeTrue("Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
コード例 #3
0
        public void MergeRulesets_PrimaryRulesetDoesNotExist()
        {
            // Arrange
            var projectDir            = TestContext.TestDeploymentDir;
            var targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt");

            var dummyEngine = new DummyBuildEngine();
            var task        = CreateTask(dummyEngine, projectDir, "missing.ruleset", targetRulesetFilePath);

            // Act and Assert
            var ex = AssertException.Expects <FileNotFoundException>(() => task.Execute());

            Assert.AreEqual("missing.ruleset", ex.FileName);
        }
コード例 #4
0
        public void MergeRulesets_PrimaryRulesetDoesNotExist()
        {
            // Arrange
            var projectDir            = TestContext.TestDeploymentDir;
            var targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt");

            var dummyEngine = new DummyBuildEngine();
            var task        = CreateTask(dummyEngine, projectDir, "missing.ruleset", targetRulesetFilePath);

            // Act and Assert
            Action act = () => task.Execute();

            act.Should().ThrowExactly <FileNotFoundException>().And.FileName.Should().Be("missing.ruleset");
        }
コード例 #5
0
        private static void ExecuteAndAssert(string[] references, string expectedTestReference, string expectedLog)
        {
            var dummyEngine = new DummyBuildEngine();
            var task        = new IsTestByReference {
                BuildEngine = dummyEngine, References = references
            };

            var taskSucess = task.Execute();

            taskSucess.Should().BeTrue("Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleMessageExists(expectedLog);

            task.TestReference.Should().Be(expectedTestReference);
        }
コード例 #6
0
        public void MergeRulesets_MergedRulesetAlreadyExists()
        {
            // Arrange
            var projectDir            = TestContext.TestDeploymentDir;
            var primaryRuleset        = CreateValidRuleset("valid.ruleset.txt");
            var targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt");

            File.WriteAllText(targetRulesetFilePath, "dummy existing ruleset");

            var dummyEngine = new DummyBuildEngine();
            var task        = CreateTask(dummyEngine, projectDir, primaryRuleset, targetRulesetFilePath);

            // Act and Assert
            Action act = () => task.Execute();

            act.Should().ThrowExactly <InvalidOperationException>().And.Message.Contains(targetRulesetFilePath).Should().BeTrue();
        }
コード例 #7
0
        private static MergeRuleSets CreateTask(DummyBuildEngine buildEngine,
                                                string projectDir,
                                                string primaryRuleset,
                                                string targetRulesetFilePath,
                                                params string[] rulesetsToInclude)
        {
            var task = new MergeRuleSets
            {
                BuildEngine              = buildEngine,
                ProjectDirectoryPath     = projectDir,
                PrimaryRulesetFilePath   = primaryRuleset,
                MergedRuleSetFilePath    = targetRulesetFilePath,
                IncludedRulesetFilePaths = rulesetsToInclude
            };

            return(task);
        }
コード例 #8
0
        public void MergeRulesets_MergedRulesetAlreadyExists()
        {
            // Arrange
            var projectDir            = TestContext.TestDeploymentDir;
            var primaryRuleset        = CreateValidRuleset("valid.ruleset.txt");
            var targetRulesetFilePath = Path.Combine(projectDir, "merged.ruleset.txt");

            File.WriteAllText(targetRulesetFilePath, "dummy existing ruleset");

            var dummyEngine = new DummyBuildEngine();
            var task        = CreateTask(dummyEngine, projectDir, primaryRuleset, targetRulesetFilePath);

            // Act and Assert
            var ex = AssertException.Expects <InvalidOperationException>(() => task.Execute());

            Assert.IsTrue(ex.Message.Contains(targetRulesetFilePath));
        }
コード例 #9
0
        private static bool ExecuteAndCheckSuccess(string analysisDir, string fullFileName)
        {
            var dummyEngine = new DummyBuildEngine();
            var task        = new IsTestFileByName
            {
                BuildEngine       = dummyEngine,
                FullFilePath      = fullFileName,
                AnalysisConfigDir = analysisDir
            };

            var taskSucess = task.Execute();

            taskSucess.Should().BeTrue("Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

            return(task.IsTest);
        }
コード例 #10
0
        private string ExecuteAndCheckSuccess(string projectDirectory, string primaryRuleset, params string[] rulesetsToInclude)
        {
            var mergedRulesetFileName = primaryRuleset + ".merged.txt";

            var dummyEngine = new DummyBuildEngine();
            var task        = CreateTask(dummyEngine, projectDirectory, primaryRuleset, mergedRulesetFileName, rulesetsToInclude);

            var taskSucess = task.Execute();

            taskSucess.Should().BeTrue("Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

            File.Exists(mergedRulesetFileName).Should().BeTrue("Expecting the merged ruleset to have been created: {0}", mergedRulesetFileName);
            TestContext.AddResultFile(primaryRuleset);
            TestContext.AddResultFile(mergedRulesetFileName);

            return(mergedRulesetFileName);
        }
コード例 #11
0
        public void IsTestFile_InvalidRegexInConfig()
        {
            // Arrange
            var testFolder   = TestUtils.CreateTestSpecificFolder(TestContext);
            var invalidRegEx = "Invalid regex ((";

            EnsureAnalysisConfig(testFolder, invalidRegEx);

            var dummyEngine = new DummyBuildEngine();
            var task        = new IsTestFileByName
            {
                BuildEngine       = dummyEngine,
                FullFilePath      = "Path",
                AnalysisConfigDir = testFolder
            };

            var result = task.Execute();

            result.Should().BeFalse("Expecting the task to fail");
            dummyEngine.AssertSingleErrorExists(invalidRegEx); // expecting the invalid expression to appear in the error
        }
コード例 #12
0
        private void AssertProjectGuidIsRandomlyGenerated(string projectGuid, string solutionConfigurationContents, string fullProjectPath)
        {
            // Arrange
            var testSubject = new WriteProjectInfoFile
            {
                FullProjectPath = fullProjectPath,
                ProjectGuid     = projectGuid,
                SolutionConfigurationContents = solutionConfigurationContents
            };

            var engine = new DummyBuildEngine();

            testSubject.BuildEngine = engine;


            // Act
            var actual = testSubject.GetProjectGuid();

            // Assert
            actual.Should().NotBeNullOrEmpty();
            actual.Should().NotBe(Guid.Empty.ToString());
        }
コード例 #13
0
        [TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11
        public void IsTestFile_TimeoutIfConfigLocked_TaskFails()
        {
            // Arrange
            var testFolder = TestUtils.CreateTestSpecificFolder(TestContext);

            var configFile = EnsureAnalysisConfig(testFolder, ".XX.");

            var dummyEngine = new DummyBuildEngine();
            var task        = new IsTestFileByName
            {
                BuildEngine       = dummyEngine,
                FullFilePath      = "XXX.proj",
                AnalysisConfigDir = testFolder
            };

            var result = true;

            TaskUtilitiesTests.PerformOpOnLockedFile(configFile, () => result = task.Execute(), shouldTimeoutReadingConfig: true);

            result.Should().BeFalse("Expecting the task to fail if the config file could not be read");
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleErrorExists();
        }
コード例 #14
0
        public void WriteProjectInfoFile_AnalysisSettings()
        {
            // Arrange
            var testFolder = TestUtils.CreateTestSpecificFolder(TestContext);

            var projectGuid = Guid.NewGuid();

            var task = new WriteProjectInfoFile();

            var buildEngine = new DummyBuildEngine();

            task.BuildEngine     = buildEngine;
            task.FullProjectPath = "x:\\analysisSettings.csproj";
            task.IsTest          = false;
            task.OutputFolder    = testFolder;
            task.ProjectGuid     = projectGuid.ToString("B");
            task.ProjectName     = "MyProject";
            task.ProjectLanguage = "C#";

            // Example of a valid setting:
            // <SonarQubeSetting Include="sonar.resharper.projectname">
            //    <Value>C:\zzz\reportlocation.xxx</Value>
            // </SonarQubeSetting>

            var settingsInputs = new List <ITaskItem>
            {
                // Add invalid task items
                // Note: the TaskItem class won't allow the item spec or metadata values to be null,
                // so we aren't testing those
                CreateMetadataItem("invalid.missing.value.metadata", "NotValueMetadata", "missing value 1"), // value field is missing
                CreateAnalysisSettingTaskItem(" ", "should be ignored - key is whitespace only"),            // whitespace key
                CreateAnalysisSettingTaskItem("invalid spaces in key", "spaces.in.key"),                     // spaces in key
                CreateAnalysisSettingTaskItem(" invalid.key.has.leading.whitespace", "leading whitespace in key"),
                CreateAnalysisSettingTaskItem("invalid.key.has.trailing.whitespace ", "trailing whitespace in key"),
                CreateAnalysisSettingTaskItem(".invalid.non.alpha.first.character", "non alpha first character"),

                // Add valid task items
                CreateAnalysisSettingTaskItem("valid.setting.1", @"c:\dir1\dir2\file.txt"),
                CreateAnalysisSettingTaskItem("valid.value.is.whitespace.only", " "),
                CreateMetadataItem("valid.metadata.name.is.case.insensitive", BuildTaskConstants.SettingValueMetadataName.ToUpperInvariant(), "uppercase metadata name"), // metadata name is in the wrong case
                CreateAnalysisSettingTaskItem("valid.value.has.whitespace", "valid setting with whitespace"),
                CreateAnalysisSettingTaskItem("X", "single character key"),
                CreateAnalysisSettingTaskItem("Y...", "single character followed by periods"),
                CreateAnalysisSettingTaskItem("7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key")
            };

            task.AnalysisSettings = settingsInputs.ToArray();

            // Act
            ProjectInfo createdProjectInfo;

            createdProjectInfo = ExecuteAndCheckSucceeds(task, testFolder);

            // Assert
            buildEngine.AssertSingleWarningExists("invalid.missing.value.metadata");
            // Can't easily check for the message complaining against the empty key
            buildEngine.AssertSingleWarningExists("invalid spaces in key");
            buildEngine.AssertSingleWarningExists(" invalid.key.has.leading.whitespace");
            buildEngine.AssertSingleWarningExists("invalid.key.has.trailing.whitespace ");
            buildEngine.AssertSingleWarningExists(".invalid.non.alpha.first.character");

            AssertAnalysisSettingExists(createdProjectInfo, "valid.setting.1", @"c:\dir1\dir2\file.txt");
            AssertAnalysisSettingExists(createdProjectInfo, "valid.value.is.whitespace.only", null);
            AssertAnalysisSettingExists(createdProjectInfo, "valid.value.has.whitespace", "valid setting with whitespace");
            AssertAnalysisSettingExists(createdProjectInfo, "valid.metadata.name.is.case.insensitive", "uppercase metadata name");
            AssertAnalysisSettingExists(createdProjectInfo, "X", "single character key");
            AssertAnalysisSettingExists(createdProjectInfo, "Y...", "single character followed by periods");
            AssertAnalysisSettingExists(createdProjectInfo, "7B3B7244-5031-4D74-9BBD-3316E6B5E7D5.sonar.projectName", "guid followed by key");

            AssertExpectedAnalysisSettingsCount(7, createdProjectInfo);
        }