Fixture improvements

This commit is contained in:
Andrew Radev 2019-02-24 16:08:12 +02:00
parent 852e269b6d
commit 7c1a356310

View File

@ -11,9 +11,20 @@ struct Fixture {
_tempdir: TempDir,
}
impl AsRef<Path> for Fixture {
fn as_ref(&self) -> &Path {
self.path_buf.as_path()
impl Fixture {
fn new(fixture_filename: &str) -> Self {
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(&fixture_filename);
let tempdir = tempfile::tempdir().unwrap();
let mut target_path = PathBuf::from(&tempdir.path());
target_path.push(&fixture_filename);
fs::copy(&source_path, &target_path).unwrap();
Fixture { _tempdir: tempdir, path_buf: target_path }
}
}
@ -25,33 +36,14 @@ impl Deref for Fixture {
}
}
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 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: &Path) -> id3::Tag {
id3::Tag::read_from_path(path).unwrap()
}
#[test]
fn test_unsuccessful_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.jpg");
let song = Fixture::new("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.jpg");
// Nonexistent files
assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err());
@ -66,8 +58,8 @@ fn test_unsuccessful_image_embedding() {
#[test]
fn test_successful_jpeg_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.jpg");
let song = Fixture::new("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
@ -80,8 +72,8 @@ fn test_successful_jpeg_image_embedding() {
#[test]
fn test_successful_png_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.png");
let song = Fixture::new("attempt_1_no_image.mp3");
let image = Fixture::new("attempt_1.png");
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
@ -94,8 +86,8 @@ fn test_successful_png_image_embedding() {
#[test]
fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
let song = fixture!("attempt_1.mp3");
let image = fixture!("attempt_1.jpg");
let song = Fixture::new("attempt_1.mp3");
let image = Fixture::new("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
@ -108,8 +100,8 @@ fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
#[test]
fn test_remove_and_add_image() {
let song = fixture!("attempt_1.mp3");
let image = fixture!("attempt_1.jpg");
let song = Fixture::new("attempt_1.mp3");
let image = Fixture::new("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);