Coverage Report

Created: 2026-03-14 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/home-0.5.12/src/env.rs
Line
Count
Source
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
0
    fn home_dir(&self) -> Option<PathBuf> {
33
0
        crate::home_dir_inner()
34
0
    }
35
0
    fn current_dir(&self) -> io::Result<PathBuf> {
36
0
        std::env::current_dir()
37
0
    }
38
0
    fn var_os(&self, key: &str) -> Option<OsString> {
39
0
        std::env::var_os(key)
40
0
    }
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
0
pub fn home_dir_with_env(env: &dyn Env) -> Option<PathBuf> {
47
0
    env.home_dir()
48
0
}
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
0
pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
57
0
    let cwd = env.current_dir()?;
58
0
    cargo_home_with_cwd_env(env, &cwd)
59
0
}
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
0
pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
68
0
    match env.var_os("CARGO_HOME").filter(|h| !h.is_empty()) {
69
0
        Some(home) => {
70
0
            let home = PathBuf::from(home);
71
0
            if home.is_absolute() {
72
0
                Ok(home)
73
            } else {
74
0
                Ok(cwd.join(&home))
75
            }
76
        }
77
0
        _ => home_dir_with_env(env)
78
0
            .map(|p| p.join(".cargo"))
79
0
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")),
80
    }
81
0
}
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
0
pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
90
0
    let cwd = env.current_dir()?;
91
0
    rustup_home_with_cwd_env(env, &cwd)
92
0
}
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
0
pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
101
0
    match env.var_os("RUSTUP_HOME").filter(|h| !h.is_empty()) {
102
0
        Some(home) => {
103
0
            let home = PathBuf::from(home);
104
0
            if home.is_absolute() {
105
0
                Ok(home)
106
            } else {
107
0
                Ok(cwd.join(&home))
108
            }
109
        }
110
0
        _ => home_dir_with_env(env)
111
0
            .map(|d| d.join(".rustup"))
112
0
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find rustup home dir")),
113
    }
114
0
}