Refactored test
This commit is contained in:
51
src/meta.rs
51
src/meta.rs
@@ -19,7 +19,7 @@ pub mod coverart {
|
|||||||
|
|
||||||
use lofty::{file::AudioFile, ogg::OggPictureStorage};
|
use lofty::{file::AudioFile, ogg::OggPictureStorage};
|
||||||
|
|
||||||
pub fn get_coverart(
|
pub fn set_coverart(
|
||||||
song_filepath: &String,
|
song_filepath: &String,
|
||||||
coverart_filepath: &String,
|
coverart_filepath: &String,
|
||||||
) -> Result<Vec<u8>, std::io::Error> {
|
) -> Result<Vec<u8>, std::io::Error> {
|
||||||
@@ -50,6 +50,33 @@ pub mod coverart {
|
|||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_coverart(song_filepath: &String) -> Result<Vec<u8>, std::io::Error> {
|
||||||
|
match std::fs::File::open(song_filepath) {
|
||||||
|
Ok(mut file) => {
|
||||||
|
match lofty::flac::FlacFile::read_from(
|
||||||
|
&mut file,
|
||||||
|
lofty::config::ParseOptions::new(),
|
||||||
|
) {
|
||||||
|
Ok(flac_file) => {
|
||||||
|
let pictures = flac_file.pictures();
|
||||||
|
let res = pictures.to_vec();
|
||||||
|
if !res.is_empty() {
|
||||||
|
let picture = &res[0];
|
||||||
|
Ok(picture.clone().0.into_data())
|
||||||
|
} else {
|
||||||
|
Ok(Vec::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidData,
|
||||||
|
err.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
||||||
@@ -155,12 +182,8 @@ mod tests {
|
|||||||
let mut file = std::fs::File::create(file)?;
|
let mut file = std::fs::File::create(file)?;
|
||||||
|
|
||||||
match file.write_all(bytes) {
|
match file.write_all(bytes) {
|
||||||
Ok(_res) => {
|
Ok(_res) => Ok(()),
|
||||||
Ok(())
|
Err(err) => Err(err),
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
Err(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_full_path(
|
pub fn get_full_path(
|
||||||
@@ -853,21 +876,27 @@ mod tests {
|
|||||||
|
|
||||||
let temp_file = tempfile::tempdir().expect("Could not create test directory");
|
let temp_file = tempfile::tempdir().expect("Could not create test directory");
|
||||||
let test_dir = String::from(temp_file.path().to_str().unwrap());
|
let test_dir = String::from(temp_file.path().to_str().unwrap());
|
||||||
let test_filename = String::from("track08.flac");
|
// let test_filename = String::from("track08.flac");
|
||||||
let new_filepath = test_dir.clone() + "/" + &test_filename;
|
// let new_filepath = test_dir.clone() + "/" + &test_filename;
|
||||||
|
|
||||||
match file_exists(&dir, &filename) {
|
match file_exists(&dir, &filename) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let filepath = get_full_path(&dir, &filename).unwrap();
|
let filepath = get_full_path(&dir, &filename).unwrap();
|
||||||
|
|
||||||
match coverart::get_coverart(&filepath, &new_filepath) {
|
match coverart::get_coverart(&filepath) {
|
||||||
Ok(coverart) => {
|
Ok(coverart) => {
|
||||||
let is_empty = coverart.is_empty();
|
let is_empty = coverart.is_empty();
|
||||||
assert_eq!(is_empty, false, "Should not be empty");
|
assert_eq!(is_empty, false, "Should not be empty");
|
||||||
// Save image
|
|
||||||
let mut new_coverart_path: String = test_dir.clone();
|
let mut new_coverart_path: String = test_dir.clone();
|
||||||
new_coverart_path += &String::from("/newcovvv.png");
|
new_coverart_path += &String::from("/newcovvv.png");
|
||||||
let _ = util::save_bytes_to_file(&coverart, &new_coverart_path);
|
let _ = util::save_bytes_to_file(&coverart, &new_coverart_path);
|
||||||
|
let created_file = std::path::Path::new(&new_coverart_path);
|
||||||
|
assert!(
|
||||||
|
created_file.exists(),
|
||||||
|
"Error: {:?} has not been created",
|
||||||
|
new_coverart_path
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {:?}", err.to_string());
|
assert!(false, "Error: {:?}", err.to_string());
|
||||||
|
Reference in New Issue
Block a user