This commit is contained in:
Andrew Radev 2019-02-17 16:11:07 +02:00
parent 03cb9fe656
commit 7b8ac1b476
6 changed files with 103 additions and 1 deletions

View File

@ -13,7 +13,7 @@ pub fn embed_image(music_filename: &str, image_filename: &str) -> Result<(), Box
tag.add_picture(id3::frame::Picture { tag.add_picture(id3::frame::Picture {
mime_type: "image/jpeg".to_string(), mime_type: "image/jpeg".to_string(),
picture_type: id3::frame::PictureType::Other, picture_type: id3::frame::PictureType::CoverFront,
description: String::new(), description: String::new(),
data: encoded_image_bytes, data: encoded_image_bytes,
}); });

BIN
tests/fixtures/attempt_1.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
tests/fixtures/attempt_1.mp3 vendored Normal file

Binary file not shown.

BIN
tests/fixtures/attempt_1.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
tests/fixtures/attempt_1_no_image.mp3 vendored Normal file

Binary file not shown.

102
tests/test_processing.rs Normal file
View File

@ -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());
});
}