- 1
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] - 2
- 3
use std::fs; - 4
use std::path::Path; - 5
use std::process::Command; - 6
- 7
use tempfile::tempdir; - 8
- 9
use vak_core::worktree; - 10
- 11
fn git(repo: &Path, args: &[&str]) { - 12
let out = Command::new("git") - 13
.current_dir(repo) - 14
.args(args) - 15
.output() - 16
.unwrap(); - 17
assert!( - 18
out.status.success(), - 19
"git {args:?} failed: {}", - 20
String::from_utf8_lossy(&out.stderr) - 21
); - 22
} - 23
- 24
fn init_repo() -> (tempfile::TempDir, std::path::PathBuf) { - 25
let dir = tempdir().unwrap(); - 26
let repo = dir.path().join("repo"); - 27
fs::create_dir_all(&repo).unwrap(); - 28
git(&repo, &["init", "-q"]); - 29
git(&repo, &["config", "user.email", "t@t"]); - 30
git(&repo, &["config", "user.name", "t"]); - 31
fs::write(repo.join("base.txt"), "base\n").unwrap(); - 32
git(&repo, &["add", "."]); - 33
git(&repo, &["commit", "-q", "-m", "init"]); - 34
(dir, repo) - 35
} - 36
- 37
#[test] - 38
fn worktree_create_and_remove_lifecycle() { - 39
let (_dir, repo) = init_repo(); - 40
- 41
assert!(worktree::is_git_repo(&repo)); - 42
let wt = worktree::create(&repo, "run-1").unwrap(); - 43
assert!(wt.path.is_dir()); - 44
assert_eq!(wt.branch, "vak/run-1"); - 45
assert!( - 46
wt.path.join("base.txt").exists(), - 47
"worktree must contain committed files" - 48
); - 49
- 50
// Mutations in the worktree never touch the main checkout. - 51
fs::write(wt.path.join("mutated.txt"), "dirty").unwrap(); - 52
assert!(!repo.join("mutated.txt").exists()); - 53
- 54
worktree::remove(&repo, &wt).unwrap(); - 55
assert!(!wt.path.exists()); - 56
let branches = Command::new("git") - 57
.current_dir(&repo) - 58
.args(["branch", "--list", "vak/run-1"]) - 59
.output() - 60
.unwrap(); - 61
assert!( - 62
String::from_utf8_lossy(&branches.stdout).trim().is_empty(), - 63
"branch must be deleted" - 64
); - 65
} - 66
- 67
#[test] - 68
fn non_repo_is_rejected() { - 69
let dir = tempdir().unwrap(); - 70
assert!(matches!( - 71
worktree::create(dir.path(), "x"), - 72
Err(worktree::WorktreeError::NotARepo) - 73
)); - 74
} - 75
Indexing the workspace…
Vakyartha documentation is discovering safe artifacts, anchors, and source references.