From f00d77d99b9c5115f82f7d30eb27d4e3ac5654c7 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sun, 24 Feb 2019 16:20:38 +0200 Subject: [PATCH] Test extracting an image --- tests/test_processing.rs | 89 ++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/tests/test_processing.rs b/tests/test_processing.rs index b5a1d05..2c04c9a 100644 --- a/tests/test_processing.rs +++ b/tests/test_processing.rs @@ -7,24 +7,29 @@ use tempfile::TempDir; use id3_image::*; struct Fixture { - path_buf: PathBuf, + path: PathBuf, + source: PathBuf, _tempdir: TempDir, } impl Fixture { - fn new(fixture_filename: &str) -> Self { + fn blank(fixture_filename: &str) -> Self { let root_dir = &env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR"); - let mut source_path = PathBuf::from(root_dir); - source_path.push("tests/fixtures"); - source_path.push(&fixture_filename); + let mut source = PathBuf::from(root_dir); + source.push("tests/fixtures"); + source.push(&fixture_filename); let tempdir = tempfile::tempdir().unwrap(); - let mut target_path = PathBuf::from(&tempdir.path()); - target_path.push(&fixture_filename); + let mut path = PathBuf::from(&tempdir.path()); + path.push(&fixture_filename); - fs::copy(&source_path, &target_path).unwrap(); + Fixture { _tempdir: tempdir, source, path } + } - Fixture { _tempdir: tempdir, path_buf: target_path } + fn copy(fixture_filename: &str) -> Self { + let fixture = Fixture::blank(fixture_filename); + fs::copy(&fixture.source, &fixture.path).unwrap(); + fixture } } @@ -32,7 +37,7 @@ impl Deref for Fixture { type Target = Path; fn deref(&self) -> &Self::Target { - self.path_buf.deref() + self.path.deref() } } @@ -42,8 +47,8 @@ fn read_tag(path: &Path) -> id3::Tag { #[test] fn test_unsuccessful_image_embedding() { - let song = Fixture::new("attempt_1_no_image.mp3"); - let image = Fixture::new("attempt_1.jpg"); + let song = Fixture::copy("attempt_1_no_image.mp3"); + let image = Fixture::copy("attempt_1.jpg"); // Nonexistent files assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err()); @@ -58,8 +63,8 @@ fn test_unsuccessful_image_embedding() { #[test] fn test_successful_jpeg_image_embedding() { - let song = Fixture::new("attempt_1_no_image.mp3"); - let image = Fixture::new("attempt_1.jpg"); + let song = Fixture::copy("attempt_1_no_image.mp3"); + let image = Fixture::copy("attempt_1.jpg"); let tag = read_tag(&song); assert!(tag.pictures().count() == 0); @@ -72,8 +77,8 @@ fn test_successful_jpeg_image_embedding() { #[test] fn test_successful_png_image_embedding() { - let song = Fixture::new("attempt_1_no_image.mp3"); - let image = Fixture::new("attempt_1.png"); + let song = Fixture::copy("attempt_1_no_image.mp3"); + let image = Fixture::copy("attempt_1.png"); let tag = read_tag(&song); assert!(tag.pictures().count() == 0); @@ -86,8 +91,8 @@ fn test_successful_png_image_embedding() { #[test] fn test_successful_image_embedding_in_a_file_that_already_has_an_image() { - let song = Fixture::new("attempt_1.mp3"); - let image = Fixture::new("attempt_1.jpg"); + let song = Fixture::copy("attempt_1.mp3"); + let image = Fixture::copy("attempt_1.jpg"); let tag = read_tag(&song); assert!(tag.pictures().count() > 0); @@ -99,9 +104,9 @@ fn test_successful_image_embedding_in_a_file_that_already_has_an_image() { } #[test] -fn test_remove_and_add_image() { - let song = Fixture::new("attempt_1.mp3"); - let image = Fixture::new("attempt_1.jpg"); +fn test_removing_and_adding_an_image() { + let song = Fixture::copy("attempt_1.mp3"); + let image = Fixture::copy("attempt_1.jpg"); let tag = read_tag(&song); assert!(tag.pictures().count() > 0); @@ -116,3 +121,45 @@ fn test_remove_and_add_image() { let tag = read_tag(&song); assert!(tag.pictures().count() > 0); } + +#[test] +fn test_extracting_a_jpg_image() { + let song = Fixture::copy("attempt_1.mp3"); + let image = Fixture::blank("attempt_1.jpg"); + + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); + assert!(!image.exists()); + + extract_first_image(&song, &image).unwrap(); + + assert!(image.exists()); +} + +#[test] +fn test_extracting_a_png_image() { + let song = Fixture::copy("attempt_1.mp3"); + let image = Fixture::blank("attempt_1.png"); + + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); + assert!(!image.exists()); + + extract_first_image(&song, &image).unwrap(); + + assert!(image.exists()); +} + +#[test] +fn test_overwriting_an_existing_image() { + let song = Fixture::copy("attempt_1.mp3"); + let image = Fixture::copy("attempt_1.jpg"); + + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); + assert!(image.exists()); + + extract_first_image(&song, &image).unwrap(); + + assert!(image.exists()); +}