Example #1
0
        public void ProcRunner_FailsOnTimeout()
        {
            // Arrange
            string exeName = TestUtils.WriteBatchFileForTest(TestContext,
                                                             @"TIMEOUT 2
@echo Hello world
");

            TestLogger             logger = new TestLogger();
            ProcessRunnerArguments args   = new ProcessRunnerArguments(exeName, logger)
            {
                TimeoutInMilliseconds = 100
            };
            ProcessRunner runner = new ProcessRunner();

            // Act
            bool success = runner.Execute(args);

            // Assert
            Assert.IsFalse(success, "Expecting the process to have failed");
            Assert.AreEqual(ProcessRunner.ErrorCode, runner.ExitCode, "Unexpected exit code");
            logger.AssertMessageNotLogged("Hello world");

            // Give the spawned process a chance to terminate.
            // This isn't essential (and having a Sleep in the test isn't ideal), but it stops
            // the test framework outputting this warning which appears in the TeamBuild summary:
            // "System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen
            // if the test(s) started a thread but did not stop it. Make sure that all the threads started by
            // the test(s) are stopped before completion."
            System.Threading.Thread.Sleep(1100);
        }
Example #2
0
        public void ProcRunner_FailsOnTimeout()
        {
            // Arrange

            // Calling TIMEOUT can fail on some older OSes (e.g. Windows 7) with the error
            // "Input redirection is not supported, exiting the process immediately."
            // However, it works reliably on the CI machines. Alternatives such as
            // pinging a non-existent address with a timeout were not reliable.
            string exeName = TestUtils.WriteBatchFileForTest(TestContext,
                                                             @"TIMEOUT 1
@echo Hello world
");

            TestLogger             logger = new TestLogger();
            ProcessRunnerArguments args   = new ProcessRunnerArguments(exeName, logger)
            {
                TimeoutInMilliseconds = 100
            };
            ProcessRunner runner = new ProcessRunner();

            Stopwatch timer = Stopwatch.StartNew();

            // Act
            bool success = runner.Execute(args);

            // Assert
            timer.Stop(); // Sanity check that the process actually timed out
            logger.LogInfo("Test output: test ran for {0}ms", timer.ElapsedMilliseconds);
            // TODO: the following line throws regularly on the CI machines (elapsed time is around 97ms);
            // Assert.IsTrue(timer.ElapsedMilliseconds >= 100, "Test error: batch process exited too early. Elapsed time(ms): {0}", timer.ElapsedMilliseconds);

            Assert.IsFalse(success, "Expecting the process to have failed");
            Assert.AreEqual(ProcessRunner.ErrorCode, runner.ExitCode, "Unexpected exit code");
            logger.AssertMessageNotLogged("Hello world");
            logger.AssertWarningsLogged(1); // expecting a warning about the timeout

            // Give the spawned process a chance to terminate.
            // This isn't essential (and having a Sleep in the test isn't ideal), but it stops
            // the test framework outputting this warning which appears in the TeamBuild summary:
            // "System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen
            // if the test(s) started a thread but did not stop it. Make sure that all the threads started by
            // the test(s) are stopped before completion."
            System.Threading.Thread.Sleep(1100);
        }
        public void SonarRunnerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            TestLogger testLogger         = new TestLogger();
            string     exePath            = CreateDummarySonarRunnerBatchFile();
            string     propertiesFilePath = CreateDummySonarRunnerPropertiesFile();

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarRunnerWrapper.SonarRunnerHomeVariableName, null);

                // Act
                bool success = SonarRunnerWrapper.ExecuteJavaRunner(new AnalysisConfig(), Enumerable.Empty <string>(), testLogger, exePath, propertiesFilePath);
                Assert.IsTrue(success, "Expecting execution to succeed");

                // Assert
                testLogger.AssertMessageNotLogged(SonarRunner.Shim.Resources.MSG_SonarRunnerHomeIsSet);
            }
        }
        public void SonarScannerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            var testLogger = new TestLogger();

            using (var scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarScannerWrapper.SonarScannerHomeVariableName, null);
                var config = new AnalysisConfig()
                {
                    SonarScannerWorkingDirectory = "C:\\working\\dir"
                };
                var mockRunner = new MockProcessRunner(executeResult: true);

                // Act
                var success = ExecuteJavaRunnerIgnoringAsserts(config, Enumerable.Empty <string>(), testLogger, "c:\\file.exe", "d:\\properties.prop", mockRunner);

                // Assert
                VerifyProcessRunOutcome(mockRunner, testLogger, "C:\\working\\dir", success, true);
                testLogger.AssertMessageNotLogged(SonarScanner.MSBuild.Shim.Resources.MSG_SonarScannerHomeIsSet);
            }
        }
