Skip to content

Commit

Permalink
Add option 'no_tty' to silent the default tty report
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Carlsson committed Dec 11, 2011
1 parent e3247f9 commit a46aeb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
7 changes: 5 additions & 2 deletions src/eunit.erl
Expand Up @@ -144,7 +144,7 @@ test(Tests, Options) ->
%% @private
%% @doc See {@link test/2}.
test(Server, Tests, Options) ->
Listeners = [eunit_tty:start(Options) | listeners(Options)],
Listeners = listeners(Options),
Serial = eunit_serial:start(Listeners),
case eunit_server:start_test(Server, Serial, Tests, Options) of
{ok, Reference} -> test_run(Reference, Listeners);
Expand Down Expand Up @@ -199,7 +199,10 @@ submit(Server, T, Options) ->
eunit_server:start_test(Server, Dummy, T, Options).

listeners(Options) ->
Ps = start_listeners(proplists:get_all_values(report, Options)),
%% note that eunit_tty must always run, because it sends the final
%% {result,...} message that the test_run() function is waiting for
Ls = [{eunit_tty, Options} | proplists:get_all_values(report, Options)],
Ps = start_listeners(Ls),
%% the event_log option is for debugging, to view the raw events
case proplists:get_value(event_log, Options) of
undefined ->
Expand Down
59 changes: 34 additions & 25 deletions src/eunit_tty.erl
Expand Up @@ -49,6 +49,7 @@ start(Options) ->

init(Options) ->
St = #state{verbose = proplists:get_bool(verbose, Options)},
put(no_tty, proplists:get_bool(no_tty, Options)),
receive
{start, _Reference} ->
if St#state.verbose -> print_header();
Expand All @@ -64,30 +65,30 @@ terminate({ok, Data}, St) ->
Cancel = proplists:get_value(cancel, Data, 0),
if Fail =:= 0, Skip =:= 0, Cancel =:= 0 ->
if Pass =:= 0 ->
io:fwrite(" There were no tests to run.\n");
fwrite(" There were no tests to run.\n");
true ->
if St#state.verbose -> print_bar();
true -> ok
end,
if Pass =:= 1 ->
io:fwrite(" Test passed.\n");
fwrite(" Test passed.\n");
true ->
io:fwrite(" All ~w tests passed.\n", [Pass])
fwrite(" All ~w tests passed.\n", [Pass])
end
end,
sync_end(ok);
true ->
print_bar(),
io:fwrite(" Failed: ~w. Skipped: ~w. Passed: ~w.\n",
[Fail, Skip, Pass]),
fwrite(" Failed: ~w. Skipped: ~w. Passed: ~w.\n",
[Fail, Skip, Pass]),
if Cancel =/= 0 ->
io:fwrite("One or more tests were cancelled.\n");
fwrite("One or more tests were cancelled.\n");
true -> ok
end,
sync_end(error)
end;
terminate({error, Reason}, _St) ->
io:fwrite("Internal error: ~P.\n", [Reason, 25]),
fwrite("Internal error: ~P.\n", [Reason, 25]),
sync_end(error).

sync_end(Result) ->
Expand All @@ -98,10 +99,10 @@ sync_end(Result) ->
end.

print_header() ->
io:fwrite("======================== EUnit ========================\n").
fwrite("======================== EUnit ========================\n").

print_bar() ->
io:fwrite("=======================================================\n").
fwrite("=======================================================\n").


handle_begin(group, Data, St) ->
Expand Down Expand Up @@ -175,18 +176,18 @@ handle_cancel(test, Data, St) ->


indent(N) when is_integer(N), N >= 1 ->
io:put_chars(lists:duplicate(N * 2, $\s));
fwrite(lists:duplicate(N * 2, $\s));
indent(_N) ->
ok.

print_group_start(I, Desc) ->
indent(I),
io:fwrite("~s\n", [Desc]).
fwrite("~s\n", [Desc]).

print_group_end(I, Time) ->
if Time > 0 ->
indent(I),
io:fwrite("[done in ~.3f s]\n", [Time/1000]);
fwrite("[done in ~.3f s]\n", [Time/1000]);
true ->
ok
end.
Expand All @@ -203,46 +204,45 @@ print_test_begin(I, Data) ->
end,
case proplists:get_value(source, Data) of
{Module, Name, _Arity} ->
io:fwrite("~s:~s ~s~s...", [Module, L, Name, D]);
fwrite("~s:~s ~s~s...", [Module, L, Name, D]);
_ ->
io:fwrite("~s~s...", [L, D])
fwrite("~s~s...", [L, D])
end.

print_test_end(Data) ->
Time = proplists:get_value(time, Data, 0),
T = if Time > 0 -> io_lib:fwrite("[~.3f s] ", [Time/1000]);
true -> ""
end,
io:fwrite("~sok\n", [T]).
fwrite("~sok\n", [T]).

print_test_error({error, Exception}, Data) ->
Output = proplists:get_value(output, Data),
io:fwrite("*failed*\n::~s",
[eunit_lib:format_exception(Exception)]),
fwrite("*failed*\n::~s", [eunit_lib:format_exception(Exception)]),
case Output of
<<>> ->
io:put_chars("\n\n");
fwrite("\n\n");
<<Text:800/binary, _:1/binary, _/binary>> ->
io:fwrite(" output:<<\"~s\">>...\n\n", [Text]);
fwrite(" output:<<\"~s\">>...\n\n", [Text]);
_ ->
io:fwrite(" output:<<\"~s\">>\n\n", [Output])
fwrite(" output:<<\"~s\">>\n\n", [Output])
end;
print_test_error({skipped, Reason}, _) ->
io:fwrite("*did not run*\n::~s\n", [format_skipped(Reason)]).
fwrite("*did not run*\n::~s\n", [format_skipped(Reason)]).

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

print_test_cancel(Reason) ->
io:fwrite(format_cancel(Reason)).
fwrite(format_cancel(Reason)).

print_group_cancel(_I, {blame, _}) ->
ok;
print_group_cancel(I, Reason) ->
indent(I),
io:fwrite(format_cancel(Reason)).
fwrite(format_cancel(Reason)).

format_cancel(undefined) ->
"*skipped*\n";
Expand All @@ -258,3 +258,12 @@ format_cancel({exit, Reason}) ->
[Reason, 15]);
format_cancel({abort, Reason}) ->
eunit_lib:format_error(Reason).

fwrite(String) ->
fwrite(String, []).

fwrite(String, Args) ->
case get(no_tty) of
false -> io:fwrite(String, Args);
true -> ok
end.

0 comments on commit a46aeb5

Please sign in to comment.