| 1 | import os |
| 2 | |
| 3 | const assert_failed_defer_cleanup_path = os.join_path(os.vtmp_dir(), |
| 4 | 'v_assert_failed_defer_cleanup_test.txt') |
| 5 | const assert_failed_deferred_file_close_path = os.join_path(os.vtmp_dir(), |
| 6 | 'v_assert_failed_deferred_file_close_test.txt') |
| 7 | |
| 8 | fn vroot_path(relpath string) string { |
| 9 | return os.real_path(os.join_path(@VMODROOT, relpath)) |
| 10 | } |
| 11 | |
| 12 | fn vexecute(relpath string) os.Result { |
| 13 | vexe := @VEXE |
| 14 | return os.execute('${os.quoted_path(vexe)} -test-runner normal ' + |
| 15 | os.quoted_path(vroot_path(relpath))) |
| 16 | } |
| 17 | |
| 18 | fn deferred_file_flush_line(i int) string { |
| 19 | return '第${i:03}: {"code":200,"msg":"数据请求成功","word":"possible","meaning":"可能的;合适的","url":"https://dict.example/api?word=possible"}' |
| 20 | } |
| 21 | |
| 22 | fn deferred_file_flush_expected() string { |
| 23 | line_sep := $if windows { '\r\n' } $else { '\n' } |
| 24 | mut lines := []string{cap: 160} |
| 25 | for i in 0 .. 160 { |
| 26 | lines << deferred_file_flush_line(i) |
| 27 | } |
| 28 | return lines.join(line_sep) + line_sep |
| 29 | } |
| 30 | |
| 31 | fn testsuite_begin() { |
| 32 | os.setenv('VCOLORS', 'never', true) |
| 33 | } |
| 34 | |
| 35 | fn test_returning_options() { |
| 36 | res := vexecute('vlib/v/tests/testdata/tests_returning_options_failing_test.v') |
| 37 | assert res.exit_code == 1 |
| 38 | // dump(res) |
| 39 | assert res.output.contains('tests_returning_options_failing_test.v:13: fn test_example failed propagation with error: failing test with return, err: oh no') |
| 40 | assert res.output.contains('tests_returning_options_failing_test.v:19: fn test_example_2 failed propagation with error: oh no') |
| 41 | } |
| 42 | |
| 43 | fn test_sizeof_in_assert() { |
| 44 | res := vexecute('vlib/v/tests/testdata/sizeof_used_in_assert_test.v') |
| 45 | assert res.exit_code == 1 |
| 46 | // dump(res) |
| 47 | assert res.output.contains('sizeof_used_in_assert_test.v:11: fn test_assert_offsetof') |
| 48 | assert res.output.contains('assert __offsetof(main.Abc, y) == 1') |
| 49 | |
| 50 | assert res.output.contains('sizeof_used_in_assert_test.v:15: fn test_assert_sizeof') |
| 51 | assert res.output.contains('assert sizeof(main.Abc) == sizeof(main.Xyz)') |
| 52 | } |
| 53 | |
| 54 | fn test_assert_failure_runs_scoped_defer_cleanup() { |
| 55 | defer { |
| 56 | os.rm(assert_failed_defer_cleanup_path) or {} |
| 57 | } |
| 58 | os.rm(assert_failed_defer_cleanup_path) or {} |
| 59 | res := vexecute('vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v') |
| 60 | assert res.exit_code == 1 |
| 61 | assert res.output.contains('assert_with_scoped_defer_cleanup_failing_test.v') |
| 62 | assert res.output.contains('assert false') |
| 63 | assert !os.exists(assert_failed_defer_cleanup_path) |
| 64 | } |
| 65 | |
| 66 | fn test_assert_failure_runs_deferred_file_close_flush() { |
| 67 | defer { |
| 68 | os.rm(assert_failed_deferred_file_close_path) or {} |
| 69 | } |
| 70 | os.rm(assert_failed_deferred_file_close_path) or {} |
| 71 | res := vexecute('vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v') |
| 72 | assert res.exit_code == 1 |
| 73 | assert res.output.contains('assert_with_scoped_defer_cleanup_failing_test.v') |
| 74 | assert res.output.contains('assert false') |
| 75 | content := os.read_file(assert_failed_deferred_file_close_path)! |
| 76 | assert content == deferred_file_flush_expected() |
| 77 | } |
| 78 | |
| 79 | fn test_run_only_reports_filtered_failures() { |
| 80 | test_path := os.join_path(os.vtmp_dir(), 'issue_22778_run_only_${os.getpid()}_test.v') |
| 81 | defer { |
| 82 | os.rm(test_path) or {} |
| 83 | } |
| 84 | test_source := [ |
| 85 | 'fn test_ok() {', |
| 86 | ' assert true', |
| 87 | '}', |
| 88 | '', |
| 89 | 'fn test_fail() {', |
| 90 | " assert false, 'expected to fail'", |
| 91 | '}', |
| 92 | ].join_lines() |
| 93 | os.write_file(test_path, test_source)! |
| 94 | res := |
| 95 | os.execute('${os.quoted_path(@VEXE)} -test-runner normal -run-only test_fail ${os.quoted_path(test_path)}') |
| 96 | assert res.exit_code == 1 |
| 97 | assert res.output.contains('fn test_fail'), res.output |
| 98 | assert res.output.contains('expected to fail'), res.output |
| 99 | assert !res.output.contains('fn test_ok'), res.output |
| 100 | } |
| 101 | |
| 102 | fn test_before_each_and_after_each_run_around_each_test() { |
| 103 | test_path := os.join_path(os.vtmp_dir(), 'issue_19699_${os.getpid()}_test.v') |
| 104 | log_path := os.join_path(os.vtmp_dir(), 'issue_19699_${os.getpid()}.log') |
| 105 | defer { |
| 106 | os.rm(test_path) or {} |
| 107 | os.rm(log_path) or {} |
| 108 | os.unsetenv('V_ISSUE_19699_LOG_PATH') |
| 109 | } |
| 110 | test_source := [ |
| 111 | 'import os', |
| 112 | '', |
| 113 | "const issue_19699_log_path = os.getenv('V_ISSUE_19699_LOG_PATH')", |
| 114 | '', |
| 115 | 'fn append_log(line string) {', |
| 116 | ' mut entries := os.read_lines(issue_19699_log_path) or { []string{} }', |
| 117 | ' entries << line', |
| 118 | " os.write_file(issue_19699_log_path, entries.join_lines() + '\\n') or { panic(err) }", |
| 119 | '}', |
| 120 | '', |
| 121 | "fn testsuite_begin() { append_log('testsuite_begin') }", |
| 122 | "fn before_each() { append_log('before_each') }", |
| 123 | "fn after_each() { append_log('after_each') }", |
| 124 | "fn test_one() { append_log('test_one') }", |
| 125 | "fn test_two() { append_log('test_two') }", |
| 126 | "fn testsuite_end() { append_log('testsuite_end') }", |
| 127 | ].join_lines() |
| 128 | os.write_file(test_path, test_source)! |
| 129 | os.rm(log_path) or {} |
| 130 | os.setenv('V_ISSUE_19699_LOG_PATH', log_path, true) |
| 131 | res := os.execute('${os.quoted_path(@VEXE)} -test-runner normal ${os.quoted_path(test_path)}') |
| 132 | assert res.exit_code == 0, res.output |
| 133 | log_lines := os.read_lines(log_path)! |
| 134 | assert log_lines == [ |
| 135 | 'testsuite_begin', |
| 136 | 'before_each', |
| 137 | 'test_one', |
| 138 | 'after_each', |
| 139 | 'before_each', |
| 140 | 'test_two', |
| 141 | 'after_each', |
| 142 | 'testsuite_end', |
| 143 | ] |
| 144 | } |
| 145 | |
| 146 | fn test_windows_c_system_info_is_undefined_on_non_windows() { |
| 147 | $if windows { |
| 148 | return |
| 149 | } |
| 150 | source_path := os.join_path(os.vtmp_dir(), 'issue_25821_${os.getpid()}.v') |
| 151 | source := [ |
| 152 | 'module main', |
| 153 | 'fn main() {', |
| 154 | ' x := C.SYSTEM_INFO{}', |
| 155 | ' dump(x)', |
| 156 | '}', |
| 157 | ].join_lines() |
| 158 | os.write_file(source_path, source)! |
| 159 | defer { |
| 160 | os.rm(source_path) or {} |
| 161 | } |
| 162 | res := os.execute('${os.quoted_path(@VEXE)} ${os.quoted_path(source_path)}') |
| 163 | assert res.exit_code != 0, res.output |
| 164 | assert res.output.contains('unknown type `C.SYSTEM_INFO`'), res.output |
| 165 | assert !res.output.contains('C compilation error'), res.output |
| 166 | assert !res.output.contains('builder error'), res.output |
| 167 | } |
| 168 | |