From f13338d68fa1a78353bdf28ccd849c8638ec3ff2 Mon Sep 17 00:00:00 2001 From: Atila Neves Date: Wed, 1 May 2024 15:54:48 -0300 Subject: [PATCH] Fix #311 - don't disable stderr by default --- .../runner/source/unit_threaded/runner/io.d | 16 +++++++++++- tests/unit_threaded/ut/io.d | 25 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/subpackages/runner/source/unit_threaded/runner/io.d b/subpackages/runner/source/unit_threaded/runner/io.d index b02e547..cac8941 100644 --- a/subpackages/runner/source/unit_threaded/runner/io.d +++ b/subpackages/runner/source/unit_threaded/runner/io.d @@ -20,6 +20,7 @@ void writelnUt(T...)(auto ref T args) { private shared(bool) _debugOutput = false; ///print debug msgs? +private shared(bool) _silenceStderr = false; ///print debug msgs? private shared(bool) _forceEscCodes = false; ///use ANSI escape codes anyway? package(unit_threaded) immutable bool _useEscCodes; @@ -89,6 +90,18 @@ package bool isDebugOutputEnabled() nothrow @trusted { } } +package bool isStderrSilenced() @safe nothrow { + synchronized { + return _silenceStderr; + } +} + +void disableStderr(bool value = true) @safe nothrow { + synchronized { + _silenceStderr = value; + } +} + package void forceEscCodes() nothrow { synchronized { _forceEscCodes = true; @@ -272,7 +285,8 @@ void threadWriter(alias OUT, alias ERR)(from!"std.concurrency".Tid tid) if (!isDebugOutputEnabled()) { OUT = typeof(OUT)(nullFileName, "w"); - ERR = typeof(ERR)(nullFileName, "w"); + if(isStderrSilenced) + ERR = typeof(ERR)(nullFileName, "w"); } void actuallyPrint(in string msg) { diff --git a/tests/unit_threaded/ut/io.d b/tests/unit_threaded/ut/io.d index 755d988..c4ae2f2 100644 --- a/tests/unit_threaded/ut/io.d +++ b/tests/unit_threaded/ut/io.d @@ -113,6 +113,28 @@ unittest { import unit_threaded.should; enableDebugOutput(false); + disableStderr(false); + resetFakeFiles; + + auto tid = spawn(&threadWriter!(gOut, gErr), thisTid); + tid.send(ThreadWait()); + receiveOnly!ThreadStarted; + + // stdoutshould have been redirected but not stderr + gOut.shouldEqual(shared FakeFile(nullFileName, "w")); + gErr.shouldEqual(shared FakeFile("err", "mode")); + + tid.send(ThreadFinish()); + receiveOnly!ThreadEnded; +} + +unittest { + import std.concurrency: spawn, thisTid, send, receiveOnly; + import unit_threaded.should; + + enableDebugOutput(false); + disableStderr(true); + scope(exit) disableStderr(false); resetFakeFiles; auto tid = spawn(&threadWriter!(gOut, gErr), thisTid); @@ -132,6 +154,7 @@ unittest { import unit_threaded.should; enableDebugOutput(true); + disableStderr(false); scope(exit) enableDebugOutput(false); resetFakeFiles; @@ -454,7 +477,7 @@ unittest { oops(); writer.output.splitLines.should == [ "OopsTest:", - " " ~ buildPath("tests", "unit_threaded", "ut", "io.d") ~ ":433 - oops", + " " ~ buildPath("tests", "unit_threaded", "ut", "io.d") ~ ":459 - oops", "", ]; }