diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index cd8b48a71fb8f6..fc893f44991604 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -261,6 +261,23 @@ fn test_no_main_exports_initialize_windows_runtime() { } } +fn test_coverage_output_checks_counter_file_open() { + os.chdir(vroot) or {} + test_source := os.join_path(os.vtmp_dir(), 'coutput_coverage_file_guard.vv') + os.write_file(test_source, 'fn main() {\n\tprintln("coverage")\n}\n')! + defer { + os.rm(test_source) or {} + } + coverage_dir := os.join_path(os.vtmp_dir(), 'coutput_coverage') + cmd := '${os.quoted_path(vexe)} -o - -coverage ${os.quoted_path(coverage_dir)} ${os.quoted_path(test_source)}' + compilation := os.execute(cmd) + ensure_compilation_succeeded(compilation, cmd) + assert compilation.output.contains('FILE *fp = fopen(cov_filename, "wb+");') + assert compilation.output.contains('if (fp == NULL) { return; }') + assert compilation.output.contains('nsecs = ts.tv_nsec;') + assert !compilation.output.contains('\nsecs = ts.tv_nsec;') +} + fn test_c_fallback_decl_uses_module_wide_c_includes() { os.chdir(vroot) or {} test_source := os.join_path(os.vtmp_dir(), 'coutput_module_c_include') diff --git a/vlib/v/gen/c/coverage.v b/vlib/v/gen/c/coverage.v index 8a8980e2b779ca..797220d6560385 100644 --- a/vlib/v/gen/c/coverage.v +++ b/vlib/v/gen/c/coverage.v @@ -98,10 +98,11 @@ fn (mut g Gen) write_coverage_stats() { g.cov_declarations.writeln('\tstruct timespec ts;') g.cov_declarations.writeln('\tclock_gettime(CLOCK_MONOTONIC, &ts);') g.cov_declarations.writeln('\tsecs = ts.tv_sec;') - g.cov_declarations.writeln('\nsecs = ts.tv_nsec;') + g.cov_declarations.writeln('\tnsecs = ts.tv_nsec;') g.cov_declarations.writeln('\t#endif') g.cov_declarations.writeln('\tsnprintf(cov_filename, sizeof(cov_filename), "%s/vcounters_${counter_ulid}.%07ld.%09ld.csv", cov_dir, secs, nsecs);') g.cov_declarations.writeln('\tFILE *fp = fopen(cov_filename, "wb+");') + g.cov_declarations.writeln('\tif (fp == NULL) { return; }') cprefpath := cesc(os.real_path(g.pref.path)) cboptions := cesc(build_options) g.cov_declarations.writeln('\tfprintf(fp, "# path: ${cprefpath}\\n");')