2019-12-01 12:50:13 +03:00
|
|
|
module testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
os
|
|
|
|
term
|
|
|
|
benchmark
|
|
|
|
filepath
|
2020-01-20 01:33:16 +03:00
|
|
|
runtime
|
|
|
|
sync
|
2019-12-01 12:50:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
pub struct TestSession {
|
|
|
|
pub mut:
|
2019-12-27 19:59:04 +03:00
|
|
|
files []string
|
|
|
|
vexe string
|
|
|
|
vargs string
|
|
|
|
failed bool
|
2019-12-01 12:50:13 +03:00
|
|
|
benchmark benchmark.Benchmark
|
2020-01-20 01:33:16 +03:00
|
|
|
|
|
|
|
ntask int // writing to this should be locked by mu.
|
|
|
|
ntask_mtx &sync.Mutex
|
|
|
|
waitgroup &sync.WaitGroup
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
|
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
|
2020-01-20 01:33:16 +03:00
|
|
|
|
|
|
|
ntask: 0
|
|
|
|
ntask_mtx: sync.new_mutex()
|
|
|
|
waitgroup: sync.new_waitgroup()
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vexe_path() string {
|
2019-12-27 19:59:04 +03:00
|
|
|
// NB: tools extracted from v require that the VEXE
|
2019-12-23 13:02:50 +03:00
|
|
|
// environment variable contains the path to the v executable location.
|
2019-12-01 12:50:13 +03:00
|
|
|
// They are usually launched by vlib/compiler/vtools.v,
|
|
|
|
// launch_tool/1 , which provides it.
|
2019-12-23 13:02:50 +03:00
|
|
|
return os.getenv('VEXE')
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ts mut TestSession) init() {
|
|
|
|
ts.benchmark = benchmark.new_benchmark()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ts mut TestSession) test() {
|
|
|
|
ts.init()
|
2019-12-30 07:23:54 +03:00
|
|
|
mut remaining_files := []string
|
2019-12-01 12:50:13 +03:00
|
|
|
for dot_relative_file in ts.files {
|
|
|
|
relative_file := dot_relative_file.replace('./', '')
|
2019-12-27 19:59:04 +03:00
|
|
|
file := os.realpath(relative_file)
|
2019-12-01 12:50:13 +03:00
|
|
|
$if windows {
|
2020-01-18 09:27:50 +03:00
|
|
|
if file.contains('sqlite') || file.contains('httpbin') {
|
2019-12-27 19:59:04 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
2019-12-06 02:11:39 +03:00
|
|
|
$if !macos {
|
2019-12-27 19:59:04 +03:00
|
|
|
if file.contains('customer') {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
$if msvc {
|
2019-12-27 19:59:04 +03:00
|
|
|
if file.contains('asm') {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
$if tinyc {
|
2019-12-27 19:59:04 +03:00
|
|
|
if file.contains('asm') {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
2019-12-30 07:23:54 +03:00
|
|
|
remaining_files << dot_relative_file
|
|
|
|
}
|
2020-01-20 01:33:16 +03:00
|
|
|
|
|
|
|
ts.files = remaining_files
|
2019-12-30 07:23:54 +03:00
|
|
|
ts.benchmark.set_total_expected_steps(remaining_files.len)
|
2020-01-20 01:33:16 +03:00
|
|
|
|
|
|
|
mut ncpus := runtime.nr_cpus()
|
|
|
|
$if msvc {
|
|
|
|
// NB: MSVC can not be launched in parallel, without giving it
|
|
|
|
// the option /FS because it uses a shared PDB file, which should
|
|
|
|
// be locked, but that makes writing slower...
|
|
|
|
// See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019
|
|
|
|
// Instead, just run tests on 1 core for now.
|
|
|
|
ncpus = 1
|
|
|
|
}
|
|
|
|
ts.waitgroup.add( ncpus )
|
|
|
|
for i:=0; i < ncpus; i++ {
|
|
|
|
go process_in_thread(ts)
|
|
|
|
}
|
|
|
|
ts.waitgroup.wait()
|
|
|
|
ts.benchmark.stop()
|
|
|
|
eprintln(term.h_divider())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn process_in_thread(ts mut TestSession){
|
|
|
|
ts.process_files()
|
|
|
|
ts.waitgroup.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (ts mut TestSession) process_files() {
|
|
|
|
tmpd := os.tmpdir()
|
|
|
|
show_stats := '-stats' in ts.vargs.split(' ')
|
|
|
|
|
|
|
|
mut tls_bench := benchmark.new_benchmark() // tls_bench is used to format the step messages/timings
|
|
|
|
tls_bench.set_total_expected_steps( ts.benchmark.nexpected_steps )
|
|
|
|
for {
|
|
|
|
|
|
|
|
ts.ntask_mtx.lock()
|
|
|
|
ts.ntask++
|
|
|
|
idx := ts.ntask-1
|
|
|
|
ts.ntask_mtx.unlock()
|
|
|
|
|
|
|
|
if idx >= ts.files.len { break }
|
|
|
|
tls_bench.cstep = idx
|
|
|
|
|
|
|
|
dot_relative_file := ts.files[ idx ]
|
2019-12-30 07:23:54 +03:00
|
|
|
relative_file := dot_relative_file.replace('./', '')
|
|
|
|
file := os.realpath(relative_file)
|
2019-12-27 19:59:04 +03:00
|
|
|
// 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 12:50:13 +03:00
|
|
|
ts.benchmark.step()
|
2020-01-20 01:33:16 +03:00
|
|
|
tls_bench.step()
|
2019-12-01 12:50:13 +03:00
|
|
|
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()
|
2020-01-20 01:33:16 +03:00
|
|
|
tls_bench.ok()
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-01 12:50:13 +03:00
|
|
|
ts.failed = true
|
2020-01-20 01:33:16 +03:00
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
2019-12-01 12:50:13 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-01 12:50:13 +03:00
|
|
|
r := os.exec(cmd) or {
|
|
|
|
ts.failed = true
|
2020-01-20 01:33:16 +03:00
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
|
|
|
eprintln(tls_bench.step_message_fail(relative_file))
|
2019-12-01 12:50:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.exit_code != 0 {
|
|
|
|
ts.failed = true
|
2020-01-20 01:33:16 +03:00
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
|
|
|
eprintln(tls_bench.step_message_fail('${relative_file}\n`$file`\n (\n$r.output\n)'))
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-01 12:50:13 +03:00
|
|
|
ts.benchmark.ok()
|
2020-01-20 01:33:16 +03:00
|
|
|
tls_bench.ok()
|
|
|
|
eprintln(tls_bench.step_message_ok(relative_file))
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
if os.exists(generated_binary_fpath) {
|
|
|
|
os.rm(generated_binary_fpath)
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 19:59:04 +03: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 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:22:54 +03:00
|
|
|
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()
|
2019-12-23 13:09:22 +03:00
|
|
|
parent_dir := filepath.dir(vexe)
|
2019-12-27 19:59:04 +03:00
|
|
|
vlib_should_be_present(parent_dir)
|
2019-12-05 14:22:54 +03:00
|
|
|
vargs := zargs.replace(vexe, '')
|
2019-12-01 16:12:51 +03:00
|
|
|
eprintln(main_label)
|
2019-12-05 14:22:54 +03:00
|
|
|
eprintln(' v compiler args: "$vargs"')
|
2019-12-27 19:59:04 +03:00
|
|
|
mut session := new_test_session(vargs)
|
|
|
|
files := os.walk_ext(filepath.join(parent_dir,folder), '.v')
|
2020-01-23 14:58:47 +03:00
|
|
|
mut mains := files.filter(!it.contains('modules') && !it.contains('preludes'))
|
|
|
|
$if windows {
|
|
|
|
// skip pico example on windows
|
|
|
|
// there was a bug using filter here
|
|
|
|
mut mains_filtered := []string
|
|
|
|
for file in mains {
|
|
|
|
if !file.ends_with('examples\\pico\\pico.v') {
|
|
|
|
mains_filtered << file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mains = mains_filtered
|
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
session.files << mains
|
2019-12-01 12:50:13 +03:00
|
|
|
session.test()
|
2019-12-27 19:59:04 +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
|
|
|
|
2019-12-27 19:59:04 +03:00
|
|
|
pub fn build_v_cmd_failed(cmd string) bool {
|
2019-12-01 16:12:51 +03:00
|
|
|
res := os.exec(cmd) or {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
eprintln('')
|
2019-12-27 19:59:04 +03:00
|
|
|
eprintln(res.output)
|
2019-12-01 16:12:51 +03: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 13:09:22 +03:00
|
|
|
parent_dir := filepath.dir(vexe)
|
2019-12-27 19:59:04 +03:00
|
|
|
testing.vlib_should_be_present(parent_dir)
|
|
|
|
os.chdir(parent_dir)
|
2019-12-06 02:11:39 +03:00
|
|
|
mut failed := false
|
2019-12-27 19:59:04 +03:00
|
|
|
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 16:12:51 +03:00
|
|
|
]
|
|
|
|
mut bmark := benchmark.new_benchmark()
|
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
|
2019-12-30 07:23:54 +03:00
|
|
|
eprintln(bmark.step_message_fail('command: ${cmd} . See details above ^^^^^^^'))
|
2019-12-01 16:12:51 +03:00
|
|
|
eprintln('')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bmark.ok()
|
2019-12-30 07:23:54 +03:00
|
|
|
eprintln(bmark.step_message_ok('command: ${cmd}'))
|
2019-12-01 16:12:51 +03:00
|
|
|
}
|
|
|
|
bmark.stop()
|
2019-12-27 19:59:04 +03:00
|
|
|
eprintln(term.h_divider())
|
|
|
|
eprintln(bmark.total_message('building v binaries'))
|
2019-12-01 16:12:51 +03:00
|
|
|
return failed
|
|
|
|
}
|