| 1 |
#![cfg(all(unix, not(target_os = "macos"), not(target_os = "android")))] |
| 2 |
|
| 3 |
use std::env; |
| 4 |
use std::ffi; |
| 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 |
|
| 13 |
static ENV_MUTEX: Lazy<sync::Mutex<()>> = Lazy::new(|| sync::Mutex::new(())); |
| 14 |
|
| 15 |
fn reset_env() { |
| 16 |
env::set_var("HOME", ""); |
| 17 |
env::set_var("XDG_CACHE_HOME", ""); |
| 18 |
env::set_var("XDG_CONFIG_HOME", ""); |
| 19 |
env::set_var("XDG_DATA_HOME", ""); |
| 20 |
env::set_var("XDG_DATA_DIRS", ""); |
| 21 |
env::set_var("XDG_CONFIG_DIRS", ""); |
| 22 |
} |
| 23 |
|
| 24 |
#[test_case(AppDataType::UserCache, ".cache"; "user cache")] |
| 25 |
#[test_case(AppDataType::UserConfig, ".config"; "user config")] |
| 26 |
#[test_case(AppDataType::UserData, ".local/share"; "user data")] |
| 27 |
#[test_case(AppDataType::SharedConfig, "/etc/xdg"; "shared config")] |
| 28 |
#[test_case(AppDataType::SharedData, "/usr/local/share"; "shared data")] |
| 29 |
fn test_home(ty: AppDataType, path: impl AsRef<path::Path>) { |
| 30 |
let _env_guard = ENV_MUTEX.lock(); |
| 31 |
|
| 32 |
let dir = tempfile::tempdir().unwrap(); |
| 33 |
reset_env(); |
| 34 |
env::set_var("HOME", dir.path()); |
| 35 |
|
| 36 |
let data_root = app_dirs2::get_data_root(ty).unwrap(); |
| 37 |
if ty.is_shared() { |
| 38 |
assert_eq!(path.as_ref(), data_root.as_path()); |
| 39 |
} else { |
| 40 |
assert_eq!(dir.path().join(path.as_ref()), data_root); |
| 41 |
} |
| 42 |
|
| 43 |
let app_info = app_dirs2::AppInfo { |
| 44 |
name: "app-name", |
| 45 |
author: "app-author", |
| 46 |
}; |
| 47 |
|
| 48 |
let app_root = app_dirs2::get_app_root(ty, &app_info).unwrap(); |
| 49 |
assert_eq!(data_root.join(app_info.name), app_root); |
| 50 |
|
| 51 |
let subdir = "testdir"; |
| 52 |
let app_dir = app_dirs2::get_app_dir(ty, &app_info, subdir).unwrap(); |
| 53 |
assert_eq!(app_root.join(subdir), app_dir); |
| 54 |
} |
| 55 |
|
| 56 |
#[test_case(AppDataType::UserCache, "XDG_CACHE_HOME"; "user cache")] |
| 57 |
#[test_case(AppDataType::UserConfig, "XDG_CONFIG_HOME"; "user config")] |
| 58 |
#[test_case(AppDataType::UserData, "XDG_DATA_HOME"; "user data")] |
| 59 |
#[test_case(AppDataType::SharedConfig, "XDG_CONFIG_DIRS"; "shared config")] |
| 60 |
#[test_case(AppDataType::SharedData, "XDG_DATA_DIRS"; "shared data")] |
| 61 |
fn test_xdg_dirs(ty: AppDataType, env_var: impl AsRef<ffi::OsStr>) { |
| 62 |
let _env_guard = ENV_MUTEX.lock(); |
| 63 |
|
| 64 |
let dir = tempfile::tempdir().unwrap(); |
| 65 |
reset_env(); |
| 66 |
env::set_var(env_var.as_ref(), dir.path()); |
| 67 |
|
| 68 |
let data_root = app_dirs2::get_data_root(ty).unwrap(); |
| 69 |
assert_eq!(dir.path(), data_root.as_path()); |
| 70 |
|
| 71 |
let app_info = app_dirs2::AppInfo { |
| 72 |
name: "app-name", |
| 73 |
author: "app-author", |
| 74 |
}; |
| 75 |
|
| 76 |
let app_root = app_dirs2::get_app_root(ty, &app_info).unwrap(); |
| 77 |
assert_eq!(data_root.join(app_info.name), app_root); |
| 78 |
|
| 79 |
let subdir = "testdir"; |
| 80 |
let app_dir = app_dirs2::get_app_dir(ty, &app_info, subdir).unwrap(); |
| 81 |
assert_eq!(app_root.join(subdir), app_dir); |
| 82 |
} |
| 83 |
|
| 84 |
#[test_case(AppDataType::UserCache, "XDG_CACHE_HOME"; "user cache")] |
| 85 |
#[test_case(AppDataType::UserConfig, "XDG_CONFIG_HOME"; "user config")] |
| 86 |
#[test_case(AppDataType::UserData, "XDG_DATA_HOME"; "user data")] |
| 87 |
#[test_case(AppDataType::SharedConfig, "XDG_CONFIG_DIRS"; "shared config")] |
| 88 |
#[test_case(AppDataType::SharedData, "XDG_DATA_DIRS"; "shared data")] |
| 89 |
fn test_home_and_xdg_dirs(ty: AppDataType, env_var: impl AsRef<ffi::OsStr>) { |
| 90 |
let _env_guard = ENV_MUTEX.lock(); |
| 91 |
|
| 92 |
let home_dir = tempfile::tempdir().unwrap(); |
| 93 |
let xdg_dir = tempfile::tempdir().unwrap(); |
| 94 |
reset_env(); |
| 95 |
env::set_var("HOME", home_dir.path()); |
| 96 |
env::set_var(env_var.as_ref(), xdg_dir.path()); |
| 97 |
|
| 98 |
let data_root = app_dirs2::get_data_root(ty).unwrap(); |
| 99 |
assert_eq!(xdg_dir.path(), data_root.as_path()); |
| 100 |
|
| 101 |
let app_info = app_dirs2::AppInfo { |
| 102 |
name: "app-name", |
| 103 |
author: "app-author", |
| 104 |
}; |
| 105 |
|
| 106 |
let app_root = app_dirs2::get_app_root(ty, &app_info).unwrap(); |
| 107 |
assert_eq!(data_root.join(app_info.name), app_root); |
| 108 |
|
| 109 |
let subdir = "testdir"; |
| 110 |
let app_dir = app_dirs2::get_app_dir(ty, &app_info, subdir).unwrap(); |
| 111 |
assert_eq!(app_root.join(subdir), app_dir); |
| 112 |
} |