Fix test escaping __CARGO_TEST_ROOT

This commit is contained in:
asdf 2020-12-01 21:58:32 -08:00
parent 63d0fe4344
commit c3e01b8fe3
2 changed files with 11 additions and 13 deletions

View 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> {

View 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,);