Skip to content

Commit

Permalink
Merge pull request #312 from atilaneves/io
Browse files Browse the repository at this point in the history
Fix #311 - don't disable stderr by default
  • Loading branch information
atilaneves authored May 1, 2024
2 parents a4f20ed + f13338d commit edb765e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
22 changes: 17 additions & 5 deletions subpackages/runner/source/unit_threaded/runner/io.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ 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) shared(bool) _useEscCodes;
package(unit_threaded) immutable bool _useEscCodes;


version (Windows) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -259,8 +272,6 @@ void threadWriter(alias OUT, alias ERR)(from!"std.concurrency".Tid tid)
{
import std.concurrency: receive, send, OwnerTerminated, Tid;

auto done = false;

auto saveStdout = OUT;
auto saveStderr = ERR;

Expand All @@ -274,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) {
Expand Down Expand Up @@ -305,7 +317,7 @@ void threadWriter(alias OUT, alias ERR)(from!"std.concurrency".Tid tid)

Tid currentTid;

while (!done) {
for(auto done = false; !done; ) {
receive(
(string msg, Tid originTid) {

Expand Down
36 changes: 31 additions & 5 deletions tests/unit_threaded/ut/io.d
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,35 @@ 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);
tid.send(ThreadWait());
receiveOnly!ThreadStarted;

// stdout/stderr should have been redirected
gOut.shouldEqual(shared FakeFile(nullFileName, "w"));
gErr.shouldEqual(shared FakeFile(nullFileName, "w"));

Expand All @@ -131,13 +154,16 @@ unittest {
import unit_threaded.should;

enableDebugOutput(true);
disableStderr(false);
scope(exit) enableDebugOutput(false);
resetFakeFiles;

auto tid = spawn(&threadWriter!(gOut, gErr), thisTid);
tid.send(ThreadWait());
receiveOnly!ThreadStarted;

// since debug output is enabled we don't expect stdout/stderr to
// be redirected.
gOut.shouldEqual(shared FakeFile("out", "mode"));
gErr.shouldEqual(shared FakeFile("err", "mode"));

Expand Down Expand Up @@ -259,8 +285,8 @@ unittest {

receiveOnly!bool; //wait for spawned thread to do its thing

// from now on, we've send "foo\n" but not flushed
// and the other tid has send "bar\n" and flushed
// from now on, we've sent "foo\n" but not flushed
// and the other tid has sent "bar\n" and flushed

writerTid.send(Flush(), thisTid);

Expand Down Expand Up @@ -298,8 +324,8 @@ unittest {

receiveOnly!bool; //wait for spawned thread to do its thing

// from now on, we've send "foo\n" but not flushed
// and the other tid has send "bar\n", flushed, then "baz\n"
// from now on, we've sent "foo\n" but not flushed
// and the other tid has sent "bar\n", flushed, then "baz\n"

writerTid.send(Flush(), thisTid);

Expand Down Expand Up @@ -451,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",
"",
];
}

0 comments on commit edb765e

Please sign in to comment.