Mpdreamz / shellprogressbar

ShellProgressBar - display progress in your console application
MIT License
1.44k stars 134 forks source link

allow option to disable rendering for code that's ran by unit testers. #69

Open bonesoul opened 4 years ago

bonesoul commented 4 years ago

right now with in my unit tests getting invalid handle error;

"System.IO.IOException: The handle is invalid.
   at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.Console.get_CursorTop()
   at ShellProgressBar.ProgressBar..ctor(Int32 maxTicks, String message, ProgressBarOptions options)
LevYas commented 3 years ago

I had exactly the same problem. As a quick fix, I used a "proxy" pattern:

public sealed class ProgressBarProxy : IDisposable
{
    private readonly ProgressBar? _progressBar;

    public ProgressBarProxy(int maxTicks, string message)
        => _progressBar = Console.IsOutputRedirected ? null : new ProgressBar(maxTicks, message);

    public void Tick(string? message = null) => _progressBar?.Tick(message);
    public void Dispose() => _progressBar?.Dispose();
}

But it would be nice if that check will be inside the constructor.

mrwensveen commented 3 years ago

Maybe you could implement/mock IProgressBar and use dependency injection in parts that will be unit tested?