Example #5
0
        public void SonarScannerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            var testLogger         = new TestLogger();
            var exePath            = CreateDummarySonarScannerBatchFile();
            var propertiesFilePath = CreateDummySonarScannerPropertiesFile();

            using (var scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarScannerWrapper.SonarScannerHomeVariableName, null);
                var config = new AnalysisConfig()
                {
                    SonarScannerWorkingDirectory = TestContext.DeploymentDirectory
                };

                // Act
                var success = SonarScannerWrapper.ExecuteJavaRunner(config, Enumerable.Empty <string>(), testLogger, exePath, propertiesFilePath);

                // Assert
                VerifyProcessRunOutcome(testLogger, TestContext.DeploymentDirectory, success, true);
                testLogger.AssertMessageNotLogged(SonarScanner.Shim.Resources.MSG_SonarScannerHomeIsSet);
            }
        }
        public void SonarRunnerHome_NoMessageIfNotAlreadySet()
        {
            // Arrange
            TestLogger testLogger         = new TestLogger();
            string     exePath            = CreateDummarySonarRunnerBatchFile();
            string     propertiesFilePath = CreateDummySonarRunnerPropertiesFile();

            using (EnvironmentVariableScope scope = new EnvironmentVariableScope())
            {
                scope.SetVariable(SonarRunnerWrapper.SonarRunnerHomeVariableName, null);
                AnalysisConfig config = new AnalysisConfig()
                {
                    SonarRunnerWorkingDirectory = this.TestContext.DeploymentDirectory
                };

                // Act
                bool success = SonarRunnerWrapper.ExecuteJavaRunner(config, Enumerable.Empty <string>(), testLogger, exePath, propertiesFilePath);

                // Assert
                VerifySuccessfullRun(testLogger, success, this.TestContext.DeploymentDirectory);
                testLogger.AssertMessageNotLogged(SonarRunner.Shim.Resources.MSG_SonarRunnerHomeIsSet);
            }
        }
        public void ProcRunner_FailsOnTimeout()
        {
            // Arrange

            // Calling TIMEOUT can fail on some OSes (e.g. Windows 7) with the error
            // "Input redirection is not supported, exiting the process immediately."
            // Alternatives such as
            // pinging a non-existent address with a timeout were not reliable.
            var exeName = TestUtils.WriteBatchFileForTest(TestContext,
                                                          @"waitfor /t 2 somethingThatNeverHappen
@echo Hello world
");

            var logger = new TestLogger();
            var args   = new ProcessRunnerArguments(exeName, true)
            {
                TimeoutInMilliseconds = 100
            };
            var runner = new ProcessRunner(logger);

            var timer = Stopwatch.StartNew();

            // Act
            var success = runner.Execute(args);

            // Assert
            timer.Stop(); // Sanity check that the process actually timed out
            logger.LogInfo("Test output: test ran for {0}ms", timer.ElapsedMilliseconds);
            // TODO: the following line throws regularly on the CI machines (elapsed time is around 97ms);
            // Assert.IsTrue(timer.ElapsedMilliseconds >= 100, "Test error: batch process exited too early. Elapsed time(ms): {0}", timer.ElapsedMilliseconds);

            Assert.IsFalse(success, "Expecting the process to have failed");
            Assert.AreEqual(ProcessRunner.ErrorCode, runner.ExitCode, "Unexpected exit code");
            logger.AssertMessageNotLogged("Hello world");
            logger.AssertWarningsLogged(1); // expecting a warning about the timeout
            Assert.IsTrue(logger.Warnings.Single().Contains("has been terminated"));
        }
        public void ProcRunner_FailsOnTimeout()
        {
            // Arrange

            // Calling TIMEOUT can fail on some OSes (e.g. Windows 7) with the error
            // "Input redirection is not supported, exiting the process immediately."
            // Alternatives such as
            // pinging a non-existent address with a timeout were not reliable.
            var exeName = WriteBatchFileForTest(TestContext,
                                                $@"waitfor /t 2 {Guid.NewGuid():N}
@echo Hello world
");

            var logger = new TestLogger();
            var args   = new ProcessRunnerArguments(exeName, true)
            {
                TimeoutInMilliseconds = 100
            };
            var runner = CreateProcessRunner(logger);

            var timer = Stopwatch.StartNew();

            // Act
            var success = runner.Execute(args);

            // Assert
            timer.Stop(); // Sanity check that the process actually timed out
            logger.WriteLine("Test output: test ran for {0}ms", timer.ElapsedMilliseconds);
            // TODO: the following line throws regularly on the CI machines (elapsed time is around 97ms)
            // timer.ElapsedMilliseconds >= 100.Should().BeTrue("Test error: batch process exited too early. Elapsed time(ms): {0}", timer.ElapsedMilliseconds)

            success.Should().BeFalse("Expecting the process to have failed");
            runner.ExitCode.Should().Be(ProcessRunner.ErrorCode, "Unexpected exit code");
            logger.AssertMessageNotLogged("Hello world");
            // expecting a warning about the timeout
            logger.AssertPartialOutputStringExists("has been terminated");
        }