Skip to content

Commit

Permalink
Adding GetStandardError to IProcess and RedirectStandardError to Proc…
Browse files Browse the repository at this point in the history
…essSettings. (#1175)
  • Loading branch information
CaptainCow95 authored and devlead committed Oct 20, 2016
1 parent ae42155 commit edd401b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Cake.Common/Tools/DotCover/DotCoverProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public int GetExitCode()
return 0;
}

public IEnumerable<string> GetStandardError()
{
return Enumerable.Empty<string>();
}

public IEnumerable<string> GetStandardOutput()
{
return Enumerable.Empty<string>();
Expand Down
5 changes: 5 additions & 0 deletions src/Cake.Common/Tools/OpenCover/OpenCoverProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public int GetExitCode()
return 0;
}

public IEnumerable<string> GetStandardError()
{
return Enumerable.Empty<string>();
}

public IEnumerable<string> GetStandardOutput()
{
return Enumerable.Empty<string>();
Expand Down
5 changes: 5 additions & 0 deletions src/Cake.Common/Tools/SpecFlow/SpecFlowProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public int GetExitCode()
return 0;
}

public IEnumerable<string> GetStandardError()
{
return Enumerable.Empty<string>();
}

public IEnumerable<string> GetStandardOutput()
{
return Enumerable.Empty<string>();
Expand Down
6 changes: 6 additions & 0 deletions src/Cake.Core/IO/IProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public interface IProcess : IDisposable
/// <returns>The exit code of the process.</returns>
int GetExitCode();

/// <summary>
/// Get the standard error of process.
/// </summary>
/// <returns>Returns process error output <see cref="ProcessSettings.RedirectStandardError">RedirectStandardError</see> is true</returns>
IEnumerable<string> GetStandardError();

/// <summary>
/// Get the standard output of process
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion src/Cake.Core/IO/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,25 @@ public IProcess Start(FilePath filePath, ProcessSettings settings)
? SubscribeStandardConsoleOutputQueue(process)
: null;

return new ProcessWrapper(process, _log, arguments.FilterUnsafe, consoleOutputQueue);
var consoleErrorQueue = settings.RedirectStandardError
? SubscribeStandardConsoleErrorQueue(process)
: null;

return new ProcessWrapper(process, _log, arguments.FilterUnsafe, consoleOutputQueue, arguments.FilterUnsafe, consoleErrorQueue);
}

private static ConcurrentQueue<string> SubscribeStandardConsoleErrorQueue(Process process)
{
var consoleErrorQueue = new ConcurrentQueue<string>();
process.ErrorDataReceived += (s, e) =>
{
if (e.Data != null)
{
consoleErrorQueue.Enqueue(e.Data);
}
};
process.BeginErrorReadLine();
return consoleErrorQueue;
}

private static ConcurrentQueue<string> SubscribeStandardConsoleOutputQueue(Process process)
Expand Down
6 changes: 6 additions & 0 deletions src/Cake.Core/IO/ProcessSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public sealed class ProcessSettings
/// <value>The working directory for the process to be started.</value>
public DirectoryPath WorkingDirectory { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the error output of an application is written to the <see cref="P:System.Diagnostics.Process.StandardError"/> stream.
/// </summary>
/// <value>true if error output should be written to <see cref="P:System.Diagnostics.Process.StandardError"/>; otherwise, false. The default is false.</value>
public bool RedirectStandardError { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the output of an application is written to the <see cref="P:System.Diagnostics.Process.StandardOutput"/> stream.
/// </summary>
Expand Down
24 changes: 23 additions & 1 deletion src/Cake.Core/IO/ProcessWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ internal sealed class ProcessWrapper : IProcess
{
private readonly Process _process;
private readonly ICakeLog _log;
private readonly Func<string, string> _filterError;
private readonly Func<string, string> _filterOutput;
private readonly ConcurrentQueue<string> _consoleErrorQueue;
private readonly ConcurrentQueue<string> _consoleOutputQueue;

public ProcessWrapper(Process process, ICakeLog log, Func<string, string> filterOutput, ConcurrentQueue<string> consoleOutputQueue)
public ProcessWrapper(Process process, ICakeLog log, Func<string, string> filterOutput, ConcurrentQueue<string> consoleOutputQueue, Func<string, string> filterError, ConcurrentQueue<string> consoleErrorQueue)
{
_process = process;
_log = log;
_filterOutput = filterOutput ?? (source => "[REDACTED]");
_consoleOutputQueue = consoleOutputQueue;
_filterError = filterError ?? (source => "[REDACTED]");
_consoleErrorQueue = consoleErrorQueue;
}

public void WaitForExit()
Expand All @@ -49,6 +53,24 @@ public int GetExitCode()
return _process.ExitCode;
}

public IEnumerable<string> GetStandardError()
{
if (_consoleErrorQueue == null)
{
yield break;
}
while (!_consoleErrorQueue.IsEmpty || !_process.HasExited)
{
string line;
if (!_consoleErrorQueue.TryDequeue(out line))
{
continue;
}
_log.Debug(log => log("{0}", _filterOutput(line)));
yield return line;
}
}

public IEnumerable<string> GetStandardOutput()
{
if (_consoleOutputQueue == null)
Expand Down
19 changes: 19 additions & 0 deletions src/Cake.Testing/FakeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Cake.Testing
public sealed class FakeProcess : IProcess
{
private int _exitCode;
private IEnumerable<string> _standardError;
private IEnumerable<string> _standardOutput;

/// <summary>
Expand Down Expand Up @@ -48,6 +49,15 @@ public int GetExitCode()
return _exitCode;
}

/// <summary>
/// Get the standard error of process
/// </summary>
/// <returns>Returns process error output <see cref="ProcessSettings.RedirectStandardError">RedirectStandardError</see> is true</returns>
public IEnumerable<string> GetStandardError()
{
return _standardError;
}

/// <summary>
/// Get the standard output of process
/// </summary>
Expand All @@ -73,6 +83,15 @@ public void SetExitCode(int exitCode)
_exitCode = exitCode;
}

/// <summary>
/// Sets the standard error.
/// </summary>
/// <param name="standardError">The standard error.</param>
public void SetStandardError(IEnumerable<string> standardError)
{
_standardError = standardError;
}

/// <summary>
/// Sets the standard output.
/// </summary>
Expand Down

0 comments on commit edd401b

Please sign in to comment.