Skip to content

Commit c26bbda

Browse files
committed
chore(tests): add logging in tests
1 parent 62f4dee commit c26bbda

3 files changed

Lines changed: 37 additions & 48 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kitc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ which = "8.0.0"
1212

1313
[dev-dependencies]
1414
assert_cmd = "2.1.1"
15-
escargot = "0.5.15"
1615
predicates = "3.1.3"
16+
log = "0.4.29"

kitc/tests/examples.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
use assert_cmd::{Command as AssertCommand, cargo::*};
22
use predicates::prelude::*;
3-
use std::{path::Path, process::Command};
3+
use std::{path::Path, process::Command, sync::OnceLock};
4+
5+
static LOGGER_INIT: OnceLock<()> = OnceLock::new();
6+
7+
fn setup_logging() {
8+
LOGGER_INIT.get_or_init(|| {
9+
env_logger::builder().is_test(true).init();
10+
});
11+
}
412

513
fn run_example_test(
614
example_name: &str,
715
stdin: Option<&str>,
816
) -> Result<(), Box<dyn std::error::Error>> {
17+
setup_logging();
918
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
1019
let workspace_root = Path::new(&manifest_dir).parent().unwrap();
1120

1221
let examples_dir = Path::new("examples");
1322
let example_file = examples_dir.join(format!("{}.kit", example_name));
1423
let expected_file = examples_dir.join(format!("{}.kit.expected", example_name));
1524

25+
log::info!(
26+
"Running example {} in {} (path: {}). Expected file is at {}",
27+
example_name,
28+
workspace_root.display(),
29+
example_file.display(),
30+
expected_file.display()
31+
);
32+
1633
let source_path = workspace_root.join(example_file);
1734
let c_path = source_path.with_extension("c");
1835
let executable_path = source_path.with_extension("");
1936

2037
// Compile the example
21-
let mut cmd = AssertCommand::from_std(Command::new(cargo_bin!("kitc")));
38+
let kitc = cargo_bin!("kitc");
39+
log::info!("kitc path: {}", kitc.display());
40+
let mut cmd = AssertCommand::from_std(Command::new(kitc));
2241

2342
// Run from workspace root
2443
cmd.current_dir(workspace_root);
@@ -31,7 +50,9 @@ fn run_example_test(
3150
compiled_cmd.write_stdin(stdin_data);
3251
}
3352

34-
let expected_output = std::fs::read_to_string(workspace_root.join(expected_file))?;
53+
let asjdlksaj = workspace_root.join(expected_file);
54+
log::info!("Expected output: {}", asjdlksaj.display());
55+
let expected_output = std::fs::read_to_string(asjdlksaj)?;
3556

3657
// Assert the output
3758
compiled_cmd
@@ -40,12 +61,17 @@ fn run_example_test(
4061
.success();
4162

4263
// Clean up the executable and .c file
43-
std::fs::remove_file(&executable_path)?;
44-
std::fs::remove_file(&c_path)?;
64+
if let Err(err) = std::fs::remove_file(&executable_path) {
65+
log::error!("Failed to remove executable: {err}");
66+
}
67+
if let Err(err) = std::fs::remove_file(&c_path) {
68+
log::error!("Failed to remove C source file: {err}");
69+
}
4570

4671
Ok(())
4772
}
4873

74+
4975
#[test]
5076
fn test_helloworld() -> Result<(), Box<dyn std::error::Error>> {
5177
run_example_test("helloworld", None)

0 commit comments

Comments
 (0)