use assert_cmd::prelude::*;
use std::fs;
use std::process::Command;
use tempfile::TempDir;
fn bin() -> Command {
Command::cargo_bin("grex").expect("grex binary built")
}
const PACK_ROOT_REQUIRED: &str = "<pack_root> required";
fn seed_pack_marker(dir: &std::path::Path) {
let grex_dir = dir.join(".grex");
fs::create_dir_all(&grex_dir).expect("create .grex/");
let yaml = "name: cwd-default-test\n";
fs::write(grex_dir.join("pack.yaml"), yaml).expect("write pack.yaml");
}
#[test]
fn sync_defaults_pack_root_to_cwd_when_marker_present() {
let tmp = TempDir::new().expect("tempdir");
seed_pack_marker(tmp.path());
let out = bin()
.current_dir(tmp.path())
.args(["sync", "--dry-run", "--no-validate"])
.output()
.expect("spawn grex sync");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains(PACK_ROOT_REQUIRED),
"cwd has `.grex/pack.yaml` so `<pack_root> required` must NOT fire; got stderr:\n---\n{stderr}\n---"
);
}
#[test]
fn sync_emits_usage_error_when_cwd_lacks_marker() {
let tmp = TempDir::new().expect("tempdir");
bin()
.current_dir(tmp.path())
.args(["sync"])
.assert()
.failure()
.code(2)
.stderr(predicates::str::contains(PACK_ROOT_REQUIRED));
}
#[test]
fn sync_explicit_pack_root_wins_over_cwd_default() {
let cwd_with_marker = TempDir::new().expect("cwd tempdir");
seed_pack_marker(cwd_with_marker.path());
let elsewhere = TempDir::new().expect("elsewhere tempdir");
let out = bin()
.current_dir(cwd_with_marker.path())
.arg("sync")
.arg(elsewhere.path())
.arg("--dry-run")
.arg("--no-validate")
.output()
.expect("spawn grex sync <elsewhere>");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains(PACK_ROOT_REQUIRED),
"explicit positional supplied — `<pack_root> required` must NOT fire; got stderr:\n---\n{stderr}\n---"
);
}
#[test]
fn teardown_defaults_pack_root_to_cwd_when_marker_present() {
let tmp = TempDir::new().expect("tempdir");
seed_pack_marker(tmp.path());
let out = bin()
.current_dir(tmp.path())
.args(["teardown", "--no-validate"])
.output()
.expect("spawn grex teardown");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains(PACK_ROOT_REQUIRED),
"cwd has `.grex/pack.yaml` so `grex teardown` must NOT emit `<pack_root> required`; got stderr:\n---\n{stderr}\n---"
);
}