public async Task AsyncStreamWriterBasicTests(string input, string[] expects)
        {
            var size = 4;
            var encoding = Console.OutputEncoding;
            var actual = new List<string>();
            Action<string> onWrite = data => actual.Add(data);

            // Test
            using (var writer = new AsyncStreamWriter(onWrite, encoding))
            {
                var bytes = encoding.GetBytes(input);
                for (int i = 0; i < bytes.Length; i += size)
                {
                    await writer.WriteAsync(bytes, i, Math.Min(size, bytes.Length - i), default(CancellationToken));
                }
            }

            // Assert
            Assert.Equal(expects, actual);
        }
Пример #2
0
        private Tuple<string, string> ExecuteInternal(ITracer tracer, Func<string, bool> onWriteOutput, Func<string, bool> onWriteError, Encoding encoding, string arguments, params object[] args)
        {
            var cmdArguments = String.Format(arguments, args);
            var errorBuffer = new StringBuilder();
            var outputBuffer = new StringBuilder();
            var idleManager = new IdleManager(IdleTimeout, tracer);
            var outputStream = new AsyncStreamWriter(data =>
            {
                idleManager.UpdateActivity();
                if (data != null)
                {
                    if (onWriteOutput(data))
                    {
                        outputBuffer.AppendLine(Encoding.UTF8.GetString(encoding.GetBytes(data)));
                    }
                }
            }, Encoding ?? Console.OutputEncoding);
            var errorStream = new AsyncStreamWriter(data =>
            {
                idleManager.UpdateActivity();
                if (data != null)
                {
                    if (onWriteError(data))
                    {
                        errorBuffer.AppendLine(Encoding.UTF8.GetString(encoding.GetBytes(data)));
                    }
                }
            }, Encoding ?? Console.OutputEncoding);

            int exitCode;
            try
            {
                // common execute
                exitCode = Task.Run(() => ExecuteAsync(tracer, cmdArguments, outputStream, errorStream, idleManager: idleManager)).Result;
            }
            catch (AggregateException ex)
            {
                foreach (var inner in ex.Flatten().InnerExceptions)
                {
                    onWriteError(inner.Message);
                }
                throw;
            }
            catch (Exception ex)
            {
                onWriteError(ex.Message);
                throw;
            }
            finally
            {
                // flush out last buffer if any
                outputStream.Dispose();
                errorStream.Dispose();
            }

            string output = outputBuffer.ToString().Trim();
            string error = errorBuffer.ToString().Trim();

            if (exitCode != 0)
            {
                throw new CommandLineException(Path, cmdArguments, !String.IsNullOrEmpty(error) ? error : output)
                {
                    ExitCode = exitCode,
                    Output = output,
                    Error = error
                };
            }

            return Tuple.Create(output, error);
        }
Пример #3
0
        private Tuple <string, string> ExecuteInternal(ITracer tracer, Func <string, bool> onWriteOutput, Func <string, bool> onWriteError, Encoding encoding, string arguments, params object[] args)
        {
            var cmdArguments = String.Format(arguments, args);
            var errorBuffer  = new StringBuilder();
            var outputBuffer = new StringBuilder();
            var idleManager  = new IdleManager(IdleTimeout, tracer);
            var outputStream = new AsyncStreamWriter(data =>
            {
                idleManager.UpdateActivity();
                if (data != null)
                {
                    if (onWriteOutput(data))
                    {
                        outputBuffer.AppendLine(Encoding.UTF8.GetString(encoding.GetBytes(data)));
                    }
                }
            }, Encoding ?? Console.OutputEncoding);
            var errorStream = new AsyncStreamWriter(data =>
            {
                idleManager.UpdateActivity();
                if (data != null)
                {
                    if (onWriteError(data))
                    {
                        errorBuffer.AppendLine(Encoding.UTF8.GetString(encoding.GetBytes(data)));
                    }
                }
            }, Encoding ?? Console.OutputEncoding);

            int exitCode;

            try
            {
                // common execute
                exitCode = Task.Run(() => ExecuteAsync(tracer, cmdArguments, outputStream, errorStream, idleManager: idleManager)).Result;
            }
            catch (AggregateException ex)
            {
                foreach (var inner in ex.Flatten().InnerExceptions)
                {
                    onWriteError(inner.Message);
                }
                throw;
            }
            catch (Exception ex)
            {
                onWriteError(ex.Message);
                throw;
            }
            finally
            {
                // flush out last buffer if any
                outputStream.Dispose();
                errorStream.Dispose();
            }

            string output = outputBuffer.ToString().Trim();
            string error  = errorBuffer.ToString().Trim();

            if (exitCode != 0)
            {
                throw new CommandLineException(Path, cmdArguments, error)
                      {
                          ExitCode = exitCode,
                          Output   = output,
                          Error    = error
                      };
            }

            return(Tuple.Create(output, error));
        }