mod common;
use common::TestRepo;
use std::process::Command;
fn run_complete_targets(cwd: &std::path::Path) -> std::process::Output {
Command::new(TestRepo::cw_bin())
.args(["_complete-targets"])
.current_dir(cwd)
.output()
.expect("failed to run gw _complete-targets")
}
#[test]
fn complete_targets_lists_main_worktree_name_and_branch() {
let repo = TestRepo::new();
let output = run_complete_targets(repo.path());
assert!(
output.status.success(),
"_complete-targets exited non-zero: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
!stdout.trim().is_empty(),
"_complete-targets produced no output inside a git repo"
);
let dir_name = repo
.path()
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
assert!(
stdout.lines().any(|l| l == dir_name),
"expected worktree name '{}' in output:\n{}",
dir_name,
stdout
);
assert!(
stdout.lines().any(|l| l == "main"),
"expected branch 'main' in output:\n{}",
stdout
);
}
#[test]
fn complete_targets_silent_outside_repo() {
let tmp = tempfile::TempDir::new().expect("tempdir");
let output = run_complete_targets(tmp.path());
assert!(
output.status.success(),
"_complete-targets should exit 0 outside a repo, got: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.trim().is_empty(),
"_complete-targets should produce no output outside a repo, got: {}",
stdout
);
}
#[test]
fn complete_targets_dedupes_when_name_equals_branch() {
let repo = TestRepo::new();
let output = run_complete_targets(repo.path());
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
let lines: Vec<&str> = stdout.lines().collect();
let unique_count = {
let mut set = std::collections::HashSet::new();
lines.iter().filter(|l| set.insert(*l)).count()
};
assert_eq!(
lines.len(),
unique_count,
"_complete-targets output contains duplicate lines:\n{}",
stdout
);
}