use regex::Regex;
use std::fs;
pub fn init_test_env() {
let _ = tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.try_init();
}
pub fn create_test_project(_name: &str, content: &[(String, String)]) -> tempfile::TempDir {
let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
let base_path = temp_dir.path();
for (file_path, file_content) in content {
let full_path = base_path.join(file_path);
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent).expect("Failed to create parent directory");
}
fs::write(&full_path, file_content).expect("Failed to write test file");
}
temp_dir
}
#[macro_export]
macro_rules! assert_matches {
($value:expr, $pattern:pat) => {
match &$value {
$pattern => {}
_ => panic!("Value did not match pattern: {:?}", $value),
}
};
}
pub struct OutputCapture {
output: String,
}
impl OutputCapture {
pub fn new() -> Self {
Self {
output: String::new(),
}
}
pub fn add_line(&mut self, line: &str) {
self.output.push_str(line);
self.output.push('\n');
}
pub fn output(&self) -> &str {
&self.output
}
pub fn assert_contains(&self, substring: &str) {
assert!(
self.output.contains(substring),
"Output does not contain '{}'. Output: {}",
substring,
self.output
);
}
pub fn assert_matches_regex(&self, pattern: &str) {
let re = Regex::new(pattern).expect("Invalid regex pattern");
assert!(
re.is_match(&self.output),
"Output does not match regex '{}'. Output: {}",
pattern,
self.output
);
}
}
impl Default for OutputCapture {
fn default() -> Self {
Self::new()
}
}