use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::process::Command;
use tempfile::TempDir;
#[cfg(test)]
mod tests {
use super::*;
fn setup_test_dir() -> TempDir {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let src_dir = temp_dir.path().join("src");
fs::create_dir(&src_dir).expect("Failed to create src directory");
let js_file = src_dir.join("test.js");
let mut file = File::create(js_file).expect("Failed to create test.js");
writeln!(file, "console.log('hello');").expect("Failed to write to test.js");
let css_file = src_dir.join("style.css");
let mut file = File::create(css_file).expect("Failed to create style.css");
writeln!(file, "body {{ color: black; }}").expect("Failed to write to style.css");
temp_dir
}
#[test]
fn test_flash_binary_builds_and_shows_help() {
let build_result = Command::new("cargo")
.args(["build"])
.status()
.expect("Failed to run cargo build");
if !build_result.success() {
panic!("Failed to build flash-watcher binary");
}
let flash_binary = env::current_dir()
.expect("Failed to get current dir")
.join("target/debug/flash-watcher");
if !flash_binary.exists() {
println!(
"Binary not found at {:?}, but build succeeded",
flash_binary
);
return;
}
let output = Command::new(&flash_binary).args(["--help"]).output();
match output {
Ok(output) => {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("Help output: {}", stdout);
if !stderr.is_empty() {
println!("Help stderr: {}", stderr);
}
assert!(stdout.contains("flash-watcher") || stdout.contains("Flash"));
assert!(stdout.contains("USAGE") || stdout.contains("Usage"));
}
Err(e) => {
println!("Could not run binary (this may be expected in CI): {}", e);
}
}
}
#[test]
fn test_integration_setup() {
let temp_dir = setup_test_dir();
let js_file = temp_dir.path().join("src/test.js");
let css_file = temp_dir.path().join("src/style.css");
assert!(js_file.exists(), "test.js was not created");
assert!(css_file.exists(), "style.css was not created");
}
}