@@ -21,6 +21,7 @@ pub struct ConfigBuilder {
21
21
unstable : Vec < String > ,
22
22
config_args : Vec < String > ,
23
23
cwd : Option < PathBuf > ,
24
+ root : Option < PathBuf > ,
24
25
enable_nightly_features : bool ,
25
26
}
26
27
@@ -30,6 +31,7 @@ impl ConfigBuilder {
30
31
env : HashMap :: new ( ) ,
31
32
unstable : Vec :: new ( ) ,
32
33
config_args : Vec :: new ( ) ,
34
+ root : None ,
33
35
cwd : None ,
34
36
enable_nightly_features : false ,
35
37
}
@@ -60,8 +62,28 @@ impl ConfigBuilder {
60
62
}
61
63
62
64
/// Sets the current working directory where config files will be loaded.
65
+ ///
66
+ /// Default is the root from [`ConfigBuilder::root`] or [`paths::root`].
63
67
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 ( ) ) ;
65
87
self
66
88
}
67
89
@@ -72,14 +94,15 @@ impl ConfigBuilder {
72
94
73
95
/// Creates the `Config`, returning a Result.
74
96
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 ( ) ) ;
76
99
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" ) ;
79
102
let mut config = Config :: new ( shell, cwd, homedir) ;
80
103
config. nightly_features_allowed = self . enable_nightly_features || !self . unstable . is_empty ( ) ;
81
104
config. set_env ( self . env . clone ( ) ) ;
82
- config. set_search_stop_path ( paths :: root ( ) ) ;
105
+ config. set_search_stop_path ( & root) ;
83
106
config. configure (
84
107
0 ,
85
108
false ,
0 commit comments