Lines
99.84 %
Functions
100 %
Branches
39.94 %
//use crate::predicates::ends_with_pkg_version;
mod internal_predicates;
use internal_predicates::file;
use assert_cmd::assert::IntoOutputPredicate;
use assert_cmd::Command;
#[cfg(system_deps_have_cairo_pdf)]
use chrono::{TimeZone, Utc};
use predicates::boolean::*;
use predicates::prelude::*;
use predicates::str::*;
use rsvg::{Length, LengthUnit};
use std::path::Path;
use tempfile::Builder;
use url::Url;
// What should be tested here?
// The goal is to test the code in rsvg-convert, not the entire library.
//
// - command-line options that affect size (width, height, zoom, resolution) ✔
// - pixel dimensions of the output (should be sufficient to do that for PNG) ✔
// - limit on output size (32767 pixels) ✔
// - output formats (PNG, PDF, PS, EPS, SVG) ✔
// - multi-page output (for PDF) ✔
// - output file option ✔
// - SOURCE_DATA_EPOCH environment variable for PDF output ✔
// - background color option ✔
// - optional CSS stylesheet ✔
// - error handling for missing SVG dimensions ✔
// - error handling for export lookup ID ✔
// - error handling for invalid input ✔
struct RsvgConvert {}
impl RsvgConvert {
fn new() -> Command {
Command::cargo_bin("rsvg-convert").unwrap()
}
fn new_with_input<P>(file: P) -> Command
where
P: AsRef<Path>,
{
let mut command = RsvgConvert::new();
match command.pipe_stdin(&file) {
Ok(_) => command,
Err(e) => panic!("Error opening file '{}': {}", file.as_ref().display(), e),
fn accepts_arg(option: &str) {
RsvgConvert::new_with_input("tests/fixtures/dpi.svg")
.arg(option)
.assert()
.success();
fn option_yields_output<I, P>(option: &str, output_pred: I)
I: IntoOutputPredicate<P>,
P: Predicate<[u8]>,
RsvgConvert::new()
.success()
.stdout(output_pred);
#[test]
fn converts_svg_from_stdin_to_png() {
RsvgConvert::new_with_input("tests/fixtures/bug521-with-viewbox.svg")
.stdout(file::is_png());
fn converts_svg_from_stdin_to_png_using_stdin_argument() {
.arg("-")
fn argument_is_input_filename() {
let input = Path::new("tests/fixtures/bug521-with-viewbox.svg");
.arg(input)
fn argument_is_url() {
let path = Path::new("tests/fixtures/bug521-with-viewbox.svg")
.canonicalize()
.unwrap();
let url = Url::from_file_path(path).unwrap();
let stringified = url.as_str();
assert!(stringified.starts_with("file://"));
.arg(stringified)
fn output_format_png() {
.arg("--format=png")
#[cfg(system_deps_have_cairo_ps)]
fn output_format_ps() {
.arg("--format=ps")
.stdout(file::is_ps());
fn output_format_eps() {
.arg("--format=eps")
.stdout(file::is_eps());
fn output_format_pdf() {
.arg("--format=pdf")
.stdout(file::is_pdf());
fn output_format_pdf_1_7() {
.arg("--format=pdf1.7")
.stdout(file::is_pdf().with_version("1.7"));
fn output_format_pdf_1_6() {
.arg("--format=pdf1.6")
.stdout(file::is_pdf().with_version("1.6"));
fn output_format_pdf_1_5() {
.arg("--format=pdf1.5")
.stdout(file::is_pdf().with_version("1.5"));
fn output_format_pdf_1_4() {
.arg("--format=pdf1.4")
.stdout(file::is_pdf().with_version("1.4"));
#[cfg(system_deps_have_cairo_svg)]
fn output_format_svg_short_option() {
.arg("-f")
.arg("svg")
.stdout(file::is_svg());
fn user_specified_width_and_height() {
.arg("--format")
.arg("--width")
.arg("42cm")
.arg("--height")
.arg("43cm")
.stdout(file::is_svg().with_size(
Length::new(42.0, LengthUnit::Cm),
Length::new(43.0, LengthUnit::Cm),
));
fn user_specified_width_and_height_px_output() {
.arg("1920")
.arg("508mm")
Length::new(1920.0, LengthUnit::Px),
fn user_specified_width_and_height_a4() {
.arg("--page-width")
.arg("210mm")
.arg("--page-height")
.arg("297mm")
.arg("--left")
.arg("1cm")
.arg("--top")
.arg("190mm")
.arg("277mm")
Length::new(210.0, LengthUnit::Mm),
Length::new(297.0, LengthUnit::Mm),
fn output_file_option() {
let output = {
let tempfile = Builder::new().suffix(".png").tempfile().unwrap();
tempfile.path().to_path_buf()
};
assert!(predicates::path::is_file().not().eval(&output));
.arg(format!("--output={}", output.display()))
.stdout(is_empty());
assert!(predicates::path::is_file().eval(&output));
std::fs::remove_file(&output).unwrap();
fn output_file_short_option() {
.arg("-o")
.arg(format!("{}", output.display()))
fn overwrites_existing_output_file() {
for _ in 0..2 {
fn empty_input_yields_error() {
let starts_with = starts_with("Error reading SVG");
let ends_with = ends_with("Input file is too short").trim();
.failure()
.stderr(starts_with.and(ends_with));
fn empty_svg_yields_error() {
RsvgConvert::new_with_input("tests/fixtures/empty.svg")
.stderr("The SVG stdin has no dimensions\n");
fn multiple_input_files_not_allowed_for_png_output() {
let one = Path::new("tests/fixtures/bug521-with-viewbox.svg");
let two = Path::new("tests/fixtures/sub-rect-no-unit.svg");
.arg(one)
.arg(two)
.stderr(contains(
"Multiple SVG files are only allowed for PDF and (E)PS output",
fn multiple_input_files_accepted_for_eps_output() {
fn multiple_input_files_accepted_for_ps_output() {
fn multiple_input_files_create_multi_page_pdf_output() {
let three = Path::new("tests/fixtures/example.svg");
.arg(three)
.stdout(
file::is_pdf()
.with_page_count(3)
.and(file::is_pdf().with_page_size(0, 150.0, 75.0))
.and(file::is_pdf().with_page_size(1, 123.0, 123.0))
.and(file::is_pdf().with_page_size(2, 75.0, 300.0)),
);
fn multiple_input_files_create_multi_page_pdf_output_fixed_size() {
.arg("--page-width=8.5in")
.arg("--page-height=11in")
.arg("--width=7.5in")
.arg("--height=10in")
.arg("--left=0.5in")
.arg("--top=0.5in")
.arg("--keep-aspect-ratio")
// https://www.wolframalpha.com/input/?i=convert+11+inches+to+desktop+publishing+points
.and(file::is_pdf().with_page_size(0, 612.0, 792.0))
.and(file::is_pdf().with_page_size(1, 612.0, 792.0))
.and(file::is_pdf().with_page_size(2, 612.0, 792.0)),
fn pdf_has_link() {
let input = Path::new("tests/fixtures/a-link.svg");
.stdout(file::is_pdf().with_link("https://example.com"));
fn pdf_has_link_inside_text() {
let input = Path::new("tests/fixtures/text-a-link.svg");
.with_link("https://example.com")
.and(file::is_pdf().with_link("https://another.example.com")),
fn pdf_has_text() {
let input = Path::new("tests/fixtures/hello-world.svg");
.with_text("Hello world!")
.and(file::is_pdf().with_text("Hello again!")),
fn env_source_data_epoch_controls_pdf_creation_date() {
let date = 1581411039; // seconds since epoch
.env("SOURCE_DATE_EPOCH", format!("{}", date))
.stdout(file::is_pdf().with_creation_date(Utc.timestamp_opt(date, 0).unwrap()));
fn env_source_data_epoch_no_digits() {
// intentionally not testing for the full error string here
.env("SOURCE_DATE_EPOCH", "foobar")
.stderr(starts_with("Environment variable $SOURCE_DATE_EPOCH"));
fn env_source_data_epoch_trailing_garbage() {
.env("SOURCE_DATE_EPOCH", "1234556+")
fn env_source_data_epoch_empty() {
.env("SOURCE_DATE_EPOCH", "")
fn width_option() {
.arg("--width=300")
.stdout(file::is_png().with_size(300, 150));
fn height_option() {
.arg("--height=200")
.stdout(file::is_png().with_size(400, 200));
fn width_and_height_options() {
.stdout(file::is_png().with_size(300, 200));
fn unsupported_unit_in_width_and_height() {
.arg("--height=200ex")
.stderr(contains("supported units"));
fn invalid_length() {
.arg("--page-width=foo")
.stderr(contains("can not be parsed as a length"));
fn zoom_factor() {
.arg("--zoom=0.8")
.stdout(file::is_png().with_size(160, 80));
fn zoom_factor_and_larger_size() {
.arg("--width=400")
.arg("--zoom=1.5")
fn zoom_factor_and_smaller_size() {
.arg("--zoom=3.5")
fn x_zoom_option() {
.arg("--x-zoom=2")
.stdout(file::is_png().with_size(400, 100));
fn x_short_option() {
.arg("-x")
.arg("2.0")
fn y_zoom_option() {
.arg("--y-zoom=2.0")
.stdout(file::is_png().with_size(200, 200));
fn y_short_option() {
.arg("-y")
.arg("2")
fn huge_zoom_factor_yields_error() {
.arg("--zoom=1000")
.stderr(starts_with(
"The resulting image would be larger than 32767 pixels",
fn negative_zoom_factor_yields_error() {
.arg("--zoom=-2")
.stderr(contains("Invalid zoom"));
fn invalid_zoom_factor_yields_error() {
.arg("--zoom=foo")
.stderr(contains("invalid value"));
fn default_resolution_is_96dpi() {
.stdout(file::is_png().with_size(96, 384));
fn x_resolution() {
.arg("--dpi-x=300")
.stdout(file::is_png().with_size(300, 384));
fn x_resolution_short_option() {
.arg("-d")
.arg("45")
.stdout(file::is_png().with_size(45, 384));
fn y_resolution() {
.arg("--dpi-y=300")
.stdout(file::is_png().with_size(96, 1200));
fn y_resolution_short_option() {
.arg("-p")
.stdout(file::is_png().with_size(96, 180));
fn x_and_y_resolution() {
.arg("--dpi-y=150")
.stdout(file::is_png().with_size(300, 600));
fn zero_resolution_is_invalid() {
.arg("--dpi-x=0")
.arg("--dpi-y=0")
.stderr(contains("Invalid resolution"));
fn negative_resolution_is_invalid() {
.arg("--dpi-x=-100")
.arg("--dpi-y=-100")
fn zero_offset_png() {
RsvgConvert::new_with_input("tests/fixtures/dimensions-in.svg")
.arg("--page-width=640")
.arg("--page-height=480")
.arg("--width=200")
.arg("--height=100")
.stdout(file::is_png().with_contents("tests/fixtures/zero-offset-png.png"));
fn offset_png() {
.arg("--left=100")
.arg("--top=50")
.stdout(file::is_png().with_contents("tests/fixtures/offset-png.png"));
fn unscaled_pdf_size() {
.stdout(file::is_pdf().with_page_size(0, 72.0, 72.0));
fn pdf_size_width_height() {
.arg("--width=2in")
.arg("--height=3in")
.stdout(file::is_pdf().with_page_size(0, 144.0, 216.0));
fn pdf_size_width_height_proportional() {
.stdout(file::is_pdf().with_page_size(0, 144.0, 144.0));
fn pdf_page_size() {
.arg("--page-width=210mm")
.arg("--page-height=297mm")
.stdout(file::is_pdf().with_page_size(0, 210.0 / 25.4 * 72.0, 297.0 / 25.4 * 72.0));
fn multiple_input_files_create_multi_page_pdf_size_override() {
.arg("--width=300pt")
.arg("--height=200pt")
.and(file::is_pdf().with_page_size(0, 300.0, 200.0))
.and(file::is_pdf().with_page_size(1, 300.0, 200.0))
.and(file::is_pdf().with_page_size(2, 300.0, 200.0)),
fn missing_page_size_yields_error() {
.stderr(contains("both").and(contains("options")));
fn does_not_clip_partial_coverage_pixels() {
RsvgConvert::new_with_input("tests/fixtures/bug677-partial-pixel.svg")
.stdout(file::is_png().with_size(2, 2));
fn background_color_option_with_valid_color() {
RsvgConvert::accepts_arg("--background-color=LimeGreen");
fn background_color_option_none() {
RsvgConvert::accepts_arg("--background-color=None");
fn background_color_short_option() {
.arg("-b")
.arg("#aabbcc")
fn background_color_option_invalid_color_yields_error() {
.arg("--background-color=foobar")
.stderr(contains("Invalid").and(contains("color")));
fn background_color_is_rendered() {
RsvgConvert::new_with_input("tests/fixtures/gimp-wilber.svg")
.arg("--background-color=purple")
.stdout(file::is_png().with_contents("tests/fixtures/gimp-wilber-ref.png"));
fn background_color_rgb() {
RsvgConvert::new_with_input("tests/fixtures/empty-10x10.svg")
.arg("--width=10")
.arg("--height=10")
.arg("--background-color=rgb(0, 255, 0)")
.stdout(file::is_png().with_contents("tests/fixtures/lime-ref.png"));
fn background_color_rgba() {
.arg("--background-color=rgba(0, 255, 0, 0.5)")
.stdout(file::is_png().with_contents("tests/fixtures/lime-transparent-ref.png"));
fn background_color_hsl() {
.arg("--background-color=hsl(120, 100%, 50%)")
fn background_color_hsla() {
.arg("--background-color=hsla(120, 100%, 50%, 0.5)")
fn background_color_hwb() {
.arg("--background-color=hwb(120 0% 0%)")
fn background_color_hwba() {
.arg("--background-color=hwb(120 0% 0% / 0.5)")
fn test_unsupported_background_color(color: &str) {
let color_arg = format!("--background-color={color}");
.arg(&color_arg)
.stderr(contains("Invalid value").and(contains("unsupported color syntax")));
fn unsupported_background_color() {
let colors = [
"lab(62.2345% -34.9638 47.7721)",
"lch(62.2345% 59.2 126.2)",
"oklab(66.016% -0.1084 0.1114)",
"oklch(0.66016 0.15546 134.231)",
"color(display-p3 -0.6112 1.0079 -0.2192)",
];
for c in &colors {
test_unsupported_background_color(c);
fn stylesheet_option() {
.arg("--stylesheet=tests/fixtures/empty.css")
fn stylesheet_short_option() {
.arg("-s")
.arg("tests/fixtures/empty.css")
fn stylesheet_option_error() {
.arg("--stylesheet=foobar")
.stderr(starts_with("Error reading stylesheet"));
fn export_id_option() {
RsvgConvert::new_with_input("tests/fixtures/geometry-element.svg")
.arg("--export-id=foo")
.stdout(file::is_png().with_size(40, 50));
fn export_id_with_zero_stroke_width() {
// https://gitlab.gnome.org/GNOME/librsvg/-/issues/601
// This tests a bug that manifested itself easily with the --export-id option, but it
// is not a bug with the option itself. An object with stroke_width=0 was causing
// an extra point at the origin to be put in the bounding box, so the final image
// spanned the origin to the actual visible bounds of the rendered object.
// We can probably test this more cleanly once we have a render tree.
RsvgConvert::new_with_input("tests/fixtures/bug601-zero-stroke-width.svg")
file::is_png()
.with_contents("tests/fixtures/bug601-zero-stroke-width-render-only-foo.png"),
fn export_id_short_option() {
.arg("-i")
.arg("two")
.stdout(file::is_png().with_size(100, 200));
fn export_id_with_hash_prefix() {
.arg("#two")
fn export_id_option_error() {
.arg("--export-id=foobar")
.stderr(starts_with("File stdin does not have an object with id \""));
fn unlimited_option() {
RsvgConvert::accepts_arg("--unlimited");
fn unlimited_short_option() {
RsvgConvert::accepts_arg("-u");
fn keep_aspect_ratio_option() {
let input = Path::new("tests/fixtures/dpi.svg");
RsvgConvert::new_with_input(input)
.arg("--width=500")
.arg("--height=1000")
.stdout(file::is_png().with_size(500, 1000));
.stdout(file::is_png().with_size(250, 1000));
fn keep_aspect_ratio_short_option() {
.arg("--width=1000")
.arg("--height=500")
.stdout(file::is_png().with_size(1000, 500));
.arg("-a")
.stdout(file::is_png().with_size(125, 500));
fn overflowing_size_is_detected() {
RsvgConvert::new_with_input("tests/fixtures/bug591-vbox-overflow.svg")
fn accept_language_given() {
RsvgConvert::new_with_input("tests/fixtures/accept-language.svg")
.arg("--accept-language=es-MX")
.stdout(file::is_png().with_contents("tests/fixtures/accept-language-es.png"));
.arg("--accept-language=de")
.stdout(file::is_png().with_contents("tests/fixtures/accept-language-de.png"));
fn accept_language_fallback() {
.arg("--accept-language=fr")
.stdout(file::is_png().with_contents("tests/fixtures/accept-language-fallback.png"));
fn accept_language_invalid_tag() {
// underscores are not valid in BCP47 language tags
.arg("--accept-language=foo_bar")
.stderr(contains("invalid language tag"));
fn keep_image_data_option() {
RsvgConvert::accepts_arg("--keep-image-data");
fn no_keep_image_data_option() {
RsvgConvert::accepts_arg("--no-keep-image-data");
fn is_version_output() -> AndPredicate<StartsWithPredicate, TrimPredicate<EndsWithPredicate>, str> {
starts_with("rsvg-convert version ")
.and(predicates::str::ends_with(env!("CARGO_PKG_VERSION")).trim())
fn version_option() {
RsvgConvert::option_yields_output("--version", is_version_output())
fn version_short_option() {
RsvgConvert::option_yields_output("-v", is_version_output())
fn is_usage_output() -> OrPredicate<ContainsPredicate, ContainsPredicate, str> {
contains("Usage:").or(contains("USAGE:"))
fn help_option() {
RsvgConvert::option_yields_output("--help", is_usage_output())
fn help_short_option() {
RsvgConvert::option_yields_output("-?", is_usage_output())
fn multiple_stdin_arguments_not_allowed() {
.stderr(contains("Only one input file can be read from stdin"));