Exemplo n.º 1
0
        public static bool DoChore(string name, Action action, bool continueOnFail = false)
        {
            WriteFormat(ChoreOptions.StartedFormat, new[] { name, "started" }, ChoreOptions.StartedColors, ChoreOptions.StartedColors.Count);

            PushGroup("job");

            ++layer;

            ConsoleOutWrapper.Capture();
            ConsoleErrWrapper.Capture();

            ConsoleErrWrapper.ResetWrittenTo();

            var success   = false;
            var stopWatch = new Stopwatch();

            try
            {
                stopWatch.Start();
                action?.Invoke();
                stopWatch.Stop();
                success = !ConsoleErrWrapper.WasWrittenTo;
            }
            catch (Exception x)
            {
                stopWatch.Stop();
                Write(x, "Error while running job:");
            }

            // TODO: This is not correct, move this logic
            PopGroup(ConsoleErrWrapper.WasWrittenTo || ConsoleOutWrapper.WasWrittenTo);

            if (--layer == 0)
            {
                ConsoleOutWrapper.Release();
                ConsoleErrWrapper.Release();
            }

            if (success)
            {
                WriteFormat(ChoreOptions.EndedFormat, new object[] { name, "succeeded" }, ChoreOptions.SucceededColors, ChoreOptions.SucceededColors.Count);
            }
            else
            {
                WriteFormat(ChoreOptions.EndedFormat, new object[] { name, "failed" }, ChoreOptions.FailedColors, ChoreOptions.FailedColors.Count);
            }

            Write(stopWatch.Elapsed);

            WriteLine($".\n");

            if (!success && !continueOnFail)
            {
                throw new Exception($"Task {name} failed! Aborting.");
            }

            return(success);
        }
        public void CaptureStandardError()
        {
            var logger = new LocalLambdaLogger();

            using (var captiure = new ConsoleOutWrapper(logger))
            {
                Console.Error.WriteLine("CAPTURED");
            }
            Console.Error.WriteLine("NOT_CAPTURED");

            Assert.Contains("CAPTURED", logger.Buffer);
            Assert.DoesNotContain("NOT_CAPTURED", logger.Buffer);
        }