| 1 |
//! Lower-level utilities for mocking the process environment. |
| 2 |
|
| 3 |
use std::{ |
| 4 |
ffi::OsString, |
| 5 |
io, |
| 6 |
path::{Path, PathBuf}, |
| 7 |
}; |
| 8 |
|
| 9 |
/// Permits parameterizing the home functions via the _from variants - used for |
| 10 |
/// in-process unit testing by rustup. |
| 11 |
pub trait Env { |
| 12 |
/// Return the path to the users home dir, or None if any error occurs: |
| 13 |
/// see home_inner. |
| 14 |
fn home_dir(&self) -> Option<PathBuf>; |
| 15 |
/// Return the current working directory. |
| 16 |
fn current_dir(&self) -> io::Result<PathBuf>; |
| 17 |
/// Get an environment variable, as per std::env::var_os. |
| 18 |
fn var_os(&self, key: &str) -> Option<OsString>; |
| 19 |
} |
| 20 |
|
| 21 |
/// Implements Env for the OS context, both Unix style and Windows. |
| 22 |
/// |
| 23 |
/// This is trait permits in-process testing by providing a control point to |
| 24 |
/// allow in-process divergence on what is normally process wide state. |
| 25 |
/// |
| 26 |
/// Implementations should be provided by whatever testing framework the caller |
| 27 |
/// is using. Code that is not performing in-process threaded testing requiring |
| 28 |
/// isolated rustup/cargo directories does not need this trait or the _from |
| 29 |
/// functions. |
| 30 |
pub struct OsEnv; |
| 31 |
impl Env for OsEnv { |
| 32 |
fn home_dir(&self) -> Option<PathBuf> { |
| 33 |
crate::home_dir_inner() |
| 34 |
} |
| 35 |
fn current_dir(&self) -> io::Result<PathBuf> { |
| 36 |
std::env::current_dir() |
| 37 |
} |
| 38 |
fn var_os(&self, key: &str) -> Option<OsString> { |
| 39 |
std::env::var_os(key) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
pub const OS_ENV: OsEnv = OsEnv {}; |
| 44 |
|
| 45 |
/// Returns the path of the current user's home directory from [`Env::home_dir`]. |
| 46 |
pub fn home_dir_with_env(env: &dyn Env) -> Option<PathBuf> { |
| 47 |
env.home_dir() |
| 48 |
} |
| 49 |
|
| 50 |
/// Variant of cargo_home where the environment source is parameterized. This is |
| 51 |
/// specifically to support in-process testing scenarios as environment |
| 52 |
/// variables and user home metadata are normally process global state. See the |
| 53 |
/// [`Env`] trait. |
| 54 |
pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> { |
| 55 |
let cwd = env.current_dir()?; |
| 56 |
cargo_home_with_cwd_env(env, &cwd) |
| 57 |
} |
| 58 |
|
| 59 |
/// Variant of cargo_home_with_cwd where the environment source is |
| 60 |
/// parameterized. This is specifically to support in-process testing scenarios |
| 61 |
/// as environment variables and user home metadata are normally process global |
| 62 |
/// state. See the OsEnv trait. |
| 63 |
pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> { |
| 64 |
match env.var_os("CARGO_HOME").filter(|h| !h.is_empty()) { |
| 65 |
Some(home) => { |
| 66 |
let home = PathBuf::from(home); |
| 67 |
if home.is_absolute() { |
| 68 |
Ok(home) |
| 69 |
} else { |
| 70 |
Ok(cwd.join(&home)) |
| 71 |
} |
| 72 |
} |
| 73 |
_ => home_dir_with_env(env) |
| 74 |
.map(|p| p.join(".cargo")) |
| 75 |
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")), |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/// Variant of cargo_home_with_cwd where the environment source is |
| 80 |
/// parameterized. This is specifically to support in-process testing scenarios |
| 81 |
/// as environment variables and user home metadata are normally process global |
| 82 |
/// state. See the OsEnv trait. |
| 83 |
pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> { |
| 84 |
let cwd = env.current_dir()?; |
| 85 |
rustup_home_with_cwd_env(env, &cwd) |
| 86 |
} |
| 87 |
|
| 88 |
/// Variant of cargo_home_with_cwd where the environment source is |
| 89 |
/// parameterized. This is specifically to support in-process testing scenarios |
| 90 |
/// as environment variables and user home metadata are normally process global |
| 91 |
/// state. See the OsEnv trait. |
| 92 |
pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> { |
| 93 |
match env.var_os("RUSTUP_HOME").filter(|h| !h.is_empty()) { |
| 94 |
Some(home) => { |
| 95 |
let home = PathBuf::from(home); |
| 96 |
if home.is_absolute() { |
| 97 |
Ok(home) |
| 98 |
} else { |
| 99 |
Ok(cwd.join(&home)) |
| 100 |
} |
| 101 |
} |
| 102 |
_ => home_dir_with_env(env) |
| 103 |
.map(|d| d.join(".rustup")) |
| 104 |
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find rustup home dir")), |
| 105 |
} |
| 106 |
} |