[librsvg: 6/53] Move the loading crash tests to Rust




commit 2595b158ec8e2ed4f8692f495860781ffda651fa
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Oct 13 15:34:58 2020 -0500

    Move the loading crash tests to Rust

 Cargo.lock                 |  1 +
 tests/Cargo.toml           |  1 +
 tests/src/loading_crash.rs | 19 +++++++++++++++++++
 tests/src/main.rs          |  5 +++++
 tests/src/utils.rs         | 19 +++++++++++++++++++
 5 files changed, 45 insertions(+)
---
diff --git a/Cargo.lock b/Cargo.lock
index 3e805b59..921b094b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -781,6 +781,7 @@ dependencies = [
  "assert_cmd",
  "chrono",
  "float-cmp",
+ "librsvg",
  "lopdf",
  "png",
  "predicates",
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
index 4f65203a..3354dcf6 100644
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -9,6 +9,7 @@ edition = "2018"
 assert_cmd = "1.0.1"
 chrono = "0.4.0"
 float-cmp = "0.8.0"
+librsvg = { path = "../librsvg_crate" }
 lopdf = "0.25.0"
 png = "0.16.1"
 predicates = "1.0.2"
diff --git a/tests/src/loading_crash.rs b/tests/src/loading_crash.rs
new file mode 100644
index 00000000..2c9dce1e
--- /dev/null
+++ b/tests/src/loading_crash.rs
@@ -0,0 +1,19 @@
+//! Tests for crashes in the loading stage.
+//!
+//! Ensures that loading and parsing (but not rendering) a particular
+//! SVG doesn't crash.
+
+#![cfg(test)]
+use test_generator::test_resources;
+
+use librsvg::Loader;
+
+use crate::utils::fixture_path;
+
+#[test_resources("tests/fixtures/crash/*.svg")]
+fn loading_crash(name: &str) {
+    let path = fixture_path(name);
+
+    // We just test for crashes during loading, and don't care about success/error.
+    let _ = Loader::new().read_path(path);
+}
diff --git a/tests/src/main.rs b/tests/src/main.rs
index dd7afcb0..a14b3a8d 100644
--- a/tests/src/main.rs
+++ b/tests/src/main.rs
@@ -5,9 +5,14 @@ extern crate float_cmp;
 #[cfg(test)]
 mod cmdline;
 
+#[cfg(test)]
+mod loading_crash;
+
 #[cfg(test)]
 mod predicates;
 
+mod utils;
+
 fn main() {
     println!("Use 'cargo test' to run the tests.");
 }
diff --git a/tests/src/utils.rs b/tests/src/utils.rs
new file mode 100644
index 00000000..b2cd967f
--- /dev/null
+++ b/tests/src/utils.rs
@@ -0,0 +1,19 @@
+use std::env;
+use std::path::PathBuf;
+
+/// Given a filename from `test_generator::test_resources`, computes the correct fixture filename.
+///
+/// The `test_resources` procedural macro works by running a filename glob starting on
+/// the toplevel of the Cargo workspace.  However, when a test function gets run,
+/// its $cwd is the test crate's toplevel.  This function fixes the pathname generated
+/// by `test_resources` so that it has the correct path.
+pub fn fixture_path(filename_from_test_resources: &str) -> PathBuf {
+    let crate_toplevel = PathBuf::from(
+        env::var_os("CARGO_MANIFEST_DIR")
+            .expect(r#"CARGO_MANIFEST_DIR" is not set, please set it or run under "cargo test""#),
+    );
+
+    let workspace_toplevel = crate_toplevel.parent().unwrap();
+
+    workspace_toplevel.join(filename_from_test_resources)
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]