mod baseline;
mod env;
pub mod fonts;
mod options;
mod png;
pub mod review;
mod semantic;
#[cfg(test)]
mod tests;
pub mod themes;
mod variant;
mod visual;
use std::fmt;
use std::path::PathBuf;
pub use env::{Runner, SnapshotEnv, UpdateMode};
pub use icedwright_check::snapshot::{DiffOptions, DiffResult, Rgba, diff};
pub use icedwright_check::{MatchReport, Template, TemplateError};
pub use icedwright_semantics::yaml::YamlOptions;
pub use options::{Caret, RenderOptions, Screenshot};
pub use png::{PngMetadata, decode_png, encode_png};
pub use semantic::SemanticOptions;
pub use variant::{Variant, scale_name, size_name, theme_name};
#[doc(hidden)]
pub mod __private {
pub use super::semantic::{Prepared, prepare, settings};
pub use insta;
}
const PER_TEST_REASON: &str =
" The font database is shared by every test in this process, and a font
loaded by another test can change the glyphs this snapshot draws
(cosmic-text falls back to any loaded face for missing glyphs).
Run visual snapshot tests with `cargo nextest run`, which starts a
process per test. Semantic snapshots and actions work under cargo test.";
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SnapshotError {
#[error("{what} needs a process per test\n\n{}", PER_TEST_REASON)]
NeedsProcessPerTest {
what: String,
},
#[error(
"the program loads a font of {bytes} bytes that is not one of icedwright's bundled fonts, in a process several tests share\n\n{}\n An app with its own fonts runs its harness tests under nextest.",
PER_TEST_REASON
)]
FontsNeedProcessPerTest {
bytes: usize,
},
#[error(
"this harness loads {now} program fonts that differ from the {before} an earlier harness in this test loaded; a font loaded once stays loaded for the whole process, so give every harness in one test the same fonts"
)]
FontSetChanged {
before: usize,
now: usize,
},
#[error(
"system_fonts(true) keeps the machine's fonts, in a process several tests share\n\n{}",
PER_TEST_REASON
)]
SystemFontsNeedProcessPerTest,
#[error(
"this harness asks for {now:?} fonts but an earlier harness in this test loaded {before:?} fonts, which stay loaded for the whole process; give every harness in one test the same system_fonts setting"
)]
FontModeChanged {
before: fonts::FontMode,
now: fonts::FontMode,
},
#[error(
"the semantic snapshot {name} differs between the {theme} and {other} themes, but its name leaves the theme out; pass SemanticOptions::new().per_theme(true) to keep one snapshot per theme\n\n{theme}:\n{yaml}\n\n{other}:\n{other_yaml}"
)]
ThemesDisagree {
name: String,
theme: &'static str,
yaml: String,
other: &'static str,
other_yaml: String,
},
#[error(
"Caret::Show keeps the caret of {node}, which blinks on the real clock, so the snapshot would depend on when it was taken; use Caret::Hide, or a widget that declares caret-clock = \"probe\""
)]
CaretOnRealClock {
node: String,
},
#[error(
"the frame never stopped changing after {frames} frames; the last two are {previous} and {last}"
)]
NeverStable {
frames: u32,
previous: PathBuf,
last: PathBuf,
},
#[error("the semantic YAML of {subject} never stopped changing after {frames} frames")]
NeverStableYaml {
subject: String,
frames: u32,
},
#[error("{0}")]
Visual(Box<VisualFailure>),
#[error("{subject} does not match the template\n\n{report}")]
Semantic {
subject: String,
report: Box<MatchReport>,
},
#[error(transparent)]
Template(#[from] TemplateError),
#[error(transparent)]
Crop(#[from] icedwright_check::CropError),
#[error("the PNG could not be {action}: {message}")]
Png {
action: &'static str,
message: String,
},
#[error(
"{name:?} cannot name a snapshot: use letters, digits, `-`, `_` and `.`, and no path separators"
)]
InvalidName {
name: String,
},
#[error("{0}")]
Location(String),
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum VisualKind {
New,
Changed(DiffResult),
Resized(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct VisualFailure {
pub snapshot: String,
pub kind: VisualKind,
pub fonts_changed: bool,
pub baseline: PathBuf,
pub written: Vec<PathBuf>,
pub notes: Vec<String>,
}
impl fmt::Display for VisualFailure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.fonts_changed {
writeln!(
f,
"the baseline was drawn with another font database, which can explain every changed glyph at once"
)?;
}
match &self.kind {
VisualKind::New => writeln!(
f,
"new snapshot {}, review with cargo icedwright review (or set ICEDWRIGHT_UPDATE=always)",
self.snapshot
)?,
VisualKind::Changed(result) => {
writeln!(f, "snapshot {} differs: {result}", self.snapshot)?;
}
VisualKind::Resized(why) => writeln!(f, "snapshot {}: {why}", self.snapshot)?,
}
writeln!(f, " baseline: {}", self.baseline.display())?;
for path in &self.written {
writeln!(f, " wrote: {}", path.display())?;
}
for note in &self.notes {
writeln!(f, " note: {note}")?;
}
Ok(())
}
}