tests: colorize failing tests, improve layout (#8066)
parent
4cd50ed566
commit
a22982d662
|
@ -129,6 +129,9 @@ pub fn new_test_session(_vargs string) TestSession {
|
||||||
vexe := pref.vexe_path()
|
vexe := pref.vexe_path()
|
||||||
vroot := os.dir(vexe)
|
vroot := os.dir(vexe)
|
||||||
new_vtmp_dir := setup_new_vtmp_folder()
|
new_vtmp_dir := setup_new_vtmp_folder()
|
||||||
|
if term.can_show_color_on_stderr() {
|
||||||
|
os.setenv('VCOLORS', 'always', true)
|
||||||
|
}
|
||||||
return TestSession{
|
return TestSession{
|
||||||
vexe: vexe
|
vexe: vexe
|
||||||
vroot: vroot
|
vroot: vroot
|
||||||
|
@ -273,7 +276,8 @@ fn worker_trunner(mut p sync.PoolProcessor, idx int, thread_id int) voidptr {
|
||||||
ts.failed = true
|
ts.failed = true
|
||||||
ts.benchmark.fail()
|
ts.benchmark.fail()
|
||||||
tls_bench.fail()
|
tls_bench.fail()
|
||||||
ts.append_message(.fail, tls_bench.step_message_fail('$relative_file\n$r.output\n'))
|
ending_newline := if r.output.ends_with('\n') { '\n' } else { '' }
|
||||||
|
ts.append_message(.fail, tls_bench.step_message_fail('$relative_file\n$r.output.trim_space()$ending_newline'))
|
||||||
} else {
|
} else {
|
||||||
ts.benchmark.ok()
|
ts.benchmark.ok()
|
||||||
tls_bench.ok()
|
tls_bench.ok()
|
||||||
|
|
|
@ -3,6 +3,17 @@ module main
|
||||||
import os
|
import os
|
||||||
import term
|
import term
|
||||||
|
|
||||||
|
const use_color = term.can_show_color_on_stderr()
|
||||||
|
|
||||||
|
const use_relative_paths = can_use_relative_paths()
|
||||||
|
|
||||||
|
fn can_use_relative_paths() bool {
|
||||||
|
return match os.getenv('VERROR_PATHS') {
|
||||||
|
'absolute' { false }
|
||||||
|
else { true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
// / This file will get compiled as part of the main program,
|
// / This file will get compiled as part of the main program,
|
||||||
// / for a _test.v file.
|
// / for a _test.v file.
|
||||||
|
@ -13,32 +24,65 @@ import term
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
// TODO copy pasta builtin.v fn ___print_assert_failure
|
// TODO copy pasta builtin.v fn ___print_assert_failure
|
||||||
fn cb_assertion_failed(i &VAssertMetaInfo) {
|
fn cb_assertion_failed(i &VAssertMetaInfo) {
|
||||||
|
filepath := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
||||||
|
mut final_filepath := filepath + ':${i.line_nr + 1}:'
|
||||||
|
if use_color {
|
||||||
|
final_filepath = term.gray(final_filepath)
|
||||||
|
}
|
||||||
|
mut final_funcname := 'fn ' + i.fn_name.replace('main.', '').replace('__', '.')
|
||||||
|
if use_color {
|
||||||
|
final_funcname = term.red('✗ ' + final_funcname)
|
||||||
|
}
|
||||||
|
final_src := if use_color { term.dim('assert ${term.bold(i.src)}') } else { 'assert ' + i.src }
|
||||||
|
eprintln('$final_filepath $final_funcname')
|
||||||
|
if i.op.len > 0 && i.op != 'call' {
|
||||||
|
mut lvtitle := ' Left value:'
|
||||||
|
mut rvtitle := ' Right value:'
|
||||||
|
mut slvalue := '$i.lvalue'
|
||||||
|
mut srvalue := '$i.rvalue'
|
||||||
|
if use_color {
|
||||||
|
slvalue = term.yellow(slvalue)
|
||||||
|
srvalue = term.yellow(srvalue)
|
||||||
|
lvtitle = term.gray(lvtitle)
|
||||||
|
rvtitle = term.gray(rvtitle)
|
||||||
|
}
|
||||||
|
cutoff_limit := 30
|
||||||
|
if slvalue.len > cutoff_limit || srvalue.len > cutoff_limit {
|
||||||
|
eprintln(' > $final_src')
|
||||||
|
eprintln(lvtitle)
|
||||||
|
eprintln(' $slvalue')
|
||||||
|
eprintln(rvtitle)
|
||||||
|
eprintln(' $srvalue')
|
||||||
|
} else {
|
||||||
|
eprintln(' > $final_src')
|
||||||
|
eprintln(' $lvtitle $slvalue')
|
||||||
|
eprintln('$rvtitle $srvalue')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eprintln(' $final_src')
|
||||||
|
}
|
||||||
|
eprintln('')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cb_assertion_ok(i &VAssertMetaInfo) {
|
||||||
|
// prints for every assertion instead of per test function
|
||||||
|
// TODO: needs to be changed
|
||||||
|
/*
|
||||||
use_color := term.can_show_color_on_stderr()
|
use_color := term.can_show_color_on_stderr()
|
||||||
use_relative_paths := match os.getenv('VERROR_PATHS') {
|
use_relative_paths := match os.getenv('VERROR_PATHS') {
|
||||||
'absolute' { false }
|
'absolute' { false }
|
||||||
else { true }
|
else { true }
|
||||||
}
|
}
|
||||||
final_filename := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
filepath := if use_relative_paths { i.fpath } else { os.real_path(i.fpath) }
|
||||||
final_funcname := i.fn_name.replace('main.', '').replace('__', '.')
|
final_filepath := if use_color {
|
||||||
final_src := if use_color { term.bold(i.src) } else { i.src }
|
term.gray(filepath + ':${i.line_nr+1}')
|
||||||
eprintln('')
|
} else {
|
||||||
eprintln('$final_filename:${i.line_nr+1}: failed assert in function $final_funcname')
|
filepath + ':${i.line_nr+1}'
|
||||||
eprintln('Source : `$final_src`')
|
|
||||||
if i.op.len > 0 && i.op != 'call' {
|
|
||||||
mut slvalue := '$i.lvalue'
|
|
||||||
mut srvalue := '$i.rvalue'
|
|
||||||
// lpostfix := if slvalue == i.llabel { '.' } else { '<= `$i.llabel`' }
|
|
||||||
// rpostfix := if srvalue == i.rlabel { '.' } else { '<= `$i.rlabel`' }
|
|
||||||
if use_color {
|
|
||||||
slvalue = term.bold(term.yellow(slvalue))
|
|
||||||
srvalue = term.bold(term.yellow(srvalue))
|
|
||||||
}
|
|
||||||
eprintln(' left value: $slvalue')
|
|
||||||
eprintln(' right value: $srvalue')
|
|
||||||
}
|
}
|
||||||
}
|
mut final_funcname := i.fn_name.replace('main.', '').replace('__', '.')
|
||||||
|
if use_color {
|
||||||
fn cb_assertion_ok(i &VAssertMetaInfo) {
|
final_funcname = term.green('✓ ' + final_funcname)
|
||||||
// do nothing for now on an OK assertion
|
}
|
||||||
// println('OK ${(i.line_nr+1):5d}|${i.src}')
|
println('$final_funcname ($final_filepath)')
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
testing.header('Testing...')
|
testing.header('Testing...')
|
||||||
ts.test()
|
ts.test()
|
||||||
println(ts.benchmark.total_message('running V _test.v files'))
|
println(ts.benchmark.total_message('Ran all V _test.v files'))
|
||||||
if ts.failed {
|
if ts.failed {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,20 +189,20 @@ pub fn (b &Benchmark) step_message_skip(msg string) string {
|
||||||
|
|
||||||
// total_message returns a string with total summary of the benchmark run.
|
// total_message returns a string with total summary of the benchmark run.
|
||||||
pub fn (b &Benchmark) total_message(msg string) string {
|
pub fn (b &Benchmark) total_message(msg string) string {
|
||||||
mut tmsg := '$msg\n ok, fail, skip, total = ' + term.ok_message('${b.nok:5d}') +
|
mut tmsg := '${term.bold('Summary:')} '
|
||||||
', ' + if b.nfail > 0 { term.red('${b.nfail:5d}') } else { '${b.nfail:5d}' } + ', ' + if b.nskip >
|
if b.nfail > 0 {
|
||||||
0 { term.bright_yellow('${b.nskip:5d}') } else { '${b.nskip:5d}' } + ', ' + '${b.ntotal:5d}'
|
tmsg += term.bold(term.red('$b.nfail failed')) + ', '
|
||||||
if b.verbose {
|
|
||||||
tmsg = '<=== total time spent $tmsg'
|
|
||||||
}
|
}
|
||||||
mut spaces := ' '
|
if b.nok > 0 {
|
||||||
if b.nexpected_steps > 1 {
|
tmsg += term.bold(term.green('$b.nok passed')) + ', '
|
||||||
// NB: the formula below accounts for the progress bar [step/total]
|
|
||||||
str_steps := '$b.nexpected_steps'
|
|
||||||
x := 4 + str_steps.len * 2 + 5
|
|
||||||
spaces = ' '.repeat(x)
|
|
||||||
}
|
}
|
||||||
return spaces + b.tdiff_in_ms(tmsg, b.bench_timer.elapsed().microseconds())
|
if b.nskip > 0 {
|
||||||
|
tmsg += term.bold(term.yellow('$b.nskip skipped')) + ', '
|
||||||
|
}
|
||||||
|
tmsg += '$b.ntotal total. ${term.bold('Runtime:')} ${b.bench_timer.elapsed().microseconds() /
|
||||||
|
1000} ms.\n'
|
||||||
|
tmsg += term.gray(msg)
|
||||||
|
return tmsg
|
||||||
}
|
}
|
||||||
|
|
||||||
// total_duration returns the duration in ms.
|
// total_duration returns the duration in ms.
|
||||||
|
|
|
@ -30,7 +30,8 @@ pub fn can_show_color_on_stderr() bool {
|
||||||
// If colors are not allowed, returns a given string.
|
// If colors are not allowed, returns a given string.
|
||||||
pub fn ok_message(s string) string {
|
pub fn ok_message(s string) string {
|
||||||
return if can_show_color_on_stdout() {
|
return if can_show_color_on_stdout() {
|
||||||
green(s)
|
msg := ' $s '
|
||||||
|
green(msg)
|
||||||
} else {
|
} else {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
@ -40,7 +41,8 @@ pub fn ok_message(s string) string {
|
||||||
// If colors are not allowed, returns a given string.
|
// If colors are not allowed, returns a given string.
|
||||||
pub fn fail_message(s string) string {
|
pub fn fail_message(s string) string {
|
||||||
return if can_show_color_on_stdout() {
|
return if can_show_color_on_stdout() {
|
||||||
bold(bg_red(white(s)))
|
msg := ' $s '
|
||||||
|
inverse(bg_white(bold(red(msg))))
|
||||||
} else {
|
} else {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
@ -50,7 +52,7 @@ pub fn fail_message(s string) string {
|
||||||
// If colors are not allowed, returns a given string.
|
// If colors are not allowed, returns a given string.
|
||||||
pub fn warn_message(s string) string {
|
pub fn warn_message(s string) string {
|
||||||
return if can_show_color_on_stdout() {
|
return if can_show_color_on_stdout() {
|
||||||
bright_yellow(s)
|
bright_yellow(' $s ')
|
||||||
} else {
|
} else {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue