Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH1172: Adding GetStandardError to IProcess and RedirectStandardError to ProcessSettings #1175

Merged
merged 2 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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