Skip to content

Commit 37bff51

Browse files
committed
Detecting dead snapshots
1 parent ffe224a commit 37bff51

File tree

10 files changed

+129
-44
lines changed

10 files changed

+129
-44
lines changed

.cargo/config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[alias]
2-
xtask_image_diff_report = "run --package xtask_image_diff_report --"
2+
xtask-test = "run --package xtask_test --"

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ members = [
66
"examples/tiny_skia_render",
77
"examples/swash_render",
88
"examples/vello_editor",
9-
"xtask_image_diff_report",
9+
"xtask_test",
1010
]
1111

1212
[workspace.package]

parley/src/tests/utils/env.rs

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ fn is_accept_mode() -> bool {
7070
.unwrap_or(false)
7171
}
7272

73+
fn is_generate_all_mode() -> bool {
74+
std::env::var("PARLEY_TEST")
75+
.map(|x| x.to_ascii_lowercase() == "generate-all")
76+
.unwrap_or(false)
77+
}
78+
7379
pub(crate) fn load_fonts_dir(collection: &mut Collection, path: &Path) -> std::io::Result<()> {
7480
let paths = std::fs::read_dir(path)?;
7581
for entry in paths {
@@ -255,6 +261,9 @@ impl TestEnv {
255261
let snapshot_path = snapshot_dir().join(&image_name);
256262
let comparison_path = current_imgs_dir().join(&image_name);
257263

264+
if is_generate_all_mode() {
265+
current_img.save_png(&comparison_path).unwrap();
266+
}
258267
if let Err(e) = self.check_images(&current_img, &snapshot_path) {
259268
if is_accept_mode() {
260269
current_img.save_png(&snapshot_path).unwrap();

parley/tests/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ It will update snapshots of the failed tests.
2020
If you want to create a report with image diffs use:
2121

2222
```bash
23-
$ cargo xtask_image_diff_report
23+
$ cargo xtask-test report
2424
```

xtask_image_diff_report/README.md

-7
This file was deleted.

xtask_image_diff_report/src/main.rs

-32
This file was deleted.

xtask_image_diff_report/Cargo.toml xtask_test/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "xtask_image_diff_report"
2+
name = "xtask_test"
33
version = "0.1.0"
44
edition.workspace = true
55
rust-version.workspace = true

xtask_test/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# xtask-test
2+
3+
For creating a report for failed snapshot tests run:
4+
5+
```commandline
6+
$ cargo xtask-test report
7+
```
8+
9+
For showing dead snapshots run:
10+
11+
```commandline
12+
$ cargo xtask-test dead-snapshots
13+
```

xtask_test/src/main.rs

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2024 the Parley Authors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! # `xtask_test`
5+
//!
6+
//! xtask with helper utilities for snapshot testing
7+
8+
use image_diff_review::{CompareConfig, Error, ImageDiff, ReportConfig};
9+
use std::collections::BTreeSet;
10+
use std::ffi::OsStr;
11+
use std::path::{Path, PathBuf};
12+
use std::process::Command;
13+
14+
fn create_report(
15+
current_path: &Path,
16+
snapshot_path: &Path,
17+
output_path: &Path,
18+
) -> Result<(), Error> {
19+
let mut config = CompareConfig::default();
20+
config.set_ignore_left_missing(true);
21+
22+
let mut image_diff = ImageDiff::default();
23+
image_diff.compare_directories(&config, current_path, snapshot_path)?;
24+
25+
let mut report_config = ReportConfig::default();
26+
report_config.set_left_title("Current test");
27+
report_config.set_right_title("Snapshot");
28+
image_diff.create_report(&report_config, output_path, true)?;
29+
Ok(())
30+
}
31+
32+
fn list_image_dir(dir_path: &Path) -> Result<impl Iterator<Item = PathBuf>, std::io::Error> {
33+
Ok(std::fs::read_dir(dir_path)?.filter_map(|entry| {
34+
if let Ok(entry) = entry {
35+
let path = entry.path();
36+
if path
37+
.extension()
38+
.and_then(OsStr::to_str)
39+
.map(|ext| ext == "png")
40+
.unwrap_or(false)
41+
{
42+
Some(path)
43+
} else {
44+
None
45+
}
46+
} else {
47+
None
48+
}
49+
}))
50+
}
51+
52+
fn clean_image_path(dir_path: &Path) -> Result<(), std::io::Error> {
53+
list_image_dir(dir_path)?.try_for_each(|path| std::fs::remove_file(&path))
54+
}
55+
56+
fn detect_dead_snapshots(current_path: &Path, snapshot_path: &Path) -> Result<(), Error> {
57+
clean_image_path(current_path)?;
58+
let cargo = std::env::var("CARGO").unwrap();
59+
Command::new(&cargo)
60+
.arg("test")
61+
.env("PARLEY_TEST", "generate-all")
62+
.status()?;
63+
64+
let current_imgs: BTreeSet<String> = list_image_dir(current_path)?
65+
.map(|r| r.file_name().unwrap().to_string_lossy().to_string())
66+
.collect();
67+
let snapshot_imgs: BTreeSet<String> = list_image_dir(snapshot_path)?
68+
.map(|r| r.file_name().unwrap().to_string_lossy().to_string())
69+
.collect();
70+
let dead_paths: Vec<String> = snapshot_imgs.difference(&current_imgs).cloned().collect();
71+
72+
if dead_paths.is_empty() {
73+
println!("No dead snapshots detected");
74+
} else {
75+
println!("========== DEAD SNAPSHOTS ==========");
76+
for path in &dead_paths {
77+
println!("{}", path);
78+
}
79+
}
80+
clean_image_path(current_path)?;
81+
Ok(())
82+
}
83+
84+
fn main() -> Result<(), Error> {
85+
let test_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
86+
.parent()
87+
.unwrap()
88+
.join("parley")
89+
.join("tests");
90+
let current_path = test_dir.join("current");
91+
let snapshot_path = test_dir.join("snapshots");
92+
match std::env::args().nth(1).as_deref() {
93+
Some("clean") => clean_image_path(&current_path)?,
94+
Some("report") => {
95+
let output_path = test_dir.join("report.html");
96+
create_report(&current_path, &snapshot_path, &output_path)?;
97+
}
98+
Some("dead-snapshots") => detect_dead_snapshots(&current_path, &snapshot_path)?,
99+
_ => println!("Invalid command\n\nCommands:\n- clean: Clean 'current' directory\n- report: Create report with image diffs\n- dead-snapshots: print dead snapshots"),
100+
}
101+
Ok(())
102+
}

0 commit comments

Comments
 (0)