public void AttachBW_MissingBinaryFiles_TaskFails()
        {
            // Create a valid setup, then delete one of the required files each time
            // and check that the task fails

            // Arrange
            string testFolder = TestUtils.GetTestSpecificFolderName(this.TestContext);

            for (int i = 0; i < TaskExecutionContext.RequiredFileNames.Length; i++)
            {
                // Clean up after the previous iteration
                if (Directory.Exists(testFolder))
                {
                    Directory.Delete(testFolder, true);
                }
                Directory.CreateDirectory(testFolder);
                DummyBuildEngine dummyEngine = new DummyBuildEngine();
                TaskExecutionContext taskContext = new TaskExecutionContext(testFolder, testFolder, testFolder, 0 /* returns failure code */ );

                string missingFilePath = taskContext.RequiredFilePaths[i];

                File.Delete(missingFilePath); // delete one of the required files

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

                // Act and assert
                bool success = testSubject.Execute();
                Assert.IsFalse(success, "Expecting the task execution to fail");
                dummyEngine.AssertSingleErrorExists(missingFilePath);
            }
        }
        public void IsTestFile_InvalidRegexInConfig()
        {
            // Arrange
            string testFolder = TestUtils.CreateTestSpecificFolder(this.TestContext);
            string invalidRegEx = "Invalid regex ((";
            EnsureAnalysisConfig(testFolder, invalidRegEx);

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

            bool result = task.Execute();

            Assert.IsFalse(result, "Expecting the task to fail");
            dummyEngine.AssertSingleErrorExists(invalidRegEx); // expecting the invalid expression to appear in the error
        }
Example #3
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();

            Assert.IsFalse(result, "Expecting the task to fail");
            dummyEngine.AssertSingleErrorExists(invalidRegEx); // expecting the invalid expression to appear in the error
        }
        [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();
        }
        [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_Timeout_TaskFails()
        {
            // 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 - pause execution for 100 ms
                @"System.Threading.Thread.Sleep(200);");

            AttachBuildWrapper testSubject = new AttachBuildWrapper(11 /* timeout after 11 ms */);
            InitializeTask(testSubject, dummyEngine, testFolder, testFolder);

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

            // Assert
            Assert.IsFalse(result, "Not expecting the task to succeed - should have timed out");

            dummyEngine.AssertSingleErrorExists(SonarQube.MSBuild.Tasks.Resources.BuildWrapper_FailedToAttach);
            dummyEngine.AssertSingleWarningExists("11"); // expecting a warning with the timeout value
        }
        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 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);
        }
        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();
        }