io_providers/env/
virtual_provider.rs1use std::io;
2use std::path::{Path, PathBuf};
3use env;
4
5pub struct Virtual {
8 args: Vec<String>,
9 current_dir: PathBuf,
10}
11
12impl Virtual {
13 pub fn new() -> Virtual {
15 Virtual {
16 args: Vec::new(),
17 current_dir: PathBuf::from("/"),
18 }
19 }
20
21 pub fn set_args(&mut self, args: Vec<String>) {
23 self.args = args;
24 }
25}
26
27impl env::Provider for Virtual {
28 fn args(&self) -> Vec<String> {
29 self.args.clone()
30 }
31
32 fn current_dir(&self) -> io::Result<PathBuf> {
33 Ok(self.current_dir.clone())
34 }
35
36 fn set_current_dir<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
37 self.current_dir = PathBuf::from(path.as_ref());
38 Ok(())
39 }
40}
41
42#[cfg(test)]
43#[allow(non_snake_case)]
44mod tests {
45 use std::path::{Path, PathBuf};
46 use super::Virtual;
47 use env::Provider;
48
49 #[test]
50 fn current_dir__default__returns_root() {
51 let provider = Virtual::new();
52 let result = provider.current_dir().unwrap();
53 assert_eq!(PathBuf::from("/"), result);
54 }
55
56 #[test]
57 fn current_dir__set_and_get__success() {
58 let mut provider = Virtual::new();
59 let path = Path::new("/foo/bar");
60
61 provider.set_current_dir(path).unwrap();
62 let result = provider.current_dir().unwrap();
63
64 assert_eq!(path, result.as_path());
65 }
66
67 #[test]
68 fn args__default__returns_empty() {
69 let provider = Virtual::new();
70 let result = provider.args();
71 assert_eq!(0, result.len());
72 }
73
74 #[test]
75 fn args__set_and_get__success() {
76 let mut provider = Virtual::new();
77 let args = vec!["app".to_string(), "arg1".to_string(), "arg2".to_string()];
78
79 provider.set_args(args.clone());
80 let result = provider.args();
81
82 assert_eq!(args, result);
83 }
84}