Debian Codegraph @

tests/fs.rs

raw symbols SCIP
1 #![cfg(all(unix, not(target_os = "macos"), not(target_os = "android")))]
2
3 use std::env;
4 use std::io;
5 use std::path;
6 use std::sync;
7
8 use app_dirs2::AppDataType;
9 use once_cell::sync::Lazy;
10 use test_case::test_case;
11
12 // This test suite checks the effects of the app_dirs2 crate on the file system.
13 //
14 // The functions with the prefix get_ should not touch the file system. The functions without the
15 // prefix should create the returned directory if it doesn’t exist.
16 //
17 // As only the unix/XDG implementation supports changing the root configuration directory, we can
18 // only run this test suite on this platform. As we use environment variables to set the
19 // configuration root, we have to make sure that the tests are run in sequence and don’t overlap,
20 // see the `ENV_MUTEX` mutex.
21
22 // For test cases that depend on environment variables
23 static ENV_MUTEX: Lazy<sync::Mutex<()>> = Lazy::new(|| sync::Mutex::new(()));
24
25 fn set_root_dir(path: &path::Path) -> path::PathBuf {
26 let root = path.join("root");
27 env::set_var("HOME", &root.join("home"));
28 env::set_var("XDG_CACHE_HOME", "");
29 env::set_var("XDG_CONFIG_HOME", "");
30 env::set_var("XDG_DATA_HOME", "");
31 env::set_var("XDG_DATA_DIRS", &root.join("data"));
32 env::set_var("XDG_CONFIG_DIRS", &root.join("config"));
33 root
34 }
35
36 #[test_case(AppDataType::UserCache; "user cache")]
37 #[test_case(AppDataType::UserConfig; "user config")]
38 #[test_case(AppDataType::UserData; "user data")]
39 #[test_case(AppDataType::SharedConfig; "shared config")]
40 #[test_case(AppDataType::SharedData; "shared data")]
41 fn test_no_create(ty: AppDataType) -> io::Result<()> {
42 let _env_guard = ENV_MUTEX.lock();
43
44 let dir = tempfile::tempdir()?;
45 let root_dir = set_root_dir(dir.path());
46
47 let info = app_dirs2::AppInfo {
48 name: "test-app",
49 author: "test-author",
50 };
51
52 let data_root = app_dirs2::get_data_root(ty).unwrap();
53 assert!(
54 data_root.starts_with(&root_dir),
55 "Data root does not start with root dir: data root = {}, root dir = {}",
56 data_root.display(),
57 root_dir.display()
58 );
59 assert!(!root_dir.exists());
60
61 let app_root = app_dirs2::get_app_root(ty, &info).unwrap();
62 assert!(
63 app_root.starts_with(&data_root),
64 "App root does not start with data root: app root = {}, data root = {}",
65 app_root.display(),
66 data_root.display()
67 );
68 assert!(!root_dir.exists());
69
70 let app_dir = app_dirs2::get_app_dir(ty, &info, "testdir").unwrap();
71 assert!(
72 app_dir.starts_with(&app_root),
73 "App dir does not start with app root: app dir = {}, app root = {}",
74 app_dir.display(),
75 app_root.display()
76 );
77 assert!(!root_dir.exists());
78
79 dir.close()
80 }
81
82 #[test_case(AppDataType::UserCache; "user cache")]
83 #[test_case(AppDataType::UserConfig; "user config")]
84 #[test_case(AppDataType::UserData; "user data")]
85 #[test_case(AppDataType::SharedConfig; "shared config")]
86 #[test_case(AppDataType::SharedData; "shared data")]
87 fn test_create(ty: AppDataType) -> io::Result<()> {
88 let _env_guard = ENV_MUTEX.lock();
89
90 let dir = tempfile::tempdir()?;
91 let root_dir = set_root_dir(dir.path());
92
93 let info = app_dirs2::AppInfo {
94 name: "test-app",
95 author: "test-author",
96 };
97
98 let data_root = app_dirs2::data_root(ty).unwrap();
99 assert!(
100 data_root.starts_with(&root_dir),
101 "Data root does not start with root dir: data root = {}, root dir = {}",
102 data_root.display(),
103 root_dir.display()
104 );
105 assert!(data_root.is_dir());
106
107 let app_root = app_dirs2::app_root(ty, &info).unwrap();
108 assert!(
109 app_root.starts_with(&data_root),
110 "App root does not start with data root: app root = {}, data root = {}",
111 app_root.display(),
112 data_root.display()
113 );
114 assert!(app_root.is_dir());
115
116 let app_dir = app_dirs2::app_dir(ty, &info, "testdir").unwrap();
117 assert!(
118 app_dir.starts_with(&app_root),
119 "App dir does not start with app root: app dir = {}, app root = {}",
120 app_dir.display(),
121 app_root.display()
122 );
123 assert!(app_dir.is_dir());
124
125 dir.close()
126 }