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.
This commit is contained in:
parent
915a6e8ff4
commit
394b332147
@ -1,112 +1,126 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::{PathBuf, Path};
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
use tempfile::TempDir;
|
||||||
use id3_image::*;
|
use id3_image::*;
|
||||||
|
|
||||||
macro_rules! in_temp_dir {
|
struct Fixture {
|
||||||
($block:block) => {
|
path_buf: PathBuf,
|
||||||
let tmpdir = tempfile::tempdir().unwrap();
|
_tempdir: TempDir,
|
||||||
env::set_current_dir(&tmpdir).unwrap();
|
}
|
||||||
|
|
||||||
$block;
|
impl AsRef<Path> 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 {
|
macro_rules! fixture {
|
||||||
($filename:expr) => {
|
($filename:expr) => {
|
||||||
|
{
|
||||||
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_path = PathBuf::from(root_dir);
|
||||||
source_path.push("tests/fixtures");
|
source_path.push("tests/fixtures");
|
||||||
source_path.push($filename);
|
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]
|
#[test]
|
||||||
fn test_unsuccessful_image_embedding() {
|
fn test_unsuccessful_image_embedding() {
|
||||||
in_temp_dir!({
|
let song = fixture!("attempt_1_no_image.mp3");
|
||||||
fixture!("attempt_1_no_image.mp3");
|
let image = fixture!("attempt_1.jpg");
|
||||||
fixture!("attempt_1.jpg");
|
|
||||||
|
|
||||||
// Nonexistent files
|
// Nonexistent files
|
||||||
assert!(embed_image("attempt_1_no_image.mp3", "nonexisting.jpg").is_err());
|
assert!(embed_image(&song, "nonexisting.jpg").is_err());
|
||||||
assert!(embed_image("nonexisting.mp3", "attempt_1.jpg").is_err());
|
assert!(embed_image("nonexisting.mp3", &image).is_err());
|
||||||
assert!(embed_image("nonexisting.mp3", "nonexisting.jpg").is_err());
|
assert!(embed_image("nonexisting.mp3", "nonexisting.jpg").is_err());
|
||||||
|
|
||||||
// Wrong kinds of files
|
// Wrong kinds of files
|
||||||
assert!(embed_image("attempt_1.jpg", "attempt_1_no_image.mp3").is_err());
|
assert!(embed_image(&image, &song).is_err());
|
||||||
assert!(embed_image("attempt_1_no_image.mp3", "attempt_1_no_image.mp3").is_err());
|
assert!(embed_image(&song, &song).is_err());
|
||||||
assert!(embed_image("attempt_1.jpg", "attempt_1.jpg").is_err());
|
assert!(embed_image(&image, &image).is_err());
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_successful_jpeg_image_embedding() {
|
fn test_successful_jpeg_image_embedding() {
|
||||||
in_temp_dir!({
|
let song = fixture!("attempt_1_no_image.mp3");
|
||||||
fixture!("attempt_1_no_image.mp3");
|
let image = fixture!("attempt_1.jpg");
|
||||||
fixture!("attempt_1.jpg");
|
|
||||||
|
|
||||||
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() == 0);
|
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();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
assert!(tag.pictures().count() > 0);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_successful_png_image_embedding() {
|
fn test_successful_png_image_embedding() {
|
||||||
in_temp_dir!({
|
let song = fixture!("attempt_1_no_image.mp3");
|
||||||
fixture!("attempt_1_no_image.mp3");
|
let image = fixture!("attempt_1.png");
|
||||||
fixture!("attempt_1.png");
|
|
||||||
|
|
||||||
let tag = id3::Tag::read_from_path("attempt_1_no_image.mp3").unwrap();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() == 0);
|
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();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
assert!(tag.pictures().count() > 0);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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() {
|
||||||
in_temp_dir!({
|
let song = fixture!("attempt_1.mp3");
|
||||||
fixture!("attempt_1.mp3");
|
let image = fixture!("attempt_1.jpg");
|
||||||
fixture!("attempt_1.jpg");
|
|
||||||
|
|
||||||
let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
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();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
assert!(tag.pictures().count() > 0);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_remove_and_add_image() {
|
fn test_remove_and_add_image() {
|
||||||
in_temp_dir!({
|
let song = fixture!("attempt_1.mp3");
|
||||||
fixture!("attempt_1.mp3");
|
let image = fixture!("attempt_1.jpg");
|
||||||
fixture!("attempt_1.jpg");
|
|
||||||
|
|
||||||
let tag = id3::Tag::read_from_path("attempt_1.mp3").unwrap();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
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();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() == 0);
|
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();
|
let tag = read_tag(&song);
|
||||||
assert!(tag.pictures().count() > 0);
|
assert!(tag.pictures().count() > 0);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user