|
1 | 1 | #[path = "common/mod.rs"] |
2 | 2 | pub mod common; |
3 | 3 |
|
| 4 | +use std::fs; |
| 5 | + |
4 | 6 | use common::{ |
5 | 7 | create_new_file, first_commit_all, generate_path_to_repo, get_current_branch_name, |
6 | 8 | run_test_bin_expect_err, setup_git_repo, teardown_git_repo, |
@@ -31,3 +33,55 @@ fn no_subcommand() { |
31 | 33 |
|
32 | 34 | teardown_git_repo(repo_name); |
33 | 35 | } |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn not_a_git_repo() { |
| 39 | + // Create a directory in the system temp location to avoid finding parent git repos |
| 40 | + let temp_dir = std::env::temp_dir(); |
| 41 | + let path_to_non_git_dir = temp_dir.join("git_chain_test_not_a_repo"); |
| 42 | + |
| 43 | + // Create a directory that is NOT a git repository |
| 44 | + fs::remove_dir_all(&path_to_non_git_dir).ok(); |
| 45 | + fs::create_dir_all(&path_to_non_git_dir).unwrap(); |
| 46 | + |
| 47 | + // Run git chain in the non-git directory |
| 48 | + let args: Vec<String> = vec![]; |
| 49 | + let output = run_test_bin_expect_err(&path_to_non_git_dir, args); |
| 50 | + |
| 51 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 52 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 53 | + |
| 54 | + // Diagnostic printing |
| 55 | + println!("=== TEST DIAGNOSTICS ==="); |
| 56 | + println!("Test directory: {:?}", path_to_non_git_dir); |
| 57 | + println!("STDOUT: {}", stdout); |
| 58 | + println!("STDERR: {}", stderr); |
| 59 | + println!("EXIT STATUS: {}", output.status); |
| 60 | + println!("Is directory a git repo: false (intentional test condition)"); |
| 61 | + println!("======"); |
| 62 | + |
| 63 | + // Uncomment to stop test execution and inspect state with captured output |
| 64 | + // assert!(false, "DEBUG STOP: not_a_git_repo test section"); |
| 65 | + // assert!(false, "stdout: {}", stdout); |
| 66 | + // assert!(false, "stderr: {}", stderr); |
| 67 | + // assert!(false, "status code: {}", output.status.code().unwrap_or(0)); |
| 68 | + |
| 69 | + // Specific assertions based on expected behavior |
| 70 | + assert!( |
| 71 | + !output.status.success(), |
| 72 | + "Command should fail when run in non-git directory" |
| 73 | + ); |
| 74 | + assert!( |
| 75 | + stderr.contains("Not a git repository"), |
| 76 | + "Error message should mention 'Not a git repository', got: {}", |
| 77 | + stderr |
| 78 | + ); |
| 79 | + assert!( |
| 80 | + stderr.contains("This command must be run inside a git repository"), |
| 81 | + "Error message should provide helpful hint, got: {}", |
| 82 | + stderr |
| 83 | + ); |
| 84 | + |
| 85 | + // Clean up |
| 86 | + fs::remove_dir_all(&path_to_non_git_dir).ok(); |
| 87 | +} |
0 commit comments