|
| 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(¤t_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(¤t_path)?, |
| 94 | + Some("report") => { |
| 95 | + let output_path = test_dir.join("report.html"); |
| 96 | + create_report(¤t_path, &snapshot_path, &output_path)?; |
| 97 | + } |
| 98 | + Some("dead-snapshots") => detect_dead_snapshots(¤t_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