diff --git a/doc/docs.md b/doc/docs.md index 2c7da50c0b..19679417a2 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1069,6 +1069,12 @@ fn test_hello() { All test functions have to be placed in `*_test.v` files and begin with `test_`. +You can also define a special test function: `testsuite_begin`, which will be +run *before* all other test functions in a `_test.v` file. + +You can also define a special test function: `testsuite_end`, which will be +run *after* all other test functions in a `_test.v` file. + To run the tests do `v hello_test.v`. To test an entire module, do `v test mymodule`. diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index 52ed2419d7..2a715e592f 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -819,11 +819,28 @@ fn (t &Table) main_exists() bool { } fn (t &Table) all_test_function_names() []string { - mut res := []string + mut fn_begin_test_name := '' + mut fn_end_test_name := '' + + mut fn_test_names := []string for _, f in t.fns { if f.name.contains('__test_') { - res << f.name + fn_test_names << f.name } + else if f.name.contains('__testsuite_begin') { + fn_begin_test_name = f.name + } + else if f.name.contains('__testsuite_end') { + fn_end_test_name = f.name + } + } + mut res := []string + if fn_begin_test_name.len > 0 { + res << fn_begin_test_name + } + res << fn_test_names + if fn_end_test_name.len > 0 { + res << fn_end_test_name } return res } @@ -1142,4 +1159,3 @@ fn type_cat_str(tc TypeCategory) string { 'unknown'}} return tc_str } - diff --git a/vlib/os/os.v b/vlib/os/os.v index 47b3651ddd..01f57265de 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -624,7 +624,7 @@ pub fn rmdir(path string) { pub fn rmdir_recursive(path string) { items := os.ls(path) or { - panic(err) + return } for item in items { if os.is_dir(filepath.join(path,item)) { @@ -637,7 +637,7 @@ pub fn rmdir_recursive(path string) { pub fn is_dir_empty(path string) bool { items := os.ls(path) or { - panic(err) + return true } return items.len == 0 } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 0c1710a943..ce36409195 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -1,9 +1,14 @@ -import os -import filepath +import ( + os + filepath +) -fn test_aaa_setup(){ +fn testsuite_begin() { + cleanup_leftovers() +} + +fn testsuite_end() { cleanup_leftovers() - assert true } fn test_setenv() { @@ -240,11 +245,6 @@ fn test_make_symlink_check_is_link_and_remove_symlink() { // } //} -fn test_zzz_cleanup(){ - cleanup_leftovers() assert true -} - - fn test_symlink() { $if windows { return } os.mkdir('symlink') or { panic(err) } @@ -285,18 +285,14 @@ fn test_is_executable_writable_readable() { // this function is called by both test_aaa_setup & test_zzz_cleanup // it ensures that os tests do not polute the filesystem with leftover // files so that they can be run several times in a row. -fn cleanup_leftovers(){ +fn cleanup_leftovers() { // possible leftovers from test_cp os.rm('cp_example.txt') os.rm('cp_new_example.txt') // possible leftovers from test_cp_r - os.rm('ex/ex2/ex2.txt') - os.rmdir('ex/ex2') - os.rm('ex/ex1.txt') - os.rmdir('ex') - os.rm('ex2/ex2.txt') - os.rmdir('ex2') + os.rmdir_recursive('ex') + os.rmdir_recursive('ex2') os.rm('ex1.txt') os.rm('ex2.txt') }