Skip to content

Commit 37f385c

Browse files
authored
testing: implement a separate -show-asserts option, for cleaner test output (-stats still works, and still shows both the compilation stats and the asserts) (#21578)
1 parent c689f80 commit 37f385c

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

cmd/tools/modules/testing/common.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub mut:
8585
build_tools bool // builds only executables in cmd/tools; used by `v build-tools'
8686
silent_mode bool
8787
show_stats bool
88+
show_asserts bool
8889
progress_mode bool
8990
root_relative bool // used by CI runs, so that the output is stable everywhere
9091
nmessages chan LogMessage // many publishers, single consumer/printer
@@ -314,6 +315,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
314315
skip_files: skip_files
315316
fail_fast: testing.fail_fast
316317
show_stats: '-stats' in vargs.split(' ')
318+
show_asserts: '-show-asserts' in vargs.split(' ')
317319
vargs: vargs
318320
vtmp_dir: new_vtmp_dir
319321
hash: hash
@@ -658,6 +660,9 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
658660
mut r := os.execute(run_cmd)
659661
cmd_duration = d_cmd.elapsed()
660662
ts.append_message_with_duration(.cmd_end, r.output, cmd_duration, mtc)
663+
if ts.show_asserts && r.exit_code == 0 {
664+
println(r.output.split_into_lines().filter(it.contains(' assert')).join('\n'))
665+
}
661666
if r.exit_code != 0 {
662667
mut details := get_test_details(file)
663668
mut trimmed_output := r.output.trim_space()

vlib/v/builder/compile.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ pub fn (v &Builder) get_user_files() []string {
329329
}
330330
user_files << v_test_runner_prelude
331331
}
332-
if v.pref.is_test && v.pref.is_stats {
332+
if v.pref.is_test && v.pref.show_asserts {
333333
user_files << os.join_path(preludes_path, 'tests_with_stats.v')
334-
}
335-
if v.pref.backend.is_js() && v.pref.is_stats && v.pref.is_test {
336-
user_files << os.join_path(preludes_path, 'stats_import.js.v')
334+
if v.pref.backend.is_js() {
335+
user_files << os.join_path(preludes_path, 'stats_import.js.v')
336+
}
337337
}
338338
if v.pref.is_prof {
339339
user_files << os.join_path(preludes_path, 'profiled_program.v')

vlib/v/gen/c/cmain.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ pub fn (mut g Gen) gen_c_main_for_tests() {
305305
mut all_tfuncs := g.get_all_test_function_names()
306306
all_tfuncs = g.filter_only_matching_fn_names(all_tfuncs)
307307
g.writeln('\tstring v_test_file = ${ctoslit(g.pref.path)};')
308-
if g.pref.is_stats {
308+
if g.pref.show_asserts {
309309
g.writeln('\tmain__BenchedTests bt = main__start_testing(${all_tfuncs.len}, v_test_file);')
310310
}
311311
g.writeln('')
@@ -328,7 +328,7 @@ pub fn (mut g Gen) gen_c_main_for_tests() {
328328
g.writeln('\t_vtrunner._method_fn_start(_vtobj);')
329329
g.writeln('\tif (!setjmp(g_jump_buffer)) {')
330330
//
331-
if g.pref.is_stats {
331+
if g.pref.show_asserts {
332332
g.writeln('\t\tmain__BenchedTests_testing_step_start(&bt, tcname_${tnumber});')
333333
}
334334
g.writeln('\t\t${tcname}();')
@@ -339,12 +339,12 @@ pub fn (mut g Gen) gen_c_main_for_tests() {
339339
g.writeln('\t\t_vtrunner._method_fn_fail(_vtobj);')
340340
//
341341
g.writeln('\t}')
342-
if g.pref.is_stats {
342+
if g.pref.show_asserts {
343343
g.writeln('\tmain__BenchedTests_testing_step_end(&bt);')
344344
}
345345
g.writeln('')
346346
}
347-
if g.pref.is_stats {
347+
if g.pref.show_asserts {
348348
g.writeln('\tmain__BenchedTests_end_testing(&bt);')
349349
}
350350
g.writeln('')

vlib/v/pref/pref.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ pub mut:
104104
is_crun bool // similar to run, but does not recompile the executable, if there were no changes to the sources
105105
is_debug bool // turned on by -g or -cg, it tells v to pass -g to the C backend compiler.
106106
is_vlines bool // turned on by -g (it slows down .tmp.c generation slightly).
107-
is_stats bool // `v -stats file_test.v` will produce more detailed statistics for the tests that were run
107+
is_stats bool // `v -stats file.v` will produce more detailed statistics for the file that is compiled
108+
show_asserts bool // `VTEST_SHOW_ASSERTS=1 v file_test.v` will show details about the asserts done by a test file. Also activated for `-stats` and `-show-asserts`.
108109
show_timings bool // show how much time each compiler stage took
109110
is_fmt bool
110111
is_vet bool
@@ -362,6 +363,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
362363
'-show-timings' {
363364
res.show_timings = true
364365
}
366+
'-show-asserts' {
367+
res.show_asserts = true
368+
}
365369
'-check-syntax' {
366370
res.only_check_syntax = true
367371
}
@@ -974,6 +978,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
974978
if command == 'run' {
975979
res.is_run = true
976980
}
981+
res.show_asserts = res.show_asserts || res.is_stats || os.getenv('VTEST_SHOW_ASSERTS') != ''
977982
if command == 'run' && res.is_prod && os.is_atty(1) > 0 {
978983
eprintln_cond(show_output && !res.is_quiet, "Note: building an optimized binary takes much longer. It shouldn't be used with `v run`.")
979984
eprintln_cond(show_output && !res.is_quiet, 'Use `v run` without optimization, or build an optimized binary with -prod first, then run it separately.')

0 commit comments

Comments
 (0)