v2 / cmd / tools / vtest-self.v
429 lines · 422 sloc · 14.94 KB · 447f876ef989a1750b76421f19ef0818ecbb370d
Raw
1module main
2
3import os
4import testing
5
6struct Config {
7 run_just_essential bool = '${os.getenv('VTEST_JUST_ESSENTIAL')}${os.getenv('VTEST_SANDBOXED_PACKAGING')}' != ''
8 is_musl_ci bool = os.getenv('V_CI_MUSL') != ''
9 is_ubuntu_musl_ci bool = os.getenv('V_CI_UBUNTU_MUSL') != ''
10 is_sandboxed_packaging bool = os.getenv('VTEST_SANDBOXED_PACKAGING') != ''
11 github_job string = os.getenv('GITHUB_JOB')
12mut:
13 test_dirs []string = ['cmd', 'vlib']
14 is_asan_compiler bool
15 is_msan_compiler bool
16 is_ubsan_compiler bool
17 // Options relating to the v command itself (passed in the prefix) `v [...args] test-self`.
18 werror bool
19 sanitize_memory bool
20 sanitize_address bool
21 sanitize_undefined bool
22}
23
24const vroot = os.dir(os.real_path(os.getenv_opt('VEXE') or { @VEXE }))
25
26const essential_list = [
27 'cmd/tools/vvet/vet_test.v',
28 'cmd/tools/vdoc/document/doc_test.v',
29 'vlib/arrays/arrays_test.v',
30 'vlib/bitfield/bitfield_test.v',
31 //
32 'vlib/builtin/int_test.v',
33 'vlib/builtin/array_test.v',
34 'vlib/builtin/float_test.v',
35 'vlib/builtin/byte_test.v',
36 'vlib/builtin/rune_test.v',
37 'vlib/builtin/builtin_test.c.v',
38 'vlib/builtin/map_of_floats_test.v',
39 'vlib/builtin/string_int_test.v',
40 'vlib/builtin/utf8_test.v',
41 'vlib/builtin/map_test.v',
42 'vlib/builtin/string_test.v',
43 'vlib/builtin/sorting_test.v',
44 'vlib/builtin/gated_array_string_test.v',
45 'vlib/builtin/isnil_test.v',
46 'vlib/builtin/string_match_glob_test.v',
47 'vlib/builtin/string_strip_margin_test.v',
48 //
49 'vlib/cli/command_test.v',
50 'vlib/crypto/md5/md5_test.v',
51 'vlib/dl/dl_test.v',
52 'vlib/encoding/base64/base64_test.v',
53 'vlib/encoding/utf8/validate/encoding_utf8_test.v',
54 'vlib/encoding/utf8/utf8_util_test.v',
55 'vlib/flag/flag_test.v',
56 'vlib/json/tests/json_decode_test.v',
57 'vlib/math/math_test.v',
58 'vlib/net/tcp_test.v',
59 'vlib/net/http/http_test.v',
60 'vlib/net/http/server_test.v',
61 'vlib/net/http/request_test.v',
62 'vlib/io/io_test.v',
63 'vlib/io/os_file_reader_test.v',
64 'vlib/os/process_test.v',
65 'vlib/os/file_test.v',
66 'vlib/os/notify/notify_test.c.v',
67 'vlib/os/filepath_test.v',
68 'vlib/os/environment_test.v',
69 'vlib/os/glob_test.v',
70 'vlib/os/os_test.c.v',
71 'vlib/rand/random_numbers_test.v',
72 'vlib/rand/wyrand/wyrand_test.v',
73 'vlib/runtime/runtime_test.v',
74 'vlib/semver/semver_test.v',
75 'vlib/sync/stdatomic/atomic_test.v',
76 'vlib/sync/thread_test.v',
77 'vlib/sync/waitgroup_test.v',
78 'vlib/sync/pool/pool_test.v',
79 'vlib/strings/builder_test.v',
80 'vlib/strconv/atof_test.c.v',
81 'vlib/strconv/atoi_test.v',
82 'vlib/strconv/f32_f64_to_string_test.v',
83 'vlib/strconv/format_test.v',
84 'vlib/strconv/number_to_base_test.v',
85 'vlib/time/time_test.v',
86 'vlib/toml/tests/toml_test.v',
87 'vlib/v/compiler_errors_test.v',
88 'vlib/v/fmt/fmt_keep_test.v',
89 'vlib/v/fmt/fmt_test.v',
90 'vlib/v/gen/c/coutput_test.v',
91 'vlib/v/gen/js/program_test.v',
92 'vlib/v/pkgconfig/pkgconfig_test.v',
93 'vlib/v/slow_tests/inout/compiler_test.v',
94 'vlib/x/json2/tests/json2_test.v',
95]
96const skip_with_fsanitize_memory = [
97 'do_not_remove',
98 'cmd/tools/vpm/dependency_test.v', // known flaky, for fsanitize_memory, due to using mbedtls
99 'cmd/tools/vpm/install_test.v', // known flaky, for fsanitize_memory, due to using mbedtls
100 'cmd/tools/vpm/install_version_input_test.v', // known flaky, for fsanitize_memory, due to using mbedtls
101 'cmd/tools/vpm/install_version_test.v', // known flaky, for fsanitize_memory, due to using mbedtls
102 'cmd/tools/vpm/update_test.v', // known flaky, for fsanitize_memory, due to using mbedtls
103 'vlib/net/tcp_simple_client_server_test.v',
104 'vlib/net/http/cookie_test.v',
105 'vlib/net/http/http_test.v',
106 'vlib/net/http/http_proxy_test.v',
107 'vlib/net/http/status_test.v',
108 'vlib/net/http/header_test.v',
109 'vlib/net/http/server_test.v',
110 'vlib/net/mbedtls/mbedtls_head_with_content_length_test.v',
111 'vlib/net/ssl/ssl_read_all_test.v',
112 'vlib/net/udp_test.v',
113 'vlib/net/tcp_test.v',
114 'vlib/orm/orm_test.v',
115 'vlib/orm/orm_sql_or_blocks_test.v',
116 'vlib/orm/orm_create_and_drop_test.v',
117 'vlib/orm/orm_dynamic_test.v',
118 'vlib/orm/orm_insert_test.v',
119 'vlib/orm/orm_insert_reserved_name_test.v',
120 'vlib/orm/orm_sum_type_insert_test.v',
121 'vlib/orm/orm_fn_calls_test.v',
122 'vlib/orm/orm_last_id_test.v',
123 'vlib/orm/orm_string_interpolation_in_where_test.v',
124 'vlib/orm/orm_interface_test.v',
125 'vlib/orm/orm_mut_db_test.v',
126 'vlib/orm/orm_null_test.v',
127 'vlib/orm/orm_result_test.v',
128 'vlib/orm/orm_custom_operators_test.v',
129 'vlib/orm/orm_fk_test.v',
130 'vlib/orm/orm_nested_struct_test.v',
131 'vlib/orm/orm_references_test.v',
132 'vlib/orm/orm_option_array_test.v',
133 'vlib/orm/orm_option_time_test.v',
134 'vlib/orm/orm_order_by_custom_field_test.v',
135 'vlib/orm/orm_serial_attribute_test.v',
136 'vlib/orm/orm_option_subselect_test.v',
137 'vlib/orm/orm_save_test.v',
138 'vlib/orm/orm_upsert_test.v',
139 'vlib/orm/orm_func_test.v',
140 'vlib/db/sqlite/sqlite_test.v',
141 'vlib/db/sqlite/sqlite_orm_test.v',
142 'vlib/db/sqlite/sqlite_comptime_field_test.v',
143 'vlib/db/sqlite/parent_child_test.v',
144 'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
145 'vlib/db/sqlite/sqlite_f32_test.v',
146 'vlib/v/tests/orm_enum_test.v',
147 'vlib/v/tests/orm_sub_struct_test.v',
148 'vlib/v/tests/orm_sub_array_struct_test.v',
149 'vlib/v/tests/orm_generic_struct_select_test.v',
150 'vlib/v/tests/orm_joined_tables_select_test.v',
151 'vlib/v/tests/orm_if_expr_value_test.v',
152 'vlib/v/tests/orm_option_field_issue_18333_test.v',
153 'vlib/v/tests/sql_statement_inside_fn_call_test.v',
154 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
155 'vlib/v/tests/orm_table_name_test.v',
156 'vlib/v/tests/orm_array_field_test.v',
157 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
158 'vlib/v/tests/orm_create_several_tables_test.v',
159 'vlib/v/tests/orm_update_test.v',
160 'vlib/v/tests/orm_or_test.v',
161 'vlib/net/http/request_test.v',
162 'vlib/net/http/response_test.v',
163 'vlib/net/websocket/websocket_test.v',
164 'vlib/net/smtp/smtp_test.v',
165 'vlib/v/tests/websocket_logger_interface_should_compile_test.v',
166 'vlib/v/tests/fns/fn_literal_type_test.v',
167 'vlib/v/tests/unions/union_implementing_interface_test.v',
168 'vlib/x/async/timeout_test.v', // spawn + channels + IError default field; tripped by MSan padding tracking
169 'vlib/x/sessions/tests/db_store_test.v',
170]
171const skip_with_fsanitize_address = [
172 'do_not_remove',
173 'vlib/compress/zstd/zstd_test.v', // ASan reports leaks from zstd library
174 'vlib/crypto/argon2/argon2_test.v', // ASan flags large alloc on test setup
175 'vlib/gg/text_rendering_test.v', // depends on freetype/font assets not available under sanitize CI
176 'vlib/json/tests/json_decode_with_sumtype_test.v', // ASan flake on sumtype decode buffer reuse
177 'vlib/net/mbedtls/mbedtls_read_timeout_test.v', // network timing test, ASan-incompatible
178 'vlib/net/websocket/websocket_test.v',
179 'vlib/orm/orm_create_and_drop_test.v',
180 'vlib/orm/orm_insert_test.v',
181 'vlib/orm/orm_insert_reserved_name_test.v',
182 'vlib/orm/orm_sum_type_insert_test.v',
183 'vlib/orm/orm_references_test.v',
184 'vlib/v/tests/websocket_client_default_read_timeout_test.v', // network timing
185 'vlib/v/tests/websocket_logger_interface_should_compile_test.v',
186 'vlib/v/tests/orm_enum_test.v',
187 'vlib/v/tests/orm_sub_array_struct_test.v',
188 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
189 'vlib/v/tests/orm_create_several_tables_test.v',
190 'vlib/v/tests/orm_update_test.v',
191 'vlib/v/tests/orm_or_test.v',
192 'vlib/veb/sse/sse_test.v', // long-lived event stream + sockets, ASan flake
193 'vlib/v2/gen/cleanc/flag_enum_codegen_test.v', // v2 self-host, ASan-incompatible
194 'vlib/v2/gen/x64/x64_issue_27039_test.v', // v2 x64 backend emits FnType__hash_fn refs under ASan/GCC, see issue #27039
195]
196const skip_with_fsanitize_undefined = [
197 'do_not_remove',
198 'vlib/crypto/argon2/argon2_test.v', // UBSan flags shift in argon2 reference impl
199 'vlib/orm/orm_create_and_drop_test.v',
200 'vlib/orm/orm_insert_test.v',
201 'vlib/orm/orm_insert_reserved_name_test.v',
202 'vlib/orm/orm_sum_type_insert_test.v',
203 'vlib/orm/orm_references_test.v',
204 'vlib/v/tests/orm_enum_test.v',
205 'vlib/v/tests/orm_sub_array_struct_test.v',
206 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
207 'vlib/v/tests/orm_create_several_tables_test.v',
208 'vlib/v/tests/orm_update_test.v',
209 'vlib/v/tests/orm_or_test.v',
210 'vlib/v/tests/project_with_cpp_code/compiling_cpp_files_with_a_cplusplus_compiler_test.c.v', // fails compilation with: undefined reference to vtable for __cxxabiv1::__function_type_info'
211 'vlib/v2/gen/cleanc/flag_enum_codegen_test.v', // v2 self-host, UBSan-incompatible
212 'vlib/v2/gen/x64/x64_issue_27039_test.v', // v2 x64 backend exercises raw bit manipulation flagged by UBSan
213 'vlib/v2/transformer/transformer_test.v', // v2 transformer, UBSan-incompatible
214 'vlib/yaml/yaml_conformance_test.v', // upstream libyaml-style integer overflow flagged by UBSan
215]
216const skip_on_ubuntu_musl = [
217 'do_not_remove',
218 'vlib/arrays/parallel/parallel_test.v',
219 'vlib/builtin/js/array_test.js.v',
220 'vlib/db/sqlite/sqlite_test.v',
221 'vlib/db/sqlite/sqlite_orm_test.v',
222 'vlib/db/sqlite/sqlite_comptime_field_test.v',
223 'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
224 'vlib/db/sqlite/parent_child_test.v',
225 'vlib/db/sqlite/sqlite_f32_test.v',
226 'vlib/orm/orm_test.v',
227 'vlib/orm/orm_sql_or_blocks_test.v',
228 'vlib/orm/orm_create_and_drop_test.v',
229 'vlib/orm/orm_insert_test.v',
230 'vlib/orm/orm_insert_reserved_name_test.v',
231 'vlib/orm/orm_sum_type_insert_test.v',
232 'vlib/orm/orm_fn_calls_test.v',
233 'vlib/orm/orm_null_test.v',
234 'vlib/orm/orm_last_id_test.v',
235 'vlib/orm/orm_string_interpolation_in_where_test.v',
236 'vlib/orm/orm_interface_test.v',
237 'vlib/orm/orm_mut_db_test.v',
238 'vlib/orm/orm_result_test.v',
239 'vlib/orm/orm_custom_operators_test.v',
240 'vlib/orm/orm_fk_test.v',
241 'vlib/orm/orm_nested_struct_test.v',
242 'vlib/orm/orm_references_test.v',
243 'vlib/orm/orm_option_array_test.v',
244 'vlib/orm/orm_option_time_test.v',
245 'vlib/orm/orm_order_by_custom_field_test.v',
246 'vlib/orm/orm_serial_attribute_test.v',
247 'vlib/orm/orm_option_subselect_test.v',
248 'vlib/orm/orm_func_test.v',
249 'vlib/orm/orm_where_in_test.v',
250 'vlib/v/tests/orm_enum_test.v',
251 'vlib/v/tests/orm_sub_struct_test.v',
252 'vlib/v/tests/orm_sub_array_struct_test.v',
253 'vlib/v/tests/orm_joined_tables_select_test.v',
254 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
255 'vlib/v/tests/orm_table_name_test.v',
256 'vlib/v/tests/orm_array_field_test.v',
257 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
258 'vlib/v/tests/orm_create_several_tables_test.v',
259 'vlib/v/tests/orm_update_test.v',
260 'vlib/v/tests/orm_or_test.v',
261 'vlib/v/tests/sql_statement_inside_fn_call_test.v',
262 'vlib/v/tests/websocket_logger_interface_should_compile_test.v',
263 'vlib/v/tests/fns/fn_literal_type_test.v',
264 'vlib/clipboard/clipboard_test.v',
265 'vlib/net/http/request_test.v',
266 'vlib/net/websocket/websocket_test.v',
267 'vlib/net/http/header_test.v',
268 'vlib/net/http/server_test.v',
269 'vlib/net/http/response_test.v',
270 'vlib/net/smtp/smtp_test.v',
271 'vlib/net/http/cookie_test.v',
272 'vlib/net/http/status_test.v',
273 'vlib/x/sessions/tests/db_store_test.v',
274 'vlib/veb/tests/veb_app_test.v',
275 'vlib/ncurses/ncurses_test.v',
276 'vlib/v/tests/fixed_array_update_c_struct_alias_test.v',
277 'vlib/x/crypto/mldsa/mldsa_test.v',
278]
279
280fn Config.init(vargs []string, targs []string) !Config {
281 mut cfg := Config{}
282 for arg in vargs {
283 match arg {
284 '-Werror', '-cstrict' { cfg.werror = true }
285 else {}
286 }
287
288 // Match -fsanitize prefix, since CI may pass combined values like
289 // `-fsanitize=address,pointer-compare,pointer-subtract`.
290 if arg.starts_with('-fsanitize=') {
291 vals := arg.all_after('=').split(',')
292 if 'memory' in vals {
293 cfg.sanitize_memory = true
294 }
295 if 'address' in vals {
296 cfg.sanitize_address = true
297 }
298 if 'undefined' in vals {
299 cfg.sanitize_undefined = true
300 }
301 }
302 }
303 if targs.len == 0 {
304 return cfg
305 }
306 mut tdirs := []string{}
307 mut errs := []string{}
308 for arg in targs {
309 match arg {
310 '-asan-compiler', '--asan-compiler' {
311 cfg.is_asan_compiler = true
312 }
313 '-msan-compiler', '--msan-compiler' {
314 cfg.is_msan_compiler = true
315 }
316 '-ubsan-compiler', '--ubsan-compiler' {
317 cfg.is_ubsan_compiler = true
318 }
319 else {
320 if arg.starts_with('-') {
321 errs << 'error: unknown flag `${arg}`'
322 continue
323 }
324 if !os.is_dir(os.join_path(vroot, arg)) {
325 errs << 'error: failed to find directory `${arg}`'
326 continue
327 }
328 tdirs << arg
329 }
330 }
331 }
332 if errs.len > 0 {
333 return error(errs.join_lines())
334 }
335 if tdirs.len > 0 {
336 cfg.test_dirs = tdirs
337 }
338 return cfg
339}
340
341fn main() {
342 unbuffer_stdout()
343 os.chdir(vroot)!
344 args_idx := os.args.index('test-self')
345 if args_idx < 0 {
346 eprintln('vtest-self: could not find `test-self` in os.args: ${os.args}')
347 exit(1)
348 }
349 vargs := os.args[1..args_idx]
350 targs := os.args#[args_idx + 1..]
351 cfg := Config.init(vargs, targs) or {
352 eprintln(err)
353 exit(1)
354 }
355 // dump(cfg)
356 title := 'testing: ${cfg.test_dirs.join(', ')}'
357 mut tpaths := map[string]bool{}
358 mut tpaths_ref := &tpaths
359 for dir in cfg.test_dirs {
360 os.walk(os.join_path(vroot, dir), fn [mut tpaths_ref] (p string) {
361 if p.ends_with('_test.v') || p.ends_with('_test.c.v')
362 || (testing.is_node_present && p.ends_with('_test.js.v')) {
363 unsafe {
364 tpaths_ref[p] = true
365 }
366 }
367 })
368 }
369 mut all_test_files := tpaths.keys()
370 if cfg.run_just_essential {
371 all_test_files = essential_list.map(os.join_path(vroot, it))
372 }
373 mut tsession := testing.new_test_session(vargs.join(' '), true)
374 tsession.exec_mode = .compile_and_run
375 tsession.files << all_test_files.filter(!it.contains('testdata' + os.path_separator))
376 $if self_ignore_v2 ? {
377 v2_dir_fragment := '${os.path_separator}vlib${os.path_separator}v2${os.path_separator}'
378 tsession.skip_files << tsession.files.filter(it.contains(v2_dir_fragment))
379 }
380 if cfg.werror {
381 tsession.custom_defines << 'self_werror'
382 }
383 if cfg.sanitize_memory {
384 tsession.skip_files << skip_with_fsanitize_memory
385 tsession.custom_defines << 'self_sanitize_memory'
386 }
387 if cfg.sanitize_address {
388 tsession.skip_files << skip_with_fsanitize_address
389 tsession.custom_defines << 'self_sanitize_address'
390 }
391 if cfg.sanitize_undefined {
392 tsession.skip_files << skip_with_fsanitize_undefined
393 tsession.custom_defines << 'self_sanitize_undefined'
394 }
395 if cfg.is_asan_compiler {
396 tsession.custom_defines << 'self_asan_compiler'
397 }
398 if cfg.is_msan_compiler {
399 tsession.custom_defines << 'self_msan_compiler'
400 }
401 if cfg.is_ubsan_compiler {
402 tsession.custom_defines << 'self_ubsan_compiler'
403 }
404 if cfg.is_sandboxed_packaging {
405 tsession.custom_defines << 'self_sandboxed_packaging'
406 }
407 if cfg.is_ubuntu_musl_ci {
408 tsession.skip_files << skip_on_ubuntu_musl
409 tsession.custom_defines << 'self_ubuntu_musl_ci'
410 }
411 // dump(tsession.skip_files)
412 mut unavailable_files := tsession.files.filter(!os.exists(it))
413 unavailable_files << tsession.skip_files.filter(it != 'do_not_remove' && !os.exists(it))
414 if unavailable_files.len > 0 {
415 for f in unavailable_files {
416 eprintln('error: failed to find file: ${f}')
417 }
418 exit(1)
419 }
420 tsession.skip_files = tsession.skip_files.map(os.abs_path)
421 tsession.session_start(title)
422 tsession.test()
423 tsession.session_stop(title)
424 flush_stdout()
425 if tsession.benchmark.nfail > 0 {
426 eprintln('\nError: failed ${tsession.benchmark.nfail} times.\n')
427 exit(1)
428 }
429}
430