|
1 | | -use super::{DockerGit, TestDir}; |
| 1 | +use super::{DockerGit, TestDir, should_use_native_git}; |
2 | 2 | use crate::vcs::{Vcs, VcsData, git::GitVcs}; |
| 3 | +use std::fs; |
| 4 | +use std::process::Command; |
3 | 5 | use std::sync::OnceLock; |
4 | 6 |
|
5 | 7 | static SEMVER_VCS_DATA: OnceLock<VcsData> = OnceLock::new(); |
6 | 8 | static PEP440_VCS_DATA: OnceLock<VcsData> = OnceLock::new(); |
7 | 9 |
|
8 | 10 | fn create_vcs_data_with_tag(tag: &str, filename: &str, content: &str, commit_msg: &str) -> VcsData { |
9 | 11 | let test_dir = TestDir::new().expect("Failed to create test dir"); |
| 12 | + |
| 13 | + if should_use_native_git() { |
| 14 | + create_vcs_data_with_native_git(&test_dir, tag, filename, content, commit_msg) |
| 15 | + } else { |
| 16 | + create_vcs_data_with_docker(&test_dir, tag, filename, content, commit_msg) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +fn create_vcs_data_with_native_git( |
| 21 | + test_dir: &TestDir, |
| 22 | + tag: &str, |
| 23 | + filename: &str, |
| 24 | + content: &str, |
| 25 | + commit_msg: &str, |
| 26 | +) -> VcsData { |
| 27 | + let path = test_dir.path(); |
| 28 | + |
| 29 | + // Create initial file |
| 30 | + fs::write(path.join("README.md"), "# Test Repository").expect("Failed to create README"); |
| 31 | + |
| 32 | + // Use isolated Git config |
| 33 | + let git_cmd = |args: &[&str]| { |
| 34 | + Command::new("git") |
| 35 | + .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 36 | + .env("GIT_CONFIG_SYSTEM", "/dev/null") |
| 37 | + .args(args) |
| 38 | + .current_dir(path) |
| 39 | + .output() |
| 40 | + .expect("should execute git command") |
| 41 | + }; |
| 42 | + |
| 43 | + // Initialize repo and create initial commit |
| 44 | + let commands = [ |
| 45 | + &["init"][..], |
| 46 | + &["config", "user.name", "Test User"], |
| 47 | + &["config", "user.email", "test@example.com"], |
| 48 | + &["add", "."], |
| 49 | + &["commit", "-m", "Initial commit"], |
| 50 | + &["tag", tag], |
| 51 | + ]; |
| 52 | + |
| 53 | + for args in commands { |
| 54 | + let output = git_cmd(args); |
| 55 | + assert!( |
| 56 | + output.status.success(), |
| 57 | + "Git command '{:?}' failed: {}", |
| 58 | + args, |
| 59 | + String::from_utf8_lossy(&output.stderr) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // Create additional file and commit |
| 64 | + test_dir |
| 65 | + .create_file(filename, content) |
| 66 | + .expect("Failed to create file"); |
| 67 | + |
| 68 | + let output = git_cmd(&["add", "."]); |
| 69 | + assert!( |
| 70 | + output.status.success(), |
| 71 | + "Git add failed: {}", |
| 72 | + String::from_utf8_lossy(&output.stderr) |
| 73 | + ); |
| 74 | + |
| 75 | + let output = git_cmd(&["commit", "-m", commit_msg]); |
| 76 | + assert!( |
| 77 | + output.status.success(), |
| 78 | + "Git commit failed: {}", |
| 79 | + String::from_utf8_lossy(&output.stderr) |
| 80 | + ); |
| 81 | + |
| 82 | + let git_vcs = GitVcs::new(test_dir.path()).expect("Failed to create GitVcs"); |
| 83 | + git_vcs.get_vcs_data().expect("Failed to get VCS data") |
| 84 | +} |
| 85 | + |
| 86 | +fn create_vcs_data_with_docker( |
| 87 | + test_dir: &TestDir, |
| 88 | + tag: &str, |
| 89 | + filename: &str, |
| 90 | + content: &str, |
| 91 | + commit_msg: &str, |
| 92 | +) -> VcsData { |
10 | 93 | let docker_git = DockerGit::new(); |
11 | 94 |
|
| 95 | + docker_git.init_repo(test_dir).expect("Failed to init repo"); |
12 | 96 | docker_git |
13 | | - .init_repo(&test_dir) |
14 | | - .expect("Failed to init repo"); |
15 | | - docker_git |
16 | | - .create_tag(&test_dir, tag) |
| 97 | + .create_tag(test_dir, tag) |
17 | 98 | .expect("Failed to create tag"); |
18 | 99 |
|
19 | 100 | test_dir |
20 | 101 | .create_file(filename, content) |
21 | 102 | .expect("Failed to create file"); |
22 | 103 | docker_git |
23 | | - .create_commit(&test_dir, commit_msg) |
| 104 | + .create_commit(test_dir, commit_msg) |
24 | 105 | .expect("Failed to create commit"); |
25 | 106 |
|
26 | 107 | let git_vcs = GitVcs::new(test_dir.path()).expect("Failed to create GitVcs"); |
|
0 commit comments