2019-12-01 12:50:13 +03:00
|
|
|
module testing
|
|
|
|
|
2020-04-26 09:32:05 +03:00
|
|
|
import os
|
|
|
|
import term
|
|
|
|
import benchmark
|
|
|
|
import sync
|
|
|
|
import v.pref
|
2019-12-01 12:50:13 +03:00
|
|
|
|
2020-04-18 05:49:03 +03:00
|
|
|
pub struct TestMessageHandler {
|
2020-04-18 18:44:21 +03:00
|
|
|
mut:
|
2020-04-18 05:49:03 +03:00
|
|
|
messages []string
|
2020-04-18 18:44:21 +03:00
|
|
|
pub mut:
|
2020-04-18 05:49:03 +03:00
|
|
|
message_idx int
|
|
|
|
mtx &sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-12-01 12:50:13 +03:00
|
|
|
pub struct TestSession {
|
|
|
|
pub mut:
|
2020-03-04 22:28:42 +03:00
|
|
|
files []string
|
2020-03-21 11:48:02 +03:00
|
|
|
skip_files []string
|
2020-03-04 22:28:42 +03:00
|
|
|
vexe string
|
2020-03-21 11:48:02 +03:00
|
|
|
vroot string
|
2020-03-04 22:28:42 +03:00
|
|
|
vargs string
|
|
|
|
failed bool
|
|
|
|
benchmark benchmark.Benchmark
|
2020-01-24 22:07:44 +03:00
|
|
|
show_ok_tests bool
|
2020-04-18 05:49:03 +03:00
|
|
|
message_handler &TestMessageHandler
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut mh TestMessageHandler) append_message(msg string) {
|
2020-04-18 18:44:21 +03:00
|
|
|
mh.mtx.lock()
|
|
|
|
mh.messages << msg
|
|
|
|
mh.mtx.unlock()
|
|
|
|
}
|
|
|
|
|
2020-03-06 20:53:29 +03:00
|
|
|
pub fn new_test_session(_vargs string) TestSession {
|
2020-04-26 14:49:31 +03:00
|
|
|
mut skip_files := []string{}
|
2020-03-21 11:48:02 +03:00
|
|
|
skip_files << '_non_existing_'
|
|
|
|
$if solaris {
|
2020-03-31 13:46:46 +03:00
|
|
|
skip_files << "examples/gg/gg2.v"
|
2020-03-21 11:48:02 +03:00
|
|
|
skip_files << "examples/pico/pico.v"
|
2020-03-31 13:46:46 +03:00
|
|
|
skip_files << "examples/sokol/fonts.v"
|
|
|
|
skip_files << "examples/sokol/drawing.v"
|
2020-03-21 11:48:02 +03:00
|
|
|
}
|
2020-03-06 20:53:29 +03:00
|
|
|
vargs := _vargs.replace('-silent', '')
|
2020-03-21 11:48:02 +03:00
|
|
|
vexe := pref.vexe_path()
|
2019-12-01 12:50:13 +03:00
|
|
|
return TestSession{
|
2020-03-21 11:48:02 +03:00
|
|
|
vexe: vexe
|
|
|
|
vroot: os.dir(vexe)
|
|
|
|
skip_files: skip_files
|
2019-12-01 12:50:13 +03:00
|
|
|
vargs: vargs
|
2020-03-06 20:53:29 +03:00
|
|
|
show_ok_tests: !_vargs.contains('-silent')
|
2020-05-27 06:42:48 +03:00
|
|
|
message_handler: &TestMessageHandler(0)
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ts TestSession) init() {
|
2020-04-18 05:49:03 +03:00
|
|
|
ts.benchmark = benchmark.new_benchmark_no_cstep()
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut ts TestSession) test() {
|
2019-12-01 12:50:13 +03:00
|
|
|
ts.init()
|
2020-04-26 14:49:31 +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('./', '')
|
2020-03-20 18:41:18 +03:00
|
|
|
file := os.real_path(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-04-01 22:24:58 +03:00
|
|
|
// QTODO
|
|
|
|
//mut pool_of_test_runners := sync.new_pool_processor({
|
|
|
|
mut pool_of_test_runners := sync.new_pool_processor(sync.PoolProcessorConfig{
|
2020-03-04 22:28:42 +03:00
|
|
|
callback: worker_trunner
|
|
|
|
})
|
2020-04-18 05:49:03 +03:00
|
|
|
// for handling messages across threads
|
|
|
|
ts.message_handler = &TestMessageHandler{
|
|
|
|
mtx: sync.new_mutex()
|
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
pool_of_test_runners.set_shared_context(ts)
|
2020-01-20 01:33:16 +03:00
|
|
|
$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.
|
2020-03-04 22:28:42 +03:00
|
|
|
pool_of_test_runners.set_max_jobs(1)
|
2020-01-20 01:33:16 +03:00
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
pool_of_test_runners.work_on_pointers(remaining_files.pointers())
|
2020-01-20 01:33:16 +03:00
|
|
|
ts.benchmark.stop()
|
2020-01-28 07:18:19 +03:00
|
|
|
eprintln(term.h_divider('-'))
|
2020-01-20 01:33:16 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut m TestMessageHandler) display_message() {
|
2020-04-18 05:49:03 +03:00
|
|
|
m.mtx.lock()
|
|
|
|
defer {
|
|
|
|
m.messages.clear()
|
|
|
|
m.mtx.unlock()
|
|
|
|
}
|
|
|
|
for msg in m.messages {
|
|
|
|
m.message_idx++
|
|
|
|
eprintln(msg.
|
|
|
|
replace("TMP1", "${m.message_idx:1d}").
|
|
|
|
replace("TMP2", "${m.message_idx:2d}").
|
|
|
|
replace("TMP3", "${m.message_idx:3d}")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:28:42 +03:00
|
|
|
fn worker_trunner(p mut sync.PoolProcessor, idx int, thread_id int) voidptr {
|
|
|
|
mut ts := &TestSession(p.get_shared_context())
|
2020-04-18 18:44:21 +03:00
|
|
|
defer { ts.message_handler.display_message() }
|
2020-03-10 17:02:09 +03:00
|
|
|
tmpd := os.temp_dir()
|
2020-01-20 01:33:16 +03:00
|
|
|
show_stats := '-stats' in ts.vargs.split(' ')
|
2020-03-04 22:28:42 +03:00
|
|
|
// tls_bench is used to format the step messages/timings
|
|
|
|
mut tls_bench := &benchmark.Benchmark(p.get_thread_context(idx))
|
|
|
|
if isnil(tls_bench) {
|
|
|
|
tls_bench = benchmark.new_benchmark_pointer()
|
|
|
|
tls_bench.set_total_expected_steps(ts.benchmark.nexpected_steps)
|
|
|
|
p.set_thread_context(idx, tls_bench)
|
|
|
|
}
|
2020-04-18 05:49:03 +03:00
|
|
|
tls_bench.no_cstep = true
|
2020-03-04 22:28:42 +03:00
|
|
|
dot_relative_file := p.get_string_item(idx)
|
2020-03-21 11:48:02 +03:00
|
|
|
relative_file := dot_relative_file.replace(ts.vroot + os.path_separator, '').replace('./', '')
|
2020-03-20 18:41:18 +03:00
|
|
|
file := os.real_path(relative_file)
|
2020-03-04 22:28:42 +03:00
|
|
|
// Ensure that the generated binaries will be stored in the temporary folder.
|
|
|
|
// Remove them after a test passes/fails.
|
2020-03-19 17:49:07 +03:00
|
|
|
fname := os.file_name(file)
|
2020-03-04 22:28:42 +03:00
|
|
|
generated_binary_fname := if os.user_os() == 'windows' { fname.replace('.v', '.exe') } else { fname.replace('.v', '') }
|
2020-03-09 04:23:34 +03:00
|
|
|
generated_binary_fpath := os.join_path(tmpd, generated_binary_fname)
|
2020-03-04 22:28:42 +03:00
|
|
|
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')
|
|
|
|
ts.benchmark.step()
|
|
|
|
tls_bench.step()
|
2020-04-11 17:57:02 +03:00
|
|
|
if relative_file.replace('\\', '/') in ts.skip_files {
|
2020-03-21 11:48:02 +03:00
|
|
|
ts.benchmark.skip()
|
|
|
|
tls_bench.skip()
|
2020-04-18 18:44:21 +03:00
|
|
|
ts.message_handler.append_message(tls_bench.step_message_skip(relative_file))
|
2020-03-21 11:48:02 +03:00
|
|
|
return sync.no_result
|
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
if show_stats {
|
2020-04-18 18:44:21 +03:00
|
|
|
ts.message_handler.append_message(term.h_divider('-'))
|
2020-03-04 22:28:42 +03:00
|
|
|
status := os.system(cmd)
|
|
|
|
if status == 0 {
|
|
|
|
ts.benchmark.ok()
|
|
|
|
tls_bench.ok()
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
else {
|
|
|
|
ts.failed = true
|
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
|
|
|
return sync.no_result
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
r := os.exec(cmd) or {
|
|
|
|
ts.failed = true
|
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
2020-04-18 18:44:21 +03:00
|
|
|
ts.message_handler.append_message(tls_bench.step_message_fail(relative_file))
|
2020-03-04 22:28:42 +03:00
|
|
|
return sync.no_result
|
|
|
|
}
|
|
|
|
if r.exit_code != 0 {
|
|
|
|
ts.failed = true
|
|
|
|
ts.benchmark.fail()
|
|
|
|
tls_bench.fail()
|
2020-04-18 18:44:21 +03:00
|
|
|
ts.message_handler.append_message(tls_bench.step_message_fail('${relative_file}\n$r.output\n'))
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-03-04 22:28:42 +03:00
|
|
|
ts.benchmark.ok()
|
|
|
|
tls_bench.ok()
|
|
|
|
if ts.show_ok_tests {
|
2020-04-18 18:44:21 +03:00
|
|
|
ts.message_handler.append_message(tls_bench.step_message_ok(relative_file))
|
2019-12-01 12:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
if os.exists(generated_binary_fpath) {
|
|
|
|
os.rm(generated_binary_fpath)
|
|
|
|
}
|
|
|
|
return sync.no_result
|
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) {
|
2020-03-09 04:23:34 +03:00
|
|
|
vlib_dir := os.join_path(parent_dir,'vlib')
|
2019-12-27 19:59:04 +03:00
|
|
|
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 {
|
2020-05-17 00:14:06 +03:00
|
|
|
return v_build_failing_skipped(zargs, folder, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn v_build_failing_skipped(zargs string, folder string, skipped []string) bool {
|
2019-12-01 12:50:13 +03:00
|
|
|
main_label := 'Building $folder ...'
|
|
|
|
finish_label := 'building $folder'
|
2020-02-20 13:33:01 +03:00
|
|
|
vexe := pref.vexe_path()
|
2020-03-08 00:26:26 +03:00
|
|
|
parent_dir := os.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, '')
|
2020-02-08 19:01:10 +03:00
|
|
|
eheader(main_label)
|
|
|
|
eprintln('v compiler args: "$vargs"')
|
2019-12-27 19:59:04 +03:00
|
|
|
mut session := new_test_session(vargs)
|
2020-03-09 04:23:34 +03:00
|
|
|
files := os.walk_ext(os.join_path(parent_dir, folder), '.v')
|
2020-04-26 14:49:31 +03:00
|
|
|
mut mains := []string{}
|
2020-03-04 22:28:42 +03:00
|
|
|
for f in files {
|
|
|
|
if !f.contains('modules') && !f.contains('preludes') {
|
|
|
|
$if windows {
|
|
|
|
// skip pico example on windows
|
|
|
|
if f.ends_with('examples\\pico\\pico.v') {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-23 14:58:47 +03:00
|
|
|
}
|
2020-03-04 22:28:42 +03:00
|
|
|
mains << f
|
2020-01-23 14:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
session.files << mains
|
2020-05-17 00:14:06 +03:00
|
|
|
session.skip_files << skipped
|
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 {
|
2020-02-08 19:01:10 +03:00
|
|
|
eheader('Building V binaries...')
|
2019-12-01 16:12:51 +03:00
|
|
|
eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"')
|
2020-02-20 13:33:01 +03:00
|
|
|
vexe := pref.vexe_path()
|
2020-03-08 00:26:26 +03:00
|
|
|
parent_dir := os.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
|
2020-02-09 12:08:04 +03:00
|
|
|
v_build_commands := ['$vexe -o v_g -g cmd/v',
|
|
|
|
'$vexe -o v_prod_g -prod -g cmd/v',
|
|
|
|
'$vexe -o v_cg -cg cmd/v',
|
|
|
|
'$vexe -o v_prod_cg -prod -cg cmd/v',
|
|
|
|
'$vexe -o v_prod -prod cmd/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()
|
2020-01-28 07:18:19 +03:00
|
|
|
eprintln(term.h_divider('-'))
|
2019-12-27 19:59:04 +03:00
|
|
|
eprintln(bmark.total_message('building v binaries'))
|
2019-12-01 16:12:51 +03:00
|
|
|
return failed
|
|
|
|
}
|
2020-02-08 19:01:10 +03:00
|
|
|
|
|
|
|
pub fn eheader(msg string) {
|
2020-03-04 22:28:42 +03:00
|
|
|
eprintln(term.header(msg, '-'))
|
2020-02-08 19:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn header(msg string) {
|
2020-03-04 22:28:42 +03:00
|
|
|
println(term.header(msg, '-'))
|
2020-02-08 19:01:10 +03:00
|
|
|
}
|