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