mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 22:34:43 +00:00
60 lines
3.5 KiB
Rust
60 lines
3.5 KiB
Rust
//! #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();
|
||
{
|
||
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]))).into();
|
||
let (__w, __h) = (__dyn.width(), __dyn.height());
|
||
let (__layout, __sample, __raw) = match __dyn {
|
||
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
|
||
image::DynamicImage::ImageLuma16(b) => (
|
||
craft_image_codecs::ChannelLayout::Gray,
|
||
craft_image_codecs::SampleType::U16,
|
||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||
),
|
||
image::DynamicImage::ImageLumaA16(b) => (
|
||
craft_image_codecs::ChannelLayout::GrayA,
|
||
craft_image_codecs::SampleType::U16,
|
||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||
),
|
||
image::DynamicImage::ImageRgb16(b) => (
|
||
craft_image_codecs::ChannelLayout::Rgb,
|
||
craft_image_codecs::SampleType::U16,
|
||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||
),
|
||
image::DynamicImage::ImageRgba16(b) => (
|
||
craft_image_codecs::ChannelLayout::Rgba,
|
||
craft_image_codecs::SampleType::U16,
|
||
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
|
||
),
|
||
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
|
||
};
|
||
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
|
||
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
|
||
.map(|__b| png.extend_from_slice(&__b))
|
||
}
|
||
.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}");
|
||
}
|