From fcd5c4d1b11344e17637ce863eebc6b6f31a1e3b Mon Sep 17 00:00:00 2001 From: astar76 Date: Thu, 19 Nov 2026 06:56:46 -0600 Subject: [PATCH] tags: open files by content; round-trip tests on every format Co-Authored-By: Claude Opus 5.5 --- crates/tunal-tags/src/io.rs | 10 ++- crates/tunal-tags/tests/formats.rs | 103 +++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 crates/tunal-tags/tests/formats.rs diff --git a/crates/tunal-tags/src/io.rs b/crates/tunal-tags/src/io.rs index 47bc966..0a2274e 100644 --- a/crates/tunal-tags/src/io.rs +++ b/crates/tunal-tags/src/io.rs @@ -6,6 +6,7 @@ use std::path::{Path, PathBuf}; use lofty::config::WriteOptions; use lofty::file::{AudioFile, TaggedFile, TaggedFileExt}; use lofty::picture::{MimeType, Picture, PictureType}; +use lofty::probe::Probe; use lofty::tag::{Accessor, ItemKey, Tag, TagType}; use crate::model::{Cover, CoverEdit, Edit, TagEdit, TagSet, TrackTags}; @@ -100,8 +101,15 @@ fn read_tag(tag: &Tag) -> TrackTags { } } +/// Opens a file by its content (MP3, FLAC, Ogg Vorbis/Opus/Speex, MP4/M4A, WAV, AIFF, APE, +/// WavPack, Musepack, AAC); the extension only decides when the content can't. fn open(path: &Path) -> Result { - lofty::read_from_path(path).map_err(tag_err) + Probe::open(path) + .map_err(tag_err)? + .guess_file_type() + .map_err(Error::Io)? + .read() + .map_err(tag_err) } /// Reads the tags of one file. A file without a tag gives empty tags. diff --git a/crates/tunal-tags/tests/formats.rs b/crates/tunal-tags/tests/formats.rs new file mode 100644 index 0000000..28e64a9 --- /dev/null +++ b/crates/tunal-tags/tests/formats.rs @@ -0,0 +1,103 @@ +//! Tags round-trip on every format the editor offers, on files made with ffmpeg in a temp dir; +//! skipped without ffmpeg (the fixed fixtures in tags.rs cover the common five). + +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use tunal_tags::{Edit, TagEdit, duration_ms, read, write}; + +fn ffmpeg() -> bool { + Command::new("ffmpeg") + .arg("-version") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .is_ok_and(|s| s.success()) +} + +/// A one-second tone in `name`, encoded with `args`, or `None` when this ffmpeg lacks the +/// encoder. +fn make(dir: &Path, name: &str, args: &[&str]) -> Option { + let out = dir.join(name); + let ok = Command::new("ffmpeg") + .args(["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i"]) + .arg("sine=frequency=440:duration=1") + .args(args) + .arg(&out) + .status() + .is_ok_and(|s| s.success()); + ok.then_some(out) +} + +fn edit() -> TagEdit { + let s = |v: &str| Edit::Set(v.to_owned()); + TagEdit { + title: s("Wave"), + artist: s("Tester"), + album: s("Formats"), + track_no: Edit::Set(7), + date: s("2024"), + ..TagEdit::default() + } +} + +#[test] +fn tags_round_trip_on_every_offered_format() { + if !ffmpeg() { + eprintln!("skipped: no ffmpeg"); + return; + } + let dir = tempfile::tempdir().unwrap(); + let formats: [(&str, &[&str]); 11] = [ + ("t.mp3", &["-c:a", "libmp3lame"]), + ("t.flac", &["-c:a", "flac"]), + ("t.ogg", &["-c:a", "libvorbis"]), + ("t.opus", &["-c:a", "libopus"]), + ("t.spx", &["-c:a", "libspeex", "-ar", "16000"]), + ("t.m4a", &["-c:a", "aac"]), + ("alac.m4a", &["-c:a", "alac"]), + ("t.wav", &["-c:a", "pcm_s16le"]), + ("t.aiff", &["-c:a", "pcm_s16be"]), + ("t.wv", &["-c:a", "wavpack"]), + // Opus content under another format's name: lofty goes by the content. + ("opus-named.ogg", &["-c:a", "libopus", "-f", "ogg"]), + ]; + let mut tried = 0; + for (name, args) in formats { + let Some(path) = make(dir.path(), name, args) else { + eprintln!("{name}: encoder missing, skipped"); + continue; + }; + tried += 1; + let before = duration_ms(&path).unwrap_or_else(|| panic!("{name}: no duration")); + write(&path, &edit()).unwrap_or_else(|e| panic!("{name}: {e}")); + let t = read(&path).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!(t.title.as_deref(), Some("Wave"), "{name}"); + assert_eq!(t.artist.as_deref(), Some("Tester"), "{name}"); + assert_eq!(t.album.as_deref(), Some("Formats"), "{name}"); + assert_eq!(t.track_no, Some(7), "{name}"); + // A second edit touches only its field. + let retitle = TagEdit { + title: Edit::Set("Wave 2".into()), + ..TagEdit::default() + }; + write(&path, &retitle).unwrap_or_else(|e| panic!("{name}: {e}")); + let t = read(&path).unwrap(); + assert_eq!(t.title.as_deref(), Some("Wave 2"), "{name}"); + assert_eq!(t.artist.as_deref(), Some("Tester"), "{name}"); + // The audio is still all there. + let after = duration_ms(&path).unwrap_or_else(|| panic!("{name}: no duration after")); + assert!( + before.abs_diff(after) <= 50, + "{name}: {before} → {after} ms" + ); + let decoded = Command::new("ffmpeg") + .args(["-nostdin", "-v", "error", "-i"]) + .arg(&path) + .args(["-f", "null", "-"]) + .status() + .unwrap(); + assert!(decoded.success(), "{name}: still decodes"); + } + assert!(tried >= 5, "only {tried} formats could be made"); +}