From 2be35f97ade8c98f9402b1c3282f44b49b98e5bb Mon Sep 17 00:00:00 2001 From: winlogon Date: Wed, 21 Jan 2026 05:58:42 +0100 Subject: [PATCH] fix(tests): improve example test file handling Adjusted file extensions and cleanup for cross-platform compatibility in example tests. Summary of changes: - Reordered `assert_cmd` imports for clarity - Set executable extension based on OS (`exe` for Windows) - Removed redundant C file handling - Ensure generated executables are cleaned up after tests - Added safety notes about single-threaded tests and setting KEEP_C env var for testing incorrect C code --- kitc/tests/examples.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kitc/tests/examples.rs b/kitc/tests/examples.rs index d2690f9..4aca461 100644 --- a/kitc/tests/examples.rs +++ b/kitc/tests/examples.rs @@ -46,12 +46,18 @@ fn run_example_test( ); let source_path = workspace_root.join(example_file); - let c_path = source_path.with_extension("c"); - let executable_path = source_path.with_extension(""); + let ext = if cfg!(windows) { "exe" } else { "" }; + let executable_path = source_path.with_extension(ext); // Compile the example let kitc = cargo_bin!("kitc"); log::info!("kitc path: {}", kitc.display()); + + // Note: C file cleanup is handled by the compiler itself (see frontend.rs). + // SAFETY: these unit tests are single-threaded, so no race conditions + // unsafe { + // std::env::set_var("KEEP_C", ""); + // } let mut cmd = AssertCommand::from_std(Command::new(kitc)); // Run from workspace root @@ -75,14 +81,10 @@ fn run_example_test( .stdout(predicate::eq(expected_output.as_str())) .success(); - // TODO: executable files are actually generated in the CWD, not in the examples folder. - // This explains why the executable is not actually generated in the examples folder. + // Cleanup generated files if let Err(err) = std::fs::remove_file(&executable_path) { log::error!("Failed to remove executable: {err}"); } - if let Err(err) = std::fs::remove_file(&c_path) { - log::error!("Failed to remove C source file: {err}"); - } Ok(()) }