Can read image from memory

This commit is contained in:
2022-03-22 17:44:13 +01:00
parent 7276236936
commit b94d312a9d
2 changed files with 78 additions and 28 deletions

View File

@ -1,9 +1,10 @@
use std::env;
use std::fs;
use std::path::{PathBuf, Path};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use id3_image::*;
struct Fixture {
@ -45,9 +46,10 @@ fn read_tag(path: &Path) -> id3::Tag {
id3::Tag::read_from_path(path).unwrap()
}
#[test]
fn test_unsuccessful_image_embedding() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.jpg");
// Nonexistent files
@ -63,11 +65,11 @@ fn test_unsuccessful_image_embedding() {
#[test]
fn test_successful_jpeg_image_embedding() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
assert_eq!(tag.pictures().count(), 0);
embed_image(&song, &image).unwrap();
@ -77,7 +79,7 @@ fn test_successful_jpeg_image_embedding() {
#[test]
fn test_successful_jpeg_image_embedding_with_a_broken_file() {
let song = Fixture::copy("attempt_1_broken.mp3");
let song = Fixture::copy("attempt_1_broken.mp3");
let image = Fixture::copy("attempt_1.jpg");
embed_image(&song, &image).unwrap();
@ -88,11 +90,11 @@ fn test_successful_jpeg_image_embedding_with_a_broken_file() {
#[test]
fn test_successful_png_image_embedding() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
assert_eq!(tag.pictures().count(), 0);
embed_image(&song, &image).unwrap();
@ -100,9 +102,41 @@ fn test_successful_png_image_embedding() {
assert!(tag.pictures().count() > 0);
}
#[test]
fn test_successful_png_image_embedding_from_memory() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song);
assert_eq!(tag.pictures().count(), 0);
embed_image_from_memory(&song, &image::open(&*image).unwrap()).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
}
#[test]
fn test_successful_png_image_embedding_and_extracting() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::copy("attempt_1.png");
let tag = read_tag(&song);
assert_eq!(tag.pictures().count(), 0);
extract_first_image_as_img(&song).unwrap_err();
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
extract_first_image_as_img(&song).unwrap();
}
#[test]
fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
let song = Fixture::copy("attempt_1.mp3");
let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song);
@ -116,7 +150,7 @@ fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
#[test]
fn test_removing_and_adding_an_image() {
let song = Fixture::copy("attempt_1.mp3");
let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song);
@ -125,7 +159,7 @@ fn test_removing_and_adding_an_image() {
remove_images(&song).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
assert_eq!(tag.pictures().count(), 0);
embed_image(&song, &image).unwrap();
@ -135,13 +169,13 @@ fn test_removing_and_adding_an_image() {
#[test]
fn test_removing_and_adding_an_image_to_a_broken_file() {
let song = Fixture::copy("attempt_1_broken.mp3");
let song = Fixture::copy("attempt_1_broken.mp3");
let image = Fixture::copy("attempt_1.jpg");
remove_images(&song).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
assert_eq!(tag.pictures().count(), 0);
embed_image(&song, &image).unwrap();
@ -151,7 +185,7 @@ fn test_removing_and_adding_an_image_to_a_broken_file() {
#[test]
fn test_extracting_a_jpg_image() {
let song = Fixture::copy("attempt_1.mp3");
let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::blank("attempt_1.jpg");
let tag = read_tag(&song);
@ -165,7 +199,7 @@ fn test_extracting_a_jpg_image() {
#[test]
fn test_extracting_a_jpg_image_from_a_broken_file() {
let song = Fixture::copy("attempt_1_broken.mp3");
let song = Fixture::copy("attempt_1_broken.mp3");
let image = Fixture::blank("attempt_1.jpg");
extract_first_image(&song, &image).unwrap();
@ -175,7 +209,7 @@ fn test_extracting_a_jpg_image_from_a_broken_file() {
#[test]
fn test_extracting_a_png_image() {
let song = Fixture::copy("attempt_1.mp3");
let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::blank("attempt_1.png");
let tag = read_tag(&song);
@ -189,7 +223,7 @@ fn test_extracting_a_png_image() {
#[test]
fn test_overwriting_an_existing_image() {
let song = Fixture::copy("attempt_1.mp3");
let song = Fixture::copy("attempt_1.mp3");
let image = Fixture::copy("attempt_1.jpg");
let tag = read_tag(&song);
@ -203,11 +237,11 @@ fn test_overwriting_an_existing_image() {
#[test]
fn test_extracting_an_image_with_no_pictures() {
let song = Fixture::copy("attempt_1_no_image.mp3");
let song = Fixture::copy("attempt_1_no_image.mp3");
let image = Fixture::blank("attempt_1.jpg");
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
assert_eq!(tag.pictures().count(), 0);
assert!(!image.exists());
assert!(extract_first_image(&song, &image).is_err());