From: asdf Date: Wed, 2 Dec 2020 05:58:32 +0000 (-0800) Subject: Fix test escaping __CARGO_TEST_ROOT X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=c3e01b8fe330d18ba1f88d1667a9198e40d5fa10;p=cargo.git Fix test escaping __CARGO_TEST_ROOT --- diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 5e994f1fa..e80f9edb6 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -835,20 +835,21 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option)> { fn find_git_config(path: &Path) -> Option { match env::var("__CARGO_TEST_ROOT") { - Ok(test_root) => find_tests_git_config(test_root), + Ok(_) => find_tests_git_config(path), Err(_) => find_real_git_config(path), } } -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_tests_git_config(path: &Path) -> Option { + // Don't escape the test sandbox when looking for a git repository. + // NOTE: libgit2 has support to define the path ceiling in + // git_repository_discover, but the git2 bindings do not expose that. + for path in paths::ancestors(path) { + if let Ok(repo) = GitRepository::open(path) { + return Some(repo.config().expect("test repo should have valid config")); + } } + GitConfig::open_default().ok() } fn find_real_git_config(path: &Path) -> Option { diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index b1122d5ea..114618319 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -340,10 +340,7 @@ fn finds_git_author_in_included_config() { ) .unwrap(); - cargo_process("new foo/bar") - // Avoid the special treatment of tests to find git configuration - .env_remove("__CARGO_TEST_ROOT") - .run(); + cargo_process("new foo/bar").run(); let toml = paths::root().join("foo/bar/Cargo.toml"); let contents = fs::read_to_string(&toml).unwrap(); assert!(contents.contains(r#"authors = ["foo "]"#), contents,);