]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/env-home-dir.rs
Auto merge of #61361 - estebank:infer-type, r=varkor
[rust.git] / src / test / run-pass / env-home-dir.rs
1 #![allow(unused_variables)]
2 #![allow(deprecated)]
3 // ignore-cloudabi no environment variables present
4 // ignore-emscripten env vars don't work?
5 // ignore-sgx env vars cannot be modified
6
7 use std::env::*;
8 use std::path::PathBuf;
9
10 #[cfg(unix)]
11 fn main() {
12     let oldhome = var("HOME");
13
14     set_var("HOME", "/home/MountainView");
15     assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
16
17     remove_var("HOME");
18     if cfg!(target_os = "android") {
19         assert!(home_dir().is_none());
20     } else {
21         // When HOME is not set, some platforms return `None`,
22         // but others return `Some` with a default.
23         // Just check that it is not "/home/MountainView".
24         assert_ne!(home_dir(), Some(PathBuf::from("/home/MountainView")));
25     }
26 }
27
28 #[cfg(windows)]
29 fn main() {
30     let oldhome = var("HOME");
31     let olduserprofile = var("USERPROFILE");
32
33     remove_var("HOME");
34     remove_var("USERPROFILE");
35
36     assert!(home_dir().is_some());
37
38     set_var("HOME", "/home/MountainView");
39     assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
40
41     remove_var("HOME");
42
43     set_var("USERPROFILE", "/home/MountainView");
44     assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
45
46     set_var("HOME", "/home/MountainView");
47     set_var("USERPROFILE", "/home/PaloAlto");
48     assert_eq!(home_dir(), Some(PathBuf::from("/home/MountainView")));
49 }