v/tools/modules/testing/common.v

194 lines
4.7 KiB
V
Raw Normal View History

2019-12-01 10:50:13 +01:00
module testing
import (
os
term
benchmark
filepath
)
pub struct TestSession {
pub mut:
files []string
vexe string
vargs string
failed bool
2019-12-01 10:50:13 +01:00
benchmark benchmark.Benchmark
}
2019-12-06 00:11:39 +01:00
pub fn new_test_session(vargs string) TestSession {
2019-12-01 10:50:13 +01:00
return TestSession{
vexe: vexe_path()
vargs: vargs
}
}
pub fn vexe_path() string {
// NB: tools extracted from v require that the VEXE
// environment variable contains the path to the v executable location.
2019-12-01 10:50:13 +01:00
// They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it.
return os.getenv('VEXE')
2019-12-01 10:50:13 +01:00
}
pub fn (ts mut TestSession) init() {
ts.benchmark = benchmark.new_benchmark()
}
pub fn (ts mut TestSession) test() {
tmpd := os.tmpdir()
2019-12-01 10:50:13 +01:00
ts.init()
show_stats := '-stats' in ts.vargs.split(' ')
2019-12-30 05:23:54 +01:00
mut remaining_files := []string
2019-12-01 10:50:13 +01:00
for dot_relative_file in ts.files {
relative_file := dot_relative_file.replace('./', '')
file := os.realpath(relative_file)
2019-12-01 10:50:13 +01:00
$if windows {
if file.contains('sqlite') {
continue
}
2019-12-01 10:50:13 +01:00
}
2019-12-06 00:11:39 +01:00
$if !macos {
if file.contains('customer') {
continue
}
2019-12-06 00:11:39 +01:00
}
2019-12-01 10:50:13 +01:00
$if msvc {
if file.contains('asm') {
continue
}
2019-12-01 10:50:13 +01:00
}
$if tinyc {
if file.contains('asm') {
continue
}
2019-12-01 10:50:13 +01:00
}
2019-12-30 05:23:54 +01:00
remaining_files << dot_relative_file
}
ts.benchmark.set_total_expected_steps(remaining_files.len)
for dot_relative_file in remaining_files {
relative_file := dot_relative_file.replace('./', '')
file := os.realpath(relative_file)
// Ensure that the generated binaries will be stored in the temporary folder.
// Remove them after a test passes/fails.
fname := filepath.filename(file)
generated_binary_fname := if os.user_os() == 'windows' { fname.replace('.v', '.exe') } else { fname.replace('.v', '') }
generated_binary_fpath := filepath.join(tmpd,generated_binary_fname)
if os.exists(generated_binary_fpath) {
os.rm(generated_binary_fpath)
}
mut cmd_options := [ts.vargs]
if !ts.vargs.contains('fmt') {
cmd_options << ' -o "$generated_binary_fpath"'
}
cmd := '"${ts.vexe}" ' + cmd_options.join(' ') + ' "${file}"'
// eprintln('>>> v cmd: $cmd')
2019-12-01 10:50:13 +01:00
ts.benchmark.step()
if show_stats {
2019-12-01 14:12:51 +01:00
eprintln('-------------------------------------------------')
2019-12-01 10:50:13 +01:00
status := os.system(cmd)
if status == 0 {
ts.benchmark.ok()
}
else {
2019-12-01 10:50:13 +01:00
ts.benchmark.fail()
ts.failed = true
continue
}
}
else {
2019-12-01 10:50:13 +01:00
r := os.exec(cmd) or {
ts.benchmark.fail()
ts.failed = true
2019-12-30 05:23:54 +01:00
eprintln(ts.benchmark.step_message_fail(relative_file))
2019-12-01 10:50:13 +01:00
continue
}
if r.exit_code != 0 {
ts.benchmark.fail()
ts.failed = true
2019-12-30 05:23:54 +01:00
eprintln(ts.benchmark.step_message_fail('${relative_file}\n`$file`\n (\n$r.output\n)'))
}
else {
2019-12-01 10:50:13 +01:00
ts.benchmark.ok()
2019-12-30 05:23:54 +01:00
eprintln(ts.benchmark.step_message_ok(relative_file))
2019-12-01 10:50:13 +01:00
}
}
if os.exists(generated_binary_fpath) {
os.rm(generated_binary_fpath)
}
2019-12-01 10:50:13 +01:00
}
ts.benchmark.stop()
eprintln(term.h_divider())
2019-12-01 10:50:13 +01:00
}
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 14:12:51 +01:00
eprintln('$vlib_dir is missing, it must be next to the V executable')
2019-12-01 10:50:13 +01:00
exit(1)
}
}
pub fn v_build_failing(zargs string, folder string) bool {
2019-12-01 10:50:13 +01:00
main_label := 'Building $folder ...'
finish_label := 'building $folder'
vexe := vexe_path()
2019-12-23 11:09:22 +01:00
parent_dir := filepath.dir(vexe)
vlib_should_be_present(parent_dir)
vargs := zargs.replace(vexe, '')
2019-12-01 14:12:51 +01:00
eprintln(main_label)
eprintln(' v compiler args: "$vargs"')
mut session := new_test_session(vargs)
files := os.walk_ext(filepath.join(parent_dir,folder), '.v')
2019-12-01 10:50:13 +01:00
mains := files.filter(!it.contains('modules'))
session.files << mains
2019-12-01 10:50:13 +01:00
session.test()
eprintln(session.benchmark.total_message(finish_label))
2019-12-01 10:50:13 +01:00
return session.failed
}
2019-12-01 14:12:51 +01:00
pub fn build_v_cmd_failed(cmd string) bool {
2019-12-01 14:12:51 +01:00
res := os.exec(cmd) or {
return true
}
if res.exit_code != 0 {
eprintln('')
eprintln(res.output)
2019-12-01 14:12:51 +01:00
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()
2019-12-23 11:09:22 +01:00
parent_dir := filepath.dir(vexe)
testing.vlib_should_be_present(parent_dir)
os.chdir(parent_dir)
2019-12-06 00:11:39 +01:00
mut failed := false
v_build_commands := ['$vexe -o v_g -g v.v',
'$vexe -o v_prod_g -prod -g v.v',
'$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-01 14:12:51 +01:00
]
mut bmark := benchmark.new_benchmark()
2019-12-06 00:11:39 +01:00
for cmd in v_build_commands {
2019-12-01 14:12:51 +01:00
bmark.step()
if build_v_cmd_failed(cmd) {
bmark.fail()
failed = true
2019-12-30 05:23:54 +01:00
eprintln(bmark.step_message_fail('command: ${cmd} . See details above ^^^^^^^'))
2019-12-01 14:12:51 +01:00
eprintln('')
continue
}
bmark.ok()
2019-12-30 05:23:54 +01:00
eprintln(bmark.step_message_ok('command: ${cmd}'))
2019-12-01 14:12:51 +01:00
}
bmark.stop()
eprintln(term.h_divider())
eprintln(bmark.total_message('building v binaries'))
2019-12-01 14:12:51 +01:00
return failed
}