|
| 1 | +use zerv::test_utils::{ |
| 2 | + GitRepoFixture, |
| 3 | + TestDir, |
| 4 | + should_run_docker_tests, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::util::TestCommand; |
| 8 | + |
| 9 | +mod directory_git_integration { |
| 10 | + use super::*; |
| 11 | + |
| 12 | + #[test] |
| 13 | + fn test_directory_flag_with_subdirectory() { |
| 14 | + if !should_run_docker_tests() { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + let git_repo = |
| 19 | + GitRepoFixture::tagged("v1.0.0").expect("Failed to create tagged Git repository"); |
| 20 | + |
| 21 | + let parent_dir = git_repo |
| 22 | + .path() |
| 23 | + .parent() |
| 24 | + .expect("Git repo should have parent directory"); |
| 25 | + |
| 26 | + let output = TestCommand::new() |
| 27 | + .current_dir(parent_dir) |
| 28 | + .args_from_str(format!( |
| 29 | + "version -C {} --source git --output-format semver", |
| 30 | + git_repo.path().file_name().unwrap().to_string_lossy() |
| 31 | + )) |
| 32 | + .assert_success(); |
| 33 | + |
| 34 | + assert_eq!( |
| 35 | + output.stdout().trim(), |
| 36 | + "1.0.0", |
| 37 | + "Should detect version from Git repo in subdirectory using -C flag" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn test_directory_flag_relative_vs_absolute() { |
| 43 | + if !should_run_docker_tests() { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + let git_repo = |
| 48 | + GitRepoFixture::tagged("v1.0.0").expect("Failed to create tagged Git repository"); |
| 49 | + |
| 50 | + let relative_output = TestCommand::new() |
| 51 | + .current_dir(git_repo.path().parent().unwrap()) |
| 52 | + .args_from_str(format!( |
| 53 | + "version -C {} --source git --output-format semver", |
| 54 | + git_repo.path().file_name().unwrap().to_string_lossy() |
| 55 | + )) |
| 56 | + .assert_success(); |
| 57 | + |
| 58 | + let absolute_output = TestCommand::new() |
| 59 | + .args_from_str(format!( |
| 60 | + "version -C {} --source git --output-format semver", |
| 61 | + git_repo.path().display() |
| 62 | + )) |
| 63 | + .assert_success(); |
| 64 | + |
| 65 | + assert_eq!( |
| 66 | + relative_output.stdout().trim(), |
| 67 | + "1.0.0", |
| 68 | + "Relative path should work" |
| 69 | + ); |
| 70 | + assert_eq!( |
| 71 | + absolute_output.stdout().trim(), |
| 72 | + "1.0.0", |
| 73 | + "Absolute path should work" |
| 74 | + ); |
| 75 | + assert_eq!( |
| 76 | + relative_output.stdout(), |
| 77 | + absolute_output.stdout(), |
| 78 | + "Relative and absolute paths should produce identical output" |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +mod directory_error_handling { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_directory_flag_nonexistent_path() { |
| 88 | + let output = TestCommand::new() |
| 89 | + .args_from_str("version -C /nonexistent/path/to/directory") |
| 90 | + .assert_failure(); |
| 91 | + |
| 92 | + let stderr = output.stderr(); |
| 93 | + assert!( |
| 94 | + stderr.contains("Error") && stderr.contains("VCS not found"), |
| 95 | + "Should show VCS not found error when directory doesn't exist. Got: {stderr}" |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_directory_flag_exists_but_not_git() { |
| 101 | + let test_dir = TestDir::new().expect("Failed to create test directory"); |
| 102 | + |
| 103 | + let output = TestCommand::new() |
| 104 | + .args_from_str(format!("version -C {}", test_dir.path().display())) |
| 105 | + .assert_failure(); |
| 106 | + |
| 107 | + let stderr = output.stderr(); |
| 108 | + assert_eq!( |
| 109 | + stderr.trim(), |
| 110 | + "Error: VCS not found: Not in a git repository (--source git)", |
| 111 | + "Should show proper error when directory exists but is not a git repo" |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments