Skip to content

Commit ddf0477

Browse files
authored
cgen: guard test coverage counter file creation (#27075)
1 parent 31f5677 commit ddf0477

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

vlib/v/gen/c/coutput_test.v

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ fn test_no_main_exports_initialize_windows_runtime() {
376376
}
377377
}
378378

379+
fn test_coverage_output_checks_counter_file_open() {
380+
os.chdir(vroot) or {}
381+
test_source := os.join_path(os.vtmp_dir(), 'coutput_coverage_file_guard.vv')
382+
os.write_file(test_source, 'fn main() {\n\tprintln("coverage")\n}\n')!
383+
defer {
384+
os.rm(test_source) or {}
385+
}
386+
coverage_dir := os.join_path(os.vtmp_dir(), 'coutput_coverage')
387+
cmd := '${os.quoted_path(vexe)} -o - -coverage ${os.quoted_path(coverage_dir)} ${os.quoted_path(test_source)}'
388+
compilation := os.execute(cmd)
389+
ensure_compilation_succeeded(compilation, cmd)
390+
assert compilation.output.contains('FILE *fp = fopen(cov_filename, "wb+");')
391+
assert compilation.output.contains('if (fp == NULL) { return; }')
392+
assert compilation.output.contains('nsecs = ts.tv_nsec;')
393+
assert !compilation.output.contains('\nsecs = ts.tv_nsec;')
394+
}
395+
379396
fn test_c_fallback_decl_uses_module_wide_c_includes() {
380397
os.chdir(vroot) or {}
381398
test_source := os.join_path(os.vtmp_dir(), 'coutput_module_c_include')

vlib/v/gen/c/coverage.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ fn (mut g Gen) write_coverage_stats() {
9898
g.cov_declarations.writeln('\tstruct timespec ts;')
9999
g.cov_declarations.writeln('\tclock_gettime(CLOCK_MONOTONIC, &ts);')
100100
g.cov_declarations.writeln('\tsecs = ts.tv_sec;')
101-
g.cov_declarations.writeln('\nsecs = ts.tv_nsec;')
101+
g.cov_declarations.writeln('\tnsecs = ts.tv_nsec;')
102102
g.cov_declarations.writeln('\t#endif')
103103
g.cov_declarations.writeln('\tsnprintf(cov_filename, sizeof(cov_filename), "%s/vcounters_${counter_ulid}.%07ld.%09ld.csv", cov_dir, secs, nsecs);')
104104
g.cov_declarations.writeln('\tFILE *fp = fopen(cov_filename, "wb+");')
105+
g.cov_declarations.writeln('\tif (fp == NULL) { return; }')
105106
cprefpath := cesc(os.real_path(g.pref.path))
106107
cboptions := cesc(build_options)
107108
g.cov_declarations.writeln('\tfprintf(fp, "# path: ${cprefpath}\\n");')

0 commit comments

Comments
 (0)