Skip to content

Commit a46aeb5

Browse files
author
Richard Carlsson
committed
Add option 'no_tty' to silent the default tty report
1 parent e3247f9 commit a46aeb5

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/eunit.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ test(Tests, Options) ->
144144
%% @private
145145
%% @doc See {@link test/2}.
146146
test(Server, Tests, Options) ->
147-
Listeners = [eunit_tty:start(Options) | listeners(Options)],
147+
Listeners = listeners(Options),
148148
Serial = eunit_serial:start(Listeners),
149149
case eunit_server:start_test(Server, Serial, Tests, Options) of
150150
{ok, Reference} -> test_run(Reference, Listeners);
@@ -199,7 +199,10 @@ submit(Server, T, Options) ->
199199
eunit_server:start_test(Server, Dummy, T, Options).
200200

201201
listeners(Options) ->
202-
Ps = start_listeners(proplists:get_all_values(report, Options)),
202+
%% note that eunit_tty must always run, because it sends the final
203+
%% {result,...} message that the test_run() function is waiting for
204+
Ls = [{eunit_tty, Options} | proplists:get_all_values(report, Options)],
205+
Ps = start_listeners(Ls),
203206
%% the event_log option is for debugging, to view the raw events
204207
case proplists:get_value(event_log, Options) of
205208
undefined ->

src/eunit_tty.erl

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ start(Options) ->
4949

5050
init(Options) ->
5151
St = #state{verbose = proplists:get_bool(verbose, Options)},
52+
put(no_tty, proplists:get_bool(no_tty, Options)),
5253
receive
5354
{start, _Reference} ->
5455
if St#state.verbose -> print_header();
@@ -64,30 +65,30 @@ terminate({ok, Data}, St) ->
6465
Cancel = proplists:get_value(cancel, Data, 0),
6566
if Fail =:= 0, Skip =:= 0, Cancel =:= 0 ->
6667
if Pass =:= 0 ->
67-
io:fwrite(" There were no tests to run.\n");
68+
fwrite(" There were no tests to run.\n");
6869
true ->
6970
if St#state.verbose -> print_bar();
7071
true -> ok
7172
end,
7273
if Pass =:= 1 ->
73-
io:fwrite(" Test passed.\n");
74+
fwrite(" Test passed.\n");
7475
true ->
75-
io:fwrite(" All ~w tests passed.\n", [Pass])
76+
fwrite(" All ~w tests passed.\n", [Pass])
7677
end
7778
end,
7879
sync_end(ok);
7980
true ->
8081
print_bar(),
81-
io:fwrite(" Failed: ~w. Skipped: ~w. Passed: ~w.\n",
82-
[Fail, Skip, Pass]),
82+
fwrite(" Failed: ~w. Skipped: ~w. Passed: ~w.\n",
83+
[Fail, Skip, Pass]),
8384
if Cancel =/= 0 ->
84-
io:fwrite("One or more tests were cancelled.\n");
85+
fwrite("One or more tests were cancelled.\n");
8586
true -> ok
8687
end,
8788
sync_end(error)
8889
end;
8990
terminate({error, Reason}, _St) ->
90-
io:fwrite("Internal error: ~P.\n", [Reason, 25]),
91+
fwrite("Internal error: ~P.\n", [Reason, 25]),
9192
sync_end(error).
9293

9394
sync_end(Result) ->
@@ -98,10 +99,10 @@ sync_end(Result) ->
9899
end.
99100

100101
print_header() ->
101-
io:fwrite("======================== EUnit ========================\n").
102+
fwrite("======================== EUnit ========================\n").
102103

103104
print_bar() ->
104-
io:fwrite("=======================================================\n").
105+
fwrite("=======================================================\n").
105106

106107

107108
handle_begin(group, Data, St) ->
@@ -175,18 +176,18 @@ handle_cancel(test, Data, St) ->
175176

176177

