|
1 | | -#![cfg(not(target_arch = "wasm32"))] |
2 | | - |
| 1 | +// With the custom test harness we unfortunately cannot use #![cfg(not(target_arch = "wasm32"))]. |
| 2 | +use std::ffi::OsStr; |
3 | 3 | use std::fs; |
4 | 4 | use std::path::PathBuf; |
5 | 5 |
|
| 6 | +#[cfg(not(target_arch = "wasm32"))] |
6 | 7 | use assert_cmd::cargo::cargo_bin_cmd; |
7 | | - |
8 | | -#[test] |
9 | | -fn generates_svg_for_all_fixtures() -> Result<(), Box<dyn std::error::Error>> { |
| 8 | +#[cfg(not(target_arch = "wasm32"))] |
| 9 | +use libtest_mimic::Failed; |
| 10 | +#[cfg(not(target_arch = "wasm32"))] |
| 11 | +use similar_asserts::SimpleDiff; |
| 12 | +#[cfg(not(target_arch = "wasm32"))] |
| 13 | +use tempfile::TempDir; |
| 14 | + |
| 15 | +#[cfg(target_arch = "wasm32")] |
| 16 | +fn main() {} |
| 17 | + |
| 18 | +#[cfg(not(target_arch = "wasm32"))] |
| 19 | +fn main() { |
10 | 20 | let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
11 | 21 | let input_dir = manifest_dir.join("tests/input"); |
12 | | - let output_dir = manifest_dir.join("tests/output"); |
13 | | - let svg_output_dir = output_dir.join("svg"); |
14 | | - let png_output_dir = output_dir.join("png"); |
15 | 22 |
|
16 | | - assert!( |
17 | | - input_dir.exists(), |
18 | | - "tests/input directory should exist for CLI fixtures" |
| 23 | + let in_paths: Vec<_> = fs::read_dir(&input_dir) |
| 24 | + .expect("read dir") |
| 25 | + .flatten() |
| 26 | + .map(|entry| entry.path()) |
| 27 | + .collect(); |
| 28 | + |
| 29 | + assert!(in_paths.len() > 0, "expected tests/input to contain files"); |
| 30 | + |
| 31 | + assert_eq!( |
| 32 | + in_paths |
| 33 | + .iter() |
| 34 | + .filter(|input| input.extension().and_then(OsStr::to_str) != Some("mmd")) |
| 35 | + .map(|input| input.file_name().unwrap()) |
| 36 | + .collect::<Vec<_>>(), |
| 37 | + Vec::<&OsStr>::new(), |
| 38 | + "expected files in tests/input/ to have the .mmd extension" |
19 | 39 | ); |
20 | 40 |
|
21 | | - fs::create_dir_all(&svg_output_dir)?; |
22 | | - fs::create_dir_all(&png_output_dir)?; |
23 | | - |
24 | | - for entry in fs::read_dir(&input_dir)? { |
25 | | - let entry = entry?; |
26 | | - let path = entry.path(); |
27 | | - |
28 | | - if path.extension().and_then(|ext| ext.to_str()) != Some("mmd") { |
29 | | - continue; |
30 | | - } |
31 | | - |
32 | | - let stem = path |
33 | | - .file_stem() |
34 | | - .and_then(|s| s.to_str()) |
35 | | - .ok_or("failed to read fixture stem")?; |
36 | | - let svg_output_path = svg_output_dir.join(format!("{stem}.svg")); |
37 | | - let png_output_path = png_output_dir.join(format!("{stem}.png")); |
38 | | - |
39 | | - if svg_output_path.exists() { |
40 | | - fs::remove_file(&svg_output_path)?; |
41 | | - } |
42 | | - |
43 | | - if png_output_path.exists() { |
44 | | - fs::remove_file(&png_output_path)?; |
45 | | - } |
46 | | - |
47 | | - let mut cmd = cargo_bin_cmd!("oxdraw"); |
48 | | - cmd.arg("--input") |
49 | | - .arg(&path) |
50 | | - .arg("--output") |
51 | | - .arg(&svg_output_path) |
52 | | - .arg("--output-format") |
53 | | - .arg("svg"); |
54 | | - |
55 | | - cmd.assert().success(); |
56 | | - |
57 | | - let svg_contents = fs::read_to_string(&svg_output_path)?; |
58 | | - assert!( |
59 | | - svg_contents.contains("<svg"), |
60 | | - "{} output should contain an <svg> element", |
61 | | - svg_output_path.display() |
62 | | - ); |
| 41 | + let tests: Vec<_> = in_paths |
| 42 | + .into_iter() |
| 43 | + .flat_map(|in_path| { |
| 44 | + let stem = in_path.file_stem().unwrap().to_str().unwrap(); |
| 45 | + [ |
| 46 | + libtest_mimic::Trial::test(format!("svg_{stem}"), { |
| 47 | + let in_path = in_path.clone(); |
| 48 | + let expected_path = manifest_dir |
| 49 | + .join("tests/expected") |
| 50 | + .join(format!("{stem}.svg")); |
| 51 | + move || test_svg(in_path, expected_path) |
| 52 | + }), |
| 53 | + libtest_mimic::Trial::test(format!("png_{stem}"), { |
| 54 | + let in_path = in_path.clone(); |
| 55 | + move || smoke_test_png(in_path) |
| 56 | + }), |
| 57 | + ] |
| 58 | + }) |
| 59 | + .collect(); |
| 60 | + |
| 61 | + let args = libtest_mimic::Arguments::from_args(); |
| 62 | + libtest_mimic::run(&args, tests).exit(); |
| 63 | +} |
63 | 64 |
|
64 | | - let mut png_cmd = cargo_bin_cmd!("oxdraw"); |
65 | | - png_cmd |
66 | | - .arg("--input") |
67 | | - .arg(&path) |
68 | | - .arg("--output") |
69 | | - .arg(&png_output_path) |
70 | | - .arg("--png"); |
| 65 | +#[cfg(not(target_arch = "wasm32"))] |
| 66 | +fn test_svg(in_path: PathBuf, expected_path: PathBuf) -> Result<(), Failed> { |
| 67 | + let temp_dir = TempDir::new().expect("create temp dir"); |
71 | 68 |
|
72 | | - png_cmd.assert().success(); |
| 69 | + let stem = in_path.file_stem().unwrap().to_str().unwrap(); |
| 70 | + let out_path = temp_dir.path().join(format!("{stem}.svg")); |
73 | 71 |
|
74 | | - let png_bytes = fs::read(&png_output_path)?; |
75 | | - assert!( |
76 | | - png_bytes.starts_with(b"\x89PNG\r\n\x1a\n"), |
77 | | - "{} output should begin with the PNG magic header", |
78 | | - png_output_path.display() |
| 72 | + let mut cmd = cargo_bin_cmd!("oxdraw"); |
| 73 | + cmd.arg("--input") |
| 74 | + .arg(in_path) |
| 75 | + .arg("--output") |
| 76 | + .arg(&out_path) |
| 77 | + .arg("--output-format") |
| 78 | + .arg("svg"); |
| 79 | + |
| 80 | + cmd.assert().success(); |
| 81 | + |
| 82 | + let actual = fs::read_to_string(&out_path)?; |
| 83 | + |
| 84 | + if std::env::var("UPDATE_EXPECTED").is_ok() { |
| 85 | + fs::write(expected_path, actual)?; |
| 86 | + return Ok(()); |
| 87 | + } |
| 88 | + |
| 89 | + let expected = fs::read_to_string(&expected_path)?; |
| 90 | + |
| 91 | + if expected != actual { |
| 92 | + let diff = format!( |
| 93 | + "{}", |
| 94 | + SimpleDiff::from_str(&actual, &expected, "actual", "expected") |
79 | 95 | ); |
| 96 | + return Err(diff.into()); |
80 | 97 | } |
81 | 98 |
|
82 | 99 | Ok(()) |
83 | 100 | } |
| 101 | + |
| 102 | +#[cfg(not(target_arch = "wasm32"))] |
| 103 | +fn smoke_test_png(in_path: PathBuf) -> Result<(), Failed> { |
| 104 | + let temp_dir = TempDir::new().expect("create temp dir"); |
| 105 | + |
| 106 | + let stem = in_path.file_stem().unwrap().to_str().unwrap(); |
| 107 | + let out_path = temp_dir.path().join(format!("{stem}.png")); |
| 108 | + |
| 109 | + let mut cmd = cargo_bin_cmd!("oxdraw"); |
| 110 | + cmd.arg("--input") |
| 111 | + .arg(&in_path) |
| 112 | + .arg("--output") |
| 113 | + .arg(&out_path) |
| 114 | + .arg("--output-format") |
| 115 | + .arg("png") |
| 116 | + .arg("--scale=1"); // greatly speeds up tests |
| 117 | + |
| 118 | + cmd.assert().success(); |
| 119 | + |
| 120 | + let png_bytes = fs::read(&out_path)?; |
| 121 | + let starts_with_header = png_bytes.starts_with(b"\x89PNG\r\n\x1a\n"); |
| 122 | + if !starts_with_header { |
| 123 | + let _ = temp_dir.keep(); |
| 124 | + panic!( |
| 125 | + "{} output should begin with the PNG magic header", |
| 126 | + out_path.display() |
| 127 | + ); |
| 128 | + } |
| 129 | + Ok(()) |
| 130 | +} |
0 commit comments