1use std::env;
2use std::env::VarError;
3
4#[cfg(test)]
5use mockall::automock;
6
7#[cfg_attr(test, automock)]
8pub trait Env {
9 fn get(&self, key: &str) -> Result<String, VarError>;
10}
11
12pub struct EnvImpl;
13
14impl EnvImpl {
15 pub fn new() -> Self {
16 EnvImpl {}
17 }
18}
19
20impl Default for EnvImpl {
21 fn default() -> Self {
22 EnvImpl::new()
23 }
24}
25
26impl Env for EnvImpl {
27 fn get(&self, key: &str) -> Result<String, VarError> {
28 env::var(key)
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 #[test]
37 fn should_return_value() {
38 let env = EnvImpl::new();
39 assert!(env.get("HOME").unwrap().contains('/'))
40 }
41
42 #[test]
43 fn should_not_return_value() {
44 let env = EnvImpl::new();
45 assert!(env.get("NON_EXISTING_VAR").is_err())
46 }
47}