|
1 | 1 | mod common; |
2 | 2 |
|
| 3 | +use assert_cmd::Command as AssertCmd; |
3 | 4 | use common::repo_with_branch; |
4 | 5 | use git_x::rename_branch::*; |
| 6 | +use predicates::prelude::*; |
| 7 | +use std::fs; |
| 8 | +use std::path::PathBuf; |
5 | 9 | use std::process::Command; |
| 10 | +use tempfile::TempDir; |
6 | 11 |
|
7 | 12 | #[test] |
8 | 13 | fn test_rename_branch_in_isolated_repo() { |
@@ -139,5 +144,187 @@ fn test_format_rename_success_message() { |
139 | 144 | ); |
140 | 145 | } |
141 | 146 |
|
142 | | -// Note: rename_branch::run() interacts with remotes, making it difficult to test directly |
143 | | -// The CLI integration tests cover this functionality instead |
| 147 | +// Helper function to create a test git repository |
| 148 | +fn create_test_repo() -> (TempDir, PathBuf) { |
| 149 | + let temp_dir = TempDir::new().expect("Failed to create temp directory"); |
| 150 | + let repo_path = temp_dir.path().to_path_buf(); |
| 151 | + |
| 152 | + // Initialize git repo |
| 153 | + Command::new("git") |
| 154 | + .args(["init"]) |
| 155 | + .current_dir(&repo_path) |
| 156 | + .output() |
| 157 | + .expect("Failed to init repo"); |
| 158 | + |
| 159 | + // Configure git |
| 160 | + Command::new("git") |
| 161 | + .args(["config", "user.name", "Test User"]) |
| 162 | + .current_dir(&repo_path) |
| 163 | + .output() |
| 164 | + .expect("Failed to configure user"); |
| 165 | + |
| 166 | + Command::new("git") |
| 167 | + .args(["config", "user.email", "test@example.com"]) |
| 168 | + .current_dir(&repo_path) |
| 169 | + .output() |
| 170 | + .expect("Failed to configure email"); |
| 171 | + |
| 172 | + // Create initial commit |
| 173 | + fs::write(repo_path.join("README.md"), "Initial commit").expect("Failed to write file"); |
| 174 | + Command::new("git") |
| 175 | + .args(["add", "README.md"]) |
| 176 | + .current_dir(&repo_path) |
| 177 | + .output() |
| 178 | + .expect("Failed to add file"); |
| 179 | + |
| 180 | + Command::new("git") |
| 181 | + .args(["commit", "-m", "Initial commit"]) |
| 182 | + .current_dir(&repo_path) |
| 183 | + .output() |
| 184 | + .expect("Failed to commit"); |
| 185 | + |
| 186 | + (temp_dir, repo_path) |
| 187 | +} |
| 188 | + |
| 189 | +#[test] |
| 190 | +fn test_rename_branch_run_outside_git_repo() { |
| 191 | + let temp_dir = TempDir::new().expect("Failed to create temp directory"); |
| 192 | + |
| 193 | + let mut cmd = AssertCmd::cargo_bin("git-x").expect("Failed to find binary"); |
| 194 | + cmd.args(["rename-branch", "new-name"]) |
| 195 | + .current_dir(temp_dir.path()) |
| 196 | + .assert() |
| 197 | + .failure() |
| 198 | + .code(1) |
| 199 | + .stderr(predicate::str::contains( |
| 200 | + "Failed to get current branch name", |
| 201 | + )); |
| 202 | +} |
| 203 | + |
| 204 | +#[test] |
| 205 | +fn test_rename_branch_same_name() { |
| 206 | + let (_temp_dir, repo_path) = create_test_repo(); |
| 207 | + |
| 208 | + // Get current branch name (should be main or master) |
| 209 | + let output = Command::new("git") |
| 210 | + .args(["rev-parse", "--abbrev-ref", "HEAD"]) |
| 211 | + .current_dir(&repo_path) |
| 212 | + .output() |
| 213 | + .expect("Failed to get current branch"); |
| 214 | + |
| 215 | + let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 216 | + |
| 217 | + let mut cmd = AssertCmd::cargo_bin("git-x").expect("Failed to find binary"); |
| 218 | + cmd.args(["rename-branch", ¤t_branch]) |
| 219 | + .current_dir(&repo_path) |
| 220 | + .assert() |
| 221 | + .success() |
| 222 | + .stdout(predicate::str::contains(format!( |
| 223 | + "Current branch is already named '{current_branch}'. Nothing to do." |
| 224 | + ))); |
| 225 | +} |
| 226 | + |
| 227 | +#[test] |
| 228 | +fn test_rename_branch_local_rename_failure() { |
| 229 | + let (_temp_dir, repo_path) = create_test_repo(); |
| 230 | + |
| 231 | + // Create a branch with an invalid name that would cause rename to fail |
| 232 | + // Use invalid characters that git doesn't allow |
| 233 | + let mut cmd = AssertCmd::cargo_bin("git-x").expect("Failed to find binary"); |
| 234 | + cmd.args(["rename-branch", "branch..with..double..dots"]) |
| 235 | + .current_dir(&repo_path) |
| 236 | + .assert() |
| 237 | + .failure() |
| 238 | + .code(1) |
| 239 | + .stderr(predicate::str::contains("Failed to rename local branch")); |
| 240 | +} |
| 241 | + |
| 242 | +#[test] |
| 243 | +fn test_rename_branch_command_help() { |
| 244 | + let mut cmd = AssertCmd::cargo_bin("git-x").expect("Failed to find binary"); |
| 245 | + cmd.args(["rename-branch", "--help"]) |
| 246 | + .assert() |
| 247 | + .success() |
| 248 | + .stdout(predicate::str::contains("Rename the current branch")); |
| 249 | +} |
| 250 | + |
| 251 | +#[test] |
| 252 | +fn test_rename_branch_push_failure() { |
| 253 | + let (_temp_dir, repo_path) = create_test_repo(); |
| 254 | + |
| 255 | + // Add a fake remote that doesn't exist to cause push failure |
| 256 | + Command::new("git") |
| 257 | + .args([ |
| 258 | + "remote", |
| 259 | + "add", |
| 260 | + "origin", |
| 261 | + "https://github.com/nonexistent/repo.git", |
| 262 | + ]) |
| 263 | + .current_dir(&repo_path) |
| 264 | + .output() |
| 265 | + .expect("Failed to add remote"); |
| 266 | + |
| 267 | + let mut cmd = AssertCmd::cargo_bin("git-x").expect("Failed to find binary"); |
| 268 | + cmd.args(["rename-branch", "new-branch-name"]) |
| 269 | + .current_dir(&repo_path) |
| 270 | + .assert() |
| 271 | + .failure() |
| 272 | + .code(1) |
| 273 | + .stderr(predicate::str::contains( |
| 274 | + "Failed to push new branch to origin", |
| 275 | + )); |
| 276 | +} |
| 277 | + |
| 278 | +#[test] |
| 279 | +fn test_rename_branch_edge_cases() { |
| 280 | + // Test various edge cases in formatting and validation |
| 281 | + |
| 282 | + // Test empty branch name handling |
| 283 | + assert_eq!( |
| 284 | + format_already_named_message(""), |
| 285 | + "Current branch is already named ''. Nothing to do." |
| 286 | + ); |
| 287 | + |
| 288 | + // Test special characters in branch names |
| 289 | + assert_eq!( |
| 290 | + format_rename_start_message("feature/test-123", "hotfix/urgent_fix"), |
| 291 | + "Renaming branch 'feature/test-123' to 'hotfix/urgent_fix'" |
| 292 | + ); |
| 293 | + |
| 294 | + // Test long branch names |
| 295 | + let long_name = "very-long-branch-name-that-exceeds-normal-length"; |
| 296 | + assert_eq!( |
| 297 | + format_delete_success_message(long_name), |
| 298 | + format!("Deleted old branch '{long_name}' from origin.") |
| 299 | + ); |
| 300 | +} |
| 301 | + |
| 302 | +#[test] |
| 303 | +fn test_rename_branch_args_completeness() { |
| 304 | + // Ensure all argument generation functions produce valid arrays |
| 305 | + let current_args = get_current_branch_args(); |
| 306 | + assert_eq!(current_args.len(), 3); |
| 307 | + assert!(current_args.contains(&"rev-parse")); |
| 308 | + assert!(current_args.contains(&"--abbrev-ref")); |
| 309 | + assert!(current_args.contains(&"HEAD")); |
| 310 | + |
| 311 | + let local_rename_args = get_local_rename_args("test"); |
| 312 | + assert_eq!(local_rename_args.len(), 3); |
| 313 | + assert!(local_rename_args.contains(&"branch".to_string())); |
| 314 | + assert!(local_rename_args.contains(&"-m".to_string())); |
| 315 | + assert!(local_rename_args.contains(&"test".to_string())); |
| 316 | + |
| 317 | + let push_args = get_push_new_branch_args("test"); |
| 318 | + assert_eq!(push_args.len(), 4); |
| 319 | + assert!(push_args.contains(&"push".to_string())); |
| 320 | + assert!(push_args.contains(&"-u".to_string())); |
| 321 | + assert!(push_args.contains(&"origin".to_string())); |
| 322 | + assert!(push_args.contains(&"test".to_string())); |
| 323 | + |
| 324 | + let delete_args = get_delete_old_branch_args("old"); |
| 325 | + assert_eq!(delete_args.len(), 4); |
| 326 | + assert!(delete_args.contains(&"push".to_string())); |
| 327 | + assert!(delete_args.contains(&"origin".to_string())); |
| 328 | + assert!(delete_args.contains(&"--delete".to_string())); |
| 329 | + assert!(delete_args.contains(&"old".to_string())); |
| 330 | +} |
0 commit comments