use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::process::{Command, Stdio};
use std::thread;
use std::time::Duration;
use tempfile::TempDir;
#[test]
fn test_unreadable_file_prints_error_and_continues() {
let temp_dir = TempDir::new().unwrap();
let root_path = temp_dir.path().to_path_buf();
let db_path = temp_dir.path().join("magellan.db");
let good_file = root_path.join("good.rs");
let bad_file = root_path.join("bad.rs");
let bin_path = std::env::var("CARGO_BIN_EXE_magellan").unwrap_or_else(|_| {
let mut path = std::env::current_exe().unwrap();
path.pop();
path.pop();
path.push("magellan");
path.to_str().unwrap().to_string()
});
thread::sleep(Duration::from_millis(10));
fs::write(&good_file, b"fn good() {}").unwrap();
fs::write(&bad_file, b"fn bad() {}").unwrap();
let mut child = Command::new(&bin_path)
.arg("watch")
.arg("--root")
.arg(&root_path)
.arg("--db")
.arg(&db_path)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to start magellan binary");
thread::sleep(Duration::from_millis(100));
let mut perms = fs::metadata(&bad_file).unwrap().permissions();
perms.set_mode(0o000);
fs::set_permissions(&bad_file, perms.clone()).unwrap();
fs::write(&good_file, b"fn good() { fn updated() {} }").unwrap();
thread::sleep(Duration::from_millis(1000));
perms.set_mode(0o644);
fs::set_permissions(&bad_file, perms).unwrap();
let _ = child.kill();
let output = child
.wait_with_output()
.expect("Failed to wait for process");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("ERROR"),
"Expected ERROR in stdout for unreadable file"
);
assert!(
stdout.contains("bad.rs"),
"Expected filename in error message"
);
assert!(
stdout.contains("MODIFY"),
"Expected MODIFY event for good.rs"
);
assert!(stdout.contains("good.rs"), "Expected good.rs in stdout");
let mut graph = magellan::CodeGraph::open(&db_path).unwrap();
let path_str = good_file.to_string_lossy().to_string();
let symbols = graph.symbols_in_file(&path_str).unwrap();
assert_eq!(
symbols.len(),
2,
"Should have indexed 2 symbols from good.rs despite bad.rs error"
);
}