1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/tools/modules/testing/common.v

184 lines
4.3 KiB
V
Raw Normal View History

2019-12-01 12:50:13 +03:00
module testing
import (
os
term
benchmark
filepath
)
pub struct TestSession {
pub mut:
files []string
vexe string
vargs string
failed bool
benchmark benchmark.Benchmark
ok string
fail string
}
2019-12-06 02:11:39 +03:00
pub fn new_test_session(vargs string) TestSession {
2019-12-01 12:50:13 +03:00
return TestSession{
vexe: vexe_path()
vargs: vargs
}
}
pub fn vexe_path() string {
// NB: tools extracted from v require that the first
// argument to them to be the v executable location.
// They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it.
return os.args[1]
}
pub fn (ts mut TestSession) init() {
ts.ok = term.ok_message('OK')
ts.fail = term.fail_message('FAIL')
ts.benchmark = benchmark.new_benchmark()
}
pub fn (ts mut TestSession) test() {
ts.init()
show_stats := '-stats' in ts.vargs.split(' ')
for dot_relative_file in ts.files {
relative_file := dot_relative_file.replace('./', '')
file := os.realpath( relative_file )
$if windows {
if file.contains('sqlite') { continue }
}
2019-12-06 02:11:39 +03:00
$if !macos {
if file.contains('customer') { continue }
}
2019-12-01 12:50:13 +03:00
$if msvc {
if file.contains('asm') { continue }
}
$if tinyc {
if file.contains('asm') { continue }
}
tmpc_filepath := file.replace('.v', '.tmp.c')
cmd := '"$ts.vexe" $ts.vargs "$file"'
//eprintln('>>> v cmd: $cmd')
2019-12-01 12:50:13 +03:00
ts.benchmark.step()
if show_stats {
2019-12-01 16:12:51 +03:00
eprintln('-------------------------------------------------')
2019-12-01 12:50:13 +03:00
status := os.system(cmd)
if status == 0 {
ts.benchmark.ok()
}else{
ts.benchmark.fail()
ts.failed = true
continue
}
}else{
r := os.exec(cmd) or {
ts.benchmark.fail()
ts.failed = true
2019-12-01 16:12:51 +03:00
eprintln(ts.benchmark.step_message('$relative_file ${ts.fail}'))
2019-12-01 12:50:13 +03:00
continue
}
if r.exit_code != 0 {
ts.benchmark.fail()
ts.failed = true
2019-12-01 16:12:51 +03:00
eprintln(ts.benchmark.step_message('$relative_file ${ts.fail}\n`$file`\n (\n$r.output\n)'))
2019-12-01 12:50:13 +03:00
} else {
ts.benchmark.ok()
2019-12-01 16:12:51 +03:00
eprintln(ts.benchmark.step_message('$relative_file ${ts.ok}'))
2019-12-01 12:50:13 +03:00
}
}
os.rm( tmpc_filepath )
}
ts.benchmark.stop()
}
pub fn vlib_should_be_present( parent_dir string ) {
vlib_dir := filepath.join( parent_dir, 'vlib' )
if !os.is_dir( vlib_dir ){
2019-12-01 16:12:51 +03:00
eprintln('$vlib_dir is missing, it must be next to the V executable')
2019-12-01 12:50:13 +03:00
exit(1)
}
}
pub fn v_build_failing(zargs string, folder string) bool {
2019-12-01 12:50:13 +03:00
main_label := 'Building $folder ...'
finish_label := 'building $folder'
vexe := vexe_path()
parent_dir := os.dir(vexe)
vlib_should_be_present( parent_dir )
vargs := zargs.replace(vexe, '')
2019-12-06 02:11:39 +03:00
2019-12-01 16:12:51 +03:00
eprintln(main_label)
eprintln(' v compiler args: "$vargs"')
2019-12-06 02:11:39 +03:00
mut session := new_test_session( vargs )
2019-12-01 12:50:13 +03:00
files := os.walk_ext(filepath.join(parent_dir, folder),'.v')
mains := files.filter(!it.contains('modules'))
mut rebuildable_mains := mains
if os.user_os() == 'windows' {
// on windows, an executable can not be rebuilt, while it is running
myself := os.executable().replace('.exe', '') + '.v'
mains_without_myself := mains.filter(!it.contains(myself))
rebuildable_mains = mains_without_myself // workaround a bug in it.contains generation
}
session.files << rebuildable_mains
session.test()
2019-12-01 16:12:51 +03:00
eprintln( session.benchmark.total_message( finish_label ) )
2019-12-01 12:50:13 +03:00
return session.failed
}
2019-12-01 16:12:51 +03:00
pub fn build_v_cmd_failed (cmd string) bool {
res := os.exec(cmd) or {
return true
}
if res.exit_code != 0 {
eprintln('')
eprintln( res.output )
return true
}
return false
}
pub fn building_any_v_binaries_failed() bool {
eprintln('Building V binaries...')
eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"')
vexe := testing.vexe_path()
parent_dir := os.dir(vexe)
testing.vlib_should_be_present( parent_dir )
os.chdir( parent_dir )
2019-12-06 02:11:39 +03:00
mut failed := false
2019-12-01 16:12:51 +03:00
v_build_commands := [
'$vexe -o v_g -g v.v',
'$vexe -o v_prod_g -prod -g v.v',
2019-12-01 16:12:51 +03:00
'$vexe -o v_cg -cg v.v',
'$vexe -o v_prod_cg -prod -cg v.v',
'$vexe -o v_prod -prod v.v',
]
2019-12-06 02:11:39 +03:00
2019-12-01 16:12:51 +03:00
mut bmark := benchmark.new_benchmark()
bok := term.ok_message('OK')
bfail := term.fail_message('FAIL')
2019-12-06 02:11:39 +03:00
for cmd in v_build_commands {
2019-12-01 16:12:51 +03:00
bmark.step()
if build_v_cmd_failed(cmd) {
bmark.fail()
failed = true
eprintln(bmark.step_message('$cmd => ${bfail} . See details above ^^^^^^^'))
eprintln('')
continue
}
bmark.ok()
2019-12-06 02:11:39 +03:00
eprintln(bmark.step_message('$cmd => ${bok}'))
2019-12-01 16:12:51 +03:00
}
bmark.stop()
eprintln( bmark.total_message( 'building v binaries' ) )
2019-12-06 02:11:39 +03:00
2019-12-01 16:12:51 +03:00
return failed
}