Пример #1
0
        [TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11
        public void IsTestFile_TimeoutIfConfigLocked()
        {
            // Arrange
            // We'll lock the file and sleep for long enough for the task to timeout
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            IsTestFileByName task        = new IsTestFileByName();

            task.BuildEngine       = dummyEngine;
            task.FullFilePath      = "XXX.proj";
            task.AnalysisConfigDir = testFolder;

            bool result;

            using (FileStream lockingStream = File.OpenWrite(configFile))
            {
                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period
                    lockingStream.Close();
                });

                result = task.Execute();
            }

            Assert.IsFalse(result, "Expecting the task to fail");

            dummyEngine.AssertMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString());
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleErrorExists();
        }
        private static void ExecuteAndCheckSuccess(Task task)
        {
            var dummyEngine = new DummyBuildEngine();

            task.BuildEngine = dummyEngine;

            var taskSucess = task.Execute();

            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
        private void ExecuteAndCheckSuccess(string primaryRuleset, params string[] rulesetsToInclude)
        {
            this.TestContext.AddResultFile(primaryRuleset);

            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            MergeRuleSets    task        = CreateTask(dummyEngine, primaryRuleset, rulesetsToInclude);

            bool taskSucess = task.Execute();

            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
Пример #4
0
        private static bool ExecuteAndCheckSuccess(string analysisDir, string fullFileName)
        {
            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            IsTestFileByName task        = new IsTestFileByName();

            task.BuildEngine       = dummyEngine;
            task.FullFilePath      = fullFileName;
            task.AnalysisConfigDir = analysisDir;

            bool taskSucess = task.Execute();

            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

            return(task.IsTest);
        }
Пример #5
0
        [TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11
        public void IsTestFile_RetryIfConfigLocked()
        {
            // Arrange
            // We'll lock the file and sleep for long enough for the retry period to occur, but
            // not so long that the task times out
            int lockPeriodInMilliseconds = 1000;

            Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long");

            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            IsTestFileByName task        = new IsTestFileByName();

            task.BuildEngine       = dummyEngine;
            task.FullFilePath      = "XXX.proj";
            task.AnalysisConfigDir = testFolder;

            bool result;

            Stopwatch testDuration = Stopwatch.StartNew();

            using (FileStream lockingStream = File.OpenWrite(configFile))
            {
                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    System.Threading.Thread.Sleep(lockPeriodInMilliseconds);     // unlock the file after a short delay
                    lockingStream.Close();
                });

                result = task.Execute();
            }

            testDuration.Stop();
            Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}",
                          lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds);

            Assert.IsTrue(result, "Expecting the task to succeed");
            Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test");

            dummyEngine.AssertMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString());
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
Пример #6
0
        private string ExecuteAndCheckSuccess(string projectDirectory, string primaryRuleset, params string[] rulesetsToInclude)
        {
            string mergedRulesetFileName = primaryRuleset + ".merged.txt";

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

            bool taskSucess = task.Execute();

            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

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

            return(mergedRulesetFileName);
        }
        [TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11
        public void IsTestFile_TimeoutIfConfigLocked_TaskFails()
        {
            // Arrange
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            IsTestFileByName task        = new IsTestFileByName();

            task.BuildEngine       = dummyEngine;
            task.FullFilePath      = "XXX.proj";
            task.AnalysisConfigDir = testFolder;

            bool result = true;

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

            Assert.IsFalse(result, "Expecting the task to fail if the config file could not be read");
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleErrorExists();
        }
        public void AttachBW_NotAttached_ExeThrows_TaskFails()
        {
            // Arrange
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output");
            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext dummy = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */,
                // Embed additional code in the dummy exe
                @"throw new System.Exception(""XXX thrown error should be captured"");");

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsFalse(result, "Not expecting the task to succeed");

            dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach);
            dummyEngine.AssertNoWarnings();

            dummyEngine.AssertSingleErrorExists("XXX thrown error should be captured");
        }
        public void IsTestFile_RetryIfConfigLocked()
        {
            // Arrange
            // We'll lock the file and sleep for long enough for the retry period to occur, but
            // not so long that the task times out
            int lockPeriodInMilliseconds = 1000;
            Assert.IsTrue(lockPeriodInMilliseconds < IsTestFileByName.MaxConfigRetryPeriodInMilliseconds, "Test setup error: the test is sleeping for too long");

            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

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

            bool result;

            Stopwatch testDuration = Stopwatch.StartNew();

            using (FileStream lockingStream = File.OpenWrite(configFile))
            {
                System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        System.Threading.Thread.Sleep(lockPeriodInMilliseconds); // unlock the file after a short delay
                        lockingStream.Close();
                    });

                result = task.Execute();
            }

            testDuration.Stop();
            Assert.IsTrue(testDuration.ElapsedMilliseconds > 1000, "Test error: expecting the test to have taken at least {0} milliseconds to run. Actual: {1}",
                lockPeriodInMilliseconds, testDuration.ElapsedMilliseconds);

            Assert.IsTrue(result, "Expecting the task to succeed");
            Assert.IsTrue(task.IsTest, "Expecting the file to be recognised as a test");

            dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString());
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
        public void IsTestFile_TimeoutIfConfigLocked()
        {
            // Arrange
            // We'll lock the file and sleep for long enough for the task to timeout
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

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

            bool result;

            using (FileStream lockingStream = File.OpenWrite(configFile))
            {
                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    System.Threading.Thread.Sleep(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds + 600); // sleep for longer than the timeout period
                    lockingStream.Close();
                });

                result = task.Execute();
            }

            Assert.IsFalse(result, "Expecting the task to fail");

            dummyEngine.AssertSingleMessageExists(IsTestFileByName.MaxConfigRetryPeriodInMilliseconds.ToString(), IsTestFileByName.DelayBetweenRetriesInMilliseconds.ToString());
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleErrorExists();
        }
        private static bool ExecuteAndCheckSuccess(string analysisDir, string fullFileName)
        {
            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            IsTestFileByName task = new IsTestFileByName();
            task.BuildEngine = dummyEngine;
            task.FullFilePath = fullFileName;
            task.AnalysisConfigDir = analysisDir;

            bool taskSucess = task.Execute();
            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

            return task.IsTest;
        }
        [TestCategory("IsTest")] // Regression test for bug http://jira.codehaus.org/browse/SONARMSBRU-11
        public void IsTestFile_TimeoutIfConfigLocked_TaskFails()
        {
            // Arrange
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);

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

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

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

            Assert.IsFalse(result, "Expecting the task to fail if the config file could not be read");
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleErrorExists();
        }
        private void ExecuteAndCheckSuccess(string primaryRuleset, params string[] rulesetsToInclude)
        {
            this.TestContext.AddResultFile(primaryRuleset);

            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            MergeRuleSets task = CreateTask(dummyEngine, primaryRuleset, rulesetsToInclude);

            bool taskSucess = task.Execute();
            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
        public void AttachBW_NotAttached_ExeReturnsSuccess_TaskSucceeds()
        {
            // Arrange
            string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config");
            string outputFolder = Path.Combine(binFolder, "output"); // expected location: does not exist
            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, outputFolder, 0 /* returns success code */ );

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsTrue(result, "Expecting task to succeed");
            Assert.IsTrue(Directory.Exists(outputFolder), "Expecting the output folder to have been created");

            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
            dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_AttachedSuccessfully);

            // Check the parameters passed to the exe
            string logPath = AssertLogFileExists(taskContext);
            DummyExeHelper.AssertExpectedLogContents(logPath,
                "--msbuild-task",
                Process.GetCurrentProcess().Id.ToString(),
                outputFolder);
        }
        public void AttachBW_NotAttached_ExeReturnsFailure_TaskFails()
        {
            // Arrange
            string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config");
            string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output");
            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, outputFolder, 1 /* returns failure code */ );

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsFalse(result, "Not expecting the task to succeed");

            dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach);
            dummyEngine.AssertNoWarnings();

            // Check the parameters passed to the exe
            string logPath = AssertLogFileExists(taskContext);
            DummyExeHelper.AssertExpectedLogContents(logPath,
                "--msbuild-task",
                Process.GetCurrentProcess().Id.ToString(),
                outputFolder);
        }
        public void AttachBW_NotAttached_ConsoleOutputIsCaptured()
        {
            // Arrange
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string outputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "bw_output");

            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, outputFolder, 0 /* returns success code */,
                // Embed additional code in the dummy exe
                @"System.Console.WriteLine(""AAA standard output should be captured"");
                  System.Console.Error.WriteLine(""BBB standard error should be captured"");");

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, testFolder, testFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsTrue(result, "Expecting the task to succeed");

            dummyEngine.AssertNoWarnings();

            dummyEngine.AssertSingleMessageExists("AAA standard output should be captured");
            dummyEngine.AssertSingleErrorExists("BBB standard error should be captured");
        }
        public void AttachBW_MissingOutputDirectorySetting_TaskFails()
        {
            // Arrange
            string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config");
            string outputFolder = Path.Combine(binFolder, "output");
            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, null /* no output dir set */, 0 /* returns success code */ );

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, configFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsFalse(result, "Expecting task to fail");
            Assert.IsFalse(Directory.Exists(outputFolder), "Not expecting the output folder to have been created");

            dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_MissingOutputDirectory);
            dummyEngine.AssertNoWarnings();

            AssertLogFileDoesNotExist(taskContext);
        }
        private static void ExecuteAndCheckSuccess(Task task)
        {
            DummyBuildEngine dummyEngine = new DummyBuildEngine();
            task.BuildEngine = dummyEngine;

            bool taskSucess = task.Execute();
            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();
        }
        public void AttachBW_MissingConfigFile_TaskFails()
        {
            // Arrange
            string binFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string configFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "config");
            DummyBuildEngine dummyEngine = new DummyBuildEngine();

            TaskExecutionContext taskContext = new TaskExecutionContext(binFolder, configFolder, null /* output folder */, 0 /* returns success code */ );

            // Remove the config file
            File.Delete(taskContext.ConfigFilePath);

            AttachBuildWrapper testSubject = CreateTestSubject(dummyEngine, binFolder, binFolder);

            // Act
            bool result = testSubject.Execute();

            // Assert
            Assert.IsFalse(result, "Expecting task to fail");

            dummyEngine.AssertSingleMessageExists(SonarQube.MSBuild.Tasks.Resources.Shared_ConfigFileNotFound);
            dummyEngine.AssertNoWarnings();

            AssertLogFileDoesNotExist(taskContext);
        }
        private string ExecuteAndCheckSuccess(string projectDirectory, string primaryRuleset, params string[] rulesetsToInclude)
        {
            string mergedRulesetFileName = primaryRuleset + ".merged.txt";

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

            bool taskSucess = task.Execute();
            Assert.IsTrue(taskSucess, "Expecting the task to succeed");
            dummyEngine.AssertNoErrors();
            dummyEngine.AssertNoWarnings();

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

            return mergedRulesetFileName;
        }