From 9c0ec1be0f3af528beb59b80d3b1b4eda5061a84 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 21 Apr 2026 14:55:41 +0300 Subject: [PATCH] tests: add test for assert with scoped defer cleanup and invisible chars (fixes #24727) --- vlib/v/tests/failing_tests_test.v | 28 +++++++++++++++++++ ...t_with_scoped_defer_cleanup_failing_test.v | 18 ++++++++++++ 2 files changed, 46 insertions(+) diff --git a/vlib/v/tests/failing_tests_test.v b/vlib/v/tests/failing_tests_test.v index c7a6c1272..37e2299e3 100644 --- a/vlib/v/tests/failing_tests_test.v +++ b/vlib/v/tests/failing_tests_test.v @@ -2,6 +2,8 @@ import os const assert_failed_defer_cleanup_path = os.join_path(os.vtmp_dir(), 'v_assert_failed_defer_cleanup_test.txt') +const assert_failed_deferred_file_close_path = os.join_path(os.vtmp_dir(), + 'v_assert_failed_deferred_file_close_test.txt') fn vroot_path(relpath string) string { return os.real_path(os.join_path(@VMODROOT, relpath)) @@ -13,6 +15,19 @@ fn vexecute(relpath string) os.Result { os.quoted_path(vroot_path(relpath))) } +fn deferred_file_flush_line(i int) string { + return '第${i:03}: {"code":200,"msg":"数据请求成功","word":"possible","meaning":"可能的;合适的","url":"https://dict.example/api?word=possible"}' +} + +fn deferred_file_flush_expected() string { + line_sep := $if windows { '\r\n' } $else { '\n' } + mut lines := []string{cap: 160} + for i in 0 .. 160 { + lines << deferred_file_flush_line(i) + } + return lines.join(line_sep) + line_sep +} + fn testsuite_begin() { os.setenv('VCOLORS', 'never', true) } @@ -48,6 +63,19 @@ fn test_assert_failure_runs_scoped_defer_cleanup() { assert !os.exists(assert_failed_defer_cleanup_path) } +fn test_assert_failure_runs_deferred_file_close_flush() { + defer { + os.rm(assert_failed_deferred_file_close_path) or {} + } + os.rm(assert_failed_deferred_file_close_path) or {} + res := vexecute('vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v') + assert res.exit_code == 1 + assert res.output.contains('assert_with_scoped_defer_cleanup_failing_test.v') + assert res.output.contains('assert false') + content := os.read_file(assert_failed_deferred_file_close_path)! + assert content == deferred_file_flush_expected() +} + fn test_run_only_reports_filtered_failures() { test_path := os.join_path(os.vtmp_dir(), 'issue_22778_run_only_${os.getpid()}_test.v') defer { diff --git a/vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v b/vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v index 90dafaa6b..7942e21c3 100644 --- a/vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v +++ b/vlib/v/tests/testdata/assert_with_scoped_defer_cleanup_failing_test.v @@ -1,9 +1,16 @@ import os const cleanup_path = os.join_path(os.vtmp_dir(), 'v_assert_failed_defer_cleanup_test.txt') +const deferred_file_close_path = os.join_path(os.vtmp_dir(), + 'v_assert_failed_deferred_file_close_test.txt') + +fn deferred_file_close_line(i int) string { + return '第${i:03}: {"code":200,"msg":"数据请求成功","word":"possible","meaning":"可能的;合适的","url":"https://dict.example/api?word=possible"}' +} fn testsuite_begin() { os.rm(cleanup_path) or {} + os.rm(deferred_file_close_path) or {} } fn test_scoped_defer_cleanup_runs_on_assert_failure() { @@ -15,3 +22,14 @@ fn test_scoped_defer_cleanup_runs_on_assert_failure() { assert false } } + +fn test_deferred_file_close_flushes_on_assert_failure() { + mut f := os.open_file(deferred_file_close_path, 'w+', 0o666)! + defer { + f.close() + } + for i in 0 .. 160 { + f.writeln(deferred_file_close_line(i))! + } + assert false +} -- 2.39.5