use std::{
path::Path,
process::{Output, Stdio},
};
use pretty_assertions::assert_eq;
use rho_protocol::{
methods::{JobList, SessionObserve},
Client, JobListParams, SessionId, SessionObserveParams, SessionWorktree,
};
use super::{
detach::{attach, detach, session_of},
git_repo::{git, init_repo},
sessions::{settled_job, Sandbox},
};
const UNLOAD_SOON: [&str; 2] = ["--unload-idle-after", "100ms"];
fn prune(sandbox: &Sandbox, mode: &str, args: &[&str]) -> Output {
sandbox
.rho(mode)
.args(["worktrees", "prune"])
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.unwrap()
}
async fn finished_worktree_session(
sandbox: &Sandbox,
client: &Client,
) -> (SessionId, SessionWorktree) {
let session = session_of(&detach(sandbox, "fixed", &["--worktree", "hello"]));
let job = settled_job(sandbox, client, &session).await;
sandbox.wait_unloaded(&session, 1).await;
(session, job.worktree.expect("the job names its worktree"))
}
fn commit(worktree: &Path, name: &str) {
std::fs::write(worktree.join(name), "work").unwrap();
git(worktree, &["add", name]);
git(worktree, &["commit", "--quiet", "-m", name]);
}
fn line(action: &str, worktree: &SessionWorktree, reason: &str) -> String {
format!(
"{action:<12} {} {}: {reason}",
worktree.branch,
worktree.path.display()
)
}
fn sorted_lines(output: &Output) -> Vec<String> {
let mut lines: Vec<String> = String::from_utf8_lossy(&output.stdout)
.lines()
.map(str::to_owned)
.collect();
lines.sort();
lines
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn prune_removes_only_clean_merged_worktrees_and_keeps_their_branches() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (_, merged) = finished_worktree_session(&sandbox, &client).await;
let (_, dirty) = finished_worktree_session(&sandbox, &client).await;
let (_, unmerged) = finished_worktree_session(&sandbox, &client).await;
commit(&merged.path, "merged.txt");
git(
&sandbox.workspace,
&["merge", "--quiet", "--squash", merged.branch.as_str()],
);
git(&sandbox.workspace, &["commit", "--quiet", "-m", "squash"]);
std::fs::write(dirty.path.join("notes.txt"), "draft").unwrap();
commit(&unmerged.path, "unmerged.txt");
let on_disk = || [&merged, &dirty, &unmerged].map(|worktree| worktree.path.exists());
let dry_run = prune(&sandbox, "fixed", &["--dry-run"]);
let after_dry_run = on_disk();
let pruned = prune(&sandbox, "fixed", &[]);
let report = |merged_action: &str| {
let mut lines = vec![
line(
merged_action,
&merged,
"clean and merged into main; the branch stays",
),
line("kept", &dirty, "uncommitted or untracked changes"),
line("kept", &unmerged, "not merged into main"),
];
lines.sort();
lines
};
assert_eq!(
(
(dry_run.status.code(), sorted_lines(&dry_run), after_dry_run),
(pruned.status.code(), sorted_lines(&pruned), on_disk()),
git(
&sandbox.workspace,
&["branch", "--list", merged.branch.as_str()]
),
),
(
(Some(0), report("would remove"), [true, true, true]),
(Some(0), report("removed"), [false, true, true]),
merged.branch.clone(),
),
"stderr: {}{}\nlog:\n{}",
String::from_utf8_lossy(&dry_run.stderr),
String::from_utf8_lossy(&pruned.stderr),
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn prune_leaves_a_running_sessions_worktree_alone() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon("delay").await;
let session = session_of(&detach(&sandbox, "delay", &["--worktree", "hello"]));
let worktree = client
.request::<JobList>(&JobListParams {})
.await
.unwrap()
.jobs
.into_iter()
.find(|job| job.session.as_ref() == Some(&session))
.and_then(|job| job.worktree)
.expect("the daemon lists the session's worktree");
let pruned = prune(&sandbox, "delay", &[]);
assert_eq!(
(
pruned.status.code(),
sorted_lines(&pruned),
worktree.path.exists(),
),
(
Some(0),
vec![line("kept", &worktree, "its session is loaded or running")],
true,
),
"stderr: {}\nlog:\n{}",
String::from_utf8_lossy(&pruned.stderr),
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_reopened_session_gets_its_removed_worktree_back() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (session, worktree) = finished_worktree_session(&sandbox, &client).await;
let pruned = prune(&sandbox, "fixed", &[]);
let removed = !worktree.path.exists();
let attached = attach(&sandbox, "fixed", &session);
assert_eq!(
(
pruned.status.code(),
sorted_lines(&pruned),
removed,
attached.status.code(),
git(&worktree.path, &["branch", "--show-current"]),
),
(
Some(0),
vec![line(
"removed",
&worktree,
"clean and merged into main; the branch stays"
)],
true,
Some(0),
worktree.branch.clone(),
),
"stderr: {}\n{}\nlog:\n{}",
String::from_utf8_lossy(&pruned.stderr),
String::from_utf8_lossy(&attached.stderr),
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reading_a_session_brings_back_its_worktree_after_its_branch_was_deleted() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (session, worktree) = finished_worktree_session(&sandbox, &client).await;
let pruned = prune(&sandbox, "fixed", &[]);
git(
&sandbox.workspace,
&["branch", "--quiet", "-d", worktree.branch.as_str()],
);
let observed = client
.request::<SessionObserve>(&SessionObserveParams {
session,
after: None,
})
.await;
assert_eq!(
(
pruned.status.code(),
observed.map(drop).map_err(|error| error.to_string()),
git(&worktree.path, &["branch", "--show-current"]),
git(&worktree.path, &["rev-parse", "HEAD"]),
),
(
Some(0),
Ok(()),
worktree.branch.clone(),
git(&sandbox.workspace, &["rev-parse", "HEAD"]),
),
"log:\n{}",
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_worktree_a_person_removed_stays_removed() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (session, worktree) = finished_worktree_session(&sandbox, &client).await;
let path = worktree.path.to_string_lossy().into_owned();
git(&sandbox.workspace, &["worktree", "remove", path.as_str()]);
git(
&sandbox.workspace,
&["branch", "--quiet", "-D", worktree.branch.as_str()],
);
let observed = client
.request::<SessionObserve>(&SessionObserveParams {
session,
after: None,
})
.await;
assert_eq!(
(
observed.is_err(),
worktree.path.exists(),
git(
&sandbox.workspace,
&["branch", "--list", worktree.branch.as_str()]
),
),
(true, false, String::new()),
"log:\n{}",
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_branch_checked_out_elsewhere_lets_a_read_but_not_turns_restore() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (session, worktree) = finished_worktree_session(&sandbox, &client).await;
let pruned = prune(&sandbox, "fixed", &[]);
git(
&sandbox.workspace,
&["checkout", "--quiet", worktree.branch.as_str()],
);
let observed = client
.request::<SessionObserve>(&SessionObserveParams {
session: session.clone(),
after: None,
})
.await;
let after_read = worktree.path.exists();
let attached = attach(&sandbox, "fixed", &session);
assert_eq!(
(
pruned.status.code(),
observed.map(drop).map_err(|error| error.to_string()),
after_read,
attached.status.success(),
worktree.path.exists(),
),
(Some(0), Ok(()), false, false, false),
"{}\nlog:\n{}",
String::from_utf8_lossy(&attached.stderr),
sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_checkout_left_by_a_crash_does_not_block_reading() {
let sandbox = Sandbox::new();
init_repo(&sandbox.workspace);
let client = sandbox.start_daemon_with("fixed", &UNLOAD_SOON).await;
let (session, worktree) = finished_worktree_session(&sandbox, &client).await;
let pruned = prune(&sandbox, "fixed", &[]);
let path = worktree.path.to_string_lossy().into_owned();
git(
&sandbox.workspace,
&["worktree", "add", "--quiet", "--detach", path.as_str()],
);
let observed = client
.request::<SessionObserve>(&SessionObserveParams {
session,
after: None,
})
.await;
assert_eq!(
(
pruned.status.code(),
observed.map(drop).map_err(|error| error.to_string()),
git(&worktree.path, &["branch", "--show-current"]),
),
(Some(0), Ok(()), worktree.branch.clone()),
"log:\n{}",
sandbox.isolated.daemon_log()
);
}