Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Showing 1 changed file with 36 additions and 1 deletion.
| @@ -141,7 +141,42 @@ pub fn slug(title: &str) -> String { | ||
| 141 | 141 | |
| 142 | 142 | #[cfg(test)] |
| 143 | 143 | mod tests { |
| 144 | - use super::slug; | |
| 144 | + use super::{WorktreePlan, create, remove, slug}; | |
| 145 | + use crate::git::git; | |
| 146 | + use crate::settings::Submodules; | |
| 147 | + | |
| 148 | + #[::core::prelude::v1::test] | |
| 149 | + fn creates_and_removes_worktrees() { | |
| 150 | + let root = std::env::temp_dir().join(format!("vectrum-worktree-test-{}", std::process::id())); | |
| 151 | + let repo = root.join("repo"); | |
| 152 | + std::fs::create_dir_all(repo.join("sub")).unwrap(); | |
| 153 | + // Worktrees go under the data folder; keep this test's apart. | |
| 154 | + unsafe { std::env::set_var("XDG_DATA_HOME", root.join("data")) }; | |
| 155 | + git(&repo, &["init", "-q", "-b", "main"]).unwrap(); | |
| 156 | + std::fs::write(repo.join("sub/file.txt"), "hello\n").unwrap(); | |
| 157 | + git(&repo, &["add", "."]).unwrap(); | |
| 158 | + git(&repo, &["-c", "user.email=t@t", "-c", "user.name=t", "commit", "-qm", "init"]).unwrap(); | |
| 159 | + | |
| 160 | + let plan = WorktreePlan { | |
| 161 | + project: repo.join("sub"), | |
| 162 | + base: Some("main".into()), | |
| 163 | + from_origin: true, | |
| 164 | + submodules: Submodules::Recursive, | |
| 165 | + }; | |
| 166 | + let (worktree, cwd) = create(&plan, "Fix the thing").unwrap(); | |
| 167 | + assert_eq!(worktree.branch, "vectrum/fix-the-thing"); | |
| 168 | + assert!(cwd.ends_with("fix-the-thing/sub")); | |
| 169 | + assert_eq!(std::fs::read_to_string(cwd.join("file.txt")).unwrap(), "hello\n"); | |
| 170 | + | |
| 171 | + // A second thread with the same title gets its own branch. | |
| 172 | + let (second, _) = create(&plan, "Fix the thing").unwrap(); | |
| 173 | + assert_eq!(second.branch, "vectrum/fix-the-thing-2"); | |
| 174 | + | |
| 175 | + remove(&worktree).unwrap(); | |
| 176 | + assert!(!worktree.root.exists()); | |
| 177 | + let _ = remove(&second); | |
| 178 | + let _ = std::fs::remove_dir_all(&root); | |
| 179 | + } | |
| 145 | 180 | |
| 146 | 181 | #[::core::prelude::v1::test] |
| 147 | 182 | fn slugs() { |