use crate::core::test_helpers::TestRepo;
#[test]
fn reword_commit_with_message() {
let test_repo = TestRepo::new();
let c1_oid = test_repo.commit("First commit", "file1.txt");
test_repo.commit("Second commit", "file2.txt");
let c3_oid = test_repo.commit("Third commit", "file3.txt");
let result = super::reword_commit(
&test_repo.repo,
&c1_oid.to_string(),
Some("Updated first commit".to_string()),
);
if result.is_err() {
eprintln!("Note: This test may fail on Windows due to PowerShell sequence editor issues");
eprintln!("Error: {:?}", result);
}
assert!(result.is_ok(), "Failed to reword commit: {:?}", result);
assert_eq!(test_repo.get_message(2), "Updated first commit");
assert_eq!(test_repo.get_message(1), "Second commit");
assert_eq!(test_repo.get_message(0), "Third commit");
assert_ne!(
test_repo.get_oid(2),
c1_oid,
"First commit hash should have changed"
);
assert_ne!(
test_repo.get_oid(0),
c3_oid,
"Third commit hash should have changed"
);
assert!(test_repo.is_on_branch());
}
#[test]
fn reword_commit_without_message() {
let test_repo = TestRepo::new();
let c1_oid = test_repo.commit("First commit", "file1.txt");
test_repo.commit("Second commit", "file2.txt");
test_repo.set_fake_editor("Reworded by editor");
let result = super::reword_commit(&test_repo.repo, &c1_oid.to_string(), None);
if result.is_err() {
eprintln!("Note: This test may fail due to platform-specific editor or PowerShell issues");
eprintln!("Error: {:?}", result);
}
assert!(result.is_ok(), "Failed to reword commit: {:?}", result);
assert_eq!(test_repo.get_message(1), "Reworded by editor");
}
#[test]
fn reword_root_commit() {
let test_repo = TestRepo::new();
let root_commit = test_repo.get_commit(0);
let root_oid = root_commit.id();
assert_eq!(root_commit.parent_count(), 0, "Should be a root commit");
let result = super::reword_commit(
&test_repo.repo,
&root_oid.to_string(),
Some("Updated initial commit".to_string()),
);
if result.is_err() {
eprintln!("Note: This test may fail on Windows due to PowerShell sequence editor issues");
eprintln!("Error: {:?}", result);
}
assert!(result.is_ok(), "Failed to reword root commit: {:?}", result);
assert_eq!(test_repo.get_message(0), "Updated initial commit");
assert_eq!(
test_repo.get_commit(0).parent_count(),
0,
"Should still be a root commit"
);
assert_ne!(
test_repo.get_oid(0),
root_oid,
"Root commit hash should have changed"
);
}
#[test]
fn reword_root_commit_with_descendants() {
let test_repo = TestRepo::new();
let root_oid = test_repo.get_oid(0);
test_repo.commit("Second commit", "file2.txt");
test_repo.commit("Third commit", "file3.txt");
let result = super::reword_commit(
&test_repo.repo,
&root_oid.to_string(),
Some("Updated root".to_string()),
);
if result.is_err() {
eprintln!("Note: This test may fail on Windows due to PowerShell sequence editor issues");
eprintln!("Error: {:?}", result);
}
assert!(
result.is_ok(),
"Failed to reword root commit with descendants: {:?}",
result
);
assert_eq!(test_repo.get_message(2), "Updated root");
assert_eq!(
test_repo.get_commit(2).parent_count(),
0,
"Should still be a root commit"
);
assert_eq!(test_repo.get_message(1), "Second commit");
assert_eq!(test_repo.get_message(0), "Third commit");
}
#[test]
fn reword_commit_with_working_tree_changes() {
let test_repo = TestRepo::new();
let c1_oid = test_repo.commit("First commit", "file1.txt");
test_repo.commit("Second commit", "file2.txt");
test_repo.write_file("file2.txt", "modified content");
let result = super::reword_commit(
&test_repo.repo,
&c1_oid.to_string(),
Some("Updated first".to_string()),
);
if result.is_err() {
eprintln!("Note: This test may fail on Windows due to PowerShell sequence editor issues");
eprintln!("Error: {:?}", result);
}
assert!(
result.is_ok(),
"Failed to reword with working tree changes: {:?}",
result
);
assert_eq!(
test_repo.read_file("file2.txt"),
"modified content",
"Working tree changes should be preserved"
);
}
#[test]
fn reword_branch_by_name() {
let test_repo = TestRepo::new();
test_repo.create_branch("feature-old");
let result = super::reword_branch(&test_repo.repo, "feature-old", "feature-new");
assert!(result.is_ok(), "Failed to rename branch: {:?}", result);
assert!(
!test_repo.branch_exists("feature-old"),
"Old branch should not exist after rename"
);
assert!(
test_repo.branch_exists("feature-new"),
"New branch should exist after rename"
);
assert_eq!(
test_repo.get_branch_target("feature-new"),
test_repo.get_oid(0),
"New branch should point to same commit"
);
}
#[test]
fn reword_current_branch() {
let test_repo = TestRepo::new();
let current_branch_name = test_repo.current_branch_name();
let result = super::reword_branch(&test_repo.repo, ¤t_branch_name, "renamed-main");
assert!(
result.is_ok(),
"Failed to rename current branch: {:?}",
result
);
assert!(test_repo.is_on_branch(), "HEAD should still be on a branch");
assert_eq!(
test_repo.current_branch_name(),
"renamed-main",
"HEAD should track renamed branch"
);
}
#[test]
fn reword_commit_with_partial_hash() {
let test_repo = TestRepo::new();
let c1_oid = test_repo.commit("First commit", "file1.txt");
test_repo.commit("Second commit", "file2.txt");
let partial_hash = &c1_oid.to_string()[..7];
let result = super::reword_commit(
&test_repo.repo,
partial_hash,
Some("Updated via partial hash".to_string()),
);
if result.is_err() {
eprintln!("Note: This test may fail on Windows due to PowerShell sequence editor issues");
eprintln!("Error: {:?}", result);
}
assert!(
result.is_ok(),
"Failed to reword commit with partial hash: {:?}",
result
);
assert_eq!(test_repo.get_message(1), "Updated via partial hash");
}
#[test]
fn reword_nonexistent_commit_fails() {
let test_repo = TestRepo::new();
let result = super::reword_commit(
&test_repo.repo,
"0000000000000000000000000000000000000000",
Some("New message".to_string()),
);
assert!(result.is_err(), "Should fail on nonexistent commit");
}
#[test]
fn reword_nonexistent_branch_fails() {
let test_repo = TestRepo::new();
let result = super::reword_branch(&test_repo.repo, "nonexistent-branch", "new-name");
assert!(result.is_err(), "Should fail on nonexistent branch");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("Failed to rename branch"),
"Error should mention branch rename failure"
);
}
#[test]
fn reword_branch_by_full_name_via_run() {
let test_repo = TestRepo::new();
test_repo.create_branch("feature-original");
let result = test_repo.in_dir(|| {
super::run(
"feature-original".to_string(),
Some("feature-renamed".to_string()),
)
});
assert!(
result.is_ok(),
"Failed to rename branch via run: {:?}",
result
);
assert!(
!test_repo.branch_exists("feature-original"),
"Old branch should not exist after rename"
);
assert!(
test_repo.branch_exists("feature-renamed"),
"New branch should exist after rename"
);
}