v2 / vlib / v / slow_tests / repl / repl_test.v
103 lines · 97 sloc · 3.18 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// vtest build: !musl? && !sanitized_job?
2module main
3
4import os
5import v.slow_tests.repl.runner
6import benchmark
7import sync.pool
8
9@[markused]
10const turn_off_vcolors = os.setenv('VCOLORS', 'never', true)
11const is_silent = $if silent ? { true } $else { false }
12
13fn test_the_v_compiler_can_be_invoked() {
14 vexec := runner.full_path_to_v(5)
15 if !is_silent {
16 println('vexecutable: ${vexec}')
17 }
18 assert vexec != ''
19 vcmd := '${os.quoted_path(vexec)} -version'
20 r := os.execute_or_exit(vcmd)
21 assert r.exit_code == 0
22 // println('"${vcmd}" exit_code: ${r.exit_code} | output: ${r.output}')
23 vcmd_error := '${os.quoted_path(vexec)} nonexisting.v'
24 r_error := os.execute(vcmd_error)
25 if r_error.exit_code < 0 {
26 panic(r_error.output)
27 }
28 // println('"${vcmd_error}" exit_code: ${r_error.exit_code} | output: ${r_error.output}')
29 assert r_error.exit_code == 1
30 actual_error := r_error.output.trim_space()
31 assert actual_error == "builder error: nonexisting.v doesn't exist"
32}
33
34struct Session {
35mut:
36 options runner.RunnerOptions
37 bmark benchmark.Benchmark
38}
39
40fn test_all_v_repl_files() {
41 if os.user_os() == 'windows' {
42 if os.getenv('VTEST_ENABLE_REPL') == '' {
43 println('This test is disabled on windows temporarily')
44 println('set VTEST_ENABLE_REPL=1')
45 println('if you do want to run it anyway.')
46 exit(0)
47 }
48 }
49 mut session := &Session{
50 options: runner.new_options()
51 bmark: benchmark.new_benchmark()
52 }
53 // warmup, and ensure that the vrepl is compiled in single threaded mode if it does not exist
54 runner.run_repl_file(os.cache_dir(), session.options.vexec,
55 'vlib/v/slow_tests/repl/nothing.repl') or { panic(err) }
56 session.bmark.set_total_expected_steps(session.options.files.len)
57 mut pool_repl := pool.new_pool_processor(callback: worker_repl)
58 pool_repl.set_shared_context(session)
59 $if windows {
60 // See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019
61 pool_repl.set_max_jobs(1)
62 }
63 pool_repl.work_on_items[string](session.options.files)
64 session.bmark.stop()
65 println(session.bmark.total_message('total time spent running REPL files'))
66}
67
68fn worker_repl(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
69 cdir := os.cache_dir()
70 mut session := unsafe { &Session(p.get_shared_context()) }
71 mut tls_bench := unsafe { &benchmark.Benchmark(p.get_thread_context(idx)) }
72 if isnil(tls_bench) {
73 tls_bench = benchmark.new_benchmark_pointer()
74 tls_bench.set_total_expected_steps(session.bmark.nexpected_steps)
75 p.set_thread_context(idx, tls_bench)
76 }
77 tls_bench.cstep = idx
78 mut tfolder := os.join_path(cdir, 'vrepl_tests_${idx}')
79 if os.is_dir(tfolder) {
80 os.rmdir_all(tfolder) or { panic(err) }
81 }
82 os.mkdir(tfolder) or { panic(err) }
83 tfolder = os.real_path(tfolder)
84 file := p.get_item[string](idx)
85 session.bmark.step()
86 tls_bench.step()
87 fres := runner.run_repl_file(tfolder, session.options.vexec, file) or {
88 session.bmark.fail()
89 tls_bench.fail()
90 os.rmdir_all(tfolder) or { panic(err) }
91 eprintln(tls_bench.step_message_fail(err.msg()))
92 assert false
93 return pool.no_result
94 }
95 session.bmark.ok()
96 tls_bench.ok()
97 os.rmdir_all(tfolder) or { panic(err) }
98 if !is_silent {
99 println(tls_bench.step_message_ok(fres))
100 }
101 assert true
102 return pool.no_result
103}
104