Lutra/crates/vcs/tests/stash.rs
danny e09ef8b8c0 M4.9: Git stash (stash, list, apply, pop, drop) with clear refusals
- crates/vcs: stash.rs (append) with Repo::stash, stash_list, stash_apply
  and stash_drop; refusals decided structurally (no commit, nothing to
  stash, unmerged files, conflicts after apply, missing or changed entry)
  plus hints for overwritten and untracked files. Works around git leaving
  untracked files behind under --literal-pathspecs. Repo::lock is
  pub(crate), as for M4.8.
- engine: git.stash, git.stashList, git.stashApply {index, pop, hash?},
  git.stashDrop through the M4.6 hook; gitChanged after changes.
- ui: stash dialog (vcs.stash, vcs.showStashes; Git menu, no shortcuts),
  two buttons in the Changes window, dirty buffers saved first, drop asks.
- Tests: vcs integration and property tests, engine test, 13 Vitest tests.
- Docs: architecture section, G8, tooling checklist, roadmap, Git row
  evidence (score left to the PM).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 17:37:53 +01:00

246 lines
8.2 KiB
Rust

//! Stash (M4.9) against real repositories in temp folders: stash with and
//! without a message and untracked files, the list, apply, pop and drop,
//! and every refusal (no commit, nothing to stash, unmerged files,
//! conflicts, overwritten changes, untracked files in the way, a missing or
//! changed entry). Every test skips with a message when git is not
//! installed.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::Path;
use std::process::Command;
use lutra_vcs::{Git, Repo, VcsError};
/// The installed git, or `None` (with a message) when there is none.
fn installed() -> Option<Git> {
match Git::detect("") {
Ok(git) => Some(git),
Err(e) => {
eprintln!("skipped: no usable git installed ({e})");
None
}
}
}
/// Runs git in `dir` for test setup and asserts it succeeded.
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.args(args)
.current_dir(dir)
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE")
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
/// A new repository on `main` with a local identity.
fn init(dir: &Path) {
git(dir, &["init", "-q", "-b", "main"]);
for (key, value) in [
("user.name", "Lutra Test"),
("user.email", "test@lutra.invalid"),
("commit.gpgsign", "false"),
("core.autocrlf", "false"),
("core.hooksPath", ".git/hooks"),
] {
git(dir, &["config", key, value]);
}
}
/// Writes a file.
fn write(dir: &Path, path: &str, text: &str) {
std::fs::write(dir.join(path), text).unwrap();
}
/// Reads a file.
fn read(dir: &Path, path: &str) -> String {
std::fs::read_to_string(dir.join(path)).unwrap()
}
/// A repository with `a.txt` committed, opened.
fn repo_with_commit(git_: &Git, dir: &Path) -> Repo {
init(dir);
write(dir, "a.txt", "one\n");
git(dir, &["add", "-A"]);
git(dir, &["commit", "-q", "-m", "init"]);
git_.open(dir).unwrap().expect("a repository")
}
/// The message of a `VcsError::Git`.
fn refusal(result: Result<impl std::fmt::Debug, VcsError>) -> String {
match result {
Err(VcsError::Git(message)) => message,
other => panic!("expected a refusal, got {other:?}"),
}
}
#[test]
fn stash_list_apply_pop_drop() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let repo = repo_with_commit(&g, dir);
assert!(repo.stash_list().unwrap().is_empty());
write(dir, "a.txt", "two\n");
let first = repo.stash(Some(" first\nline "), false).unwrap();
assert_eq!(first.index, 0);
assert_eq!(first.message, "first line");
assert_eq!(first.branch.as_deref(), Some("main"));
assert!(!first.wip);
assert_eq!(read(dir, "a.txt"), "one\n");
write(dir, "a.txt", "three\n");
let second = repo.stash(None, false).unwrap();
assert!(second.wip);
assert!(second.message.ends_with("init"), "{}", second.message);
let list = repo.stash_list().unwrap();
assert_eq!(list.len(), 2);
assert_eq!(list[0].hash, second.hash);
assert_eq!(list[1].hash, first.hash);
assert_eq!(list[1].index, 1);
assert!(!list[0].date.is_empty());
repo.stash_apply(1, false, Some(&first.hash)).unwrap();
assert_eq!(read(dir, "a.txt"), "two\n");
assert_eq!(repo.stash_list().unwrap().len(), 2);
git(dir, &["checkout", "--", "a.txt"]);
repo.stash_apply(0, true, second.hash.get(..8)).unwrap();
assert_eq!(read(dir, "a.txt"), "three\n");
let list = repo.stash_list().unwrap();
assert_eq!(list.len(), 1);
assert_eq!(list[0].hash, first.hash);
repo.stash_drop(0, Some(&first.hash)).unwrap();
assert!(repo.stash_list().unwrap().is_empty());
}
#[test]
fn untracked_files_only_with_the_flag() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let repo = repo_with_commit(&g, dir);
write(dir, "new.txt", "new\n");
let message = refusal(repo.stash(None, false));
assert!(message.contains("no local changes"), "{message}");
assert!(message.contains("untracked"), "{message}");
assert!(dir.join("new.txt").exists());
std::fs::create_dir_all(dir.join("d/e")).unwrap();
write(dir, "d/e/-f.txt", "deep\n");
repo.stash(Some("with untracked"), true).unwrap();
assert!(!dir.join("new.txt").exists());
assert!(!dir.join("d").exists());
assert_eq!(read(dir, "a.txt"), "one\n");
repo.stash_apply(0, true, None).unwrap();
assert_eq!(read(dir, "new.txt"), "new\n");
assert_eq!(read(dir, "d/e/-f.txt"), "deep\n");
assert!(repo.stash_list().unwrap().is_empty());
}
#[test]
fn nothing_to_stash_and_no_commit() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init(dir);
write(dir, "a.txt", "x\n");
let repo = g.open(dir).unwrap().unwrap();
let message = refusal(repo.stash(None, true));
assert!(message.contains("first commit"), "{message}");
git(dir, &["add", "-A"]);
git(dir, &["commit", "-q", "-m", "init"]);
let message = refusal(repo.stash(Some("x"), true));
assert_eq!(message, "There are no local changes to stash.");
assert!(repo.stash_list().unwrap().is_empty());
}
#[test]
fn missing_and_changed_entries_are_refused() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let repo = repo_with_commit(&g, dir);
assert_eq!(refusal(repo.stash_drop(0, None)), "The stash is empty.");
write(dir, "a.txt", "two\n");
let entry = repo.stash(None, false).unwrap();
let message = refusal(repo.stash_apply(3, false, None));
assert!(message.contains("no stash entry 3"), "{message}");
assert!(message.contains("1 entry"), "{message}");
let message = refusal(repo.stash_drop(0, Some("deadbeef")));
assert!(message.contains("changed since it was listed"), "{message}");
assert!(matches!(
repo.stash_drop(0, Some("stash@{0}")),
Err(VcsError::InvalidName(_))
));
assert_eq!(repo.stash_list().unwrap()[0].hash, entry.hash);
}
#[test]
fn overwritten_changes_and_untracked_in_the_way() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let repo = repo_with_commit(&g, dir);
write(dir, "a.txt", "stashed\n");
repo.stash(None, false).unwrap();
write(dir, "a.txt", "local\n");
let message = refusal(repo.stash_apply(0, true, None));
assert!(message.contains("would be overwritten"), "{message}");
assert_eq!(read(dir, "a.txt"), "local\n");
assert_eq!(repo.stash_list().unwrap().len(), 1);
git(dir, &["checkout", "--", "a.txt"]);
repo.stash_drop(0, None).unwrap();
write(dir, "u.txt", "stashed\n");
repo.stash(None, true).unwrap();
write(dir, "u.txt", "in the way\n");
let message = refusal(repo.stash_apply(0, false, None));
assert!(message.contains("already exist"), "{message}");
assert_eq!(read(dir, "u.txt"), "in the way\n");
}
#[test]
fn conflicts_keep_the_entry_and_block_the_next_stash() {
let Some(g) = installed() else { return };
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let repo = repo_with_commit(&g, dir);
write(dir, "a.txt", "stashed\n");
repo.stash(None, false).unwrap();
write(dir, "a.txt", "committed\n");
git(dir, &["commit", "-q", "-am", "change"]);
let message = refusal(repo.stash_apply(0, true, None));
assert!(message.contains("conflicts in 1 file"), "{message}");
assert!(message.contains("was kept"), "{message}");
assert_eq!(repo.stash_list().unwrap().len(), 1);
assert!(read(dir, "a.txt").contains("<<<<<<<"));
let message = refusal(repo.stash(None, false));
assert!(message.starts_with("Stashing is not possible"), "{message}");
let message = refusal(repo.stash_apply(0, false, None));
assert!(
message.starts_with("Applying a stash is not possible"),
"{message}"
);
}