2019-02-17 14:11:07 +00:00
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use id3_image::*;
|
|
|
|
|
|
|
|
macro_rules! in_temp_dir {
|
|
|
|
($block:block) => {
|
2019-02-17 19:55:04 +00:00
|
|
|
let tmpdir = tempfile::tempdir().unwrap();
|
2019-02-17 14:11:07 +00:00
|
|
|
env::set_current_dir(&tmpdir).unwrap();
|
2019-02-17 19:55:04 +00:00
|
|
|
|
2019-02-17 14:11:07 +00:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
#[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());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:11:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_successful_jpeg_image_embedding() {
|
|
|
|
in_temp_dir!({
|
|
|
|
fixture!("attempt_1_no_image.mp3");
|
|
|
|
fixture!("attempt_1.jpg");
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() == 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
embed_image("attempt_1_no_image.mp3", "attempt_1.jpg").unwrap();
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() > 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_successful_png_image_embedding() {
|
|
|
|
in_temp_dir!({
|
|
|
|
fixture!("attempt_1_no_image.mp3");
|
|
|
|
fixture!("attempt_1.png");
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() == 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
embed_image("attempt_1_no_image.mp3", "attempt_1.png").unwrap();
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() > 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() > 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
|
|
|
|
embed_image("attempt_1.mp3", "attempt_1.jpg").unwrap();
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() > 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-02-17 19:55:04 +00:00
|
|
|
fn test_remove_and_add_image() {
|
2019-02-17 14:11:07 +00:00
|
|
|
in_temp_dir!({
|
2019-02-17 19:55:04 +00:00
|
|
|
fixture!("attempt_1.mp3");
|
2019-02-17 14:11:07 +00:00
|
|
|
fixture!("attempt_1.jpg");
|
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap();
|
|
|
|
assert!(tag.pictures().count() > 0);
|
2019-02-17 14:11:07 +00:00
|
|
|
|
2019-02-17 19:55:04 +00:00
|
|
|
remove_images("attempt_1.mp3").unwrap();
|
|
|
|
|
|
|
|
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);
|
2019-02-17 14:11:07 +00:00
|
|
|
});
|
|
|
|
}
|