forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·7.1 MiB·490f099·Tahinli·7 hours ago·322 commits
| 1 | //! Tests for the code-browsing helpers: whitespace-free diffs, patches, the recursive file list, |
| 2 | //! blame and branch operations. |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; |
| 6 | |
| 7 | use forge_git::GitEnv; |
| 8 | use forge_git::blame::blame; |
| 9 | use forge_git::branches::{ahead_behind, create_branch, delete_branch}; |
| 10 | use forge_git::command::ObjectSource; |
| 11 | use forge_git::history::{commit_diff_ws, commit_patch, commit_raw_diff, list_files, range_patch}; |
| 12 | |
| 13 | use crate::common::{BIG, git_env}; |
| 14 | |
| 15 | struct Fixture { |
| 16 | _dir: tempfile::TempDir, |
| 17 | work: PathBuf, |
| 18 | git_dir: PathBuf, |
| 19 | env: GitEnv, |
| 20 | } |
| 21 | |
| 22 | fn run(dir: &Path, home: &Path, args: &[&str]) -> String { |
| 23 | let out = Command::new("/usr/bin/git") |
| 24 | .arg("-C") |
| 25 | .arg(dir) |
| 26 | .args([ |
| 27 | "-c", |
| 28 | "user.name=tahinli", |
| 29 | "-c", |
| 30 | "user.email=tahinli@example.com", |
| 31 | "-c", |
| 32 | "commit.gpgsign=false", |
| 33 | ]) |
| 34 | .args(args) |
| 35 | .env_clear() |
| 36 | .env("PATH", "/usr/bin:/bin") |
| 37 | .env("HOME", home) |
| 38 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 39 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 40 | .output() |
| 41 | .unwrap(); |
| 42 | assert!( |
| 43 | out.status.success(), |
| 44 | "{args:?} failed: {}", |
| 45 | String::from_utf8_lossy(&out.stderr) |
| 46 | ); |
| 47 | String::from_utf8_lossy(&out.stdout).into_owned() |
| 48 | } |
| 49 | |
| 50 | fn fixture() -> Fixture { |
| 51 | let dir = tempfile::tempdir().unwrap(); |
| 52 | let work = dir.path().join("work"); |
| 53 | std::fs::create_dir(&work).unwrap(); |
| 54 | run(&work, dir.path(), &["init", "-q", "-b", "main"]); |
| 55 | let env = git_env(dir.path()); |
| 56 | let git_dir = work.join(".git"); |
| 57 | Fixture { |
| 58 | _dir: dir, |
| 59 | work, |
| 60 | git_dir, |
| 61 | env, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl Fixture { |
| 66 | fn sh(&self, args: &[&str]) -> String { |
| 67 | run(&self.work, self._dir.path(), args) |
| 68 | } |
| 69 | |
| 70 | fn write(&self, name: &str, content: &str) { |
| 71 | let p = self.work.join(name); |
| 72 | std::fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 73 | std::fs::write(p, content).unwrap(); |
| 74 | } |
| 75 | |
| 76 | fn commit(&self, msg: &str) -> String { |
| 77 | self.sh(&["add", "-A"]); |
| 78 | self.sh(&["commit", "-q", "--allow-empty", "-m", msg]); |
| 79 | self.sh(&["rev-parse", "HEAD"]).trim().to_owned() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[tokio::test] |
| 84 | async fn ignore_whitespace_drops_whitespace_only_files() { |
| 85 | let f = fixture(); |
| 86 | f.write("x.txt", "a\nb\n"); |
| 87 | f.write("y.txt", "c\n"); |
| 88 | f.commit("one"); |
| 89 | f.write("x.txt", "a \nb\n"); |
| 90 | f.write("y.txt", "c\nd\n"); |
| 91 | let head = f.commit("two"); |
| 92 | |
| 93 | let full = commit_diff_ws(&f.env, &f.git_dir, &head, BIG, false) |
| 94 | .await |
| 95 | .unwrap(); |
| 96 | assert_eq!(full.files.len(), 2); |
| 97 | let ws = commit_diff_ws(&f.env, &f.git_dir, &head, BIG, true) |
| 98 | .await |
| 99 | .unwrap(); |
| 100 | assert_eq!(ws.files.len(), 1); |
| 101 | assert_eq!(ws.files[0].new_path.as_deref(), Some("y.txt")); |
| 102 | assert_eq!(ws.files[0].additions, 1); |
| 103 | } |
| 104 | |
| 105 | #[tokio::test] |
| 106 | async fn patch_and_diff_outputs() { |
| 107 | let f = fixture(); |
| 108 | f.write("x.txt", "a\n"); |
| 109 | let root = f.commit("root commit"); |
| 110 | f.write("x.txt", "a\nb\n"); |
| 111 | let head = f.commit("add b"); |
| 112 | |
| 113 | let patch = commit_patch(&f.env, &f.git_dir, &head).await.unwrap(); |
| 114 | assert!(patch.complete); |
| 115 | let text = String::from_utf8(patch.data).unwrap(); |
| 116 | assert!(text.starts_with(&format!("From {head} "))); |
| 117 | assert!(text.contains("Subject: [PATCH] add b")); |
| 118 | assert!(text.contains("+b")); |
| 119 | |
| 120 | let root_patch = commit_patch(&f.env, &f.git_dir, &root).await.unwrap(); |
| 121 | assert!( |
| 122 | String::from_utf8(root_patch.data) |
| 123 | .unwrap() |
| 124 | .contains("+++ b/x.txt") |
| 125 | ); |
| 126 | |
| 127 | let diff = commit_raw_diff(&f.env, &f.git_dir, &head).await.unwrap(); |
| 128 | let diff = String::from_utf8(diff.data).unwrap(); |
| 129 | assert!(diff.starts_with("diff --git a/x.txt b/x.txt")); |
| 130 | |
| 131 | let range = range_patch(&f.env, &f.git_dir, ObjectSource::default(), &root, &head) |
| 132 | .await |
| 133 | .unwrap(); |
| 134 | let range = String::from_utf8(range.data).unwrap(); |
| 135 | assert_eq!(range.matches("\nSubject: ").count(), 1); |
| 136 | assert!(range.contains("add b")); |
| 137 | assert!(!range.contains("root commit")); |
| 138 | } |
| 139 | |
| 140 | #[tokio::test] |
| 141 | async fn list_files_is_recursive_and_capped() { |
| 142 | let f = fixture(); |
| 143 | f.write("a.txt", "1"); |
| 144 | f.write("dir/b.txt", "2"); |
| 145 | f.write("dir/sub/c.txt", "3"); |
| 146 | let head = f.commit("files"); |
| 147 | |
| 148 | let (all, cut) = list_files(&f.env, &f.git_dir, &head, 100).await.unwrap(); |
| 149 | assert!(!cut); |
| 150 | assert_eq!(all, ["a.txt", "dir/b.txt", "dir/sub/c.txt"]); |
| 151 | let (two, cut) = list_files(&f.env, &f.git_dir, &head, 2).await.unwrap(); |
| 152 | assert!(cut); |
| 153 | assert_eq!(two.len(), 2); |
| 154 | } |
| 155 | |
| 156 | #[tokio::test] |
| 157 | async fn blame_groups_by_commit() { |
| 158 | let f = fixture(); |
| 159 | f.write("f.txt", "one\ntwo\nthree\n"); |
| 160 | let first = f.commit("first"); |
| 161 | f.write("f.txt", "one\nTWO\nthree\nfour\n"); |
| 162 | let second = f.commit("second"); |
| 163 | |
| 164 | let b = blame(&f.env, &f.git_dir, &second, "f.txt", false) |
| 165 | .await |
| 166 | .unwrap(); |
| 167 | assert_eq!(b.total, 4); |
| 168 | let shape: Vec<(&str, u32, u32)> = b |
| 169 | .groups |
| 170 | .iter() |
| 171 | .map(|g| (g.commit.as_str(), g.start, g.lines)) |
| 172 | .collect(); |
| 173 | assert_eq!( |
| 174 | shape, |
| 175 | [ |
| 176 | (first.as_str(), 1, 1), |
| 177 | (second.as_str(), 2, 1), |
| 178 | (first.as_str(), 3, 1), |
| 179 | (second.as_str(), 4, 1), |
| 180 | ] |
| 181 | ); |
| 182 | assert_eq!(b.commits[&second].summary, "second"); |
| 183 | assert_eq!(b.commits[&second].author, "tahinli"); |
| 184 | assert_eq!( |
| 185 | b.commits[&second].previous, |
| 186 | Some((first.clone(), "f.txt".to_owned())) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | #[tokio::test] |
| 191 | async fn branch_counts_create_and_delete() { |
| 192 | let f = fixture(); |
| 193 | f.write("a.txt", "1"); |
| 194 | let base = f.commit("base"); |
| 195 | f.sh(&["checkout", "-q", "-b", "feature"]); |
| 196 | f.write("b.txt", "2"); |
| 197 | f.commit("feature 1"); |
| 198 | let tip = f.commit("feature 2"); |
| 199 | f.sh(&["checkout", "-q", "main"]); |
| 200 | f.write("c.txt", "3"); |
| 201 | let main_tip = f.commit("main moved"); |
| 202 | |
| 203 | let (ahead, behind) = ahead_behind(&f.env, &f.git_dir, &main_tip, &tip) |
| 204 | .await |
| 205 | .unwrap(); |
| 206 | assert_eq!((ahead, behind), (2, 1)); |
| 207 | |
| 208 | create_branch(&f.env, &f.git_dir, "topic/x", &base) |
| 209 | .await |
| 210 | .unwrap(); |
| 211 | assert_eq!( |
| 212 | f.sh(&["rev-parse", "refs/heads/topic/x"]).trim(), |
| 213 | base.as_str() |
| 214 | ); |
| 215 | assert!( |
| 216 | create_branch(&f.env, &f.git_dir, "topic/x", &base) |
| 217 | .await |
| 218 | .is_err() |
| 219 | ); |
| 220 | assert!( |
| 221 | create_branch(&f.env, &f.git_dir, "bad..name", &base) |
| 222 | .await |
| 223 | .is_err() |
| 224 | ); |
| 225 | delete_branch(&f.env, &f.git_dir, "topic/x", &base) |
| 226 | .await |
| 227 | .unwrap(); |
| 228 | assert!( |
| 229 | delete_branch(&f.env, &f.git_dir, "topic/x", &base) |
| 230 | .await |
| 231 | .is_err() |
| 232 | ); |
| 233 | } |