use crate::brain::self_update::SelfUpdater;
use std::path::PathBuf;
use tempfile::TempDir;
#[test]
fn prebuilt_install_resolves_binary_to_the_running_exe() {
let tmp = TempDir::new().unwrap();
let bin_dir = tmp.path().join("usr").join("local").join("bin");
std::fs::create_dir_all(&bin_dir).unwrap();
let exe = bin_dir.join("opencrabs");
std::fs::write(&exe, b"binary").unwrap();
let source_dir = tmp.path().join("home").join(".opencrabs").join("source");
let (root, binary) = SelfUpdater::resolve_paths(&exe, source_dir.clone()).unwrap();
assert_eq!(
binary, exe,
"pre-built install must restart the running exe, not a never-built target/ path"
);
assert_ne!(
binary,
source_dir.join("target").join("release").join("opencrabs"),
"must NOT point at the unbuilt source target (the #179 regression)"
);
assert_eq!(root, source_dir);
}
#[test]
fn source_tree_resolves_binary_to_target_release() {
let tmp = TempDir::new().unwrap();
let root = tmp.path().join("project");
let exe_dir = root.join("target").join("debug");
std::fs::create_dir_all(&exe_dir).unwrap();
std::fs::write(root.join("Cargo.toml"), b"[package]\nname=\"x\"").unwrap();
let exe = exe_dir.join("opencrabs");
std::fs::write(&exe, b"binary").unwrap();
let (proot, binary) =
SelfUpdater::resolve_paths(&exe, PathBuf::from("/unused/source")).unwrap();
assert_eq!(proot, root, "project_root is the Cargo.toml dir");
assert_eq!(
binary,
root.join("target").join("release").join("opencrabs"),
"source tree restarts the release build output"
);
}
#[test]
fn nested_source_tree_walks_up_to_the_cargo_toml() {
let tmp = TempDir::new().unwrap();
let root = tmp.path().join("repo");
let deep = root.join("target").join("release").join("deps");
std::fs::create_dir_all(&deep).unwrap();
std::fs::write(root.join("Cargo.toml"), b"[package]\nname=\"x\"").unwrap();
let exe = deep.join("opencrabs-abc123");
std::fs::write(&exe, b"binary").unwrap();
let (proot, binary) = SelfUpdater::resolve_paths(&exe, PathBuf::from("/unused")).unwrap();
assert_eq!(proot, root);
assert_eq!(
binary,
root.join("target").join("release").join("opencrabs")
);
}