Skip to content

Commit c7939ae

Browse files
committed
Auto merge of #8929 - ehuss:fix-git-config-author, r=Eh2406
Fix test escaping __CARGO_TEST_ROOT #8886 added a test which unsets `__CARGO_TEST_ROOT`, but that environment variable is there for a reason. This causes problems as it causes that test to load the `.cargo/config` from the real home directory, which if it contains a `[cargo-new]` section, causes the test to fail. The fix here is to change `find_tests_git_config` so that it behaves more like the real git config loader, but avoids escaping the test sandbox. There are some subtle issues here, like #7469, which I believe should still work correctly.
2 parents 6bc510d + c3e01b8 commit c7939ae

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -835,20 +835,21 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
835835

836836
fn find_git_config(path: &Path) -> Option<GitConfig> {
837837
match env::var("__CARGO_TEST_ROOT") {
838-
Ok(test_root) => find_tests_git_config(test_root),
838+
Ok(_) => find_tests_git_config(path),
839839
Err(_) => find_real_git_config(path),
840840
}
841841
}
842842

843-
fn find_tests_git_config(cargo_test_root: String) -> Option<GitConfig> {
844-
// Path where 'git config --local' puts variables when run from inside a test
845-
let test_git_config = PathBuf::from(cargo_test_root).join(".git").join("config");
846-
847-
if test_git_config.exists() {
848-
GitConfig::open(&test_git_config).ok()
849-
} else {
850-
GitConfig::open_default().ok()
843+
fn find_tests_git_config(path: &Path) -> Option<GitConfig> {
844+
// Don't escape the test sandbox when looking for a git repository.
845+
// NOTE: libgit2 has support to define the path ceiling in
846+
// git_repository_discover, but the git2 bindings do not expose that.
847+
for path in paths::ancestors(path) {
848+
if let Ok(repo) = GitRepository::open(path) {
849+
return Some(repo.config().expect("test repo should have valid config"));
850+
}
851851
}
852+
GitConfig::open_default().ok()
852853
}
853854

854855
fn find_real_git_config(path: &Path) -> Option<GitConfig> {

tests/testsuite/new.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,7 @@ fn finds_git_author_in_included_config() {
340340
)
341341
.unwrap();
342342

343-
cargo_process("new foo/bar")
344-
// Avoid the special treatment of tests to find git configuration
345-
.env_remove("__CARGO_TEST_ROOT")
346-
.run();
343+
cargo_process("new foo/bar").run();
347344
let toml = paths::root().join("foo/bar/Cargo.toml");
348345
let contents = fs::read_to_string(&toml).unwrap();
349346
assert!(contents.contains(r#"authors = ["foo <bar>"]"#), contents,);

0 commit comments

Comments
 (0)