public async Task ErrorHandling()
        {
            using (var consoleIn = new StringReader(string.Empty))
            {
                using (var consoleError = new StringWriter())
                {
                    SetConsoleIn(consoleIn);
                    Console.SetError(consoleError);
                    int exitCode = await ModularInput.RunAsync <TestScript>(new string[] { });

                    // There will be an exception due to missing input definition in
                    // (redirected) console stdin.
                    var error = consoleError.ToString();

                    // Verify that an exception is logged with level FATAL.
                    Assert.Contains("FATAL Script.Run: Unhandled exception:", error);

                    // Verify that the exception is what we expect.
                    Assert.Contains("Root element is missing", error);

                    // Verify that an info level message is logged properly.
                    Assert.Contains("INFO Script.Run: Reading input definition", error);

                    // Verify that the logged exception does not span more than one line
                    // Splunk breaks up events using new lines for splunkd log.
                    var lines = error.Split(
                        new[] { Environment.NewLine },
                        StringSplitOptions.RemoveEmptyEntries);

                    Assert.Equal(2, lines.Length);
                    Assert.NotEqual(0, exitCode);
                }
            }
        }
        public void ShoulldWaitForAttachReturnsTrueWhenValidateArgumentsArgIsPassed()
        {
            var args       = new[] { "--validate-arguments" };
            var shouldWait = ModularInput.ShouldWaitForDebuggerToAttach(args, DebuggerAttachPoints.ValidateArguments);

            Assert.True(shouldWait);
        }
 public void ShouldThrowExceptionWhenRunIsCalledIfTimeIsZeroAndAttachPointsWereSet()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         ModularInput.Run <TestDebugInput>(new string[0], DebuggerAttachPoints.StreamEvents, 0);
     });
 }
        public void ShouldWaitForDebuggerToAttachReturnsTrueWhenNoArgIsPassed()
        {
            var args       = new string[0];
            var shouldWait = ModularInput.ShouldWaitForDebuggerToAttach(args, DebuggerAttachPoints.StreamEvents);

            Assert.True(shouldWait);
        }
        public async Task OutputScheme()
        {
            using (var consoleOut = new StringWriter())
            {
                Console.SetOut(consoleOut);
                await ModularInput.RunAsync <TestScript>(new[] { "--scheme" });

                AssertEqualWithExpectedFile(SchemeFilePath, consoleOut.ToString());
            }
        }
        public void ShouldWaitUntilTimeout()
        {
            ModularInput._isAttached = () => false;
            var start = DateTime.Now;

            ModularInput.WaitForAttach(5);
            var end = DateTime.Now;

            Assert.True((end - start).Seconds >= 5);
        }
        public async Task ExternalValidation()
        {
            using (var consoleIn = ReadFileFromDataFolderAsReader(ValidationItemsFilePath))
                using (var consoleOut = new StringWriter())
                {
                    SetConsoleIn(consoleIn);
                    Console.SetOut(consoleOut);

                    int exitCode = await ModularInput.RunAsync <TestScript>(new[] { "--validate-arguments" });

                    AssertEqualWithExpectedFile(ValidationErrorMessageFilePath, consoleOut.ToString());
                    Assert.NotEqual(0, exitCode);
                }
        }
        public async Task StreamEvents()
        {
            using (var consoleIn = ReadFileFromDataFolderAsReader(InputDefinitionFilePath))
            {
                using (var consoleOut = new StringWriter())
                {
                    SetConsoleIn(consoleIn);
                    Console.SetOut(consoleOut);
                    await ModularInput.RunAsync <TestScript>(new string[] { });

                    AssertEqualWithExpectedFile(EventsFilePath, consoleOut.ToString());
                }
            }
        }
        public void ShouldWaitForDebuggerToAttachReturnsFalseWhenNoneIsPassed()
        {
            var args       = new string[] { "--scheme" };
            var shouldWait = ModularInput.ShouldWaitForDebuggerToAttach(args, DebuggerAttachPoints.None);

            Assert.False(shouldWait);

            args       = new string[] { "--validate-arguments" };
            shouldWait = ModularInput.ShouldWaitForDebuggerToAttach(args, DebuggerAttachPoints.None);
            Assert.False(shouldWait);

            args       = new string[0];
            shouldWait = ModularInput.ShouldWaitForDebuggerToAttach(args, DebuggerAttachPoints.None);
            Assert.False(shouldWait);
        }
        public void ShouldWaitUntilAttach()
        {
            ModularInput._isAttached = () => false;
            var start  = DateTime.Now;
            var exited = false;

            var thread = new Thread(() =>
            {
                ModularInput.WaitForAttach(10);
                exited = true;
            });

            thread.Start();

            ModularInput._isAttached = () => true;
            Thread.Sleep(1000);
            var end = DateTime.Now;

            Assert.True((end - start).Seconds <= 2);
            Assert.True(exited);
        }
 public void ShouldNotThrowExceptionWhenRunIsCalledIfTimeIsZeroAndAttachPointsWereNotSet()
 {
     ModularInput.Run <TestDebugInput>(new string[0], DebuggerAttachPoints.None, 0);
 }
 public void ShouldInvokeRunAsyncWhenRunIsCalled()
 {
     ModularInput.Run <TestDebugInput>(new string[0]);
     Assert.True(TestDebugInput.Executed);
 }