From 6406da5460d1ba8de3980c72cad565a17bbd9fe1 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 10 Dec 2023 12:10:37 +0200 Subject: [PATCH] fix cmd/tools/test_if_v_test_system_works.v, add it to `v test-all` --- cmd/tools/test_if_v_test_system_works.v | 47 ++++++++++++++----------- cmd/tools/vtest-all.v | 6 ++++ vlib/v/gen/c/cgen.v | 12 +++++++ vlib/v/gen/c/cmain.v | 16 +++++---- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/cmd/tools/test_if_v_test_system_works.v b/cmd/tools/test_if_v_test_system_works.v index 61877ddf5c9ce1..eac02e28747f0d 100644 --- a/cmd/tools/test_if_v_test_system_works.v +++ b/cmd/tools/test_if_v_test_system_works.v @@ -26,13 +26,14 @@ fn get_vexe_path() string { fn new_tdir() string { dir := os.join_path(os.vtmp_dir(), rand.ulid()) os.rmdir_all(dir) or {} + eprintln('... creating tdir: ${tdir}') os.mkdir_all(dir) or { panic(err) } C.atexit(cleanup_tdir) return dir } fn cleanup_tdir() { - println('... removing tdir: ${tdir}') + eprintln('... removing tdir: ${tdir}') os.rmdir_all(tdir) or { eprintln(err) } } @@ -61,27 +62,31 @@ fn (result MyResult) matches(gpattern string) MyResult { fn create_test(tname string, tcontent string) !string { tpath := os.join_path(tdir, tname) os.write_file(tpath, tcontent)! - eprintln('>>>>>>>> tpath: ${tpath} | tcontent: ${tcontent}') + $if trace_create_test ? { + eprintln('>>>>>>>> tpath: ${tpath} | tcontent:') + eprintln(tcontent) + eprintln('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') + } return os.quoted_path(tpath) } fn check_assert_continues_works() ! { os.chdir(tdir)! - create_test('assert_continues_option_works_test.v', 'fn test_fail1() { assert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() { assert false }')! - result := check_fail('${vexe} -assert continues assert_continues_option_works_test.v') - result.has('assert_continues_option_works_test.v:1: fn test_fail1') + create_test('assert_continues_option_works_test.v', 'fn test_fail1() {\nassert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() {\n assert false\n}\n')! + result := check_fail('${vexe} -g -assert continues assert_continues_option_works_test.v') result.has('assert_continues_option_works_test.v:2: fn test_fail1') result.has('assert_continues_option_works_test.v:3: fn test_fail1') - result.has('assert_continues_option_works_test.v:5: fn test_fail2') + result.has('assert_continues_option_works_test.v:4: fn test_fail1') + result.has('assert_continues_option_works_test.v:7: fn test_fail2') result.has('> assert 2 == 4').has('> assert 2 == 1').has('> assert 2 == 0') - // Check if a test function, tagged with [assert_continues], has the same behaviour, without needing additional options - create_test('assert_continues_tag_works_test.v', '[assert_continues]fn test_fail1() { assert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() { assert false\n assert false }')! - tag_res := check_fail('${vexe} assert_continues_tag_works_test.v') - tag_res.has('assert_continues_tag_works_test.v:1: fn test_fail1') + // Check if a test function, tagged with @[assert_continues], has the same behaviour, without needing additional options + create_test('assert_continues_tag_works_test.v', '@[assert_continues]fn test_fail1() {\nassert 2==4\nassert 2==1\nassert 2==0 }\nfn test_ok(){ assert true }\nfn test_fail2() {\n assert false\n assert false\n}\n')! + tag_res := check_fail('${vexe} -g assert_continues_tag_works_test.v') tag_res.has('assert_continues_tag_works_test.v:2: fn test_fail1') tag_res.has('assert_continues_tag_works_test.v:3: fn test_fail1') - tag_res.has('assert_continues_tag_works_test.v:5: fn test_fail2') - if tag_res.contains('assert_continues_tag_works_test.v:6: fn test_fail2') { + tag_res.has('assert_continues_tag_works_test.v:4: fn test_fail1') + tag_res.has('assert_continues_tag_works_test.v:7: fn test_fail2') + if tag_res.contains('assert_continues_tag_works_test.v:8: fn test_fail2') { exit(1) } } @@ -111,22 +116,22 @@ fn main() { os.chdir(os.wd_at_startup) or {} } println('> vroot: ${vroot} | vexe: ${vexe} | tdir: ${tdir}') - ok_fpath := create_test('a_single_ok_test.v', 'fn test_ok(){ assert true }')! - if check_ok('${vexe} ${ok_fpath}') != '' { + ok_fpath := create_test('a_single_ok_test.v', 'fn test_ok(){\n assert true \n}\n')! + if check_ok('${vexe} -g ${ok_fpath}') != '' { exit(1) } - check_ok('${vexe} test ${ok_fpath}').matches('*OK*a_single_ok_test.v*') - check_ok('${vexe} test "${tdir}"').matches('*OK*a_single_ok_test.v*') + check_ok('${vexe} -g test ${ok_fpath}').matches('*OK*a_single_ok_test.v*') + check_ok('${vexe} -g test "${tdir}"').matches('*OK*a_single_ok_test.v*') // - fail_fpath := create_test('a_single_failing_test.v', 'fn test_fail(){ assert 1 == 2 }')! - check_fail('${vexe} ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:1: fn test_fail') - check_fail('${vexe} test ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:1: fn test_fail') - check_fail('${vexe} test "${tdir}"').has('> assert 1 == 2') + fail_fpath := create_test('a_single_failing_test.v', 'fn test_fail(){\n assert 1 == 2 \n}\n')! + check_fail('${vexe} -g ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:2: fn test_fail') + check_fail('${vexe} -g test ${fail_fpath}').has('> assert 1 == 2').has('a_single_failing_test.v:2: fn test_fail') + check_fail('${vexe} -g test "${tdir}"').has('> assert 1 == 2') rel_dir := os.join_path(tdir, rand.ulid()) os.mkdir(rel_dir)! os.chdir(rel_dir)! relative_path := '..' + os.path_separator + 'a_single_ok_test.v' - check_ok('${vexe} test ${os.quoted_path(relative_path)}').has('OK').has('a_single_ok_test.v') + check_ok('${vexe} -g test ${os.quoted_path(relative_path)}').has('OK').has('a_single_ok_test.v') // check_assert_continues_works()! println('> all done') diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index bf15896bd16055..7db512ffab04ad 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -270,6 +270,12 @@ fn get_all_commands() []Command { rmfile: 'str_array' } } + res << Command{ + line: '${vexe} run cmd/tools/test_if_v_test_system_works.v' + okmsg: 'The V test system works' + runcmd: .execute + contains: '> all done' + } res << Command{ line: '${vexe} ${vargs} -progress test-cleancode' okmsg: 'All .v files are invariant when processed with `v fmt`' diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 80bfdef60b8b20..924505bd4dd2ee 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5875,6 +5875,9 @@ fn (mut g Gen) write_init_function() { // ___argv is declared as voidptr here, because that unifies the windows/unix logic g.writeln('void _vinit(int ___argc, voidptr ___argv) {') + if 'trace_vinit' in g.pref.compile_defines { + g.writeln('\tfprintf(stderr, ">>>> _vinit start\\n");') + } g.write_debug_calls_typeof_functions() @@ -5945,6 +5948,9 @@ fn (mut g Gen) write_init_function() { cleaning_up_array << g.gen_call_to_mod_fn(mod_name, 'cleanup') } + if 'trace_vinit' in g.pref.compile_defines { + g.writeln('\tfprintf(stderr, ">>>> _vinit end\\n");') + } g.writeln('}') if g.pref.printfn_list.len > 0 && '_vinit' in g.pref.printfn_list { println(g.out.after(fn_vinit_start_pos)) @@ -5953,6 +5959,9 @@ fn (mut g Gen) write_init_function() { // fn_vcleanup_start_pos := g.out.len g.writeln('void _vcleanup(void) {') + if 'trace_vcleanup' in g.pref.compile_defines { + g.writeln('\tfprintf(stderr, ">>>> _vcleanup start\\n");') + } if g.pref.trace_calls { g.writeln('\tv__trace_calls__on_call(_SLIT("_vcleanup"));') } @@ -5969,6 +5978,9 @@ fn (mut g Gen) write_init_function() { for x in cleaning_up_array.reverse() { g.writeln(x) } + if 'trace_vcleanup' in g.pref.compile_defines { + g.writeln('\tfprintf(stderr, ">>>> _vcleanup end\\n");') + } g.writeln('}') if g.pref.printfn_list.len > 0 && '_vcleanup' in g.pref.printfn_list { println(g.out.after(fn_vcleanup_start_pos)) diff --git a/vlib/v/gen/c/cmain.v b/vlib/v/gen/c/cmain.v index 7918ee8f24aa52..9c0e9bb9e4330a 100644 --- a/vlib/v/gen/c/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -116,6 +116,9 @@ fn (mut g Gen) gen_c_main_header() { } g.writeln('#endif') } + if !g.pref.is_bare { + g.writeln('\tatexit(_vcleanup);') + } g.writeln('\t_vinit(___argc, (voidptr)___argv);') g.gen_c_main_profile_hook() if g.pref.is_livemain { @@ -124,7 +127,9 @@ fn (mut g Gen) gen_c_main_header() { } pub fn (mut g Gen) gen_c_main_footer() { - g.writeln('\t_vcleanup();') + if g.pref.is_bare { + g.writeln('\t_vcleanup();') + } g.writeln('\treturn 0;') g.writeln('}') } @@ -140,14 +145,12 @@ void (_vsokol_cleanup_cb)(void) { if (_vsokol_user_cleanup_ptr) { _vsokol_user_cleanup_ptr(); } - _vcleanup(); } void (_vsokol_cleanup_userdata_cb)(void* user_data) { if (_vsokol_user_cleanup_cb_ptr) { _vsokol_user_cleanup_cb_ptr(g_desc.user_data); } - _vcleanup(); } ') } @@ -168,8 +171,8 @@ sapp_desc sokol_main(int argc, char* argv[]) { } g.writeln('#endif') } - g.writeln('\t_vinit(argc, (voidptr)argv); - ') + g.writeln('\tatexit(_vcleanup);') + g.writeln('\t_vinit(argc, (voidptr)argv);') g.gen_c_main_profile_hook() g.writeln('\tmain__main();') @@ -248,6 +251,7 @@ pub fn (mut g Gen) gen_c_main_for_tests() { } g.writeln('#endif') } + g.writeln('\tatexit(_vcleanup);') g.writeln('\t_vinit(___argc, (voidptr)___argv);') g.writeln('\tmain__vtest_init();') g.gen_c_main_profile_hook() @@ -303,8 +307,6 @@ pub fn (mut g Gen) gen_c_main_for_tests() { // g.writeln('\t_vtrunner._method__v_free(_vtobj);') g.writeln('') - g.writeln('\t_vcleanup();') - g.writeln('') g.writeln('\treturn test_exit_code;') g.writeln('}') if g.pref.printfn_list.len > 0 && 'main' in g.pref.printfn_list {