2019-11-01 02:15:03 +03:00
|
|
|
module main
|
2019-10-09 06:01:43 +03:00
|
|
|
|
2020-04-17 22:41:54 +03:00
|
|
|
import os
|
|
|
|
import os.cmdline
|
|
|
|
import testing
|
2021-03-17 03:43:17 +03:00
|
|
|
import v.pref
|
2019-10-09 06:01:43 +03:00
|
|
|
|
2020-04-17 22:41:54 +03:00
|
|
|
fn main() {
|
2020-12-20 19:27:42 +03:00
|
|
|
args := os.args.clone()
|
|
|
|
if os.args.last() == 'test' {
|
2021-01-13 22:52:05 +03:00
|
|
|
show_usage()
|
2019-10-09 06:01:43 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-24 05:43:31 +03:00
|
|
|
args_to_executable := args[1..]
|
2021-11-15 12:44:54 +03:00
|
|
|
mut args_before := cmdline.options_before(args_to_executable, ['test'])
|
|
|
|
mut args_after := cmdline.options_after(args_to_executable, ['test'])
|
|
|
|
fail_fast := extract_flag('-fail-fast', mut args_after, testing.fail_fast)
|
|
|
|
if args_after == ['v'] {
|
2019-12-01 12:50:13 +03:00
|
|
|
eprintln('`v test v` has been deprecated.')
|
2021-01-17 17:04:08 +03:00
|
|
|
eprintln('Use `v test-all` instead.')
|
2019-12-01 12:50:13 +03:00
|
|
|
exit(1)
|
2019-10-09 06:01:43 +03:00
|
|
|
}
|
2021-03-17 03:43:17 +03:00
|
|
|
backend_pos := args_before.index('-b')
|
|
|
|
backend := if backend_pos == -1 { '.c' } else { args_before[backend_pos + 1] } // this giant mess because closures are not implemented
|
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
mut ts := testing.new_test_session(args_before.join(' '), true)
|
2021-11-15 12:44:54 +03:00
|
|
|
ts.fail_fast = fail_fast
|
2019-12-24 05:43:31 +03:00
|
|
|
for targ in args_after {
|
2019-12-04 23:03:12 +03:00
|
|
|
if os.is_dir(targ) {
|
2019-10-20 09:56:33 +03:00
|
|
|
// Fetch all tests from the directory
|
2021-03-17 03:43:17 +03:00
|
|
|
files, skip_files := should_test_dir(targ.trim_right(os.path_separator), backend)
|
|
|
|
ts.files << files
|
|
|
|
ts.skip_files << skip_files
|
2019-10-09 06:01:43 +03:00
|
|
|
continue
|
2021-03-17 03:43:17 +03:00
|
|
|
} else if os.exists(targ) {
|
|
|
|
match should_test(targ, backend) {
|
|
|
|
.test {
|
|
|
|
ts.files << targ
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
.skip {
|
|
|
|
ts.files << targ
|
|
|
|
ts.skip_files << targ
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
.ignore {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln('\nUnrecognized test file `$targ`.\n `v test` can only be used with folders and/or _test.v files.\n')
|
|
|
|
show_usage()
|
|
|
|
exit(1)
|
2019-10-09 06:01:43 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 19:01:10 +03:00
|
|
|
testing.header('Testing...')
|
2019-10-09 06:01:43 +03:00
|
|
|
ts.test()
|
2021-01-31 20:22:42 +03:00
|
|
|
println(ts.benchmark.total_message('all V _test.v files'))
|
2019-10-09 06:01:43 +03:00
|
|
|
if ts.failed {
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 22:52:05 +03:00
|
|
|
|
|
|
|
fn show_usage() {
|
|
|
|
println('Usage:')
|
|
|
|
println(' A)')
|
|
|
|
println(' v test folder/ : run all v tests in the given folder.')
|
|
|
|
println(' v -stats test folder/ : the same, but print more stats.')
|
|
|
|
println(' B)')
|
|
|
|
println(' v test file_test.v : run test functions in a given test file.')
|
|
|
|
println(' v -stats test file_test.v : as above, but with more stats.')
|
|
|
|
println(' NB: you can also give many and mixed folder/ file_test.v arguments after `v test` .')
|
|
|
|
println('')
|
|
|
|
}
|
2021-03-17 03:43:17 +03:00
|
|
|
|
|
|
|
pub fn should_test_dir(path string, backend string) ([]string, []string) { // return is (files, skip_files)
|
|
|
|
mut files := os.ls(path) or { return []string{}, []string{} }
|
|
|
|
mut local_path_separator := os.path_separator
|
|
|
|
if path.ends_with(os.path_separator) {
|
|
|
|
local_path_separator = ''
|
|
|
|
}
|
|
|
|
mut res_files := []string{}
|
|
|
|
mut skip_files := []string{}
|
|
|
|
for file in files {
|
|
|
|
p := path + local_path_separator + file
|
|
|
|
if os.is_dir(p) && !os.is_link(p) {
|
2021-06-02 21:50:31 +03:00
|
|
|
if file == 'testdata' {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-17 03:43:17 +03:00
|
|
|
ret_files, ret_skip_files := should_test_dir(p, backend)
|
|
|
|
res_files << ret_files
|
|
|
|
skip_files << ret_skip_files
|
|
|
|
} else if os.exists(p) {
|
|
|
|
match should_test(p, backend) {
|
|
|
|
.test {
|
|
|
|
res_files << p
|
|
|
|
}
|
|
|
|
.skip {
|
|
|
|
res_files << p
|
|
|
|
skip_files << p
|
|
|
|
}
|
|
|
|
.ignore {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res_files, skip_files
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ShouldTestStatus {
|
|
|
|
test // do test
|
|
|
|
skip
|
|
|
|
ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_test(path string, backend string) ShouldTestStatus {
|
|
|
|
if path.ends_with('_test.v') {
|
|
|
|
return .test
|
|
|
|
}
|
|
|
|
if path.ends_with('.v') && path.count('.') == 2 {
|
|
|
|
if !path.all_before_last('.v').all_before_last('.').ends_with('_test') {
|
|
|
|
return .ignore
|
|
|
|
}
|
|
|
|
backend_arg := path.all_before_last('.v').all_after_last('.')
|
|
|
|
arch := pref.arch_from_string(backend_arg) or { pref.Arch._auto }
|
|
|
|
if arch == pref.get_host_arch() {
|
|
|
|
return .test
|
|
|
|
} else if arch == ._auto {
|
|
|
|
if backend_arg == 'c' { // .c.v
|
|
|
|
return if backend == 'c' { ShouldTestStatus.test } else { ShouldTestStatus.skip }
|
|
|
|
}
|
|
|
|
if backend_arg == 'js' {
|
|
|
|
return if backend == 'js' { ShouldTestStatus.test } else { ShouldTestStatus.skip }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return .skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return .ignore
|
|
|
|
}
|
2021-11-15 12:44:54 +03:00
|
|
|
|
|
|
|
fn extract_flag(flag_name string, mut after []string, flag_default bool) bool {
|
|
|
|
mut res := flag_default
|
|
|
|
orig_after := after.clone() // workaround for after.filter() codegen bug, when `mut after []string`
|
|
|
|
matches_after := orig_after.filter(it != flag_name)
|
|
|
|
if matches_after.len < after.len {
|
|
|
|
after = matches_after.clone()
|
|
|
|
res = true
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|