diff --git a/src/lib.rs b/src/lib.rs index 0d09fa3..244dbe6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub fn embed_image(music_filename: &str, image_filename: &str) -> Result<(), Box tag.add_picture(id3::frame::Picture { mime_type: "image/jpeg".to_string(), - picture_type: id3::frame::PictureType::Other, + picture_type: id3::frame::PictureType::CoverFront, description: String::new(), data: encoded_image_bytes, }); diff --git a/tests/fixtures/attempt_1.jpg b/tests/fixtures/attempt_1.jpg new file mode 100644 index 0000000..e002644 Binary files /dev/null and b/tests/fixtures/attempt_1.jpg differ diff --git a/tests/fixtures/attempt_1.mp3 b/tests/fixtures/attempt_1.mp3 new file mode 100644 index 0000000..2a752a9 Binary files /dev/null and b/tests/fixtures/attempt_1.mp3 differ diff --git a/tests/fixtures/attempt_1.png b/tests/fixtures/attempt_1.png new file mode 100644 index 0000000..9926cfa Binary files /dev/null and b/tests/fixtures/attempt_1.png differ diff --git a/tests/fixtures/attempt_1_no_image.mp3 b/tests/fixtures/attempt_1_no_image.mp3 new file mode 100644 index 0000000..05da7db Binary files /dev/null and b/tests/fixtures/attempt_1_no_image.mp3 differ diff --git a/tests/test_processing.rs b/tests/test_processing.rs new file mode 100644 index 0000000..ff2f71c --- /dev/null +++ b/tests/test_processing.rs @@ -0,0 +1,102 @@ +use std::env; +use std::fs; +use std::path::PathBuf; + +use id3_image::*; + +macro_rules! in_temp_dir { + ($block:block) => { + let tmpdir = env::temp_dir(); + env::set_current_dir(&tmpdir).unwrap(); + $block; + } +} + +macro_rules! fixture { + ($filename:expr) => { + 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($filename); + + fs::copy(source_path, $filename).unwrap(); + } +} + +#[test] +fn test_successful_jpeg_image_embedding() { + in_temp_dir!({ + fixture!("attempt_1_no_image.mp3"); + fixture!("attempt_1.jpg"); + + { + let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); + assert!(tag.pictures().count() == 0); + } + + embed_image("attempt_1_no_image.mp3", "attempt_1.jpg").unwrap(); + + { + let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); + assert!(tag.pictures().count() > 0); + } + }); +} + +#[test] +fn test_successful_png_image_embedding() { + in_temp_dir!({ + fixture!("attempt_1_no_image.mp3"); + fixture!("attempt_1.png"); + + { + let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); + assert!(tag.pictures().count() == 0); + } + + embed_image("attempt_1_no_image.mp3", "attempt_1.png").unwrap(); + + { + let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); + assert!(tag.pictures().count() > 0); + } + }); +} + +#[test] +fn test_successful_image_embedding_in_a_file_that_already_has_an_image() { + in_temp_dir!({ + fixture!("attempt_1.mp3"); + fixture!("attempt_1.jpg"); + + { + let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); + assert!(tag.pictures().count() > 0); + } + + embed_image("attempt_1.mp3", "attempt_1.jpg").unwrap(); + + { + let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); + assert!(tag.pictures().count() > 0); + } + }); +} + +#[test] +fn test_unsuccessful_image_embedding() { + in_temp_dir!({ + fixture!("attempt_1_no_image.mp3"); + fixture!("attempt_1.jpg"); + + // Nonexistent files + assert!(embed_image("attempt_1_no_image.mp3", "nonexisting.jpg").is_err()); + assert!(embed_image("nonexisting.mp3", "attempt_1.jpg").is_err()); + assert!(embed_image("nonexisting.mp3", "nonexisting.jpg").is_err()); + + // Wrong kinds of files + assert!(embed_image("attempt_1.jpg", "attempt_1_no_image.mp3").is_err()); + assert!(embed_image("attempt_1_no_image.mp3", "attempt_1_no_image.mp3").is_err()); + assert!(embed_image("attempt_1.jpg", "attempt_1.jpg").is_err()); + }); +}