mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 10:15:00 +01:00
craft-testkit: harden PNG decoding and keep golden verdicts
- decode_png rejects a bad CRC on any ancillary chunk, before or after the image data, instead of skipping the chunk. - decode_png reads the output color type after read_info and checks the size after conversion to RGBA, and both sizes against isize::MAX, before it allocates the frame buffer. Gray and RGBA frames keep the decoder's buffer instead of copying it. - PNG encoding streams compressed data into the bounded sink, so an output over max_input_bytes stops compression early. - When the failure images of a golden mismatch cannot be encoded or written, check_golden returns GoldenMismatchUnsaved with the verdict and the error instead of the error alone. - max_decoded_bytes documents the decoder working-memory budget as best effort; the returned pixels stay a hard limit. - Module docs use code spans for items that do not build for wasm32. - Comparison tests call check_golden_with with blessing off, and the assert_similar! tests run in a child process without the bless variable.
This commit is contained in:
1 parent
52ce378f83
commit
0a3dd3dcbf
2 files changed
+199
-59
No files matched your search
+78
-34
@@ -2,8 +2,8 @@
|
||||
//! tolerance with an optional 3×3 neighborhood, a difference image, PNG reading and writing with
|
||||
//! size limits, and golden reference checks.
|
||||
//!
|
||||
//! Every function returns a [`Result`]. [`assert_similar!`](crate::assert_similar) is the one form
|
||||
//! that fails a test, and it panics in the calling test.
|
||||
//! Every function returns a [`Result`]. `assert_similar!` is the one form that fails a test, and it
|
||||
//! panics in the calling test.
|
||||
//!
|
||||
//! Statistics compare raw 8-bit sample values. PNG decoding does not apply gamma, ICC or sRGB
|
||||
//! chunks, so a reference compares as the bytes it stores.
|
||||
@@ -23,7 +23,7 @@ use std::{
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
};
|
||||
|
||||
/// The bless variable [`GoldenOptions::new`] uses.
|
||||
/// The bless variable `GoldenOptions::new` uses.
|
||||
pub const DEFAULT_BLESS_VAR: &str = "CRAFT_BLESS";
|
||||
|
||||
/// The most failure image pairs [`check_golden`] writes for one name in one folder.
|
||||
@@ -56,6 +56,9 @@ pub enum ImageError {
|
||||
MissingReference { path: PathBuf, bless_var: String },
|
||||
/// The image is outside the tolerance; the actual and difference images were written.
|
||||
GoldenMismatch(Box<GoldenMismatch>),
|
||||
/// The image is outside the tolerance, and the actual and difference images could not be
|
||||
/// encoded or written.
|
||||
GoldenMismatchUnsaved(Box<UnsavedMismatch>),
|
||||
/// The PNG stream is malformed, truncated or uses an unsupported form.
|
||||
Decode(String),
|
||||
/// The PNG encoder failed.
|
||||
@@ -77,6 +80,17 @@ pub struct GoldenMismatch {
|
||||
pub diff: PathBuf,
|
||||
}
|
||||
|
||||
/// The details of a failed golden check whose failure images could not be saved.
|
||||
#[derive(Debug)]
|
||||
pub struct UnsavedMismatch {
|
||||
/// The reference image.
|
||||
pub reference: PathBuf,
|
||||
/// The comparison that failed.
|
||||
pub verdict: Verdict,
|
||||
/// Why the actual or difference image could not be encoded or written.
|
||||
pub error: ImageError,
|
||||
}
|
||||
|
||||
impl fmt::Display for ImageError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
@@ -105,6 +119,9 @@ impl fmt::Display for ImageError {
|
||||
m.diff.display()
|
||||
)
|
||||
}
|
||||
Self::GoldenMismatchUnsaved(m) => {
|
||||
write!(f, "{}: {}; the actual and difference images could not be written: {}", m.reference.display(), m.verdict, m.error)
|
||||
}
|
||||
Self::Decode(e) => write!(f, "PNG decoding failed: {e}"),
|
||||
Self::Encode(e) => write!(f, "PNG encoding failed: {e}"),
|
||||
Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
|
||||
@@ -116,6 +133,7 @@ impl std::error::Error for ImageError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Io { source, .. } => Some(source),
|
||||
Self::GoldenMismatchUnsaved(m) => Some(&m.error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -537,8 +555,10 @@ pub struct PngLimits {
|
||||
pub max_height: u32,
|
||||
/// Largest encoded PNG, read or written, in bytes.
|
||||
pub max_input_bytes: usize,
|
||||
/// Largest decoded output in bytes, after conversion to 8-bit gray or RGBA. The decoder's own
|
||||
/// working memory is held to the same number.
|
||||
/// Largest decoded output in bytes, after conversion to 8-bit gray or RGBA. The returned pixels
|
||||
/// never exceed it. The decoder also takes it as a budget for its own working memory, but
|
||||
/// applies that budget on a best-effort basis: it allocates small fixed buffers, such as one for
|
||||
/// chunk data, before it checks the budget.
|
||||
pub max_decoded_bytes: usize,
|
||||
}
|
||||
|
||||
@@ -607,12 +627,17 @@ fn to_usize(n: u32) -> Result<usize, ImageError> {
|
||||
///
|
||||
/// Palette images and `tRNS` chunks are expanded, samples under 8 bits are scaled to 8 bits, and
|
||||
/// 16-bit samples keep their high byte. Interlaced images are returned whole. An animated PNG
|
||||
/// returns its default image. The chunks after the image, through `IEND`, are read and their CRCs
|
||||
/// checked. The input size, the header's width and height, and the decoded
|
||||
/// size are each checked against `limits` before the buffer for them is allocated.
|
||||
/// returns its default image. The chunks after the image, through `IEND`, are read, and the CRC of
|
||||
/// every chunk, ancillary chunks included, is checked. The input size, the header's width and
|
||||
/// height, and the decoded size, both as the decoder returns it and after conversion to gray or
|
||||
/// RGBA, are each checked against `limits` and against the largest buffer the platform can
|
||||
/// allocate before the buffer for them is allocated.
|
||||
pub fn decode_png(bytes: &[u8], limits: PngLimits) -> Result<DecodedPng, ImageError> {
|
||||
limit("PNG input bytes", bytes.len() as u64, limits.max_input_bytes as u64)?;
|
||||
let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: limits.max_decoded_bytes });
|
||||
let mut options = png::DecodeOptions::default();
|
||||
options.set_skip_ancillary_crc_failures(false);
|
||||
let mut decoder = png::Decoder::new_with_options(Cursor::new(bytes), options);
|
||||
decoder.set_limits(png::Limits { bytes: limits.max_decoded_bytes });
|
||||
decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
|
||||
let (width, height) = {
|
||||
let info = decoder.read_header_info().map_err(decoding_error)?;
|
||||
@@ -621,34 +646,42 @@ pub fn decode_png(bytes: &[u8], limits: PngLimits) -> Result<DecodedPng, ImageEr
|
||||
limit("PNG width", width, limits.max_width)?;
|
||||
limit("PNG height", height, limits.max_height)?;
|
||||
let mut reader = decoder.read_info().map_err(decoding_error)?;
|
||||
let size = reader.output_buffer_size().ok_or_else(|| ImageError::LimitExceeded("the decoded PNG does not fit in memory".into()))?;
|
||||
limit("decoded PNG bytes", size as u64, limits.max_decoded_bytes as u64)?;
|
||||
let mut buf = vec![0u8; size];
|
||||
let info = reader.next_frame(&mut buf).map_err(decoding_error)?;
|
||||
reader.finish().map_err(decoding_error)?;
|
||||
if info.bit_depth != png::BitDepth::Eight || (info.width, info.height) != (width, height) {
|
||||
return Err(ImageError::Decode(format!("unexpected {:?}-bit {}x{} output", info.bit_depth, info.width, info.height)));
|
||||
}
|
||||
let pixels = to_usize(width)?.checked_mul(to_usize(height)?).ok_or(ImageError::InvalidDimensions { width, height })?;
|
||||
let channels = match info.color_type {
|
||||
let channels = match reader.output_color_type().0 {
|
||||
png::ColorType::Grayscale => 1,
|
||||
png::ColorType::GrayscaleAlpha => 2,
|
||||
png::ColorType::Rgb => 3,
|
||||
png::ColorType::Rgba => 4,
|
||||
png::ColorType::Indexed => return Err(ImageError::Decode("the palette was not expanded".into())),
|
||||
};
|
||||
if channels != 1 {
|
||||
limit("decoded PNG bytes", pixels as u128 * 4, limits.max_decoded_bytes as u64)?;
|
||||
let size = reader.output_buffer_size().ok_or_else(|| ImageError::LimitExceeded("the decoded PNG does not fit in memory".into()))?;
|
||||
limit("decoded PNG bytes", size as u64, limits.max_decoded_bytes as u64)?;
|
||||
limit("decoded PNG buffer bytes", size as u64, isize::MAX as u64)?;
|
||||
let output = if channels == 1 { pixels as u128 } else { pixels as u128 * 4 };
|
||||
limit("decoded PNG bytes", output, limits.max_decoded_bytes as u64)?;
|
||||
limit("decoded PNG buffer bytes", output, isize::MAX as u64)?;
|
||||
let mut buf = vec![0u8; size];
|
||||
let info = reader.next_frame(&mut buf).map_err(decoding_error)?;
|
||||
reader.finish().map_err(decoding_error)?;
|
||||
if info.bit_depth != png::BitDepth::Eight || (info.width, info.height) != (width, height) {
|
||||
return Err(ImageError::Decode(format!("unexpected {:?}-bit {}x{} output", info.bit_depth, info.width, info.height)));
|
||||
}
|
||||
let data = pixels
|
||||
.checked_mul(channels)
|
||||
.and_then(|n| buf.get(..n))
|
||||
.ok_or_else(|| ImageError::Decode("the decoded frame is shorter than its size".into()))?;
|
||||
if info.color_type != reader.output_color_type().0 {
|
||||
return Err(ImageError::Decode(format!("unexpected {:?} output", info.color_type)));
|
||||
}
|
||||
let samples = pixels.checked_mul(channels).filter(|&n| n <= buf.len());
|
||||
let Some(samples) = samples else {
|
||||
return Err(ImageError::Decode("the decoded frame is shorter than its size".into()));
|
||||
};
|
||||
if channels == 1 || channels == 4 {
|
||||
// The frame is already in its final form: keep the buffer and drop any row padding.
|
||||
buf.truncate(samples);
|
||||
return Ok(if channels == 1 { DecodedPng::Gray { width, height, pixels: buf } } else { DecodedPng::Rgba { width, height, pixels: buf } });
|
||||
}
|
||||
let data = buf.get(..samples).unwrap_or_default();
|
||||
Ok(match channels {
|
||||
1 => DecodedPng::Gray { width, height, pixels: data.to_vec() },
|
||||
2 => DecodedPng::Rgba { width, height, pixels: data.as_chunks::<2>().0.iter().flat_map(|p| [p[0], p[0], p[0], p[1]]).collect() },
|
||||
3 => DecodedPng::Rgba { width, height, pixels: data.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], 255]).collect() },
|
||||
_ => DecodedPng::Rgba { width, height, pixels: data.to_vec() },
|
||||
_ => DecodedPng::Rgba { width, height, pixels: data.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], 255]).collect() },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -688,7 +721,11 @@ fn write_stream(sink: &mut BoundedSink, width: u32, height: u32, color: png::Col
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
encoder.set_compression(png::Compression::High);
|
||||
let mut writer = encoder.write_header()?;
|
||||
writer.write_image_data(data)?;
|
||||
// The stream writer hands each compressed block to the sink as it fills, so a sink that refuses
|
||||
// more bytes stops compression early.
|
||||
let mut stream = writer.stream_writer()?;
|
||||
std::io::Write::write_all(&mut stream, data)?;
|
||||
stream.finish()?;
|
||||
writer.finish()
|
||||
}
|
||||
|
||||
@@ -872,7 +909,9 @@ fn write_failure(options: &GoldenOptions<'_>, actual: &[u8], diff: &[u8]) -> Res
|
||||
///
|
||||
/// A missing reference returns [`ImageError::MissingReference`] unless blessing. When the
|
||||
/// comparison fails, the actual and difference images are written as new files inside
|
||||
/// `options.failures_dir`, and [`ImageError::GoldenMismatch`] names them. A reference that would
|
||||
/// `options.failures_dir`, and [`ImageError::GoldenMismatch`] names them. When they cannot be
|
||||
/// encoded or written, [`ImageError::GoldenMismatchUnsaved`] carries the verdict together with
|
||||
/// that error. A reference that would
|
||||
/// be larger than `options.limits.max_input_bytes` is not written.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn check_golden(reference: &Path, actual: Rgba8<'_>, tolerance: Tolerance, options: GoldenOptions<'_>) -> Result<Verdict, ImageError> {
|
||||
@@ -913,11 +952,16 @@ fn check_golden_with(
|
||||
if verdict.passed() {
|
||||
return Ok(verdict);
|
||||
}
|
||||
let diff = diff_image(expected, actual)?;
|
||||
let actual_png = encode_png(actual, options.limits)?;
|
||||
let diff_png = encode_png(Rgba8::new(width, height, &diff)?, options.limits)?;
|
||||
let (actual_path, diff_path) = write_failure(&options, &actual_png, &diff_png)?;
|
||||
Err(ImageError::GoldenMismatch(Box::new(GoldenMismatch { reference: reference.to_path_buf(), verdict, actual: actual_path, diff: diff_path })))
|
||||
let saved = diff_image(expected, actual).and_then(|diff| {
|
||||
let actual_png = encode_png(actual, options.limits)?;
|
||||
let diff_png = encode_png(Rgba8::new(width, height, &diff)?, options.limits)?;
|
||||
write_failure(&options, &actual_png, &diff_png)
|
||||
});
|
||||
let reference = reference.to_path_buf();
|
||||
Err(match saved {
|
||||
Ok((actual, diff)) => ImageError::GoldenMismatch(Box::new(GoldenMismatch { reference, verdict, actual, diff })),
|
||||
Err(error) => ImageError::GoldenMismatchUnsaved(Box::new(UnsavedMismatch { reference, verdict, error })),
|
||||
})
|
||||
}
|
||||
|
||||
/// Fails the test that called [`assert_similar!`](crate::assert_similar); the panic names the
|
||||
|
||||
@@ -3,14 +3,24 @@ use std::ffi::OsStr;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::{
|
||||
BoundedSink, DecodedPng, GoldenOptions, Gray8, ImageError, PngLimits, Rgba8, Tolerance, check_golden, check_golden_with, compare_gray,
|
||||
compare_rgba, decode_png, diff_gray, diff_image, diff_rgba, encode_gray_png, encode_png, is_bless_value, psnr, read_png, write_gray_png,
|
||||
write_png, write_stream,
|
||||
BoundedSink, DecodedPng, GoldenOptions, Gray8, ImageError, PngLimits, Rgba8, Tolerance, check_golden_with, compare_gray, compare_rgba,
|
||||
decode_png, diff_gray, diff_image, diff_rgba, encode_gray_png, encode_png, is_bless_value, psnr, read_png, write_gray_png, write_png,
|
||||
write_stream,
|
||||
};
|
||||
|
||||
/// A variable no test run sets, so `check_golden` compares instead of blessing.
|
||||
/// The bless variable of these tests. Comparison cases call `check_golden_with` with blessing off,
|
||||
/// and the `assert_similar!` cases run in a child process with the variable removed, so a value
|
||||
/// inherited from the environment cannot turn a comparison into a bless.
|
||||
const NO_BLESS: &str = "CRAFT_TESTKIT_SELF_TEST_BLESS";
|
||||
|
||||
/// Set in the child processes that `run_child` starts.
|
||||
const CHILD_MARKER: &str = "CRAFT_TESTKIT_IMAGE_CHILD";
|
||||
|
||||
/// A golden check that compares and never blesses.
|
||||
fn compare_golden(reference: &Path, actual: Rgba8<'_>, tolerance: Tolerance, options: GoldenOptions<'_>) -> Result<super::Verdict, ImageError> {
|
||||
check_golden_with(reference, actual, tolerance, options, false)
|
||||
}
|
||||
|
||||
fn ramp(w: u32, h: u32, alpha: u8) -> Vec<u8> {
|
||||
(0..w * h).flat_map(|i| [(i % w * 255 / w) as u8, (i / w * 255 / h) as u8, 77, alpha]).collect()
|
||||
}
|
||||
@@ -513,6 +523,27 @@ fn png_without_a_valid_end_is_an_error() {
|
||||
assert!(matches!(decode_png(&bad_crc, PngLimits::DEFAULT), Err(ImageError::Decode(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bad_ancillary_crc_is_an_error() {
|
||||
let text: &[u8] = b"Comment\0x";
|
||||
// A tEXt chunk between the image data and IEND.
|
||||
let plain = png_file(2, 1, 8, 0, 0, &[], &[0, 1, 2]);
|
||||
let (body, iend) = plain.split_at(plain.len() - 12);
|
||||
let mut after = body.to_vec();
|
||||
chunk(&mut after, b"tEXt", text);
|
||||
after.extend_from_slice(iend);
|
||||
assert!(decode_png(&after, PngLimits::DEFAULT).is_ok(), "a valid tEXt chunk is accepted");
|
||||
let crc = after.len() - iend.len() - 1;
|
||||
after[crc] ^= 0xFF;
|
||||
assert!(matches!(decode_png(&after, PngLimits::DEFAULT), Err(ImageError::Decode(_))), "after the image");
|
||||
// A tEXt chunk between IHDR and the image data: signature, IHDR, then length, type and data.
|
||||
let mut before = png_file(2, 1, 8, 0, 0, &[(b"tEXt", text)], &[0, 1, 2]);
|
||||
assert!(decode_png(&before, PngLimits::DEFAULT).is_ok());
|
||||
let crc = 8 + 25 + 8 + text.len() + 3;
|
||||
before[crc] ^= 0xFF;
|
||||
assert!(matches!(decode_png(&before, PngLimits::DEFAULT), Err(ImageError::Decode(_))), "before the image");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn png_limits_reject_header_before_allocation() {
|
||||
// The image data is a few bytes, so only the header can trigger the error.
|
||||
@@ -546,10 +577,21 @@ fn png_decoded_byte_limit() {
|
||||
let png = png_file(4, 4, 8, 2, 0, &[], &scanlines);
|
||||
let with = |max_decoded_bytes| decode_png(&png, PngLimits { max_decoded_bytes, ..PngLimits::DEFAULT });
|
||||
assert!(with(64).is_ok());
|
||||
assert!(matches!(with(50), Err(ImageError::LimitExceeded(_))), "checked after conversion");
|
||||
assert!(matches!(with(50), Err(ImageError::LimitExceeded(_))), "the converted size is checked");
|
||||
assert!(matches!(with(47), Err(ImageError::LimitExceeded(_))), "checked before allocation");
|
||||
}
|
||||
|
||||
/// The size after conversion to RGBA is checked from the header, before any image data is read,
|
||||
/// so a stream whose image data is a few bytes still fails on the limit.
|
||||
#[test]
|
||||
fn the_converted_size_is_checked_before_decoding() {
|
||||
// 4x4 RGB, 48 bytes as decoded and 64 as RGBA, with image data that would not decode.
|
||||
let png = png_file(4, 4, 8, 2, 0, &[], &[0]);
|
||||
let err = decode_png(&png, PngLimits { max_decoded_bytes: 50, ..PngLimits::DEFAULT }).unwrap_err();
|
||||
assert!(matches!(err, ImageError::LimitExceeded(_)), "{err:?}");
|
||||
assert!(matches!(decode_png(&png, PngLimits::DEFAULT), Err(ImageError::Decode(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoding_refuses_an_image_that_would_not_read_back() {
|
||||
// An image is only written when the same limits can decode it again.
|
||||
@@ -618,15 +660,15 @@ fn check_golden_accepts_within_tolerance_and_reports_failures() {
|
||||
let path = dir.path().join("ramp.png");
|
||||
let img = ramp(32, 16, 255);
|
||||
write_png(&path, rgba(32, 16, &img), PngLimits::DEFAULT).unwrap();
|
||||
assert!(check_golden(&path, rgba(32, 16, &img), Tolerance::EXACT, options(&failures, "ramp")).is_ok());
|
||||
assert!(compare_golden(&path, rgba(32, 16, &img), Tolerance::EXACT, options(&failures, "ramp")).is_ok());
|
||||
let mut near = img.clone();
|
||||
near[5] += 1;
|
||||
assert!(check_golden(&path, rgba(32, 16, &near), render(), options(&failures, "ramp")).is_ok());
|
||||
assert!(compare_golden(&path, rgba(32, 16, &near), render(), options(&failures, "ramp")).is_ok());
|
||||
let mut far = img.clone();
|
||||
far.iter_mut().step_by(4).for_each(|v| *v = 255 - *v);
|
||||
let e = check_golden(&path, rgba(32, 16, &far), render(), options(&failures, "ramp")).unwrap_err();
|
||||
let e = compare_golden(&path, rgba(32, 16, &far), render(), options(&failures, "ramp")).unwrap_err();
|
||||
assert!(e.to_string().contains("PSNR"), "{e}");
|
||||
let missing = check_golden(&dir.path().join("missing.png"), rgba(32, 16, &img), render(), options(&failures, "ramp")).unwrap_err();
|
||||
let missing = compare_golden(&dir.path().join("missing.png"), rgba(32, 16, &img), render(), options(&failures, "ramp")).unwrap_err();
|
||||
assert!(matches!(missing, ImageError::MissingReference { .. }) && missing.to_string().contains(NO_BLESS), "{missing}");
|
||||
}
|
||||
|
||||
@@ -638,21 +680,21 @@ fn the_check_passes_within_the_tolerance_and_fails_past_it() {
|
||||
let (w, h) = (20u32, 10u32);
|
||||
let base: Vec<u8> = (0..w * h).flat_map(|i| [i as u8, 100, 200, 255]).collect();
|
||||
write_png(&golden, rgba(w, h, &base), PngLimits::DEFAULT).unwrap();
|
||||
assert!(check_golden(&golden, rgba(w, h, &base), per_pixel(), options(&failures, "self-test-same")).is_ok());
|
||||
assert!(compare_golden(&golden, rgba(w, h, &base), per_pixel(), options(&failures, "self-test-same")).is_ok());
|
||||
// Small differences everywhere are within the channel tolerance.
|
||||
let near: Vec<u8> = base.iter().map(|c| c.saturating_sub(2)).collect();
|
||||
assert!(check_golden(&golden, rgba(w, h, &near), per_pixel(), options(&failures, "self-test-near")).is_ok());
|
||||
assert!(compare_golden(&golden, rgba(w, h, &near), per_pixel(), options(&failures, "self-test-near")).is_ok());
|
||||
// A whole row changed is not.
|
||||
let mut off = base.clone();
|
||||
for px in off.as_chunks_mut::<4>().0.iter_mut().take(w as usize) {
|
||||
px[1] = 0;
|
||||
}
|
||||
let m = mismatch(check_golden(&golden, rgba(w, h, &off), per_pixel(), options(&failures, "self-test-off")));
|
||||
let m = mismatch(compare_golden(&golden, rgba(w, h, &off), per_pixel(), options(&failures, "self-test-off")));
|
||||
assert_eq!(m.verdict.bad_pixels, 20);
|
||||
assert!(m.actual.exists() && m.diff.exists());
|
||||
assert!(m.diff.ends_with("self-test-off.diff.png"), "{}", m.diff.display());
|
||||
// A missing golden fails and says how to make one.
|
||||
let err = check_golden(&dir.path().join("none.png"), rgba(w, h, &base), per_pixel(), options(&failures, "self-test-missing")).unwrap_err();
|
||||
let err = compare_golden(&dir.path().join("none.png"), rgba(w, h, &base), per_pixel(), options(&failures, "self-test-missing")).unwrap_err();
|
||||
assert!(err.to_string().contains(&format!("{NO_BLESS}=1")), "{err}");
|
||||
}
|
||||
|
||||
@@ -705,13 +747,13 @@ fn gray_reference_expansion_respects_the_decoded_limit() {
|
||||
let shades = gray(4, 4, 9);
|
||||
write_gray_png(&reference, g(4, 4, &shades), PngLimits::DEFAULT).unwrap();
|
||||
let px: Vec<u8> = shades.iter().flat_map(|&v| [v, v, v, 255]).collect();
|
||||
assert!(check_golden(&reference, rgba(4, 4, &px), Tolerance::EXACT, options(dir.path(), "g")).is_ok());
|
||||
assert!(compare_golden(&reference, rgba(4, 4, &px), Tolerance::EXACT, options(dir.path(), "g")).is_ok());
|
||||
// The 16-byte gray reference fits, but its 64-byte RGBA form does not.
|
||||
let small = GoldenOptions { limits: PngLimits { max_decoded_bytes: 16, ..PngLimits::DEFAULT }, ..options(dir.path(), "g") };
|
||||
let err = check_golden(&reference, rgba(4, 4, &px), Tolerance::EXACT, small).unwrap_err();
|
||||
let err = compare_golden(&reference, rgba(4, 4, &px), Tolerance::EXACT, small).unwrap_err();
|
||||
assert!(matches!(err, ImageError::LimitExceeded(_)), "{err:?}");
|
||||
let wide = ramp(8, 2, 255);
|
||||
let err = check_golden(&reference, rgba(8, 2, &wide), Tolerance::EXACT, options(dir.path(), "g")).unwrap_err();
|
||||
let err = compare_golden(&reference, rgba(8, 2, &wide), Tolerance::EXACT, options(dir.path(), "g")).unwrap_err();
|
||||
assert!(matches!(err, ImageError::SizeMismatch { a: (4, 4), b: (8, 2) }), "{err:?}");
|
||||
}
|
||||
|
||||
@@ -739,19 +781,19 @@ fn failure_name_rejects_path_components() {
|
||||
let long = "a".repeat(101);
|
||||
for name in ["", "..", "../escape", "a/b", "a\\b", ".hidden", "C:x", "a b", "x\0", "trailing.", "CON", "nul.x", "com1", "LPT9.png", long.as_str()]
|
||||
{
|
||||
let err = check_golden(&reference, rgba(2, 2, &other), Tolerance::EXACT, options(&failures, name)).unwrap_err();
|
||||
let err = compare_golden(&reference, rgba(2, 2, &other), Tolerance::EXACT, options(&failures, name)).unwrap_err();
|
||||
assert!(matches!(err, ImageError::InvalidName(_)), "{name:?}: {err:?}");
|
||||
}
|
||||
assert!(!failures.exists() && !dir.path().join("escape.actual.png").exists());
|
||||
for name in ["ok-name_1.v2", "_x", "com0", "console"] {
|
||||
assert!(
|
||||
matches!(check_golden(&reference, rgba(2, 2, &other), Tolerance::EXACT, options(&failures, name)), Err(ImageError::GoldenMismatch(_))),
|
||||
matches!(compare_golden(&reference, rgba(2, 2, &other), Tolerance::EXACT, options(&failures, name)), Err(ImageError::GoldenMismatch(_))),
|
||||
"{name:?}"
|
||||
);
|
||||
}
|
||||
for var in ["", "A=B", "A\0B"] {
|
||||
let bad = GoldenOptions { bless_var: var, ..options(&failures, "ok") };
|
||||
assert!(matches!(check_golden(&reference, rgba(2, 2, &px), Tolerance::EXACT, bad), Err(ImageError::InvalidName(_))), "{var:?}");
|
||||
assert!(matches!(compare_golden(&reference, rgba(2, 2, &px), Tolerance::EXACT, bad), Err(ImageError::InvalidName(_))), "{var:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -763,7 +805,7 @@ fn failure_artifacts_stay_in_folder() {
|
||||
let px = ramp(3, 3, 255);
|
||||
write_png(&reference, rgba(3, 3, &px), PngLimits::DEFAULT).unwrap();
|
||||
let other = ramp(3, 3, 1);
|
||||
let m = mismatch(check_golden(&reference, rgba(3, 3, &other), Tolerance::EXACT, options(&failures, "case")));
|
||||
let m = mismatch(compare_golden(&reference, rgba(3, 3, &other), Tolerance::EXACT, options(&failures, "case")));
|
||||
assert_eq!(m.actual.parent(), Some(failures.as_path()));
|
||||
assert_eq!(m.diff.parent(), Some(failures.as_path()));
|
||||
assert_eq!(read_png(&m.actual, PngLimits::DEFAULT).unwrap(), DecodedPng::Rgba { width: 3, height: 3, pixels: other });
|
||||
@@ -779,9 +821,9 @@ fn failure_artifacts_do_not_overwrite() {
|
||||
write_png(&reference, rgba(3, 3, &px), PngLimits::DEFAULT).unwrap();
|
||||
let first_img = ramp(3, 3, 1);
|
||||
let second_img = ramp(3, 3, 2);
|
||||
let first = mismatch(check_golden(&reference, rgba(3, 3, &first_img), Tolerance::EXACT, options(dir.path(), "same")));
|
||||
let first = mismatch(compare_golden(&reference, rgba(3, 3, &first_img), Tolerance::EXACT, options(dir.path(), "same")));
|
||||
let kept = std::fs::read(&first.actual).unwrap();
|
||||
let second = mismatch(check_golden(&reference, rgba(3, 3, &second_img), Tolerance::EXACT, options(dir.path(), "same")));
|
||||
let second = mismatch(compare_golden(&reference, rgba(3, 3, &second_img), Tolerance::EXACT, options(dir.path(), "same")));
|
||||
assert_ne!(first.actual, second.actual);
|
||||
assert_ne!(first.diff, second.diff);
|
||||
assert!(second.actual.ends_with("same-1.actual.png"), "{}", second.actual.display());
|
||||
@@ -797,7 +839,7 @@ fn parallel_failures_get_distinct_paths() {
|
||||
let other = ramp(3, 3, 1);
|
||||
let paths: Vec<PathBuf> = std::thread::scope(|s| {
|
||||
let handles: Vec<_> = (0..8)
|
||||
.map(|_| s.spawn(|| mismatch(check_golden(&reference, rgba(3, 3, &other), Tolerance::EXACT, options(dir.path(), "race")))))
|
||||
.map(|_| s.spawn(|| mismatch(compare_golden(&reference, rgba(3, 3, &other), Tolerance::EXACT, options(dir.path(), "race")))))
|
||||
.collect();
|
||||
handles
|
||||
.into_iter()
|
||||
@@ -812,8 +854,58 @@ fn parallel_failures_get_distinct_paths() {
|
||||
assert!(paths.iter().all(|p| p.exists()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mismatch_keeps_its_verdict_when_the_failure_images_cannot_be_written() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let reference = dir.path().join("r.png");
|
||||
let px = ramp(3, 3, 255);
|
||||
write_png(&reference, rgba(3, 3, &px), PngLimits::DEFAULT).unwrap();
|
||||
// The failure folder is a regular file, so the failure images cannot be written.
|
||||
let blocked = dir.path().join("file");
|
||||
std::fs::write(&blocked, b"x").unwrap();
|
||||
let other = ramp(3, 3, 1);
|
||||
let err = compare_golden(&reference, rgba(3, 3, &other), Tolerance::EXACT, options(&blocked, "case")).unwrap_err();
|
||||
let ImageError::GoldenMismatchUnsaved(m) = &err else { panic!("expected an unsaved mismatch, got {err:?}") };
|
||||
assert_eq!(m.verdict.bad_pixels, 9);
|
||||
assert!(!m.verdict.passed());
|
||||
assert!(matches!(m.error, ImageError::Io { .. }), "{:?}", m.error);
|
||||
assert!(err.to_string().contains("pixels differ") && err.to_string().contains("could not be written"), "{err}");
|
||||
assert!(std::error::Error::source(&err).is_some());
|
||||
}
|
||||
|
||||
/// Runs `name`, a test of this binary, in a child process with the bless variable removed and
|
||||
/// `CHILD_MARKER` set, and returns whether it passed and what it printed.
|
||||
fn run_child(name: &str) -> (bool, String) {
|
||||
let out = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", name, "--nocapture", "--test-threads=1"])
|
||||
.env_remove(NO_BLESS)
|
||||
.env(CHILD_MARKER, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
let printed = format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
|
||||
assert!(printed.contains("running 1 test"), "the child did not run the test:\n{printed}");
|
||||
(out.status.success(), printed)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assert_similar_returns_the_verdict() {
|
||||
let (passed, printed) = run_child("image::tests::child_assert_similar_passes");
|
||||
assert!(passed, "the child failed:\n{printed}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assert_similar_fails_the_test() {
|
||||
let (passed, printed) = run_child("image::tests::child_assert_similar_fails");
|
||||
assert!(!passed, "the child passed:\n{printed}");
|
||||
assert!(printed.contains("no reference image"), "{printed}");
|
||||
}
|
||||
|
||||
/// Runs in the child process of `assert_similar_returns_the_verdict`, and passes at once elsewhere.
|
||||
#[test]
|
||||
fn child_assert_similar_passes() {
|
||||
if std::env::var_os(CHILD_MARKER).is_none() {
|
||||
return;
|
||||
}
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let reference = dir.path().join("r.png");
|
||||
let px = ramp(3, 3, 255);
|
||||
@@ -822,9 +914,13 @@ fn assert_similar_returns_the_verdict() {
|
||||
assert!(verdict.passed());
|
||||
}
|
||||
|
||||
/// Runs in the child process of `assert_similar_fails_the_test`, where it must fail, and passes at
|
||||
/// once elsewhere.
|
||||
#[test]
|
||||
#[should_panic(expected = "no reference image")]
|
||||
fn assert_similar_fails_the_test() {
|
||||
fn child_assert_similar_fails() {
|
||||
if std::env::var_os(CHILD_MARKER).is_none() {
|
||||
return;
|
||||
}
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let px = ramp(3, 3, 255);
|
||||
crate::assert_similar!(&dir.path().join("none.png"), rgba(3, 3, &px), Tolerance::EXACT, options(dir.path(), "none"));
|
||||
|
||||
Reference in new issue
Block a user