|
| 1 | +// Integration tests for health.rs run() function testing all code paths |
| 2 | + |
| 3 | +use assert_cmd::Command; |
| 4 | +use std::process::Command as StdCommand; |
| 5 | +use tempfile::TempDir; |
| 6 | + |
| 7 | +mod common; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_health_run_outside_git_repo() { |
| 11 | + // Test error path: not in a git repository |
| 12 | + let temp_dir = TempDir::new().unwrap(); |
| 13 | + |
| 14 | + let output = Command::cargo_bin("git-x") |
| 15 | + .unwrap() |
| 16 | + .current_dir(temp_dir.path()) |
| 17 | + .args(["health"]) |
| 18 | + .output() |
| 19 | + .unwrap(); |
| 20 | + |
| 21 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 22 | + |
| 23 | + // Should show health check header and not in git repo message |
| 24 | + assert!(stdout.contains("Repository Health Check")); |
| 25 | + assert!(stdout.contains("✗ Not in a Git repository")); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_health_run_clean_repo() { |
| 30 | + // Test success path: clean repository |
| 31 | + let repo = common::basic_repo(); |
| 32 | + |
| 33 | + let output = Command::cargo_bin("git-x") |
| 34 | + .unwrap() |
| 35 | + .current_dir(repo.path()) |
| 36 | + .args(["health"]) |
| 37 | + .output() |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 41 | + |
| 42 | + // Should show health check components |
| 43 | + assert!(stdout.contains("Repository Health Check")); |
| 44 | + assert!(stdout.contains("Working directory")); |
| 45 | + assert!(stdout.contains("untracked files")); |
| 46 | + assert!(stdout.contains("Health check complete!")); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_health_run_dirty_repo() { |
| 51 | + // Test path: repository with changes |
| 52 | + let repo = common::basic_repo(); |
| 53 | + |
| 54 | + // Make some changes to make the repo dirty |
| 55 | + std::fs::write(repo.path().join("README.md"), "# modified test").unwrap(); |
| 56 | + |
| 57 | + let output = Command::cargo_bin("git-x") |
| 58 | + .unwrap() |
| 59 | + .current_dir(repo.path()) |
| 60 | + .args(["health"]) |
| 61 | + .output() |
| 62 | + .unwrap(); |
| 63 | + |
| 64 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 65 | + |
| 66 | + // Should show health check with dirty status |
| 67 | + assert!(stdout.contains("Repository Health Check")); |
| 68 | + assert!(stdout.contains("✗ Working directory has changes")); |
| 69 | + assert!(stdout.contains("Health check complete!")); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_health_run_with_untracked_files() { |
| 74 | + // Test path: repository with untracked files |
| 75 | + let repo = common::basic_repo(); |
| 76 | + |
| 77 | + // Add untracked files |
| 78 | + std::fs::write(repo.path().join("untracked1.txt"), "untracked content 1").unwrap(); |
| 79 | + std::fs::write(repo.path().join("untracked2.txt"), "untracked content 2").unwrap(); |
| 80 | + |
| 81 | + let output = Command::cargo_bin("git-x") |
| 82 | + .unwrap() |
| 83 | + .current_dir(repo.path()) |
| 84 | + .args(["health"]) |
| 85 | + .output() |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 89 | + |
| 90 | + // Should show health check with untracked files |
| 91 | + assert!(stdout.contains("Repository Health Check")); |
| 92 | + assert!(stdout.contains("untracked files found") || stdout.contains("No untracked files")); |
| 93 | + assert!(stdout.contains("Health check complete!")); |
| 94 | +} |
| 95 | + |
| 96 | +#[test] |
| 97 | +fn test_health_run_with_staged_changes() { |
| 98 | + // Test path: repository with staged changes |
| 99 | + let repo = common::basic_repo(); |
| 100 | + |
| 101 | + // Add and stage a file |
| 102 | + std::fs::write(repo.path().join("staged_file.txt"), "staged content").unwrap(); |
| 103 | + StdCommand::new("git") |
| 104 | + .args(["add", "staged_file.txt"]) |
| 105 | + .current_dir(repo.path()) |
| 106 | + .output() |
| 107 | + .unwrap(); |
| 108 | + |
| 109 | + let output = Command::cargo_bin("git-x") |
| 110 | + .unwrap() |
| 111 | + .current_dir(repo.path()) |
| 112 | + .args(["health"]) |
| 113 | + .output() |
| 114 | + .unwrap(); |
| 115 | + |
| 116 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 117 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 118 | + |
| 119 | + // Should show health check with staged changes or handle git errors gracefully |
| 120 | + if stdout.contains("Repository Health Check") { |
| 121 | + assert!(stdout.contains("files staged for commit") || stdout.contains("No staged changes")); |
| 122 | + assert!(stdout.contains("Health check complete!")); |
| 123 | + } else if stderr.contains("Git command failed") { |
| 124 | + eprintln!( |
| 125 | + "Note: Git command failed in test environment - this is expected in some CI environments" |
| 126 | + ); |
| 127 | + } else { |
| 128 | + panic!("Expected either health check output or git command failure"); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn test_health_run_repo_size_check() { |
| 134 | + // Test path: repository size check |
| 135 | + let repo = common::basic_repo(); |
| 136 | + |
| 137 | + let output = Command::cargo_bin("git-x") |
| 138 | + .unwrap() |
| 139 | + .current_dir(repo.path()) |
| 140 | + .args(["health"]) |
| 141 | + .output() |
| 142 | + .unwrap(); |
| 143 | + |
| 144 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 145 | + |
| 146 | + // Should show health check with repository size |
| 147 | + assert!(stdout.contains("Repository Health Check")); |
| 148 | + assert!(stdout.contains("Repository size:")); |
| 149 | + // Should show healthy since it's a small test repo |
| 150 | + assert!(stdout.contains("healthy") || stdout.contains("moderate")); |
| 151 | + assert!(stdout.contains("Health check complete!")); |
| 152 | +} |
| 153 | + |
| 154 | +#[test] |
| 155 | +fn test_health_run_comprehensive_output() { |
| 156 | + // Test that all output components are present in success case |
| 157 | + let repo = common::basic_repo(); |
| 158 | + |
| 159 | + let output = Command::cargo_bin("git-x") |
| 160 | + .unwrap() |
| 161 | + .current_dir(repo.path()) |
| 162 | + .args(["health"]) |
| 163 | + .output() |
| 164 | + .unwrap(); |
| 165 | + |
| 166 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 167 | + |
| 168 | + // Should contain all expected health check components |
| 169 | + assert!(stdout.contains("Repository Health Check")); |
| 170 | + assert!(stdout.contains("=========================")); |
| 171 | + assert!(stdout.contains("Working directory")); // Status check |
| 172 | + assert!(stdout.contains("untracked files")); // Untracked files check |
| 173 | + assert!(stdout.contains("stale branches")); // Stale branches check |
| 174 | + assert!(stdout.contains("Repository size:")); // Repository size check |
| 175 | + assert!(stdout.contains("staged")); // Staged changes check |
| 176 | + assert!(stdout.contains("Health check complete!")); |
| 177 | + |
| 178 | + // Should contain status indicators (✓, !, or ✗) |
| 179 | + assert!(stdout.contains("✓") || stdout.contains("!") || stdout.contains("✗")); |
| 180 | +} |
| 181 | + |
| 182 | +#[test] |
| 183 | +fn test_health_run_mixed_states() { |
| 184 | + // Test comprehensive scenario with multiple states |
| 185 | + let repo = common::basic_repo(); |
| 186 | + |
| 187 | + // Create mixed scenario: |
| 188 | + // 1. Untracked files |
| 189 | + std::fs::write(repo.path().join("untracked.txt"), "untracked").unwrap(); |
| 190 | + |
| 191 | + // 2. Modified files |
| 192 | + std::fs::write(repo.path().join("README.md"), "# modified").unwrap(); |
| 193 | + |
| 194 | + // 3. Staged files |
| 195 | + std::fs::write(repo.path().join("staged.txt"), "staged content").unwrap(); |
| 196 | + StdCommand::new("git") |
| 197 | + .args(["add", "staged.txt"]) |
| 198 | + .current_dir(repo.path()) |
| 199 | + .output() |
| 200 | + .unwrap(); |
| 201 | + |
| 202 | + let output = Command::cargo_bin("git-x") |
| 203 | + .unwrap() |
| 204 | + .current_dir(repo.path()) |
| 205 | + .args(["health"]) |
| 206 | + .output() |
| 207 | + .unwrap(); |
| 208 | + |
| 209 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 210 | + |
| 211 | + // Should show health check with mixed states |
| 212 | + assert!(stdout.contains("Repository Health Check")); |
| 213 | + assert!(stdout.contains("Working directory")); |
| 214 | + assert!(stdout.contains("untracked files")); |
| 215 | + assert!(stdout.contains("staged")); |
| 216 | + assert!(stdout.contains("Health check complete!")); |
| 217 | +} |
0 commit comments