Skip to content

Commit c99abdd

Browse files
committed
chore: use unsafe versions of (add|remove)_var()
1 parent c1f4d71 commit c99abdd

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

download/tests/read-proxy-env.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@ use url::Url;
1515

1616
static SERIALISE_TESTS: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
1717

18-
fn scrub_env() {
19-
remove_var("http_proxy");
20-
remove_var("https_proxy");
21-
remove_var("HTTPS_PROXY");
22-
remove_var("ftp_proxy");
23-
remove_var("FTP_PROXY");
24-
remove_var("all_proxy");
25-
remove_var("ALL_PROXY");
26-
remove_var("no_proxy");
27-
remove_var("NO_PROXY");
18+
unsafe fn scrub_env() {
19+
unsafe {
20+
remove_var("http_proxy");
21+
remove_var("https_proxy");
22+
remove_var("HTTPS_PROXY");
23+
remove_var("ftp_proxy");
24+
remove_var("FTP_PROXY");
25+
remove_var("all_proxy");
26+
remove_var("ALL_PROXY");
27+
remove_var("no_proxy");
28+
remove_var("NO_PROXY");
29+
}
2830
}
2931

3032
// Tests for correctly retrieving the proxy (host, port) tuple from $https_proxy
3133
#[tokio::test]
3234
async fn read_basic_proxy_params() {
3335
let _guard = SERIALISE_TESTS.lock().await;
34-
scrub_env();
35-
set_var("https_proxy", "http://proxy.example.com:8080");
36+
// SAFETY: We are setting environment variables when `SERIALISE_TESTS` is locked,
37+
// and those environment variables in question are not relevant elsewhere in the test suite.
38+
unsafe {
39+
scrub_env();
40+
set_var("https_proxy", "http://proxy.example.com:8080");
41+
}
3642
let u = Url::parse("https://www.example.org").ok().unwrap();
3743
assert_eq!(
3844
for_url(&u).host_port(),
@@ -46,8 +52,12 @@ async fn socks_proxy_request() {
4652
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
4753
let _guard = SERIALISE_TESTS.lock().await;
4854

49-
scrub_env();
50-
set_var("all_proxy", "socks5://127.0.0.1:1080");
55+
// SAFETY: We are setting environment variables when `SERIALISE_TESTS` is locked,
56+
// and those environment variables in question are not relevant elsewhere in the test suite.
57+
unsafe {
58+
scrub_env();
59+
set_var("all_proxy", "socks5://127.0.0.1:1080");
60+
}
5161

5262
thread::spawn(move || {
5363
let listener = TcpListener::bind("127.0.0.1:1080").unwrap();

src/test/mock/clitools.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,33 @@ impl ConstState {
192192

193193
/// State a test can interact and mutate
194194
pub async fn setup_test_state(test_dist_dir: tempfile::TempDir) -> (tempfile::TempDir, Config) {
195-
// Unset env variables that will break our testing
196-
env::remove_var("RUSTUP_UPDATE_ROOT");
197-
env::remove_var("RUSTUP_TOOLCHAIN");
198-
env::remove_var("SHELL");
199-
env::remove_var("ZDOTDIR");
200-
// clap does its own terminal colour probing, and that isn't
201-
// trait-controllable, but it does honour the terminal. To avoid testing
202-
// claps code, lie about whatever terminal this process was started under.
203-
env::set_var("TERM", "dumb");
204-
205-
match env::var("RUSTUP_BACKTRACE") {
206-
Ok(val) => env::set_var("RUST_BACKTRACE", val),
207-
_ => env::remove_var("RUST_BACKTRACE"),
195+
// SAFETY: This is probably not the best way of doing such a thing, but it should be
196+
// okay since we are setting the environment variables for the integration tests only.
197+
// There are two types of integration test in rustup: in-process and subprocess.
198+
// For the former, the environment variables are 100% injected via [`TestContext::vars`];
199+
// for the latter, the environment variables in question are only relevant in the
200+
// corresponding subprocesses. Thus, it should be safe to assume that the following won't
201+
// cause inconsistencies as far as **this** particular process is concerned, as long as
202+
// **each subprocess gets the same value for every environment variable listed below when
203+
// it spins off**. To do so, we will have to ensure that:
204+
// - The following `unsafe` block is idempotent, making its output absolutely stable.
205+
// - The environment variables listed below are never modified to anything else
206+
// **in this process** when the tests are still running.
207+
unsafe {
208+
// Unset env variables that will break our testing
209+
env::remove_var("RUSTUP_UPDATE_ROOT");
210+
env::remove_var("RUSTUP_TOOLCHAIN");
211+
env::remove_var("SHELL");
212+
env::remove_var("ZDOTDIR");
213+
// clap does its own terminal colour probing, and that isn't
214+
// trait-controllable, but it does honour the terminal. To avoid testing
215+
// claps code, lie about whatever terminal this process was started under.
216+
env::set_var("TERM", "dumb");
217+
218+
match env::var("RUSTUP_BACKTRACE") {
219+
Ok(val) => env::set_var("RUST_BACKTRACE", val),
220+
_ => env::remove_var("RUST_BACKTRACE"),
221+
}
208222
}
209223

210224
let current_exe_path = env::current_exe().unwrap();

0 commit comments

Comments
 (0)