Test extracting an image

This commit is contained in:
Andrew Radev 2019-02-24 16:20:38 +02:00
parent 67d39f4172
commit f00d77d99b

View File

@ -7,24 +7,29 @@ use tempfile::TempDir;
use id3_image::*; use id3_image::*;
struct Fixture { struct Fixture {
path_buf: PathBuf, path: PathBuf,
source: PathBuf,
_tempdir: TempDir, _tempdir: TempDir,
} }
impl Fixture { 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 root_dir = &env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR");
let mut source_path = PathBuf::from(root_dir); let mut source = PathBuf::from(root_dir);
source_path.push("tests/fixtures"); source.push("tests/fixtures");
source_path.push(&fixture_filename); source.push(&fixture_filename);
let tempdir = tempfile::tempdir().unwrap(); let tempdir = tempfile::tempdir().unwrap();
let mut target_path = PathBuf::from(&tempdir.path()); let mut path = PathBuf::from(&tempdir.path());
target_path.push(&fixture_filename); 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; type Target = Path;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.path_buf.deref() self.path.deref()
} }
} }
@ -42,8 +47,8 @@ fn read_tag(path: &Path) -> id3::Tag {
#[test] #[test]
fn test_unsuccessful_image_embedding() { fn test_unsuccessful_image_embedding() {
let song = Fixture::new("attempt_1_no_image.mp3"); let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.jpg"); let image = Fixture::copy("attempt_1.jpg");
// Nonexistent files // Nonexistent files
assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err()); assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err());
@ -58,8 +63,8 @@ fn test_unsuccessful_image_embedding() {
#[test] #[test]
fn test_successful_jpeg_image_embedding() { fn test_successful_jpeg_image_embedding() {
let song = Fixture::new("attempt_1_no_image.mp3"); let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.jpg"); let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song); let tag = read_tag(&song);
assert!(tag.pictures().count() == 0); assert!(tag.pictures().count() == 0);
@ -72,8 +77,8 @@ fn test_successful_jpeg_image_embedding() {
#[test] #[test]
fn test_successful_png_image_embedding() { fn test_successful_png_image_embedding() {
let song = Fixture::new("attempt_1_no_image.mp3"); let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.png"); let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song); let tag = read_tag(&song);
assert!(tag.pictures().count() == 0); assert!(tag.pictures().count() == 0);
@ -86,8 +91,8 @@ fn test_successful_png_image_embedding() {
#[test] #[test]
fn test_successful_image_embedding_in_a_file_that_already_has_an_image() { fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
let song = Fixture::new("attempt_1.mp3"); let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::new("attempt_1.jpg"); let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song); let tag = read_tag(&song);
assert!(tag.pictures().count() > 0); assert!(tag.pictures().count() > 0);
@ -99,9 +104,9 @@ fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
} }
#[test] #[test]
fn test_remove_and_add_image() { fn test_removing_and_adding_an_image() {
let song = Fixture::new("attempt_1.mp3"); let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::new("attempt_1.jpg"); let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song); let tag = read_tag(&song);
assert!(tag.pictures().count() > 0); assert!(tag.pictures().count() > 0);
@ -116,3 +121,45 @@ fn test_remove_and_add_image() {
let tag = read_tag(&song); let tag = read_tag(&song);
assert!(tag.pictures().count() > 0); 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());
}