]> git.proxmox.com Git - cargo.git/commitdiff
Fix test escaping __CARGO_TEST_ROOT
authorasdf <foo@example.com>
Wed, 2 Dec 2020 05:58:32 +0000 (21:58 -0800)
committerasdf <foo@example.com>
Wed, 2 Dec 2020 05:58:32 +0000 (21:58 -0800)
src/cargo/ops/cargo_new.rs
tests/testsuite/new.rs

index 5e994f1faf7155538488d134047aa94a2454f981..e80f9edb6cf20f683ba9eca4417d9238b4d2c7ef 100644 (file)
@@ -835,20 +835,21 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
 
 fn find_git_config(path: &Path) -> Option<GitConfig> {
     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<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_tests_git_config(path: &Path) -> Option<GitConfig> {
+    // 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<GitConfig> {
index b1122d5ea524f0f53273a354130688e0dcd77324..1146183195f55b8ae35209d68e8f61555eb5d7ea 100644 (file)
@@ -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 <bar>"]"#), contents,);