177178
indent(N) when is_integer(N), N >= 1 ->
178-
io:put_chars(lists:duplicate(N * 2, $\s));
179+
fwrite(lists:duplicate(N * 2, $\s));
179180
indent(_N) ->
180181
ok.
181182

182183
print_group_start(I, Desc) ->
183184
indent(I),
184-
io:fwrite("~s\n", [Desc]).
185+
fwrite("~s\n", [Desc]).
185186

186187
print_group_end(I, Time) ->
187188
if Time > 0 ->
188189
indent(I),
189-
io:fwrite("[done in ~.3f s]\n", [Time/1000]);
190+
fwrite("[done in ~.3f s]\n", [Time/1000]);
190191
true ->
191192
ok
192193
end.
@@ -203,46 +204,45 @@ print_test_begin(I, Data) ->
203204
end,
204205
case proplists:get_value(source, Data) of
205206
{Module, Name, _Arity} ->
206-
io:fwrite("~s:~s ~s~s...", [Module, L, Name, D]);
207+
fwrite("~s:~s ~s~s...", [Module, L, Name, D]);
207208
_ ->
208-
io:fwrite("~s~s...", [L, D])
209+
fwrite("~s~s...", [L, D])
209210
end.
210211

211212
print_test_end(Data) ->
212213
Time = proplists:get_value(time, Data, 0),
213214
T = if Time > 0 -> io_lib:fwrite("[~.3f s] ", [Time/1000]);
214215
true -> ""
215216
end,
216-
io:fwrite("~sok\n", [T]).
217+
fwrite("~sok\n", [T]).
217218

218219
print_test_error({error, Exception}, Data) ->
219220
Output = proplists:get_value(output, Data),
220-
io:fwrite("*failed*\n::~s",
221-
[eunit_lib:format_exception(Exception)]),
221+
fwrite("*failed*\n::~s", [eunit_lib:format_exception(Exception)]),
222222
case Output of
223223
<<>> ->
224-
io:put_chars("\n\n");
224+
fwrite("\n\n");
225225
<<Text:800/binary, _:1/binary, _/binary>> ->
226-
io:fwrite(" output:<<\"~s\">>...\n\n", [Text]);
226+
fwrite(" output:<<\"~s\">>...\n\n", [Text]);
227227
_ ->
228-
io:fwrite(" output:<<\"~s\">>\n\n", [Output])
228+
fwrite(" output:<<\"~s\">>\n\n", [Output])
229229
end;
230230
print_test_error({skipped, Reason}, _) ->
231-
io:fwrite("*did not run*\n::~s\n", [format_skipped(Reason)]).
231+
fwrite("*did not run*\n::~s\n", [format_skipped(Reason)]).
232232

233233
format_skipped({module_not_found, M}) ->
234-
io_lib:format("missing module: ~w", [M]);
234+
io_lib:fwrite("missing module: ~w", [M]);
235235
format_skipped({no_such_function, {M,F,A}}) ->
236-
io_lib:format("no such function: ~w:~w/~w", [M,F,A]).
236+
io_lib:fwrite("no such function: ~w:~w/~w", [M,F,A]).
237237

238238
print_test_cancel(Reason) ->
239-
io:fwrite(format_cancel(Reason)).
239+
fwrite(format_cancel(Reason)).
240240

241241
print_group_cancel(_I, {blame, _}) ->
242242
ok;
243243
print_group_cancel(I, Reason) ->
244244
indent(I),
245-
io:fwrite(format_cancel(Reason)).
245+
fwrite(format_cancel(Reason)).
246246

247247
format_cancel(undefined) ->
248248
"*skipped*\n";
@@ -258,3 +258,12 @@ format_cancel({exit, Reason}) ->
258258
[Reason, 15]);
259259
format_cancel({abort, Reason}) ->
260260
eunit_lib:format_error(Reason).
261+
262+
fwrite(String) ->
263+
fwrite(String, []).
264+
265+
fwrite(String, Args) ->
266+
case get(no_tty) of
267+
false -> io:fwrite(String, Args);
268+
true -> ok
269+
end.

0 commit comments

Comments
 (0)