| 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. | 
|---|
| 51 | /// | 
|---|
| 52 | /// This is | 
|---|
| 53 | /// specifically to support in-process testing scenarios as environment | 
|---|
| 54 | /// variables and user home metadata are normally process global state. See the | 
|---|
| 55 | /// [`Env`] trait. | 
|---|
| 56 | pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> { | 
|---|
| 57 | let cwd: PathBuf = env.current_dir()?; | 
|---|
| 58 | cargo_home_with_cwd_env(env, &cwd) | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | /// Variant of `cargo_home_with_cwd` where the environment source is | 
|---|
| 62 | /// parameterized. | 
|---|
| 63 | /// | 
|---|
| 64 | /// This is specifically to support in-process testing scenarios | 
|---|
| 65 | /// as environment variables and user home metadata are normally process global | 
|---|
| 66 | /// state. See the `OsEnv` trait. | 
|---|
| 67 | pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> { | 
|---|
| 68 | match env.var_os(key: "CARGO_HOME").filter(|h: &OsString| !h.is_empty()) { | 
|---|
| 69 | Some(home: OsString) => { | 
|---|
| 70 | let home: PathBuf = PathBuf::from(home); | 
|---|
| 71 | if home.is_absolute() { | 
|---|
| 72 | Ok(home) | 
|---|
| 73 | } else { | 
|---|
| 74 | Ok(cwd.join(&home)) | 
|---|
| 75 | } | 
|---|
| 76 | } | 
|---|
| 77 | _ => home_dir_with_env(env) | 
|---|
| 78 | .map(|p| p.join( ".cargo")) | 
|---|
| 79 | .ok_or_else(|| io::Error::new(kind:io::ErrorKind::Other, error: "could not find cargo home dir")), | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /// Variant of `cargo_home_with_cwd` where the environment source is | 
|---|
| 84 | /// parameterized. | 
|---|
| 85 | /// | 
|---|
| 86 | /// This is specifically to support in-process testing scenarios | 
|---|
| 87 | /// as environment variables and user home metadata are normally process global | 
|---|
| 88 | /// state. See the `OsEnv` trait. | 
|---|
| 89 | pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> { | 
|---|
| 90 | let cwd: PathBuf = env.current_dir()?; | 
|---|
| 91 | rustup_home_with_cwd_env(env, &cwd) | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /// Variant of `cargo_home_with_cwd` where the environment source is | 
|---|
| 95 | /// parameterized. | 
|---|
| 96 | /// | 
|---|
| 97 | /// This is specifically to support in-process testing scenarios | 
|---|
| 98 | /// as environment variables and user home metadata are normally process global | 
|---|
| 99 | /// state. See the `OsEnv` trait. | 
|---|
| 100 | pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> { | 
|---|
| 101 | match env.var_os(key: "RUSTUP_HOME").filter(|h: &OsString| !h.is_empty()) { | 
|---|
| 102 | Some(home: OsString) => { | 
|---|
| 103 | let home: PathBuf = PathBuf::from(home); | 
|---|
| 104 | if home.is_absolute() { | 
|---|
| 105 | Ok(home) | 
|---|
| 106 | } else { | 
|---|
| 107 | Ok(cwd.join(&home)) | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | _ => home_dir_with_env(env) | 
|---|
| 111 | .map(|d| d.join( ".rustup")) | 
|---|
| 112 | .ok_or_else(|| io::Error::new(kind:io::ErrorKind::Other, error: "could not find rustup home dir")), | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|