Пример #1
0
        public async Task RunAsync_CallsGlobalApplication()
        {
            //Arrange
            var outputWritten = false;
            var startInfo     = new ProcessStartInfo()
            {
                FileName               = "dir",
                WindowStyle            = ProcessWindowStyle.Hidden,
                UseShellExecute        = false,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
                CreateNoWindow         = false,
            };
            var processTask = new ProcessTask(startInfo, cancellationToken);

            processTask.OutputReceived += (sender, args) => {
                var e = args as DataReceivedEventArgs;
                Assert.IsNotNull(e);

                if (e.Data != null)
                {
                    outputWritten = true;
                }
                Console.WriteLine(e.Data);
            };

            //Act
            var result = await processTask.RunAsync();

            //Assert
            Assert.AreEqual(0, result);
            Assert.IsTrue(outputWritten);
        }
Пример #2
0
        public async Task RunAsync_RaisesOutputEvent()
        {
            //Arrange
            using (var tempFile = new TemporaryFile("bat"))
            {
                var batchScript = @"
                    @echo off
                    ECHO TestOutput
                    EXIT /B 0
                ";

                File.WriteAllText(tempFile.FileName, batchScript);

                var processTask = new ProcessTask(tempFile.FileName, cancellationToken);
                processTask.OutputReceived += (sender, args) => {
                    var e = args as DataReceivedEventArgs;
                    Assert.IsNotNull(e);

                    if (e.Data != null)
                    {
                        Assert.AreEqual("TestOutput", e.Data);
                    }
                };

                //Act
                var result = await processTask.RunAsync();

                //Assert
                Assert.AreEqual(0, result);
            }
        }
Пример #3
0
        public async Task RunAsync_ReturnErrorCode()
        {
            using (var tempFile = new TemporaryFile("bat"))
            {
                var batchScript = @"
                    @echo off 
                    EXIT /B 1
                ";

                File.WriteAllText(tempFile.FileName, batchScript);

                var processTask = new ProcessTask(tempFile.FileName, cancellationToken);
                var result      = await processTask.RunAsync();

                Assert.AreEqual(1, result);
            }
        }
Пример #4
0
        public async Task RunAsync_DoesNotOverrideArguments()
        {
            //Arrange
            var startInfo = new ProcessStartInfo()
            {
                FileName               = "dotnet",
                Arguments              = $"--version",
                WindowStyle            = ProcessWindowStyle.Hidden,
                UseShellExecute        = false,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
                CreateNoWindow         = false,
            };
            var processTask = new ProcessTask(startInfo, cancellationToken);

            //Act
            var result = await processTask.RunAsync();

            //Assert
            Assert.AreEqual("--version", startInfo.Arguments);
            Assert.AreEqual(0, result);
        }
Пример #5
0
        public async Task RunAsync_ReturnsSuccessCode()
        {
            //Arrange
            using (var tempFile = new TemporaryFile("bat"))
            {
                var batchScript = @"
                    @echo off
                    EXIT /B 0
                ";

                File.WriteAllText(tempFile.FileName, batchScript);

                var processTask = new ProcessTask(tempFile.FileName, cancellationToken);


                //Act
                var result = await processTask.RunAsync();

                //Assert
                Assert.AreEqual(0, result);
            }
        }
Пример #6
0
        public async Task RunAsync_ErrorCodePropregated()
        {
            //Arrange
            using (var temp = new TemporaryFile("bat"))
            {
                using (var innerFile = new TemporaryFile("bat"))
                {
                    var batchScript = @"
                        @echo off
                        exit /B 1
                    ";

                    File.WriteAllText(innerFile.FileName, batchScript);

                    var outerScript = $@"
                        @echo off
                        call {innerFile.FileName}
                        @if errorlevel 1 goto err
                        @GOTO DONE 
                        :ERR 
                        EXIT/B 1
                        @GOTO COMPLETE
                        :DONE
                        :COMPLETE
                    ";

                    File.WriteAllText(temp.FileName, outerScript);

                    var processTask = new ProcessTask(temp.FileName, cancellationToken);

                    //Act
                    var result = await processTask.RunAsync();

                    //Assert
                    Assert.AreEqual(1, result);
                }
            }
        }
Пример #7
0
        public async Task RunAsync_HandlesNestedScripts()
        {
            //Arrange
            using (var temp = new TemporaryFile("bat"))
            {
                using (var innerFile = new TemporaryFile("bat"))
                {
                    var batchScript = $@"
                        @echo off 
                        echo Testing
                    ";

                    File.WriteAllText(innerFile.FileName, batchScript);

                    var outerScript = $@"
                        @echo off
                        call {innerFile.FileName}
                        @if errorlevel 1 goto err
                        @GOTO DONE 
                        :ERR 
                        EXIT/B 1
                        :DONE
                    ";

                    File.WriteAllText(temp.FileName, outerScript);

                    var processTask = new ProcessTask(temp.FileName, cancellationToken);

                    //Act
                    var result = await processTask.RunAsync();

                    //Assert
                    Assert.AreEqual(0, result);
                }
            }
        }