Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions kitc/tests/examples.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use assert_cmd::{Command as AssertCommand, cargo::*};
use predicates::prelude::*;
use std::{path::Path, process::Command, sync::OnceLock};

static LOGGER_INIT: OnceLock<()> = OnceLock::new();
Expand All @@ -10,6 +9,10 @@ fn setup_logging() {
});
}

fn normalize_line_endings(s: &str) -> String {
s.replace("\r\n", "\n")
}

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

fn run_example_test(
Expand Down Expand Up @@ -68,15 +71,18 @@ fn run_example_test(
let expected_output_path = workspace_root.join(expected_file);
log::info!("Expected output: {}", expected_output_path.display());
let expected_output = std::fs::read_to_string(expected_output_path)?;
let expected_normalized = normalize_line_endings(&expected_output);

// Assert the output
compiled_cmd
.assert()
.stdout(predicate::eq(expected_output.as_str()))
.success();
// Run the compiled executable, capture output, normalize line endings, compare
let output = compiled_cmd.assert().success().get_output().stdout.clone();

let actual = String::from_utf8_lossy(&output).replace("\r\n", "\n");
assert_eq!(
actual, expected_normalized,
"stdout did not match expected output"
);

// 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.
// Clean up generated files
if let Err(err) = std::fs::remove_file(&executable_path) {
log::error!("Failed to remove executable: {err}");
}
Expand Down