Example #1
0
            public void Should_Rethrow_Exception_From_Action()
            {
                // Given
                var exception = new CakeException("The exception message");
                var fixture   = new SpecFlowTestExecutionReporterFixture();

                fixture.Settings.ThrowOnTestFailure = true;
                var intercepting = true;

                fixture.Action = context =>
                {
                    context.ProcessRunner.Start(
                        new FilePath("/Working/tools/MSTest.exe"),
                        new ProcessSettings()
                    {
                        Arguments = "/resultsfile:\"/Working/TestResult.trx\""
                    });

                    // Quick fix to avoid throwing exception while intercepting action
                    if (intercepting)
                    {
                        intercepting = false;
                    }
                    else
                    {
                        throw exception;
                    }
                };

                // When
                var result = Record.Exception(() => fixture.Run());

                // Then
                AssertEx.IsCakeException(result, exception.Message);
            }
Example #2
0
        /// <summary>
        /// Runs SpecFlow Test Execution Report with the specified settings.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="action">The action.</param>
        /// <param name="projectFile">The project file path.</param>
        /// <param name="settings">The settings.</param>
        public void Run(ICakeContext context,
                        Action <ICakeContext> action,
                        FilePath projectFile,
                        SpecFlowTestExecutionReportSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            if (projectFile == null)
            {
                throw new ArgumentNullException(nameof(projectFile));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            // Run the tool using the interceptor.
            var interceptor = InterceptAction(context, action);

            // Get / Verify Arguments
            var builder = GetArguments(interceptor, settings, projectFile);

            // Execute the action
            CakeException testException = null;

            try
            {
                action(context);
            }
            catch (CakeException e)
            {
                // Write warning to log
                context.Warning(e.Message);
                testException = e;
            }

            // Run the tool.
            Run(settings, builder);

            if (settings.ThrowOnTestFailure && testException != null)
            {
                throw testException;
            }
        }
Example #3
0
            public void Should_Add_ErrorMessageWithException()
            {
                // Given
                var fixture = new AppVeyorFixture();

                fixture.IsRunningOnAppVeyor();
                var          appVeyor  = fixture.CreateAppVeyorService();
                const string message   = "Hello world";
                var          exception = new CakeException("This is an exception", new ArgumentException());

                // When
                appVeyor.AddErrorMessage(message, exception);

                // Then
                fixture.ProcessRunner.Received(1).Start(
                    Arg.Is <FilePath>(p => p.FullPath == "appveyor"),
                    Arg.Is <ProcessSettings>(p => p.Arguments.Render() == string.Format("AddMessage \"{0}\" -Category \"Error\" -Details \"{1}\"", message, exception.ToString())));
            }
Example #4
0
            public void Should_Not_Rethrow_Exception_From_Action()
            {
                // Given
                var exception = new CakeException("The exception message");
                var fixture   = new SpecFlowTestExecutionReporterFixture();

                fixture.Settings.ThrowOnTestFailure = false;
                var intercepting = true;

                fixture.Action = context =>
                {
                    var process = context.ProcessRunner.Start(
                        new FilePath("/Working/tools/MSTest.exe"),
                        new ProcessSettings()
                    {
                        Arguments = "/resultsfile:\"/Working/TestResult.trx\""
                    });

                    // Quick fix to avoid throwing exception while intercepting action
                    if (intercepting)
                    {
                        intercepting = false;
                    }
                    else
                    {
                        throw exception;
                    }
                };

                // When
                var result = fixture.Run();

                // Then
                Assert.Equal("mstestexecutionreport \"/Working/Tests.csproj\" " +
                             "/testResult:\"/Working/TestResult.trx\"", result.Args);
            }
Example #5
0
        /// <summary>
        /// Logs exception and returns exit code if available in exception.
        /// </summary>
        /// <typeparam name="T">The exception type.</typeparam>
        /// <param name="log">The log.</param>
        /// <param name="ex">The exception.</param>
        /// <returns>1 or exit code provided by <see cref="CakeException.ExitCode"/>.</returns>
        public static int LogException <T>(this ICakeLog log, T ex)
            where T : Exception
        {
            log = log ?? new CakeBuildLog(
                new CakeConsole(new CakeEnvironment(new CakePlatform(), new CakeRuntime())));

            if (log.Verbosity == Verbosity.Diagnostic)
            {
                log.Error("Error: {0}", ex);
            }
            else
            {
                log.Error("Error: {0}", ex.Message);
                if (ex is AggregateException aex)
                {
                    foreach (var exception in aex.Flatten().InnerExceptions)
                    {
                        log.Error("\t{0}", exception.Message);
                    }
                }
            }

            var exitCode = ex switch
            {
                CakeException cex => cex.ExitCode,
                AggregateException aex => aex
                .InnerExceptions
                .OfType <CakeException>()
                .Select(cex => cex.ExitCode as int?)
                .FirstOrDefault() ?? 1,
                _ => 1
            };

            return(exitCode);
        }
    }
Example #6
0
        public void Should_Return_Custom_ExitCode()
        {
            var exception = new CakeException(5);

            Assert.Equal(5, exception.ExitCode);
        }
Example #7
0
        public void Should_Return_Default_ExitCode()
        {
            var exception = new CakeException();

            Assert.Equal(1, exception.ExitCode);
        }