From 394b33214737ea48ab3a326ea54fdf16648c03f9 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Thu, 21 Feb 2019 21:35:20 +0200 Subject: [PATCH] Get tests working in parallel Instead of a single temporary directory, we just create one per file and encapsulate it in a structure, so we can get both the file path and a temp dir that's dropped upon the struct leaving scope. --- tests/test_processing.rs | 138 +++++++++++++++++++++------------------ 1 file changed, 76 insertions(+), 62 deletions(-) diff --git a/tests/test_processing.rs b/tests/test_processing.rs index d79fe73..1d8b19b 100644 --- a/tests/test_processing.rs +++ b/tests/test_processing.rs @@ -1,112 +1,126 @@ use std::env; use std::fs; -use std::path::PathBuf; +use std::path::{PathBuf, Path}; +use std::ops::Deref; +use tempfile::TempDir; use id3_image::*; -macro_rules! in_temp_dir { - ($block:block) => { - let tmpdir = tempfile::tempdir().unwrap(); - env::set_current_dir(&tmpdir).unwrap(); +struct Fixture { + path_buf: PathBuf, + _tempdir: TempDir, +} - $block; +impl AsRef for Fixture { + fn as_ref(&self) -> &Path { + self.path_buf.as_path() + } +} + +impl Deref for Fixture { + type Target = str; + + fn deref(&self) -> &Self::Target { + self.path_buf.to_str().unwrap() } } 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); + { + 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(); + let tempdir = tempfile::tempdir().unwrap(); + let mut target_path = PathBuf::from(&tempdir.path()); + target_path.push($filename); + + fs::copy(&source_path, &target_path).unwrap(); + + Fixture { _tempdir: tempdir, path_buf: target_path } + } } } +fn read_tag(path: &str) -> id3::Tag { + id3::Tag::read_from_path(path).unwrap() +} + #[test] fn test_unsuccessful_image_embedding() { - in_temp_dir!({ - fixture!("attempt_1_no_image.mp3"); - fixture!("attempt_1.jpg"); + let song = fixture!("attempt_1_no_image.mp3"); + let image = 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()); + // Nonexistent files + assert!(embed_image(&song, "nonexisting.jpg").is_err()); + assert!(embed_image("nonexisting.mp3", &image).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()); - }); + // Wrong kinds of files + assert!(embed_image(&image, &song).is_err()); + assert!(embed_image(&song, &song).is_err()); + assert!(embed_image(&image, &image).is_err()); } #[test] fn test_successful_jpeg_image_embedding() { - in_temp_dir!({ - fixture!("attempt_1_no_image.mp3"); - fixture!("attempt_1.jpg"); + let song = fixture!("attempt_1_no_image.mp3"); + let image = fixture!("attempt_1.jpg"); - let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); - assert!(tag.pictures().count() == 0); + let tag = read_tag(&song); + assert!(tag.pictures().count() == 0); - embed_image("attempt_1_no_image.mp3", "attempt_1.jpg").unwrap(); + embed_image(&song, &image).unwrap(); - let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); - assert!(tag.pictures().count() > 0); - }); + let tag = read_tag(&song); + 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 song = fixture!("attempt_1_no_image.mp3"); + let image = fixture!("attempt_1.png"); - let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); - assert!(tag.pictures().count() == 0); + let tag = read_tag(&song); + assert!(tag.pictures().count() == 0); - embed_image("attempt_1_no_image.mp3", "attempt_1.png").unwrap(); + embed_image(&song, &image).unwrap(); - let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap(); - assert!(tag.pictures().count() > 0); - }); + let tag = read_tag(&song); + 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 song = fixture!("attempt_1.mp3"); + let image = fixture!("attempt_1.jpg"); - let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); - assert!(tag.pictures().count() > 0); + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); - embed_image("attempt_1.mp3", "attempt_1.jpg").unwrap(); + embed_image(&song, &image).unwrap(); - let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); - assert!(tag.pictures().count() > 0); - }); + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); } #[test] fn test_remove_and_add_image() { - in_temp_dir!({ - fixture!("attempt_1.mp3"); - fixture!("attempt_1.jpg"); + let song = fixture!("attempt_1.mp3"); + let image = fixture!("attempt_1.jpg"); - let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); - assert!(tag.pictures().count() > 0); + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); - remove_images("attempt_1.mp3").unwrap(); + remove_images(&song).unwrap(); - let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); - assert!(tag.pictures().count() == 0); + let tag = read_tag(&song); + assert!(tag.pictures().count() == 0); - embed_image("attempt_1.mp3", "attempt_1.jpg").unwrap(); + embed_image(&song, &image).unwrap(); - let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap(); - assert!(tag.pictures().count() > 0); - }); + let tag = read_tag(&song); + assert!(tag.pictures().count() > 0); }