コード例 #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);
                }
            }
        }