From 799c95dc4e7588aa3dd8a51515a0f6ef7cfeac34 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 12 Feb 2022 15:06:09 +0200 Subject: [PATCH] tests: filter test_ fns with params from the list of automatically run test functions (fix #13443) --- vlib/v/ast/ast.v | 2 +- vlib/v/checker/fn.v | 4 +++- vlib/v/gen/c/cgen.v | 3 +++ vlib/v/gen/js/js.v | 3 +++ vlib/v/parser/fn.v | 6 ++---- vlib/v/parser/parser.v | 13 ++++++------- .../any_test.v | 5 +++++ .../main.v | 13 +++++++++++++ 8 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/any_test.v create mode 100644 vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/main.v diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index d7bb96ea26..a90227f2b8 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -460,7 +460,7 @@ pub: is_noreturn bool // true, when [noreturn] is used on a fn is_manualfree bool // true, when [manualfree] is used on a fn is_main bool // true for `fn main()` - is_test bool // true for `fn test_abcde` + is_test bool // true for `fn test_abcde() {}`, false for `fn test_abc(x int) {}`, or for fns that do not start with test_ is_conditional bool // true for `[if abc] fn abc(){}` is_exported bool // true for `[export: 'exact_C_name']` is_keep_alive bool // passed memory must not be freed (by GC) before function returns diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index d8e85ab1d1..df56c4e869 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -270,7 +270,8 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } } // TODO c.pref.is_vet - if node.language == .v && !node.is_method && node.is_test { + if c.file.is_test && (!node.is_method && (node.short_name.starts_with('test_') + || node.short_name.starts_with('testsuite_'))) { if !c.pref.is_test { // simple heuristic for st in node.stmts { @@ -285,6 +286,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { if node.params.len != 0 { c.error('test functions should take 0 parameters', node.pos) } + if node.return_type != ast.void_type_idx && node.return_type.clear_flag(.optional) != ast.void_type_idx { c.error('test functions should either return nothing at all, or be marked to return `?`', diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index da9561ec94..68804b9494 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -6304,6 +6304,9 @@ fn (g &Gen) get_all_test_function_names() []string { mut tsuite_begin := '' mut tsuite_end := '' for _, f in g.table.fns { + if !f.is_test { + continue + } if f.name.ends_with('.testsuite_begin') { tsuite_begin = f.name continue diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 2adea95436..83ab59b46a 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -383,6 +383,9 @@ fn (g &JsGen) get_all_test_function_names() []string { mut tsuite_begin := '' mut tsuite_end := '' for _, f in g.table.fns { + if !f.is_test { + continue + } if f.name.ends_with('.testsuite_begin') { tsuite_begin = f.name continue diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index a400e0f46a..9352b903ff 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -361,10 +361,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl { end_pos := p.prev_tok.pos() short_fn_name := name is_main := short_fn_name == 'main' && p.mod == 'main' - mut is_test := (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_')) - && (p.file_base.ends_with('_test.v') - || p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test')) - + is_test := (!is_method && params.len == 0) && p.inside_test_file + && (short_fn_name.starts_with('test_') || short_fn_name.starts_with('testsuite_')) file_mode := p.file_backend_mode // Register if is_method { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 8fc35758a2..deb1770cf8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -162,19 +162,18 @@ pub fn (mut p Parser) set_path(path string) { p.file_name = path p.file_base = os.base(path) p.file_name_dir = os.dir(path) - if p.file_name_dir.contains('vlib') { - p.inside_vlib_file = true - } + p.inside_vlib_file = p.file_name_dir.contains('vlib') + p.inside_test_file = p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') + || p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test') + hash := fnv1a.sum64_string(path) p.unique_prefix = hash.hex_full() - if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') { - p.inside_test_file = true - } + + p.file_backend_mode = .v before_dot_v := path.all_before_last('.v') // also works for .vv and .vsh language := before_dot_v.all_after_last('.') language_with_underscore := before_dot_v.all_after_last('_') if language == before_dot_v && language_with_underscore == before_dot_v { - p.file_backend_mode = .v return } actual_language := if language == before_dot_v { language_with_underscore } else { language } diff --git a/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/any_test.v b/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/any_test.v new file mode 100644 index 0000000000..9103b4b175 --- /dev/null +++ b/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/any_test.v @@ -0,0 +1,5 @@ +module main + +fn test_any_name() { + println(@FN) +} diff --git a/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/main.v b/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/main.v new file mode 100644 index 0000000000..36e23d758a --- /dev/null +++ b/vlib/v/tests/run_project_folders/issue_13443_test_prefixed_fns_in_normal_v_files/main.v @@ -0,0 +1,13 @@ +fn main() { + println(@FN) + println('OK') +} + +fn test_fn_in_normal_v_file() { + println(@FN) +} + +fn test_any(any_name int) int { + println(@FN) + return 0 +}