Skip to content

Commit b5ea032

Browse files
committed
Allow running tests more silently
When running tests at present via `make test`, the developer is met with lots of messages from both the testing script and logger messages emitted through libraries called by the testcases themselves. This clutters the output and makes it hard to distinguish actual test suite failures from regular progress and other logging messages. This commit introduces support for interpreting the `-s` / `--silent` / `--quiet` option from `make` and utilizing it to run tests in a more quiet manner. Specifically, this means that log output is fully captured by `cth_log_redirect` (instead of logging to both stdout and the CT log), and the banners and disclaimers from the testing script are reduced to a minimum (under the assumption that the person running the tests is familar with them). To accomplish this, the `cth_log_redirect` handler is augmented with the `mode` option, which instructs it how to work. The previous behaviour - logging to both stdout and the Common Test logs - is kept by default for now. As a small improvement that came up whilst implementing this change, `logger_std_h` is updated to skip logging filesync errors when instructeed to log to `/dev/null`. In `inets`, this reduces the output emitted by the tests from 2175 to 803 lines.
1 parent ac08794 commit b5ea032

File tree

8 files changed

+72
-19
lines changed

8 files changed

+72
-19
lines changed

HOWTO/DEVELOPMENT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ for all process you would do this:
213213
ERL_ARGS="+hmqd off_heap" make emulator_test
214214
```
215215

216+
If you want logger messages to only be printed into the Common Test HTML logs
217+
and keep test script information about starting and stopping the run to a
218+
minimum, you can pass `-s` or `--silent`:
219+
220+
```bash
221+
make test -s
222+
```
223+
216224
### Build and test a specific application
217225

218226
You can also build the application from within itself. Like this:

Makefile.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,12 @@ bootstrap_clean:
10071007

10081008
.PHONY: test dialyzer
10091009

1010+
ifeq ($(findstring s,${MAKEFLAGS}),s)
1011+
TEST_EXTRA_ARGS = --quiet
1012+
endif
1013+
10101014
test: all release release_tests
1011-
$(ERL_TOP)/make/test_target_script.sh $(ERL_TOP)
1015+
$(ERL_TOP)/make/test_target_script.sh $(TEST_EXTRA_ARGS) $(ERL_TOP)
10121016

10131017
ifeq ($(TYPE),)
10141018
dialyzer: all

lib/common_test/doc/src/ct_hooks_chapter.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,22 @@ results(State) ->
492492
use another level either change the <c>default</c> handler level before
493493
starting common_test, or use the <seemfa marker="kernel:logger#set_handler_config/3">
494494
<c>logger:set_handler_config/3</c></seemfa> API.</p>
495+
<p>This hook supports the following options:</p>
496+
<taglist>
497+
<tag><c>{mode, replace}</c></tag>
498+
<item>
499+
<p>Replace the <c>default</c> logging handler by the log redirect
500+
instead of logging to both the default handler and the
501+
<c>cth_log_redirect</c> handler. To use this mode, disable the builtin
502+
hook and reconfigure it:</p>
503+
<p><c>-enable_builtin_hooks false -ct_hooks cth_log_redirect [{mode,replace}]</c></p>
504+
</item>
505+
<tag><c>{mode, add}</c></tag>
506+
<item>
507+
<p>Add the logging handler instead of replacing the default logging
508+
handler. This is the default behaviour.</p>
509+
</item>
510+
</taglist>
495511
</item>
496512
<tag><c>cth_surefire</c></tag>
497513
<item>

lib/common_test/src/ct_hooks.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
%% All of the hooks which are to be started by default. Remove by issuing
3737
%% -enable_builtin_hooks false to when starting common test.
3838
-define(BUILTIN_HOOKS,[#ct_hook_config{ module = cth_log_redirect,
39-
opts = [],
39+
opts = [{mode, add}],
4040
prio = ctfirst }]).
4141

4242
-record(ct_hook_config, {id, module, prio, scope, opts = [],

lib/common_test/src/cth_log_redirect.erl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
%%% Common Test Framework functions handling test specifications.
2323
%%%
24-
%%% This module redirects sasl and error logger info to common test log.
24+
%%% This module redirects sasl, error logger, and standard logger messages to
25+
%%% the common test log.
2526

2627
%% CTH Callbacks
2728
-export([id/1, init/2,
@@ -55,10 +56,10 @@
5556
id(_Opts) ->
5657
?MODULE.
5758

58-
init(?MODULE, _Opts) ->
59+
init(?MODULE, Opts) ->
5960
ct_util:mark_process(),
60-
ok = start_log_handler(),
61-
tc_log_async.
61+
ok = start_log_handler(Opts),
62+
{ok, tc_log_async}.
6263

6364
pre_init_per_suite(Suite, Config, State) ->
6465
set_curr_func({Suite,init_per_suite}, Config),
@@ -114,7 +115,7 @@ post_end_per_group(_Suite, _Group, Config, Return, State) ->
114115
set_curr_func({group,undefined}, Config),
115116
{Return, State}.
116117

117-
start_log_handler() ->
118+
start_log_handler(Options) ->
118119
case whereis(?MODULE) of
119120
undefined ->
120121
ChildSpec =
@@ -136,9 +137,14 @@ start_log_handler() ->
136137
_Else ->
137138
{{?DEFAULT_FORMATTER,?DEFAULT_FORMAT_CONFIG},info}
138139
end,
139-
ok = logger:add_handler(?MODULE,?MODULE,
140-
#{level=>DefaultLevel,
141-
formatter=>DefaultFormatter}).
140+
HandlerConfig = #{level => DefaultLevel, formatter => DefaultFormatter},
141+
case proplists:get_value(mode, Options) of
142+
add ->
143+
ok = logger:add_handler(?MODULE, ?MODULE, HandlerConfig);
144+
replace ->
145+
ok = logger:remove_handler(default),
146+
ok = logger:add_handler(default, ?MODULE, HandlerConfig)
147+
end.
142148

143149
init([]) ->
144150
{ok, #eh_state{log_func = tc_log_async}}.
@@ -171,8 +177,6 @@ log(#{msg:={report,Msg},meta:=#{domain:=[otp,sasl]}}=Log,Config) ->
171177
end;
172178
log(#{meta:=#{domain:=[otp]}}=Log,Config) ->
173179
do_log(add_log_category(Log,error_logger),Config);
174-
log(#{meta:=#{domain:=_}},_) ->
175-
ok;
176180
log(Log,Config) ->
177181
do_log(add_log_category(Log,error_logger),Config).
178182

lib/kernel/src/logger_std_h.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ maybe_notify_error(Op, Result, #{write_res:=WR,sync_res:=SR})
673673
(Op==filesync andalso Result==SR) ->
674674
%% don't report same error twice
675675
ok;
676+
maybe_notify_error(filesync, {error, einval}, #{file_name:="/dev/null"}) ->
677+
%% If logging to /dev/null, output should be discarded
678+
%% Ignore any EINVAL errors for filesync operations
679+
ok;
676680
maybe_notify_error(Op, Error, #{handler_name:=HandlerName,file_name:=FileName}) ->
677681
logger_h_common:error_notify({HandlerName,Op,FileName,Error}),
678682
ok.

make/app_targets.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ APPLICATION ?= $(basename $(notdir $(PWD)))
2323
.PHONY: test info gclean dialyzer dialyzer_plt dclean
2424

2525
ifndef NO_TEST_TARGET
26+
27+
ifeq ($(findstring s,${MAKEFLAGS}),s)
28+
TEST_ENV = QUIET=1
29+
endif
30+
2631
test:
27-
TEST_NEEDS_RELEASE=$(TEST_NEEDS_RELEASE) TYPE=$(TYPE) \
32+
TEST_NEEDS_RELEASE=$(TEST_NEEDS_RELEASE) TYPE=$(TYPE) $(TEST_ENV) \
2833
$(ERL_TOP)/make/test_target_script.sh $(ERL_TOP)
2934
endif
3035

make/test_target_script.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,27 @@ LIGHT_CYAN='\033[1;36m'
2727
BOLD='\033[1m'
2828
NC='\033[0m'
2929

30+
QUIET="${QUIET:-0}"
31+
if [ "$QUIET" -eq 1 ] && [ "${WSLcross}" != "true" ]; then
32+
# cth_log_redirect is enabled by default, to configure it we need to remove and re-add it
33+
ARGS="-enable_builtin_hooks false -ct_hooks cth_log_redirect [{mode,replace}] ${ARGS}"
34+
fi
35+
3036

3137
print_highlighted_msg_with_printer () {
3238
COLOR=$1
3339
MSG_PRINTER=$2
34-
printf "\n${COLOR}======================================================================${NC}\n"
35-
echo
36-
$MSG_PRINTER
37-
echo
38-
printf "${COLOR}======================================================================${NC}\n"
40+
IMPORTANT=${3:-0}
41+
if [ "$QUIET" -ne 1 ]; then
42+
printf "\n${COLOR}======================================================================${NC}\n"
43+
echo
44+
fi
45+
if [ "$QUIET" -ne 1 ] || [ "$IMPORTANT" -eq 1 ]; then
46+
$MSG_PRINTER
47+
fi
48+
if [ "$QUIET" -ne 1 ]; then
49+
printf "${COLOR}======================================================================${NC}\n"
50+
fi
3951
}
4052

4153
print_highlighted_msg () {
@@ -44,7 +56,7 @@ print_highlighted_msg () {
4456
print_msg () {
4557
echo "$MSG"
4658
}
47-
print_highlighted_msg_with_printer $COLOR print_msg
59+
print_highlighted_msg_with_printer $COLOR print_msg 1
4860
}
4961

5062
print_all_tests_takes_long_time_warning () {

0 commit comments

Comments
 (0)