Files
vectorcraft/crates/engine/tests/open_relative_path.rs
T
Gert Pellin 13bd24ba79 Open by a relative path finds relative links; convert reports import warnings
document.open (and file.newFromTemplate) with a path relative to the
working directory took the SVG's folder from Path::parent(), which is
"" for a bare file name and was then dropped, so relatively linked
images were "not found" (and, linked by their bare name, not listed in
missingLinks either). A relative path is now made absolute first: its
folder is the working directory, as for the absolute path.

vectorcraft-cli convert printed only the export's warnings; what reading
the input left out now comes as importWarnings.

Fixes #1604
2026-10-13 13:36:40 +07:00

26 lines
1.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! #1604: a document opened by a path relative to the working directory finds its relatively
//! linked images, as when opened by its absolute path. Its own test binary: it changes the
//! process's working directory.
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md › Robustness).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use serde_json::json;
use vectorcraft_engine::Session;
#[test]
fn a_relative_path_finds_relative_links() {
let dir = std::env::temp_dir().join(format!("vectorcraft-relative-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let mut png = Vec::new();
image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255])).write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
std::fs::write(dir.join("ext.png"), &png).unwrap();
let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"><image x="0" y="0" width="40" height="40" href="ext.png"/></svg>"#;
std::fs::write(dir.join("linked.svg"), svg).unwrap();
std::env::set_current_dir(&dir).unwrap();
let mut s = Session::new();
let r = s.execute("document.open", &json!({"path": "linked.svg"})).unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(r["warnings"], json!([]), "{r}");
assert_eq!(r["missingLinks"], json!([]), "{r}");
}