@@ -61,6 +61,7 @@ pub mod tools;
61
61
62
62
pub mod prelude {
63
63
pub use crate :: ChannelChanger ;
64
+ pub use crate :: TestEnv ;
64
65
}
65
66
66
67
/*
@@ -1101,75 +1102,7 @@ pub fn process<T: AsRef<OsStr>>(t: T) -> ProcessBuilder {
1101
1102
1102
1103
fn _process ( t : & OsStr ) -> ProcessBuilder {
1103
1104
let mut p = ProcessBuilder :: new ( t) ;
1104
-
1105
- // In general just clear out all cargo-specific configuration already in the
1106
- // environment. Our tests all assume a "default configuration" unless
1107
- // specified otherwise.
1108
- for ( k, _v) in env:: vars ( ) {
1109
- if k. starts_with ( "CARGO_" ) {
1110
- p. env_remove ( & k) ;
1111
- }
1112
- }
1113
- if env:: var_os ( "RUSTUP_TOOLCHAIN" ) . is_some ( ) {
1114
- // Override the PATH to avoid executing the rustup wrapper thousands
1115
- // of times. This makes the testsuite run substantially faster.
1116
- lazy_static:: lazy_static! {
1117
- static ref RUSTC_DIR : PathBuf = {
1118
- match ProcessBuilder :: new( "rustup" )
1119
- . args( & [ "which" , "rustc" ] )
1120
- . exec_with_output( )
1121
- {
1122
- Ok ( output) => {
1123
- let s = str :: from_utf8( & output. stdout) . expect( "utf8" ) . trim( ) ;
1124
- let mut p = PathBuf :: from( s) ;
1125
- p. pop( ) ;
1126
- p
1127
- }
1128
- Err ( e) => {
1129
- panic!( "RUSTUP_TOOLCHAIN was set, but could not run rustup: {}" , e) ;
1130
- }
1131
- }
1132
- } ;
1133
- }
1134
- let path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
1135
- let paths = env:: split_paths ( & path) ;
1136
- let new_path = env:: join_paths ( std:: iter:: once ( RUSTC_DIR . clone ( ) ) . chain ( paths) ) . unwrap ( ) ;
1137
- p. env ( "PATH" , new_path) ;
1138
- }
1139
-
1140
- p. cwd ( & paths:: root ( ) )
1141
- . env ( "HOME" , paths:: home ( ) )
1142
- . env ( "CARGO_HOME" , paths:: home ( ) . join ( ".cargo" ) )
1143
- . env ( "__CARGO_TEST_ROOT" , paths:: root ( ) )
1144
- // Force Cargo to think it's on the stable channel for all tests, this
1145
- // should hopefully not surprise us as we add cargo features over time and
1146
- // cargo rides the trains.
1147
- . env ( "__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS" , "stable" )
1148
- // For now disable incremental by default as support hasn't ridden to the
1149
- // stable channel yet. Once incremental support hits the stable compiler we
1150
- // can switch this to one and then fix the tests.
1151
- . env ( "CARGO_INCREMENTAL" , "0" )
1152
- . env_remove ( "__CARGO_DEFAULT_LIB_METADATA" )
1153
- . env_remove ( "RUSTC" )
1154
- . env_remove ( "RUSTDOC" )
1155
- . env_remove ( "RUSTC_WRAPPER" )
1156
- . env_remove ( "RUSTFLAGS" )
1157
- . env_remove ( "RUSTDOCFLAGS" )
1158
- . env_remove ( "XDG_CONFIG_HOME" ) // see #2345
1159
- . env ( "GIT_CONFIG_NOSYSTEM" , "1" ) // keep trying to sandbox ourselves
1160
- . env_remove ( "EMAIL" )
1161
- . env_remove ( "USER" ) // not set on some rust-lang docker images
1162
- . env_remove ( "MFLAGS" )
1163
- . env_remove ( "MAKEFLAGS" )
1164
- . env_remove ( "GIT_AUTHOR_NAME" )
1165
- . env_remove ( "GIT_AUTHOR_EMAIL" )
1166
- . env_remove ( "GIT_COMMITTER_NAME" )
1167
- . env_remove ( "GIT_COMMITTER_EMAIL" )
1168
- . env_remove ( "MSYSTEM" ) ; // assume cmd.exe everywhere on windows
1169
- if cfg ! ( target_os = "macos" ) {
1170
- // Work-around a bug in macOS 10.15, see `link_or_copy` for details.
1171
- p. env ( "__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS" , "1" ) ;
1172
- }
1105
+ p. cwd ( & paths:: root ( ) ) . test_env ( ) ;
1173
1106
p
1174
1107
}
1175
1108
@@ -1190,6 +1123,103 @@ impl ChannelChanger for snapbox::cmd::Command {
1190
1123
}
1191
1124
}
1192
1125
1126
+ /// Establish a process's test environment
1127
+ pub trait TestEnv : Sized {
1128
+ fn test_env ( mut self ) -> Self {
1129
+ // In general just clear out all cargo-specific configuration already in the
1130
+ // environment. Our tests all assume a "default configuration" unless
1131
+ // specified otherwise.
1132
+ for ( k, _v) in env:: vars ( ) {
1133
+ if k. starts_with ( "CARGO_" ) {
1134
+ self = self . env_remove ( & k) ;
1135
+ }
1136
+ }
1137
+ if env:: var_os ( "RUSTUP_TOOLCHAIN" ) . is_some ( ) {
1138
+ // Override the PATH to avoid executing the rustup wrapper thousands
1139
+ // of times. This makes the testsuite run substantially faster.
1140
+ lazy_static:: lazy_static! {
1141
+ static ref RUSTC_DIR : PathBuf = {
1142
+ match ProcessBuilder :: new( "rustup" )
1143
+ . args( & [ "which" , "rustc" ] )
1144
+ . exec_with_output( )
1145
+ {
1146
+ Ok ( output) => {
1147
+ let s = str :: from_utf8( & output. stdout) . expect( "utf8" ) . trim( ) ;
1148
+ let mut p = PathBuf :: from( s) ;
1149
+ p. pop( ) ;
1150
+ p
1151
+ }
1152
+ Err ( e) => {
1153
+ panic!( "RUSTUP_TOOLCHAIN was set, but could not run rustup: {}" , e) ;
1154
+ }
1155
+ }
1156
+ } ;
1157
+ }
1158
+ let path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
1159
+ let paths = env:: split_paths ( & path) ;
1160
+ let new_path =
1161
+ env:: join_paths ( std:: iter:: once ( RUSTC_DIR . clone ( ) ) . chain ( paths) ) . unwrap ( ) ;
1162
+ self = self . env ( "PATH" , new_path) ;
1163
+ }
1164
+
1165
+ self = self
1166
+ . env ( "HOME" , paths:: home ( ) )
1167
+ . env ( "CARGO_HOME" , paths:: home ( ) . join ( ".cargo" ) )
1168
+ . env ( "__CARGO_TEST_ROOT" , paths:: root ( ) )
1169
+ // Force Cargo to think it's on the stable channel for all tests, this
1170
+ // should hopefully not surprise us as we add cargo features over time and
1171
+ // cargo rides the trains.
1172
+ . env ( "__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS" , "stable" )
1173
+ // For now disable incremental by default as support hasn't ridden to the
1174
+ // stable channel yet. Once incremental support hits the stable compiler we
1175
+ // can switch this to one and then fix the tests.
1176
+ . env ( "CARGO_INCREMENTAL" , "0" )
1177
+ . env_remove ( "__CARGO_DEFAULT_LIB_METADATA" )
1178
+ . env_remove ( "RUSTC" )
1179
+ . env_remove ( "RUSTDOC" )
1180
+ . env_remove ( "RUSTC_WRAPPER" )
1181
+ . env_remove ( "RUSTFLAGS" )
1182
+ . env_remove ( "RUSTDOCFLAGS" )
1183
+ . env_remove ( "XDG_CONFIG_HOME" ) // see #2345
1184
+ . env ( "GIT_CONFIG_NOSYSTEM" , "1" ) // keep trying to sandbox ourselves
1185
+ . env_remove ( "EMAIL" )
1186
+ . env_remove ( "USER" ) // not set on some rust-lang docker images
1187
+ . env_remove ( "MFLAGS" )
1188
+ . env_remove ( "MAKEFLAGS" )
1189
+ . env_remove ( "GIT_AUTHOR_NAME" )
1190
+ . env_remove ( "GIT_AUTHOR_EMAIL" )
1191
+ . env_remove ( "GIT_COMMITTER_NAME" )
1192
+ . env_remove ( "GIT_COMMITTER_EMAIL" )
1193
+ . env_remove ( "MSYSTEM" ) ; // assume cmd.exe everywhere on windows
1194
+ if cfg ! ( target_os = "macos" ) {
1195
+ // Work-around a bug in macOS 10.15, see `link_or_copy` for details.
1196
+ self = self . env ( "__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS" , "1" ) ;
1197
+ }
1198
+ self
1199
+ }
1200
+
1201
+ fn env < S : AsRef < std:: ffi:: OsStr > > ( self , key : & str , value : S ) -> Self ;
1202
+ fn env_remove ( self , key : & str ) -> Self ;
1203
+ }
1204
+
1205
+ impl TestEnv for & mut ProcessBuilder {
1206
+ fn env < S : AsRef < std:: ffi:: OsStr > > ( self , key : & str , value : S ) -> Self {
1207
+ self . env ( key, value)
1208
+ }
1209
+ fn env_remove ( self , key : & str ) -> Self {
1210
+ self . env_remove ( key)
1211
+ }
1212
+ }
1213
+
1214
+ impl TestEnv for snapbox:: cmd:: Command {
1215
+ fn env < S : AsRef < std:: ffi:: OsStr > > ( self , key : & str , value : S ) -> Self {
1216
+ self . env ( key, value)
1217
+ }
1218
+ fn env_remove ( self , key : & str ) -> Self {
1219
+ self . env_remove ( key)
1220
+ }
1221
+ }
1222
+
1193
1223
fn split_and_add_args ( p : & mut ProcessBuilder , s : & str ) {
1194
1224
for mut arg in s. split_whitespace ( ) {
1195
1225
if ( arg. starts_with ( '"' ) && arg. ends_with ( '"' ) )
0 commit comments