Skip to content

Commit

Permalink
fix cmd/tools/test_if_v_test_system_works.v, add it to v test-all
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Dec 12, 2023
1 parent 9c74099 commit 6406da5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
47 changes: 26 additions & 21 deletions cmd/tools/test_if_v_test_system_works.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions cmd/tools/vtest-all.v
Original file line number Diff line number Diff line change
Expand Up @@ -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`'
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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))
Expand All @@ -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"));')
}
Expand All @@ -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))
Expand Down
16 changes: 9 additions & 7 deletions vlib/v/gen/c/cmain.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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('}')
}
Expand All @@ -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();
}
')
}
Expand All @@ -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();')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6406da5

Please sign in to comment.