|
| 1 | +use git_iris::config::Config; |
| 2 | +use git_iris::context::ChangeType; |
| 3 | +use git_iris::git::GitRepo; |
| 4 | +use git2::Repository; |
| 5 | +use std::fs; |
| 6 | +use std::path::Path; |
| 7 | +use tempfile::TempDir; |
| 8 | + |
| 9 | +fn setup_git_repo() -> (TempDir, GitRepo) { |
| 10 | + let temp_dir = TempDir::new().expect("Failed to create temporary directory"); |
| 11 | + let repo = Repository::init(temp_dir.path()).expect("Failed to initialize repository"); |
| 12 | + |
| 13 | + // Configure git user |
| 14 | + let mut config = repo.config().expect("Failed to get repository config"); |
| 15 | + config |
| 16 | + .set_str("user.name", "Test User") |
| 17 | + .expect("Failed to set user name"); |
| 18 | + config |
| 19 | + .set_str("user.email", "[email protected]") |
| 20 | + .expect("Failed to set user email"); |
| 21 | + |
| 22 | + // Create and commit an initial file |
| 23 | + let initial_file_path = temp_dir.path().join("initial.txt"); |
| 24 | + fs::write(&initial_file_path, "Initial content").expect("Failed to write initial file"); |
| 25 | + |
| 26 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 27 | + index |
| 28 | + .add_path(Path::new("initial.txt")) |
| 29 | + .expect("Failed to add file to index"); |
| 30 | + index.write().expect("Failed to write index"); |
| 31 | + |
| 32 | + let tree_id = index.write_tree().expect("Failed to write tree"); |
| 33 | + let tree = repo.find_tree(tree_id).expect("Failed to find tree"); |
| 34 | + let signature = repo.signature().expect("Failed to create signature"); |
| 35 | + repo.commit( |
| 36 | + Some("HEAD"), |
| 37 | + &signature, |
| 38 | + &signature, |
| 39 | + "Initial commit", |
| 40 | + &tree, |
| 41 | + &[], |
| 42 | + ) |
| 43 | + .expect("Failed to commit"); |
| 44 | + |
| 45 | + // Ensure the default branch is named 'main' for consistency across environments |
| 46 | + { |
| 47 | + let head_commit = repo |
| 48 | + .head() |
| 49 | + .expect("Failed to get HEAD") |
| 50 | + .peel_to_commit() |
| 51 | + .expect("Failed to peel HEAD to commit"); |
| 52 | + let current_branch = repo |
| 53 | + .head() |
| 54 | + .ok() |
| 55 | + .and_then(|h| h.shorthand().map(std::string::ToString::to_string)) |
| 56 | + .unwrap_or_default(); |
| 57 | + if current_branch != "main" { |
| 58 | + // Create or update the 'main' branch pointing to the current HEAD commit |
| 59 | + repo.branch("main", &head_commit, true) |
| 60 | + .expect("Failed to create 'main' branch"); |
| 61 | + repo.set_head("refs/heads/main") |
| 62 | + .expect("Failed to set HEAD to 'main' branch"); |
| 63 | + repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force())) |
| 64 | + .expect("Failed to checkout 'main' branch"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + let git_repo = GitRepo::new(temp_dir.path()).expect("Failed to create GitRepo"); |
| 69 | + (temp_dir, git_repo) |
| 70 | +} |
| 71 | + |
| 72 | +#[tokio::test] |
| 73 | +async fn test_get_git_info() { |
| 74 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 75 | + let config = Config::default(); |
| 76 | + |
| 77 | + let context = git_repo |
| 78 | + .get_git_info(&config) |
| 79 | + .await |
| 80 | + .expect("Failed to get git info"); |
| 81 | + |
| 82 | + // Test branch name |
| 83 | + assert!( |
| 84 | + context.branch == "main" || context.branch == "master", |
| 85 | + "Branch should be 'main' or 'master', but got '{}'", |
| 86 | + context.branch |
| 87 | + ); |
| 88 | + |
| 89 | + // Test recent commits |
| 90 | + assert_eq!(context.recent_commits.len(), 1); |
| 91 | + assert!(context.recent_commits[0].message.contains("Initial commit")); |
| 92 | + |
| 93 | + // Test staged files (should be empty after commit) |
| 94 | + assert_eq!(context.staged_files.len(), 0); |
| 95 | + |
| 96 | + // Test project metadata |
| 97 | + assert_eq!( |
| 98 | + context.project_metadata.language, |
| 99 | + Some("Unknown".to_string()) |
| 100 | + ); |
| 101 | + |
| 102 | + // Create and stage a new file |
| 103 | + let new_file_path = temp_dir.path().join("new_file.txt"); |
| 104 | + fs::write(&new_file_path, "New content").expect("Failed to write new file"); |
| 105 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 106 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 107 | + index |
| 108 | + .add_path(Path::new("new_file.txt")) |
| 109 | + .expect("Failed to add new file to index"); |
| 110 | + index.write().expect("Failed to write index"); |
| 111 | + |
| 112 | + // Create an unstaged file |
| 113 | + let unstaged_file_path = temp_dir.path().join("unstaged.txt"); |
| 114 | + fs::write(&unstaged_file_path, "Unstaged content").expect("Failed to write unstaged file"); |
| 115 | + |
| 116 | + // Get updated git info |
| 117 | + let updated_context = git_repo |
| 118 | + .get_git_info(&config) |
| 119 | + .await |
| 120 | + .expect("Failed to get updated git info"); |
| 121 | + |
| 122 | + // Test staged files |
| 123 | + assert_eq!(updated_context.staged_files.len(), 1); |
| 124 | + assert_eq!(updated_context.staged_files[0].path, "new_file.txt"); |
| 125 | + assert!(matches!( |
| 126 | + updated_context.staged_files[0].change_type, |
| 127 | + ChangeType::Added |
| 128 | + )); |
| 129 | +} |
| 130 | + |
| 131 | +#[tokio::test] |
| 132 | +async fn test_commit() { |
| 133 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 134 | + let config = Config::default(); |
| 135 | + |
| 136 | + // Create and stage a new file |
| 137 | + let new_file_path = temp_dir.path().join("commit_test.txt"); |
| 138 | + fs::write(&new_file_path, "Commit test content").expect("Failed to write commit test file"); |
| 139 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 140 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 141 | + index |
| 142 | + .add_path(Path::new("commit_test.txt")) |
| 143 | + .expect("Failed to add commit test file to index"); |
| 144 | + index.write().expect("Failed to write index"); |
| 145 | + |
| 146 | + // Perform commit |
| 147 | + let result = git_repo.commit("Test commit message"); |
| 148 | + assert!(result.is_ok(), "Failed to perform commit"); |
| 149 | + |
| 150 | + // Verify commit |
| 151 | + let context = git_repo |
| 152 | + .get_git_info(&config) |
| 153 | + .await |
| 154 | + .expect("Failed to get git info after commit"); |
| 155 | + assert_eq!(context.recent_commits.len(), 2); |
| 156 | + assert!( |
| 157 | + context.recent_commits[0] |
| 158 | + .message |
| 159 | + .contains("Test commit message") |
| 160 | + ); |
| 161 | +} |
| 162 | + |
| 163 | +#[tokio::test] |
| 164 | +async fn test_multiple_staged_files() { |
| 165 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 166 | + let config = Config::default(); |
| 167 | + |
| 168 | + // Create and stage multiple files |
| 169 | + for i in 1..=3 { |
| 170 | + let file_path = temp_dir.path().join(format!("file{i}.txt")); |
| 171 | + fs::write(&file_path, format!("Content {i}")) |
| 172 | + .expect("Failed to write multiple staged file"); |
| 173 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 174 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 175 | + index |
| 176 | + .add_path(Path::new(&format!("file{i}.txt"))) |
| 177 | + .expect("Failed to add multiple staged file to index"); |
| 178 | + index.write().expect("Failed to write index"); |
| 179 | + } |
| 180 | + |
| 181 | + let context = git_repo |
| 182 | + .get_git_info(&config) |
| 183 | + .await |
| 184 | + .expect("Failed to get git info"); |
| 185 | + assert_eq!(context.staged_files.len(), 3); |
| 186 | + for i in 1..=3 { |
| 187 | + assert!( |
| 188 | + context |
| 189 | + .staged_files |
| 190 | + .iter() |
| 191 | + .any(|file| file.path == format!("file{i}.txt")) |
| 192 | + ); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +#[tokio::test] |
| 197 | +async fn test_modified_file() { |
| 198 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 199 | + let config = Config::default(); |
| 200 | + |
| 201 | + // Modify the initial file |
| 202 | + let initial_file_path = temp_dir.path().join("initial.txt"); |
| 203 | + fs::write(&initial_file_path, "Modified content").expect("Failed to modify file content"); |
| 204 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 205 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 206 | + index |
| 207 | + .add_path(Path::new("initial.txt")) |
| 208 | + .expect("Failed to add modified file to index"); |
| 209 | + index.write().expect("Failed to write index"); |
| 210 | + |
| 211 | + let context = git_repo |
| 212 | + .get_git_info(&config) |
| 213 | + .await |
| 214 | + .expect("Failed to get git info"); |
| 215 | + assert_eq!(context.staged_files.len(), 1); |
| 216 | + assert!( |
| 217 | + context |
| 218 | + .staged_files |
| 219 | + .iter() |
| 220 | + .any(|file| file.path == "initial.txt" |
| 221 | + && matches!(file.change_type, ChangeType::Modified)) |
| 222 | + ); |
| 223 | +} |
| 224 | + |
| 225 | +#[tokio::test] |
| 226 | +async fn test_deleted_file() { |
| 227 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 228 | + let config = Config::default(); |
| 229 | + |
| 230 | + // Delete the initial file |
| 231 | + let initial_file_path = temp_dir.path().join("initial.txt"); |
| 232 | + fs::remove_file(&initial_file_path).expect("Failed to remove initial file"); |
| 233 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 234 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 235 | + index |
| 236 | + .remove_path(Path::new("initial.txt")) |
| 237 | + .expect("Failed to remove file from index"); |
| 238 | + index.write().expect("Failed to write index"); |
| 239 | + |
| 240 | + let context = git_repo |
| 241 | + .get_git_info(&config) |
| 242 | + .await |
| 243 | + .expect("Failed to get git info"); |
| 244 | + assert_eq!(context.staged_files.len(), 1); |
| 245 | + assert!( |
| 246 | + context |
| 247 | + .staged_files |
| 248 | + .iter() |
| 249 | + .any(|file| file.path == "initial.txt" |
| 250 | + && matches!(file.change_type, ChangeType::Deleted)) |
| 251 | + ); |
| 252 | +} |
| 253 | + |
| 254 | +#[tokio::test] |
| 255 | +async fn test_binary_file() { |
| 256 | + let (temp_dir, git_repo) = setup_git_repo(); |
| 257 | + let config = Config::default(); |
| 258 | + |
| 259 | + // Create a binary file (a simple PNG file) |
| 260 | + let binary_file_path = temp_dir.path().join("image.png"); |
| 261 | + let binary_content = [ |
| 262 | + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, |
| 263 | + 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, |
| 264 | + 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, |
| 265 | + 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, |
| 266 | + 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, |
| 267 | + ]; |
| 268 | + fs::write(&binary_file_path, binary_content).expect("Failed to write binary file"); |
| 269 | + |
| 270 | + // Stage the binary file |
| 271 | + let repo = Repository::open(temp_dir.path()).expect("Failed to open repository"); |
| 272 | + let mut index = repo.index().expect("Failed to get repository index"); |
| 273 | + index |
| 274 | + .add_path(Path::new("image.png")) |
| 275 | + .expect("Failed to add binary file to index"); |
| 276 | + index.write().expect("Failed to write index"); |
| 277 | + |
| 278 | + let context = git_repo |
| 279 | + .get_git_info(&config) |
| 280 | + .await |
| 281 | + .expect("Failed to get git info"); |
| 282 | + |
| 283 | + // Check if the binary file is in staged files |
| 284 | + assert!( |
| 285 | + context |
| 286 | + .staged_files |
| 287 | + .iter() |
| 288 | + .any(|file| file.path == "image.png") |
| 289 | + ); |
| 290 | + |
| 291 | + // Check if the diff for the binary file is "[Binary file changed]" |
| 292 | + let binary_file = context |
| 293 | + .staged_files |
| 294 | + .iter() |
| 295 | + .find(|file| file.path == "image.png") |
| 296 | + .expect("Failed to find binary file in staged files"); |
| 297 | + assert_eq!(binary_file.diff, "[Binary file changed]"); |
| 298 | + |
| 299 | + // Check if the status is correct |
| 300 | + assert!(matches!(binary_file.change_type, ChangeType::Added)); |
| 301 | +} |
0 commit comments