diff --git a/crates/ods/src/lib.rs b/crates/ods/src/lib.rs index de4abf641..c816905a9 100644 --- a/crates/ods/src/lib.rs +++ b/crates/ods/src/lib.rs @@ -19,7 +19,7 @@ //! //! ``` //! # fn main() -> Result<(), craft_ods::Error> { -//! # let bytes = craft_ods::testing::spreadsheet(r#"Tea"#); +//! # let bytes = craft_ods::testing::spreadsheet(r#"Tea"#)?; //! let book = craft_ods::read(&bytes, &craft_ods::Options::default())?; //! let sheet = &book.sheets[0]; //! assert_eq!(sheet.name, "Prices"); diff --git a/crates/ods/src/testing.rs b/crates/ods/src/testing.rs index a8a663d77..fb73d9d25 100644 --- a/crates/ods/src/testing.rs +++ b/crates/ods/src/testing.rs @@ -10,14 +10,39 @@ pub fn content(tables: &str) -> String { ) } -/// An `.ods` package around [`content`]. -pub fn spreadsheet(tables: &str) -> Vec { +/// An `.ods` package around [`content`]. A package that cannot be written returns the writer's +/// error. +pub fn spreadsheet(tables: &str) -> Result, craft_odf::Error> { package(content(tables).as_bytes()) } -/// An `.ods` package with this `content.xml`. -pub fn package(content: &[u8]) -> Vec { +/// An `.ods` package with this `content.xml`. A package that cannot be written returns the +/// writer's error. +pub fn package(content: &[u8]) -> Result, craft_odf::Error> { + files(&[("content.xml", content)]) +} + +/// An `.ods` package with these XML files, in order. +fn files(files: &[(&str, &[u8])]) -> Result, craft_odf::Error> { let mut w = craft_odf::PackageWriter::new(craft_odf::mime::SPREADSHEET); - w.file("content.xml", "text/xml", content); - w.finish().unwrap_or_default() + for (path, data) in files { + w.file(path, "text/xml", data); + } + w.finish() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_package_that_cannot_be_written_returns_the_writer_error() { + let xml = content(""); + let twice = files(&[("content.xml", xml.as_bytes()), ("content.xml", xml.as_bytes())]); + match twice { + Err(craft_odf::Error::Zip(message)) => assert!(message.contains("content.xml"), "{message}"), + other => panic!("expected the writer's error, got {other:?}"), + } + assert!(crate::is_ods(&package(xml.as_bytes()).unwrap())); + } } diff --git a/crates/ods/tests/read.rs b/crates/ods/tests/read.rs index c012cb75a..530177b4c 100644 --- a/crates/ods/tests/read.rs +++ b/crates/ods/tests/read.rs @@ -5,7 +5,7 @@ use craft_ods::testing::{content, package, spreadsheet}; use craft_ods::{CellRef, Error, Options, Range, Sheet, Value, ValueType, Workbook, read}; fn ods(tables: &str) -> Vec { - spreadsheet(tables) + spreadsheet(tables).unwrap() } fn row(cells: &str) -> Vec { ods(&format!(r#"{cells}"#)) @@ -28,8 +28,8 @@ fn a_namespace_declaration_bomb_is_refused() { content(r#""#) .replacen("kept"#, )) @@ -166,7 +166,7 @@ fn bad_packages_encryption_and_bad_xml_are_refused() { let mut w = craft_odf::PackageWriter::new("application/zip"); w.file("content.xml", "text/xml", xml.as_bytes()); assert!(matches!(open(&w.finish().unwrap()), Err(Error::Invalid(_)))); - assert!(craft_ods::is_ods(&package(xml.as_bytes()))); + assert!(craft_ods::is_ods(&package(xml.as_bytes()).unwrap())); let mut w = craft_odf::PackageWriter::new(craft_odf::mime::SPREADSHEET); w.file("content.xml", "text/xml", xml.as_bytes()); if let Some(e) = w.manifest_mut().entries.iter_mut().find(|e| e.path == "content.xml") { @@ -182,10 +182,10 @@ fn bad_packages_encryption_and_bad_xml_are_refused() { xml.replace("Data", "&custom;"), content(""), ] { - assert!(open(&package(bad.as_bytes())).is_err(), "{bad}"); + assert!(open(&package(bad.as_bytes()).unwrap()).is_err(), "{bad}"); } let deep = content(&format!("{}{}", "".repeat(300), "".repeat(300))); - assert!(open(&package(deep.as_bytes())).is_err()); + assert!(open(&package(deep.as_bytes()).unwrap()).is_err()); } #[test] @@ -214,7 +214,7 @@ fn expansion_bounds_stop_small_repeat_bombs() { // One row of cells each just under the 1 MiB cell limit, past 64 MiB in all: refused while // the row is read. The row's end is never reached. let long = format!(r#""#, "x".repeat(1024 * 1024 - 1)); - let cells = package(content(&format!(r#"{}"#, long.repeat(65))).as_bytes()); + let cells = package(content(&format!(r#"{}"#, long.repeat(65))).as_bytes()).unwrap(); assert!(matches!(open(&cells), Err(Error::TooLarge(_)))); // A row repeated once more than its cells' text allows is refused, and one that fits reads. let cell = format!(r#""#, "y".repeat(1024));