Skip to content

Commit b37c8f3

Browse files
committed
Add ConfigBuilder::root
This adds the `root` method to set the root directory to be able to override paths::root().
1 parent b85ef38 commit b37c8f3

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

tests/testsuite/config.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct ConfigBuilder {
2121
unstable: Vec<String>,
2222
config_args: Vec<String>,
2323
cwd: Option<PathBuf>,
24+
root: Option<PathBuf>,
2425
enable_nightly_features: bool,
2526
}
2627

@@ -30,6 +31,7 @@ impl ConfigBuilder {
3031
env: HashMap::new(),
3132
unstable: Vec::new(),
3233
config_args: Vec::new(),
34+
root: None,
3335
cwd: None,
3436
enable_nightly_features: false,
3537
}
@@ -60,8 +62,28 @@ impl ConfigBuilder {
6062
}
6163

6264
/// Sets the current working directory where config files will be loaded.
65+
///
66+
/// Default is the root from [`ConfigBuilder::root`] or [`paths::root`].
6367
pub fn cwd(&mut self, path: impl AsRef<Path>) -> &mut Self {
64-
self.cwd = Some(paths::root().join(path.as_ref()));
68+
let path = path.as_ref();
69+
let cwd = self
70+
.root
71+
.as_ref()
72+
.map_or_else(|| paths::root().join(path), |r| r.join(path));
73+
self.cwd = Some(cwd);
74+
self
75+
}
76+
77+
/// Sets the test root directory.
78+
///
79+
/// This generally should not be necessary. It is only useful if you want
80+
/// to create a `Config` from within a thread. Since Cargo's testsuite
81+
/// uses thread-local storage, this can be used to avoid accessing that
82+
/// thread-local storage.
83+
///
84+
/// Default is [`paths::root`].
85+
pub fn root(&mut self, path: impl Into<PathBuf>) -> &mut Self {
86+
self.root = Some(path.into());
6587
self
6688
}
6789

@@ -72,14 +94,15 @@ impl ConfigBuilder {
7294

7395
/// Creates the `Config`, returning a Result.
7496
pub fn build_err(&self) -> CargoResult<Config> {
75-
let output = Box::new(fs::File::create(paths::root().join("shell.out")).unwrap());
97+
let root = self.root.clone().unwrap_or_else(|| paths::root());
98+
let output = Box::new(fs::File::create(root.join("shell.out")).unwrap());
7699
let shell = Shell::from_write(output);
77-
let cwd = self.cwd.clone().unwrap_or_else(|| paths::root());
78-
let homedir = paths::home();
100+
let cwd = self.cwd.clone().unwrap_or_else(|| root.clone());
101+
let homedir = root.join("home").join(".cargo");
79102
let mut config = Config::new(shell, cwd, homedir);
80103
config.nightly_features_allowed = self.enable_nightly_features || !self.unstable.is_empty();
81104
config.set_env(self.env.clone());
82-
config.set_search_stop_path(paths::root());
105+
config.set_search_stop_path(&root);
83106
config.configure(
84107
0,
85108
false,

0 commit comments

Comments
 (0)