run_make_support/
env.rs

1use std::ffi::OsString;
2
3#[track_caller]
4#[must_use]
5pub fn env_var(name: &str) -> String {
6    match std::env::var(name) {
7        Ok(v) => v,
8        Err(err) => panic!("failed to retrieve environment variable {name:?}: {err:?}"),
9    }
10}
11
12#[track_caller]
13#[must_use]
14pub fn env_var_os(name: &str) -> OsString {
15    match std::env::var_os(name) {
16        Some(v) => v,
17        None => panic!("failed to retrieve environment variable {name:?}"),
18    }
19}
20
21/// Check if staged `rustc`-under-test was built with debug assertions.
22#[track_caller]
23#[must_use]
24pub fn rustc_debug_assertions_enabled() -> bool {
25    // Note: we assume this env var is set when the test recipe is being executed.
26    std::env::var_os("__RUSTC_DEBUG_ASSERTIONS_ENABLED").is_some()
27}
28
29/// Check if staged `std`-under-test was built with debug assertions.
30#[track_caller]
31#[must_use]
32pub fn std_debug_assertions_enabled() -> bool {
33    // Note: we assume this env var is set when the test recipe is being executed.
34    std::env::var_os("__STD_DEBUG_ASSERTIONS_ENABLED").is_some()
35}
36
37/// A wrapper around [`std::env::set_current_dir`] which includes the directory
38/// path in the panic message.
39#[track_caller]
40pub fn set_current_dir<P: AsRef<std::path::Path>>(dir: P) {
41    std::env::set_current_dir(dir.as_ref())
42        .expect(&format!("could not set current directory to \"{}\"", dir.as_ref().display()));
43}