use std::path::{Path, PathBuf};
use crate::Snapshot;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapturedSnapshot {
snapshot: Snapshot,
path: PathBuf,
}
impl CapturedSnapshot {
#[must_use]
pub const fn new(snapshot: Snapshot, path: PathBuf) -> Self {
Self { snapshot, path }
}
#[must_use]
pub const fn snapshot(&self) -> &Snapshot {
&self.snapshot
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn into_parts(self) -> (Snapshot, PathBuf) {
(self.snapshot, self.path)
}
}
#[derive(Debug, Clone)]
pub struct TestArtifacts {
root: PathBuf,
}
impl TestArtifacts {
#[must_use]
pub fn new(suite: impl AsRef<str>) -> Self {
let root = artifact_root().join(suite.as_ref());
Self { root }
}
#[must_use]
pub fn root(&self) -> &Path {
&self.root
}
#[must_use]
pub fn case_dir(&self, case: impl AsRef<str>) -> PathBuf {
self.root.join(case.as_ref())
}
#[must_use]
pub fn snapshot_path(&self, case: impl AsRef<str>, stage: impl AsRef<str>) -> PathBuf {
self.case_dir(case).join(format!("{}.png", stage.as_ref()))
}
pub fn capture_snapshot(
&self,
case: impl AsRef<str>,
stage: impl AsRef<str>,
snapshot: Snapshot,
) -> CapturedSnapshot {
let path = self.snapshot_path(case, stage);
snapshot
.save_png(&path)
.expect("TestArtifacts::capture_snapshot: snapshot PNG should be writable");
CapturedSnapshot::new(snapshot, path)
}
pub fn save_snapshot(
&self,
case: impl AsRef<str>,
stage: impl AsRef<str>,
snapshot: &Snapshot,
) -> PathBuf {
self.capture_snapshot(case, stage, snapshot.clone())
.into_parts()
.1
}
}
#[must_use]
pub fn artifact_root() -> PathBuf {
std::env::var_os("WATERUI_TEST_ARTIFACTS_DIR").map_or_else(
|| std::env::temp_dir().join("waterui-testing-artifacts"),
PathBuf::from,
)
}