Skip to content

Commit 2e41a23

Browse files
committed
test(git): add regression tests for add/delete commits and excluded lockfile diffs
1 parent 97f9d9f commit 2e41a23

1 file changed

Lines changed: 140 additions & 15 deletions

File tree

‎src/git.rs‎

Lines changed: 140 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,25 +1284,14 @@ mod tests {
12841284
Self { path, repo }
12851285
}
12861286

1287-
fn commit_file(
1287+
fn commit_index(
12881288
&self,
1289-
relative_path: &str,
1290-
content: &str,
12911289
author_name: &str,
12921290
author_email: &str,
12931291
timestamp: i64,
12941292
message: &str,
12951293
) -> String {
1296-
let file_path = self.path.join(relative_path);
1297-
file_path
1298-
.parent()
1299-
.map(std::fs::create_dir_all)
1300-
.transpose()
1301-
.unwrap();
1302-
std::fs::write(&file_path, content).unwrap();
1303-
13041294
let mut index = self.repo.index().unwrap();
1305-
index.add_path(std::path::Path::new(relative_path)).unwrap();
13061295
index.write().unwrap();
13071296

13081297
let tree_id = index.write_tree().unwrap();
@@ -1316,7 +1305,7 @@ mod tests {
13161305
.ok()
13171306
.and_then(|head| head.peel_to_commit().ok());
13181307

1319-
let oid = match parent.as_ref() {
1308+
match parent.as_ref() {
13201309
Some(parent_commit) => self.repo.commit(
13211310
Some("HEAD"),
13221311
&signature,
@@ -1329,9 +1318,49 @@ mod tests {
13291318
.repo
13301319
.commit(Some("HEAD"), &signature, &signature, message, &tree, &[]),
13311320
}
1332-
.unwrap();
1321+
.unwrap()
1322+
.to_string()
1323+
}
1324+
1325+
fn commit_file(
1326+
&self,
1327+
relative_path: &str,
1328+
content: &str,
1329+
author_name: &str,
1330+
author_email: &str,
1331+
timestamp: i64,
1332+
message: &str,
1333+
) -> String {
1334+
let file_path = self.path.join(relative_path);
1335+
file_path
1336+
.parent()
1337+
.map(std::fs::create_dir_all)
1338+
.transpose()
1339+
.unwrap();
1340+
std::fs::write(&file_path, content).unwrap();
13331341

1334-
oid.to_string()
1342+
let mut index = self.repo.index().unwrap();
1343+
index.add_path(std::path::Path::new(relative_path)).unwrap();
1344+
index.write().unwrap();
1345+
self.commit_index(author_name, author_email, timestamp, message)
1346+
}
1347+
1348+
fn remove_file(
1349+
&self,
1350+
relative_path: &str,
1351+
author_name: &str,
1352+
author_email: &str,
1353+
timestamp: i64,
1354+
message: &str,
1355+
) -> String {
1356+
std::fs::remove_file(self.path.join(relative_path)).unwrap();
1357+
1358+
let mut index = self.repo.index().unwrap();
1359+
index
1360+
.remove_path(std::path::Path::new(relative_path))
1361+
.unwrap();
1362+
index.write().unwrap();
1363+
self.commit_index(author_name, author_email, timestamp, message)
13351364
}
13361365
}
13371366

@@ -1961,4 +1990,100 @@ mod tests {
19611990
assert!(unstaged_change.new_content.is_none());
19621991
assert!(unstaged_change.is_excluded);
19631992
}
1993+
1994+
#[test]
1995+
fn test_get_commit_handles_added_and_deleted_file_contents() {
1996+
let test_repo = TestRepo::new();
1997+
test_repo.commit_file(
1998+
"tracked.txt",
1999+
"tracked\n",
2000+
"Alice Example",
2001+
2002+
1_700_006_000,
2003+
"base commit",
2004+
);
2005+
let added_hash = test_repo.commit_file(
2006+
"added.txt",
2007+
"added\n",
2008+
"Bob Example",
2009+
2010+
1_700_006_060,
2011+
"add file",
2012+
);
2013+
let deleted_hash = test_repo.remove_file(
2014+
"tracked.txt",
2015+
"Carol Example",
2016+
2017+
1_700_006_120,
2018+
"delete file",
2019+
);
2020+
2021+
let repo = GitRepository::open(&test_repo.path).unwrap();
2022+
let added_change = repo
2023+
.get_commit(&added_hash)
2024+
.unwrap()
2025+
.changes
2026+
.into_iter()
2027+
.find(|change| change.path == "added.txt")
2028+
.unwrap();
2029+
let deleted_change = repo
2030+
.get_commit(&deleted_hash)
2031+
.unwrap()
2032+
.changes
2033+
.into_iter()
2034+
.find(|change| change.path == "tracked.txt")
2035+
.unwrap();
2036+
2037+
assert_eq!(added_change.status, FileStatus::Added);
2038+
assert!(added_change.old_content.is_none());
2039+
assert_eq!(added_change.new_content.as_deref(), Some("added\n"));
2040+
assert_eq!(deleted_change.status, FileStatus::Deleted);
2041+
assert_eq!(deleted_change.old_content.as_deref(), Some("tracked\n"));
2042+
assert!(deleted_change.new_content.is_none());
2043+
}
2044+
2045+
#[test]
2046+
fn test_working_tree_diff_excludes_lockfiles_and_preserves_context_lines() {
2047+
let test_repo = TestRepo::new();
2048+
let original = "version = 3\n[[package]]\nname = \"gitlogue\"\nchecksum = \"old\"\n";
2049+
let updated = "version = 3\n[[package]]\nname = \"gitlogue\"\nchecksum = \"new\"\n";
2050+
test_repo.commit_file(
2051+
"Cargo.lock",
2052+
original,
2053+
"Alice Example",
2054+
2055+
1_700_007_000,
2056+
"lock base",
2057+
);
2058+
2059+
std::fs::write(test_repo.path.join("Cargo.lock"), updated).unwrap();
2060+
let mut index = test_repo.repo.index().unwrap();
2061+
index.add_path(std::path::Path::new("Cargo.lock")).unwrap();
2062+
index.write().unwrap();
2063+
2064+
let repo = GitRepository::open(&test_repo.path).unwrap();
2065+
let change = repo
2066+
.get_working_tree_diff(DiffMode::Staged)
2067+
.unwrap()
2068+
.changes
2069+
.into_iter()
2070+
.find(|change| change.path == "Cargo.lock")
2071+
.unwrap();
2072+
let context_lines = change
2073+
.hunks
2074+
.iter()
2075+
.flat_map(|hunk| hunk.lines.iter())
2076+
.filter(|line| matches!(line.change_type, LineChangeType::Context))
2077+
.map(|line| (line.content.clone(), line.old_line_no, line.new_line_no))
2078+
.collect::<Vec<_>>();
2079+
2080+
assert!(change.is_excluded);
2081+
assert_eq!(
2082+
change.exclusion_reason.as_deref(),
2083+
Some("lock/generated file")
2084+
);
2085+
assert_eq!(change.old_content.as_deref(), Some(original));
2086+
assert_eq!(change.new_content.as_deref(), Some(updated));
2087+
assert!(context_lines.contains(&("name = \"gitlogue\"\n".to_string(), Some(3), Some(3),)));
2088+
}
19642089
}

0 commit comments

Comments
 (0)