Skip to content

Commit bb0fd7b

Browse files
authored
Merge pull request #2354 from rbtcollins/in-process
In process test support
2 parents fac11e1 + 245b588 commit bb0fd7b

29 files changed

+1033
-403
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ order. Any file that is not grouped like this can be rearranged whenever the
5353
file is touched - we're not precious about having it done in a separate commit,
5454
though that is helpful.
5555

56+
### No direct use of process state outside rustup::currentprocess
57+
58+
The `rustup::currentprocess` module abstracts the global state that is
59+
`std::env::args`, `std::env::vars`, `std::io::std*`, `std::process::id`,
60+
`std::env::current_dir` and `std::process::exit` permitting threaded tests of
61+
the CLI logic; use `process()` rather than those APIs directly.
62+
5663
## Version numbers
5764

5865
If you ever see a released version of rustup which has `::` in its version string

src/bin/rustup-init.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
1515
#![recursion_limit = "1024"]
1616

17-
use std::env;
1817
use std::path::PathBuf;
1918

2019
use rs_tracing::*;
@@ -25,41 +24,46 @@ use rustup::cli::proxy_mode;
2524
use rustup::cli::rustup_mode;
2625
use rustup::cli::self_update;
2726
use rustup::cli::setup_mode;
27+
use rustup::currentprocess::{process, with, OSProcess};
2828
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
2929
use rustup::utils::utils;
3030

3131
fn main() {
32-
if let Err(ref e) = run_rustup() {
33-
common::report_error(e);
34-
std::process::exit(1);
35-
}
32+
let process = OSProcess::default();
33+
with(Box::new(process), || match run_rustup() {
34+
Err(ref e) => {
35+
common::report_error(e);
36+
std::process::exit(1);
37+
}
38+
Ok(utils::ExitCode(c)) => std::process::exit(c),
39+
});
3640
}
3741

38-
fn run_rustup() -> Result<()> {
39-
if let Ok(dir) = env::var("RUSTUP_TRACE_DIR") {
42+
fn run_rustup() -> Result<utils::ExitCode> {
43+
if let Ok(dir) = process().var("RUSTUP_TRACE_DIR") {
4044
open_trace_file!(dir)?;
4145
}
4246
let result = run_rustup_inner();
43-
if env::var("RUSTUP_TRACE_DIR").is_ok() {
47+
if process().var("RUSTUP_TRACE_DIR").is_ok() {
4448
close_trace_file!();
4549
}
4650
result
4751
}
4852

49-
fn run_rustup_inner() -> Result<()> {
53+
fn run_rustup_inner() -> Result<utils::ExitCode> {
5054
// Guard against infinite proxy recursion. This mostly happens due to
5155
// bugs in rustup.
5256
do_recursion_guard()?;
5357

5458
// Before we do anything else, ensure we know where we are and who we
5559
// are because otherwise we cannot proceed usefully.
56-
utils::current_dir()?;
60+
process().current_dir()?;
5761
utils::current_exe()?;
5862

5963
// The name of arg0 determines how the program is going to behave
60-
let arg0 = match env::var("RUSTUP_FORCE_ARG0") {
64+
let arg0 = match process().var("RUSTUP_FORCE_ARG0") {
6165
Ok(v) => Some(v),
62-
Err(_) => env::args().next(),
66+
Err(_) => process().args().next(),
6367
}
6468
.map(PathBuf::from);
6569
let name = arg0
@@ -90,7 +94,8 @@ fn run_rustup_inner() -> Result<()> {
9094
}
9195

9296
fn do_recursion_guard() -> Result<()> {
93-
let recursion_count = env::var("RUST_RECURSION_COUNT")
97+
let recursion_count = process()
98+
.var("RUST_RECURSION_COUNT")
9499
.ok()
95100
.and_then(|s| s.parse().ok())
96101
.unwrap_or(0);

0 commit comments

Comments
 (0)