Skip to content

Commit 6256eca

Browse files
committed
Auto merge of #7944 - aleksator:7469_git_config_discovery, r=ehuss
Add a special case for git config discovery inside tests Fixes #7469: Some tests will fail if one have a local git config user.name/user.email
2 parents 443e276 + 0f51c48 commit 6256eca

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,15 +741,9 @@ fn get_environment_variable(variables: &[&str]) -> Option<String> {
741741
}
742742

743743
fn discover_author() -> CargoResult<(String, Option<String>)> {
744-
let cwd = env::current_dir()?;
745-
let git_config = if let Ok(repo) = GitRepository::discover(&cwd) {
746-
repo.config()
747-
.ok()
748-
.or_else(|| GitConfig::open_default().ok())
749-
} else {
750-
GitConfig::open_default().ok()
751-
};
744+
let git_config = find_git_config();
752745
let git_config = git_config.as_ref();
746+
753747
let name_variables = [
754748
"CARGO_NAME",
755749
"GIT_AUTHOR_NAME",
@@ -797,3 +791,31 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
797791

798792
Ok((name, email))
799793
}
794+
795+
fn find_git_config() -> Option<GitConfig> {
796+
match env::var("__CARGO_TEST_ROOT") {
797+
Ok(test_root) => find_tests_git_config(test_root),
798+
Err(_) => find_real_git_config(),
799+
}
800+
}
801+
802+
fn find_tests_git_config(cargo_test_root: String) -> Option<GitConfig> {
803+
// Path where 'git config --local' puts variables when run from inside a test
804+
let test_git_config = PathBuf::from(cargo_test_root).join(".git").join("config");
805+
806+
if test_git_config.exists() {
807+
GitConfig::open(&test_git_config).ok()
808+
} else {
809+
GitConfig::open_default().ok()
810+
}
811+
}
812+
813+
fn find_real_git_config() -> Option<GitConfig> {
814+
match env::current_dir() {
815+
Ok(cwd) => GitRepository::discover(cwd)
816+
.and_then(|repo| repo.config())
817+
.or_else(|_| GitConfig::open_default())
818+
.ok(),
819+
Err(_) => GitConfig::open_default().ok(),
820+
}
821+
}

0 commit comments

Comments
 (0)