use std::{fs, path::Path, sync::Arc};
use pretty_assertions::assert_eq;
use ratatui::style::{Color, Style};
use super::{PathIndex, PathLinkStyle, PathLinker};
use crate::tui::text_links::LinkedRun;
const STYLE: PathLinkStyle = PathLinkStyle {
name: Style::new().fg(Color::Cyan),
quiet: Style::new().fg(Color::Blue),
};
fn check_queued(index: &mut PathIndex) -> Vec<String> {
let check = index.take_check().expect("paths are queued");
index.apply(check.run())
}
#[test]
fn mentions_link_once_a_check_finds_their_files() {
let workspace = tempfile::tempdir().expect("tempdir");
fs::create_dir_all(workspace.path().join("src")).expect("mkdir");
fs::write(workspace.path().join("src/a.rs"), "").expect("write");
let mut index = PathIndex::new(workspace.path().to_path_buf());
let text = "see src/a.rs:4 and src/b.rs";
let mut linker = PathLinker::new(&index);
let runs = linker.prose(text, Style::new(), STYLE);
assert_eq!(runs, vec![LinkedRun::plain(text, Style::new())]);
let unlinked = linker.into_unlinked();
assert_eq!(unlinked, ["src/a.rs", "src/b.rs"]);
index.want(unlinked.iter().map(String::as_str));
assert_eq!(check_queued(&mut index), ["src/a.rs"]);
let mut linker = PathLinker::new(&index);
let runs = linker.prose(text, Style::new(), STYLE);
let url = runs[1].link.clone().expect("src/a.rs links");
assert!(url.starts_with("file://"), "{url}");
assert!(url.ends_with("/src/a.rs"), "{url}");
assert_eq!(
runs,
vec![
LinkedRun::plain("see ", Style::new()),
LinkedRun::linked("src/", STYLE.quiet, Arc::clone(&url)),
LinkedRun::linked("a.rs", STYLE.name, Arc::clone(&url)),
LinkedRun::linked(":4", STYLE.quiet, url),
LinkedRun::plain(" and src/b.rs", Style::new()),
]
);
assert_eq!(linker.into_unlinked(), ["src/b.rs"]);
index.want(["src/a.rs", "src/b.rs"]);
assert!(index.take_check().is_none());
fs::write(workspace.path().join("src/b.rs"), "").expect("write");
index.recheck_missing();
assert_eq!(check_queued(&mut index), ["src/b.rs"]);
index.want(["src/c.rs"]);
let early = index.take_check().expect("queued").run();
fs::write(workspace.path().join("src/c.rs"), "").expect("write");
index.recheck_missing();
index.apply(early);
assert_eq!(check_queued(&mut index), ["src/c.rs"]);
}
#[test]
fn checks_of_another_workspace_change_nothing() {
let workspace = tempfile::tempdir().expect("tempdir");
fs::write(workspace.path().join("a.rs"), "").expect("write");
let mut old = PathIndex::new(workspace.path().to_path_buf());
old.want(["a.rs"]);
let check = old.take_check().expect("queued");
let mut moved = PathIndex::new(Path::new("/elsewhere").to_path_buf());
assert!(moved.apply(check.run()).is_empty());
assert!(PathLinker::new(&moved).whole("a.rs", STYLE).is_none());
}
#[cfg(unix)]
#[test]
fn checks_resolve_symlinks_and_stay_in_the_workspace() {
let workspace = tempfile::tempdir().expect("tempdir");
let outside = tempfile::tempdir().expect("tempdir");
let root = workspace.path();
for dir in ["src", "other/deep"] {
fs::create_dir_all(root.join(dir)).expect("mkdir");
}
for file in ["src/a.rs", "other/a.rs"] {
fs::write(root.join(file), "").expect("write");
}
fs::write(outside.path().join("secret.rs"), "").expect("write");
std::os::unix::fs::symlink(root.join("other/deep"), root.join("src/link")).expect("symlink");
std::os::unix::fs::symlink(outside.path(), root.join("src/out")).expect("symlink");
let mut index = PathIndex::new(root.to_path_buf());
index.want(["src/link/../a.rs", "src/out/secret.rs"]);
assert_eq!(check_queued(&mut index), ["src/link/../a.rs"]);
let mut linker = PathLinker::new(&index);
let runs = linker
.whole("src/link/../a.rs", STYLE)
.expect("the path links");
let url = runs[0].link.clone().expect("linked");
assert!(url.ends_with("/other/a.rs"), "{url}");
}