2021-12-05 22:02:22 +00:00
|
|
|
use crate::{set_artist, temp_file, verify_artist};
|
2024-04-13 16:50:28 +00:00
|
|
|
use lofty::config::{ParseOptions, WriteOptions};
|
2024-04-13 17:24:14 +00:00
|
|
|
use lofty::file::FileType;
|
2024-04-10 18:08:28 +00:00
|
|
|
use lofty::prelude::*;
|
2024-04-13 18:01:48 +00:00
|
|
|
use lofty::tag::TagType;
|
|
|
|
use lofty::Probe;
|
2024-04-10 18:08:28 +00:00
|
|
|
|
2022-04-13 17:28:48 +00:00
|
|
|
use std::io::{Seek, Write};
|
2021-09-03 19:37:37 +00:00
|
|
|
|
|
|
|
// The tests for OGG Opus/Vorbis are nearly identical
|
2021-09-06 14:41:24 +00:00
|
|
|
// We have the vendor string and a title stored in the tag
|
2021-09-03 19:37:37 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-10-01 21:59:53 +00:00
|
|
|
fn opus_read() {
|
2022-02-19 15:14:14 +00:00
|
|
|
read("tests/files/assets/minimal/full_test.opus", FileType::Opus)
|
2021-09-03 19:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-10-01 21:59:53 +00:00
|
|
|
fn opus_write() {
|
2022-02-19 15:14:14 +00:00
|
|
|
write("tests/files/assets/minimal/full_test.opus", FileType::Opus)
|
2021-09-06 14:41:24 +00:00
|
|
|
}
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
#[test]
|
2021-10-01 21:59:53 +00:00
|
|
|
fn opus_remove() {
|
2022-02-13 18:15:27 +00:00
|
|
|
remove(
|
|
|
|
"tests/files/assets/minimal/full_test.opus",
|
|
|
|
TagType::VorbisComments,
|
|
|
|
)
|
2021-10-01 21:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flac_read() {
|
2021-09-06 14:41:24 +00:00
|
|
|
// FLAC does **not** require a Vorbis comment block be present, this file has one
|
2023-04-21 02:22:07 +00:00
|
|
|
read("tests/files/assets/minimal/full_test.flac", FileType::Flac)
|
2021-09-06 14:41:24 +00:00
|
|
|
}
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
#[test]
|
2021-10-01 21:59:53 +00:00
|
|
|
fn flac_write() {
|
2023-04-21 02:22:07 +00:00
|
|
|
write("tests/files/assets/minimal/full_test.flac", FileType::Flac)
|
2021-09-03 19:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-03-27 18:12:32 +00:00
|
|
|
fn flac_remove_vorbis_comments() {
|
2022-02-13 18:15:27 +00:00
|
|
|
crate::remove_tag!(
|
|
|
|
"tests/files/assets/minimal/full_test.flac",
|
|
|
|
TagType::VorbisComments
|
|
|
|
);
|
2021-10-01 21:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vorbis_read() {
|
2022-02-19 15:14:14 +00:00
|
|
|
read("tests/files/assets/minimal/full_test.ogg", FileType::Vorbis)
|
2021-09-06 14:41:24 +00:00
|
|
|
}
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
#[test]
|
2021-10-01 21:59:53 +00:00
|
|
|
fn vorbis_write() {
|
2022-02-19 15:14:14 +00:00
|
|
|
write("tests/files/assets/minimal/full_test.ogg", FileType::Vorbis)
|
2021-09-06 14:41:24 +00:00
|
|
|
}
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-10-01 21:59:53 +00:00
|
|
|
#[test]
|
|
|
|
fn vorbis_remove() {
|
2022-02-13 18:15:27 +00:00
|
|
|
remove(
|
|
|
|
"tests/files/assets/minimal/full_test.ogg",
|
|
|
|
TagType::VorbisComments,
|
|
|
|
)
|
2022-01-31 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn speex_read() {
|
2022-02-19 15:14:14 +00:00
|
|
|
read("tests/files/assets/minimal/full_test.spx", FileType::Speex)
|
2022-01-31 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn speex_write() {
|
2022-02-19 15:14:14 +00:00
|
|
|
write("tests/files/assets/minimal/full_test.spx", FileType::Speex)
|
2022-01-31 23:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn speex_remove() {
|
2022-02-13 18:15:27 +00:00
|
|
|
remove(
|
|
|
|
"tests/files/assets/minimal/full_test.spx",
|
|
|
|
TagType::VorbisComments,
|
|
|
|
)
|
2021-10-01 21:59:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 15:14:14 +00:00
|
|
|
fn read(path: &str, file_type: FileType) {
|
2022-09-24 06:34:22 +00:00
|
|
|
let file = Probe::open(path)
|
|
|
|
.unwrap()
|
|
|
|
.options(ParseOptions::new().read_properties(false))
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
assert_eq!(file.file_type(), file_type);
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
crate::verify_artist!(file, primary_tag, "Foo artist", 2);
|
2021-09-03 19:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 15:14:14 +00:00
|
|
|
fn write(path: &str, file_type: FileType) {
|
2021-12-05 22:02:22 +00:00
|
|
|
let mut file = temp_file!(path);
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2022-09-24 06:34:22 +00:00
|
|
|
let mut tagged_file = Probe::new(&mut file)
|
|
|
|
.options(ParseOptions::new().read_properties(false))
|
|
|
|
.guess_file_type()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
assert_eq!(tagged_file.file_type(), file_type);
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 2 => file, "Bar artist");
|
2021-09-03 19:37:37 +00:00
|
|
|
|
|
|
|
// Now reread the file
|
2022-04-13 17:28:48 +00:00
|
|
|
file.rewind().unwrap();
|
2022-09-24 06:34:22 +00:00
|
|
|
let mut tagged_file = Probe::new(&mut file)
|
|
|
|
.options(ParseOptions::new().read_properties(false))
|
|
|
|
.guess_file_type()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-09-03 19:37:37 +00:00
|
|
|
|
2021-09-06 14:41:24 +00:00
|
|
|
crate::set_artist!(tagged_file, primary_tag_mut, "Bar artist", 2 => file, "Foo artist");
|
2021-09-03 19:37:37 +00:00
|
|
|
}
|
2021-10-01 21:59:53 +00:00
|
|
|
|
|
|
|
fn remove(path: &str, tag_type: TagType) {
|
2021-12-05 22:02:22 +00:00
|
|
|
let mut file = temp_file!(path);
|
2021-10-01 21:59:53 +00:00
|
|
|
|
2022-09-24 06:34:22 +00:00
|
|
|
let tagged_file = Probe::new(&mut file)
|
|
|
|
.options(ParseOptions::new().read_properties(false))
|
|
|
|
.guess_file_type()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-10-01 21:59:53 +00:00
|
|
|
// Verify we have both the vendor and artist
|
|
|
|
assert!(
|
2022-07-24 20:12:14 +00:00
|
|
|
tagged_file.tag(tag_type).is_some() && tagged_file.tag(tag_type).unwrap().item_count() == 2
|
2021-10-01 21:59:53 +00:00
|
|
|
);
|
|
|
|
|
2022-04-13 17:28:48 +00:00
|
|
|
file.rewind().unwrap();
|
2022-02-03 03:10:04 +00:00
|
|
|
tag_type.remove_from(&mut file).unwrap();
|
2021-10-01 21:59:53 +00:00
|
|
|
|
2022-04-13 17:28:48 +00:00
|
|
|
file.rewind().unwrap();
|
2022-09-24 06:34:22 +00:00
|
|
|
let tagged_file = Probe::new(&mut file)
|
|
|
|
.options(ParseOptions::new().read_properties(false))
|
|
|
|
.guess_file_type()
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap();
|
2021-12-05 22:02:22 +00:00
|
|
|
|
2021-10-01 21:59:53 +00:00
|
|
|
// We can't completely remove the tag since metadata packets are mandatory, but it should only have to vendor now
|
2022-07-24 20:12:14 +00:00
|
|
|
assert_eq!(tagged_file.tag(tag_type).unwrap().item_count(), 1);
|
2021-10-01 21:59:53 +00:00
|
|
|
}
|
2022-03-16 20:59:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flac_with_id3v2() {
|
2022-03-18 19:06:42 +00:00
|
|
|
use lofty::flac::FlacFile;
|
2022-03-16 20:59:55 +00:00
|
|
|
|
|
|
|
let file = std::fs::read("tests/files/assets/flac_with_id3v2.flac").unwrap();
|
2022-09-24 06:34:22 +00:00
|
|
|
let flac_file =
|
|
|
|
FlacFile::read_from(&mut std::io::Cursor::new(file), ParseOptions::new()).unwrap();
|
2022-03-16 20:59:55 +00:00
|
|
|
|
2022-07-24 20:08:46 +00:00
|
|
|
assert!(flac_file.id3v2().is_some());
|
2022-12-12 18:40:39 +00:00
|
|
|
assert_eq!(
|
|
|
|
flac_file.id3v2().unwrap().artist().as_deref(),
|
|
|
|
Some("Foo artist")
|
|
|
|
);
|
2022-03-16 20:59:55 +00:00
|
|
|
|
|
|
|
assert!(flac_file.vorbis_comments().is_some());
|
|
|
|
}
|
2022-03-27 18:12:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flac_remove_id3v2() {
|
2023-04-21 02:29:11 +00:00
|
|
|
crate::remove_tag!("tests/files/assets/flac_with_id3v2.flac", TagType::Id3v2);
|
2022-03-27 18:12:32 +00:00
|
|
|
}
|
2022-10-13 23:37:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flac_try_write_non_empty_id3v2() {
|
2023-04-28 05:55:40 +00:00
|
|
|
use lofty::id3::v2::Id3v2Tag;
|
2022-10-13 23:37:59 +00:00
|
|
|
|
2023-04-28 05:55:40 +00:00
|
|
|
let mut tag = Id3v2Tag::default();
|
2022-10-13 23:37:59 +00:00
|
|
|
tag.set_artist(String::from("Foo artist"));
|
|
|
|
|
|
|
|
assert!(tag
|
2024-04-03 05:40:04 +00:00
|
|
|
.save_to_path(
|
|
|
|
"tests/files/assets/flac_with_id3v2.flac",
|
2024-04-03 15:09:24 +00:00
|
|
|
WriteOptions::default()
|
2024-04-03 05:40:04 +00:00
|
|
|
)
|
2022-10-13 23:37:59 +00:00
|
|
|
.is_err());
|
|
|
|
}
|