Skip to content

Commit

Permalink
Add a special case for git config discovery inside tests
Browse files Browse the repository at this point in the history
Fixes #7469: Some tests will fail if one have a local git config user.name/user.email
  • Loading branch information
aleksator committed Feb 28, 2020
1 parent e618d47 commit 0f51c48
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,9 @@ fn get_environment_variable(variables: &[&str]) -> Option<String> {
}

fn discover_author() -> CargoResult<(String, Option<String>)> {
let cwd = env::current_dir()?;
let git_config = if let Ok(repo) = GitRepository::discover(&cwd) {
repo.config()
.ok()
.or_else(|| GitConfig::open_default().ok())
} else {
GitConfig::open_default().ok()
};
let git_config = find_git_config();
let git_config = git_config.as_ref();

let name_variables = [
"CARGO_NAME",
"GIT_AUTHOR_NAME",
Expand Down Expand Up @@ -797,3 +791,31 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {

Ok((name, email))
}

fn find_git_config() -> Option<GitConfig> {
match env::var("__CARGO_TEST_ROOT") {
Ok(test_root) => find_tests_git_config(test_root),
Err(_) => find_real_git_config(),
}
}

fn find_tests_git_config(cargo_test_root: String) -> Option<GitConfig> {
// Path where 'git config --local' puts variables when run from inside a test
let test_git_config = PathBuf::from(cargo_test_root).join(".git").join("config");

if test_git_config.exists() {
GitConfig::open(&test_git_config).ok()
} else {
GitConfig::open_default().ok()
}
}

fn find_real_git_config() -> Option<GitConfig> {
match env::current_dir() {
Ok(cwd) => GitRepository::discover(cwd)
.and_then(|repo| repo.config())
.or_else(|_| GitConfig::open_default())
.ok(),
Err(_) => GitConfig::open_default().ok(),
}
}

0 comments on commit 0f51c48

Please sign in to comment.