use super::*;
use std::path::Path;
use crate::forwarded_command::Route;
use crate::test_support::{REAL_GIT, fixture, isolated_overlay, remove, repository};
use crate::worktree_identity::resolve_worktree_identity;
use crate::worktree_copy_forward::CopyFacts;
fn add_forward(repo: &Path) -> CoordinatedForward {
let prefix: Vec<OsString> = vec![OsString::from("-C"), repo.as_os_str().to_owned()];
let identity: WorktreeIdentity = resolve_worktree_identity(
Path::new(REAL_GIT),
prefix.as_slice(),
isolated_overlay().as_slice(),
)
.expect("identity");
let arguments: Vec<OsString> = vec![
OsString::from("-C"),
repo.as_os_str().to_owned(),
OsString::from("add"),
OsString::from("a.txt"),
];
let command: ResolvedCommand = ResolvedCommand {
arguments: arguments.clone(),
subcommand_index: 2,
subcommand: Some(OsString::from("add")),
route: Route::Direct,
};
return CoordinatedForward {
real_git: PathBuf::from(REAL_GIT),
arguments,
overlay: isolated_overlay(),
identity,
command,
environment: Vec::new(),
copy: None,
};
}
#[test]
fn a_coordinated_add_runs_git_and_reports_its_status() {
let root: PathBuf = fixture("forward-add");
let repo: PathBuf = repository(root.as_path(), "repo");
std::fs::write(repo.join("a.txt"), b"content\n").expect("file to add");
let forward: CoordinatedForward = add_forward(repo.as_path());
assert_eq!(run_coordinated_forward(&forward), CoordinatedRun::Ran(0));
let staged: String = crate::test_support::git_text(repo.as_path(), &["ls-files", "--", "a.txt"]);
assert_eq!(staged, "a.txt");
remove(root.as_path());
}
#[test]
fn a_foreign_index_lock_is_gits_refusal_and_is_not_waited_for() {
let root: PathBuf = fixture("forward-foreign-lock");
let repo: PathBuf = repository(root.as_path(), "repo");
std::fs::write(repo.join("a.txt"), b"content\n").expect("file to add");
std::fs::write(repo.join(".git/index.lock"), b"").expect("foreign lock");
let forward: CoordinatedForward = add_forward(repo.as_path());
let began: std::time::Instant = std::time::Instant::now();
assert_eq!(run_coordinated_forward(&forward), CoordinatedRun::Ran(128), "Git's status for a lock it cannot create");
assert!(began.elapsed() < std::time::Duration::from_secs(10), "no wait for a foreign holder");
assert!(repo.join(".git/index.lock").exists(), "the foreign lock is left in place");
let staged: String = crate::test_support::git_text(repo.as_path(), &["ls-files", "--", "a.txt"]);
assert_eq!(staged, "", "Git could not take the lock, so nothing was staged");
remove(root.as_path());
}
#[test]
fn a_non_writer_with_a_copy_runs_settled() {
let root: PathBuf = fixture("forward-status-copy");
let repo: PathBuf = repository(root.as_path(), "repo");
let mut forward: CoordinatedForward = add_forward(repo.as_path());
forward.command = ResolvedCommand {
arguments: forward.arguments.clone(),
subcommand_index: 2,
subcommand: Some(OsString::from("status")),
route: Route::Direct,
};
forward.arguments = vec![
OsString::from("-C"),
repo.as_os_str().to_owned(),
OsString::from("status"),
OsString::from("--short"),
];
forward.copy = Some(CopyFacts {
identity: forward.identity.clone(),
creates_or_moves: false,
main_checkout_paths: vec![String::from(".cache/forbidden-strings.rules.txt")],
global_prefix: vec![OsString::from("-C"), repo.as_os_str().to_owned()],
environment: Vec::new(),
});
assert_eq!(run_coordinated_forward(&forward), CoordinatedRun::Ran(0));
remove(root.as_path());
}
#[test]
fn a_non_writer_is_not_coordinated() {
let root: PathBuf = fixture("forward-status");
let repo: PathBuf = repository(root.as_path(), "repo");
let mut forward: CoordinatedForward = add_forward(repo.as_path());
forward.command = ResolvedCommand {
arguments: forward.arguments.clone(),
subcommand_index: 2,
subcommand: Some(OsString::from("status")),
route: Route::Direct,
};
assert_eq!(run_coordinated_forward(&forward), CoordinatedRun::NotCoordinated);
remove(root.as_path());
}
#[test]
fn a_writer_of_another_index_file_is_not_coordinated() {
let root: PathBuf = fixture("forward-other-index");
let repo: PathBuf = repository(root.as_path(), "repo");
let forward: CoordinatedForward = add_forward(repo.as_path());
let mut other: CoordinatedForward = forward.clone();
other.overlay.push((OsString::from("GIT_INDEX_FILE"), repo.join("other-index").into_os_string()));
assert_eq!(run_coordinated_forward(&other), CoordinatedRun::NotCoordinated);
remove(root.as_path());
}