#![allow(dead_code)]
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use assert_cmd::{assert::Assert, crate_name, Command};
use assert_fs::{
fixture::ChildPath,
prelude::{FileWriteStr, PathChild},
TempDir,
};
use chrono::{DateTime, Local, TimeZone};
use lazy_static::lazy_static;
use rstest::fixture;
lazy_static! {
pub static ref EXAMPLE_TIME: PathBuf = PathBuf::from("tests/examples/time.heic")
.canonicalize()
.unwrap();
pub static ref EXAMPLE_SUN: PathBuf = PathBuf::from("tests/examples/sun.heic")
.canonicalize()
.unwrap();
pub static ref EXAMPLE_UNSUPPORTED: PathBuf = PathBuf::from("tests/examples/unsupported.jpg")
.canonicalize()
.unwrap();
pub static ref PROPERTIES_TIME: PathBuf = PathBuf::from("tests/examples/properties_time.xml")
.canonicalize()
.unwrap();
pub static ref PROPERTIES_SUN: PathBuf = PathBuf::from("tests/examples/properties_sun.xml")
.canonicalize()
.unwrap();
pub static ref WALLPAPER_HASHES: HashMap<PathBuf, &'static str> = HashMap::from([
(EXAMPLE_TIME.to_path_buf(), "dcbcd5f96ccdbdd"),
(EXAMPLE_SUN.to_path_buf(), "a81fb8b5a1b35168"),
]);
pub static ref DATETIME_DAY: DateTime<Local> = Local.with_ymd_and_hms(2022, 10, 18, 14, 30, 30).single().unwrap();
pub static ref DATETIME_NIGHT: DateTime<Local> = Local.with_ymd_and_hms(2022, 10, 18, 22, 30, 30).single().unwrap();
}
pub const IMAGE_DAY: &str = "0.png";
pub const IMAGE_NIGHT: &str = "1.png";
pub const IMAGE_SET_MESSAGE: &str = "Set: ";
pub const COMMAND_RUN_MESSAGE: &str = "Run: ";
pub fn cached_image_path_str<CP: AsRef<Path>>(
cache_dir: CP,
wallpaper: &PathBuf,
image: &str,
) -> String {
cache_dir
.as_ref()
.join("wallpapers")
.join(WALLPAPER_HASHES.get(wallpaper).unwrap())
.join(image)
.to_str()
.unwrap()
.to_owned()
}
pub struct TestEnv {
pub cwd: TempDir,
pub config_dir: ChildPath,
pub cache_dir: ChildPath,
datetime: Option<DateTime<Local>>,
}
impl TestEnv {
pub fn new() -> Self {
TestEnv {
cwd: assert_fs::TempDir::new().unwrap(),
config_dir: assert_fs::TempDir::new().unwrap().child("config"),
cache_dir: assert_fs::TempDir::new().unwrap().child("cache"),
datetime: None,
}
}
pub fn with_config(self, config: &str) -> Self {
self.config_dir
.child("config.toml")
.write_str(config)
.unwrap();
self
}
pub fn with_time(mut self, time: DateTime<Local>) -> Self {
self.datetime = Some(time);
self
}
pub fn run(&self, args: &[&str]) -> Assert {
let mut command = Command::cargo_bin(crate_name!()).unwrap();
command
.current_dir(&self.cwd)
.env("TIMEWALL_DRY_RUN", "true")
.env("TIMEWALL_CONFIG_DIR", &self.config_dir.path())
.env("TIMEWALL_CACHE_DIR", &self.cache_dir.path())
.args(args);
if let Some(datetime) = self.datetime {
command.env("TIMEWALL_OVERRIDE_TIME", datetime.to_rfc3339());
}
command.assert()
}
}
#[fixture]
pub fn testenv() -> TestEnv {
TestEnv::new()
}