Skip to content

Commit

Permalink
Auto merge of #3869 - matklad:test-isolation, r=alexcrichton
Browse files Browse the repository at this point in the history
Don't read ~/.cargo/config in tests

Closes #3863

r? @alexcrichton
  • Loading branch information
bors committed Mar 27, 2017
2 parents e60ae3d + 290f6ab commit afeed88
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ impl<'cfg> Workspace<'cfg> {
}
}

let mut cur = manifest_path.parent().and_then(|p| p.parent());
while let Some(path) = cur {
for path in paths::ancestors(manifest_path).skip(2) {
let manifest = path.join("Cargo.toml");
debug!("find_root - trying {}", manifest.display());
if manifest.exists() {
Expand All @@ -286,7 +285,6 @@ impl<'cfg> Workspace<'cfg> {
WorkspaceConfig::Member { .. } => {}
}
}
cur = path.parent();
}

Ok(None)
Expand Down
9 changes: 2 additions & 7 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use core::shell::{Verbosity, ColorConfig};
use core::MultiShell;
use util::{CargoResult, CargoError, ChainError, Rustc, internal, human};
use util::{Filesystem, LazyCell};
use util::paths;

use util::toml as cargo_toml;

Expand Down Expand Up @@ -698,10 +699,9 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
where F: FnMut(File, &Path) -> CargoResult<()>
{
let mut current = pwd;
let mut stash: HashSet<PathBuf> = HashSet::new();

loop {
for current in paths::ancestors(pwd) {
let possible = current.join(".cargo").join("config");
if fs::metadata(&possible).is_ok() {
let file = File::open(&possible)?;
Expand All @@ -710,11 +710,6 @@ fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>

stash.insert(possible);
}

match current.parent() {
Some(p) => current = p,
None => break,
}
}

// Once we're done, also be sure to walk the home directory even if it's not
Expand Down
39 changes: 39 additions & 0 deletions src/cargo/util/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,42 @@ pub fn bytes2path(bytes: &[u8]) -> CargoResult<PathBuf> {
Err(..) => Err(human("invalid non-unicode path")),
}
}

pub fn ancestors(path: &Path) -> PathAncestors {
PathAncestors::new(path)
}

pub struct PathAncestors<'a> {
current: Option<&'a Path>,
stop_at: Option<PathBuf>
}

impl<'a> PathAncestors<'a> {
fn new(path: &Path) -> PathAncestors {
PathAncestors {
current: Some(path),
//HACK: avoid reading `~/.cargo/config` when testing Cargo itself.
stop_at: env::var("__CARGO_TEST_ROOT").ok().map(PathBuf::from),
}
}
}

impl<'a> Iterator for PathAncestors<'a> {
type Item = &'a Path;

fn next(&mut self) -> Option<&'a Path> {
if let Some(path) = self.current {
self.current = path.parent();

if let Some(ref stop_at) = self.stop_at {
if path == stop_at {
self.current = None;
}
}

Some(path)
} else {
None
}
}
}
1 change: 1 addition & 0 deletions tests/cargotest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn _process(t: &OsStr) -> cargo::util::ProcessBuilder {
.env_remove("CARGO_HOME")
.env("HOME", support::paths::home())
.env("CARGO_HOME", support::paths::home().join(".cargo"))
.env("__CARGO_TEST_ROOT", support::paths::root())
.env_remove("RUSTC")
.env_remove("RUSTFLAGS")
.env_remove("CARGO_INCREMENTAL")
Expand Down

0 comments on commit afeed88

Please sign in to comment.