diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 951c5e9ba1e..97e657c506b 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -741,15 +741,9 @@ fn get_environment_variable(variables: &[&str]) -> Option { } fn discover_author() -> CargoResult<(String, Option)> { - 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", @@ -797,3 +791,31 @@ fn discover_author() -> CargoResult<(String, Option)> { Ok((name, email)) } + +fn find_git_config() -> Option { + 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 { + // 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 { + 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(), + } +}