From 99be0933690464a4a908a5c043fdc9ee35b46297 Mon Sep 17 00:00:00 2001 From: Fedor Tolstonozhenko Date: Sat, 19 Oct 2024 18:16:19 +0200 Subject: [PATCH] Add a test to check that saving doesn't corrupt the original package --- crates/opensi-core/src/lib.rs | 5 +++-- crates/opensi-core/tests/test.rs | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/opensi-core/src/lib.rs b/crates/opensi-core/src/lib.rs index ee1fe2d..e35660c 100644 --- a/crates/opensi-core/src/lib.rs +++ b/crates/opensi-core/src/lib.rs @@ -516,7 +516,7 @@ impl Package { } } - pub fn to_bytes(self) -> Result, Error> { + pub fn to_bytes(&self) -> Result, Error> { let buffer = Vec::new(); let cursor = io::Cursor::new(buffer); let mut zip = ZipWriter::new(cursor); @@ -531,7 +531,8 @@ impl Package { zip.start_file("[Content_Types].xml", options)?; zip.write_all(Self::CONTENT_TYPE_FILE_CONTENT.as_ref())?; - for (key, value) in self.resource.into_iter() { + let resources = &self.resource; + for (key, value) in resources.into_iter() { zip.start_file(key.extract_key(), options)?; zip.write_all(&value)? } diff --git a/crates/opensi-core/tests/test.rs b/crates/opensi-core/tests/test.rs index ade0220..94afa34 100644 --- a/crates/opensi-core/tests/test.rs +++ b/crates/opensi-core/tests/test.rs @@ -16,3 +16,13 @@ fn read_package_name() { let package = Package::open_zip_file(PATH).expect("pack is not found"); assert_eq!(package.name, "SLAM JAM 2".to_owned()); } + +#[test] +fn resave_test() { + let package_original = Package::open_zip_file(PATH).expect("Pack is not found"); + let bytes = &package_original.to_bytes().expect("Can't serialize package to bytes"); + let package_resaved = Package::from_zip_buffer(bytes) + .expect("Can't read package from buffer"); + + assert_eq!(package_original, package_resaved); +} \ No newline at end of file