Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ regex = "1.12.2"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
assert_cmd = "2.1.1"
libtest-mimic = "0.8.1"
similar-asserts = "1.7.0"
tempfile = "3"

[dev-dependencies]
wasm-bindgen-test = "0.3"
Expand All @@ -65,3 +68,8 @@ server = ["axum", "tokio", "tower-http", "tower"]
[profile.release]
codegen-units = 1
lto = true

[[test]]
name = "cli"
harness = false
path = "tests/cli.rs"
3 changes: 2 additions & 1 deletion src/diagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ impl Diagram {
svg.push_str(" </g>\n");
}
}
for id in &self.order {
let node = self.nodes.get(id).unwrap();

for (id, node) in &self.nodes {
let position = geometry
.positions
.get(id)
Expand Down
179 changes: 113 additions & 66 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,130 @@
#![cfg(not(target_arch = "wasm32"))]

// With the custom test harness we unfortunately cannot use #![cfg(not(target_arch = "wasm32"))].
use std::ffi::OsStr;
use std::fs;
use std::path::PathBuf;

#[cfg(not(target_arch = "wasm32"))]
use assert_cmd::cargo::cargo_bin_cmd;

#[test]
fn generates_svg_for_all_fixtures() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(not(target_arch = "wasm32"))]
use libtest_mimic::Failed;
#[cfg(not(target_arch = "wasm32"))]
use similar_asserts::SimpleDiff;
#[cfg(not(target_arch = "wasm32"))]
use tempfile::TempDir;

#[cfg(target_arch = "wasm32")]
fn main() {}

#[cfg(not(target_arch = "wasm32"))]
fn main() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let input_dir = manifest_dir.join("tests/input");
let output_dir = manifest_dir.join("tests/output");
let svg_output_dir = output_dir.join("svg");
let png_output_dir = output_dir.join("png");

assert!(
input_dir.exists(),
"tests/input directory should exist for CLI fixtures"
let in_paths: Vec<_> = fs::read_dir(&input_dir)
.expect("read dir")
.flatten()
.map(|entry| entry.path())
.collect();

assert!(in_paths.len() > 0, "expected tests/input to contain files");

assert_eq!(
in_paths
.iter()
.filter(|input| input.extension().and_then(OsStr::to_str) != Some("mmd"))
.map(|input| input.file_name().unwrap())
.collect::<Vec<_>>(),
Vec::<&OsStr>::new(),
"expected files in tests/input/ to have the .mmd extension"
);

fs::create_dir_all(&svg_output_dir)?;
fs::create_dir_all(&png_output_dir)?;

for entry in fs::read_dir(&input_dir)? {
let entry = entry?;
let path = entry.path();

if path.extension().and_then(|ext| ext.to_str()) != Some("mmd") {
continue;
}

let stem = path
.file_stem()
.and_then(|s| s.to_str())
.ok_or("failed to read fixture stem")?;
let svg_output_path = svg_output_dir.join(format!("{stem}.svg"));
let png_output_path = png_output_dir.join(format!("{stem}.png"));

if svg_output_path.exists() {
fs::remove_file(&svg_output_path)?;
}

if png_output_path.exists() {
fs::remove_file(&png_output_path)?;
}

let mut cmd = cargo_bin_cmd!("oxdraw");
cmd.arg("--input")
.arg(&path)
.arg("--output")
.arg(&svg_output_path)
.arg("--output-format")
.arg("svg");

cmd.assert().success();

let svg_contents = fs::read_to_string(&svg_output_path)?;
assert!(
svg_contents.contains("<svg"),
"{} output should contain an <svg> element",
svg_output_path.display()
);
let tests: Vec<_> = in_paths
.into_iter()
.flat_map(|in_path| {
let stem = in_path.file_stem().unwrap().to_str().unwrap();
[
libtest_mimic::Trial::test(format!("svg_{stem}"), {
let in_path = in_path.clone();
let expected_path = manifest_dir
.join("tests/expected")
.join(format!("{stem}.svg"));
move || test_svg(in_path, expected_path)
}),
libtest_mimic::Trial::test(format!("png_{stem}"), {
let in_path = in_path.clone();
move || smoke_test_png(in_path)
}),
]
})
.collect();

let args = libtest_mimic::Arguments::from_args();
libtest_mimic::run(&args, tests).exit();
}

let mut png_cmd = cargo_bin_cmd!("oxdraw");
png_cmd
.arg("--input")
.arg(&path)
.arg("--output")
.arg(&png_output_path)
.arg("--png");
#[cfg(not(target_arch = "wasm32"))]
fn test_svg(in_path: PathBuf, expected_path: PathBuf) -> Result<(), Failed> {
let temp_dir = TempDir::new().expect("create temp dir");

png_cmd.assert().success();
let stem = in_path.file_stem().unwrap().to_str().unwrap();
let out_path = temp_dir.path().join(format!("{stem}.svg"));

let png_bytes = fs::read(&png_output_path)?;
assert!(
png_bytes.starts_with(b"\x89PNG\r\n\x1a\n"),
"{} output should begin with the PNG magic header",
png_output_path.display()
let mut cmd = cargo_bin_cmd!("oxdraw");
cmd.arg("--input")
.arg(in_path)
.arg("--output")
.arg(&out_path)
.arg("--output-format")
.arg("svg");

cmd.assert().success();

let actual = fs::read_to_string(&out_path)?;

if std::env::var("UPDATE_EXPECTED").is_ok() {
fs::write(expected_path, actual)?;
return Ok(());
}

let expected = fs::read_to_string(&expected_path)?;

if expected != actual {
let diff = format!(
"{}",
SimpleDiff::from_str(&actual, &expected, "actual", "expected")
);
return Err(diff.into());
}

Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
fn smoke_test_png(in_path: PathBuf) -> Result<(), Failed> {
let temp_dir = TempDir::new().expect("create temp dir");

let stem = in_path.file_stem().unwrap().to_str().unwrap();
let out_path = temp_dir.path().join(format!("{stem}.png"));

let mut cmd = cargo_bin_cmd!("oxdraw");
cmd.arg("--input")
.arg(&in_path)
.arg("--output")
.arg(&out_path)
.arg("--output-format")
.arg("png")
.arg("--scale=1"); // greatly speeds up tests

cmd.assert().success();

let png_bytes = fs::read(&out_path)?;
let starts_with_header = png_bytes.starts_with(b"\x89PNG\r\n\x1a\n");
if !starts_with_header {
let _ = temp_dir.keep();
panic!(
"{} output should begin with the PNG magic header",
out_path.display()
);
}
Ok(())
}
40 changes: 20 additions & 20 deletions tests/output/svg/conditional.svg → tests/expected/conditional.svg
File renamed without changes
Loading