From 5c391249b3c0641c97018ebbdec7126b302e1104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 11 Aug 2026 18:49:14 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20test(scratch):=20a=20fixture=20d?= =?UTF-8?q?irectory=20is=20deleted=20by=20the=20test=20that=20made=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every suite here made its own directory under /tmp with a name and a process id and then walked away from it: 2907 `ve_*` directories, 6.4G of a 16G tmpfs, none of them reclaimable by anything but a person with `rm`. `engine::scratch::Scratch` is the one place a throwaway path comes from now -- a directory, or the single file inside one -- and `Drop` is what takes it back. That is the whole reason it is a guard rather than a cleanup call at the end of a test: unwinding runs `Drop`, so the *failing* tests, which are exactly the ones that used to leave the most behind, clean up like the passing ones. It derefs to a `Path`, so the call sites did not have to change: the helpers that handed back a `PathBuf` hand back one of these. The prefixes stay what they were, so a directory is still recognisable while a test is stopped in it. Nothing about what a test *does* moved -- only where its fixtures live and when they die. Driven: the whole engine suite and the app's own tests run with the count of `/tmp/ve_*` before equal to the count after. --- crates/app/src/keymap.rs | 6 +- crates/app/src/main.rs | 3 +- crates/engine/src/edith.rs | 4 +- crates/engine/src/lib.rs | 1 + crates/engine/src/mux.rs | 14 +-- crates/engine/src/scratch.rs | 130 +++++++++++++++++++++++++++ crates/engine/tests/audio_decode.rs | 3 +- crates/engine/tests/audio_export.rs | 14 ++- crates/engine/tests/audio_files.rs | 18 ++-- crates/engine/tests/audio_streams.rs | 5 +- crates/engine/tests/av1.rs | 14 ++- crates/engine/tests/backends.rs | 8 +- crates/engine/tests/color.rs | 4 +- crates/engine/tests/eq_audio.rs | 7 +- crates/engine/tests/export.rs | 15 ++-- crates/engine/tests/export_subs.rs | 10 +-- crates/engine/tests/hevc.rs | 7 +- crates/engine/tests/hevc_export.rs | 10 +-- crates/engine/tests/hevc_hvc1.rs | 4 +- crates/engine/tests/images.rs | 19 ++-- crates/engine/tests/lanes.rs | 13 ++- crates/engine/tests/mixed_fps.rs | 10 +-- crates/engine/tests/playback.rs | 11 ++- crates/engine/tests/saveload.rs | 11 +-- crates/engine/tests/silence.rs | 4 +- crates/engine/tests/speed.rs | 5 +- crates/engine/tests/subtitles.rs | 8 +- crates/engine/tests/vp9.rs | 4 +- 28 files changed, 231 insertions(+), 131 deletions(-) create mode 100644 crates/engine/src/scratch.rs diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index b71ed8a..4e25649 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -961,6 +961,8 @@ fn parse(text: &str) -> Result<(Keymap, Vec), String> { #[cfg(test)] mod tests { + use engine::scratch::Scratch; + use super::{ActionId, Chord, Keymap, config_path_in, emit, parse}; fn chord(key: &str, ctrl: bool) -> Chord { @@ -1124,7 +1126,7 @@ mod tests { // The notice names the line and the file, and the defaults do *not* // take over -- that was the data loss. - let dir = std::env::temp_dir().join(format!("edith-keymap-drop-{}", std::process::id())); + let dir = Scratch::dir("edith-keymap-drop"); let path = dir.join("edith").join("keybindings"); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(&path, text).unwrap(); @@ -1230,7 +1232,7 @@ mod tests { #[test] fn a_saved_keymap_comes_back_off_the_disk() { // Its own directory, never the real config one: this test writes. - let dir = std::env::temp_dir().join(format!("edith-keymap-{}", std::process::id())); + let dir = Scratch::dir("edith-keymap"); let path = dir.join("edith").join("keybindings"); let mut written = Keymap::defaults(); written diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 3e7b97e..f10ff25 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -12973,8 +12973,7 @@ mod tests { // Saved and loaded back with the lanes in the order a hand-written // project may hold them: the sound last, behind the layer. - let dir = std::env::temp_dir().join(format!("ve_group_{}", std::process::id())); - std::fs::create_dir_all(&dir).expect("a scratch directory"); + let dir = engine::scratch::Scratch::dir("ve_group"); let file = dir.join("lanes.edith"); session.save_project(&file).expect("save the project"); let text = std::fs::read_to_string(&file).expect("read it back"); diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index e908ded..69cd184 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -1960,10 +1960,8 @@ mod tests { "a bare filename is saved into the working directory" ); - let dir = std::env::temp_dir().join(format!("ve_edith_link_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); + let dir = crate::scratch::Scratch::dir("ve_edith_link"); std::fs::create_dir_all(dir.join("real")).expect("scratch dir"); - let dir = std::fs::canonicalize(&dir).expect("canonical scratch dir"); std::os::unix::fs::symlink(dir.join("real"), dir.join("link")).expect("symlink"); let source = dir.join("real/a.mp4"); let path = dir.join("link/p.edith"); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index fd598da..85d65b8 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -17,6 +17,7 @@ pub mod mux; pub mod playback; pub mod project; pub mod scale; +pub mod scratch; pub mod silence; pub mod subtitle; pub mod waveform; diff --git a/crates/engine/src/mux.rs b/crates/engine/src/mux.rs index e6539c0..332a856 100644 --- a/crates/engine/src/mux.rs +++ b/crates/engine/src/mux.rs @@ -1283,6 +1283,8 @@ fn nal_type(nal: &[u8]) -> u8 { mod tests { use std::io::BufReader; + use crate::scratch::Scratch; + use super::*; const SPS: &[u8] = &[0x67, 0x42, 0x00, 0x1E, 0xAB]; @@ -1372,7 +1374,7 @@ mod tests { #[test] fn create_rejects_unusable_config() { - let out = std::env::temp_dir().join("ve_mux_never_written.mp4"); + let out = Scratch::file("ve_mux_never_written", "mp4"); let ok = VideoParams { width: 64, height: 64, @@ -1420,7 +1422,7 @@ mod tests { au(&[&[0x41, 0x04, 0x05]]), au(&[&[0x41, 0x06]]), ]; - let out = std::env::temp_dir().join(format!("ve_mux_{}.mp4", std::process::id())); + let out = Scratch::file("ve_mux", "mp4"); let mut muxer = Mp4Muxer::create( &out, @@ -1565,7 +1567,7 @@ mod tests { let packets = tone_packets(48_000, 2); let heard: usize = packets.iter().map(|p| p.samples as usize).sum(); - let out = std::env::temp_dir().join(format!("ve_mkv_sound_{}.mkv", std::process::id())); + let out = Scratch::file("ve_mkv_sound", "mkv"); let mut muxer = MkvMuxer::create( &out, &Av1Params { @@ -1637,7 +1639,7 @@ mod tests { let inter = obu(6, &[0xBB; 5]); let packets = tone_packets(48_000, 2); - let out = std::env::temp_dir().join(format!("ve_mp4_av1_{}.mp4", std::process::id())); + let out = Scratch::file("ve_mp4_av1", "mp4"); let mut muxer = Mp4Muxer::create_av1( &out, &Av1Params { @@ -1698,7 +1700,7 @@ mod tests { key.extend_from_slice(&obu(6, &[0xAA; 8])); let inter = obu(6, &[0xBB; 5]); - let out = std::env::temp_dir().join(format!("ve_mkv_{}.mkv", std::process::id())); + let out = Scratch::file("ve_mkv", "mkv"); let config = av1_sequence_header(&key).expect("the keyframe carries one"); assert_eq!(config, &sequence[..], "the sequence header OBU, whole"); let mut muxer = MkvMuxer::create( @@ -1769,7 +1771,7 @@ mod tests { #[test] fn an_ntsc_rate_survives_the_round_trip() { const NTSC: f64 = 24_000.0 / 1001.0; - let out = std::env::temp_dir().join(format!("ve_mux_ntsc_{}.mp4", std::process::id())); + let out = Scratch::file("ve_mux_ntsc", "mp4"); let mut muxer = Mp4Muxer::create( &out, &VideoParams { diff --git a/crates/engine/src/scratch.rs b/crates/engine/src/scratch.rs new file mode 100644 index 0000000..3905989 --- /dev/null +++ b/crates/engine/src/scratch.rs @@ -0,0 +1,130 @@ +//! Throwaway paths for the tests, and the one rule about them: everything a +//! test writes goes inside a directory that deletes itself. +//! +//! [`Scratch`] is that directory. It derefs to a path -- the directory itself, +//! or the one file inside it -- so a helper that used to hand back a `PathBuf` +//! hands back one of these instead and its call sites do not change. `Drop` is +//! what cleans up, which is the whole point: unwinding runs it, so a *failing* +//! test leaves nothing behind either. The suites used to litter `/tmp` with a +//! fresh `ve_*` directory per run and never take one back; on this machine that +//! reached 2907 of them. +//! +//! Nothing outside a test has any use for this. It lives in the library rather +//! than in a test file because the integration suites, the unit tests here and +//! the app's own tests all need the same guard, and none of them can see each +//! other's modules. + +use std::ffi::OsStr; +use std::ops::Deref; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +/// A temporary directory and the path a test uses inside it, gone when the +/// value is. +#[derive(Debug)] +pub struct Scratch { + dir: PathBuf, + path: PathBuf, +} + +/// A fresh, empty directory under the system temporary one. `name` leads it so +/// it is recognisable while a test is stopped in it; the process id and a +/// counter follow, because two suites (or two tests of one suite, in parallel) +/// must not write each other's files. Canonical, because saved projects hold +/// paths relative to their own directory and `Project` canonicalises what it is +/// handed. +fn fresh(name: &str) -> PathBuf { + static NTH: AtomicU64 = AtomicU64::new(0); + let dir = std::env::temp_dir().join(format!( + "{name}_{}_{}", + std::process::id(), + NTH.fetch_add(1, Ordering::Relaxed) + )); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).expect("scratch dir"); + std::fs::canonicalize(&dir).expect("canonical scratch dir") +} + +impl Scratch { + /// A directory to litter: the value *is* the directory. + pub fn dir(name: &str) -> Self { + let dir = fresh(name); + Self { + path: dir.clone(), + dir, + } + } + + /// A path for the one file a test writes, `name.ext`, alone in a directory + /// of its own. Nothing creates it -- what is under test is what writes it, + /// and several of these tests are about a file that must *not* appear. + pub fn file(name: &str, ext: &str) -> Self { + let dir = fresh(name); + let path = dir.join(format!("{name}.{ext}")); + Self { dir, path } + } +} + +impl Deref for Scratch { + type Target = Path; + + fn deref(&self) -> &Path { + &self.path + } +} + +impl AsRef for Scratch { + fn as_ref(&self) -> &Path { + &self.path + } +} + +/// So one of these can be handed to a `Command` as an argument, which is how +/// the suites that check our output with ffmpeg name the file. +impl AsRef for Scratch { + fn as_ref(&self) -> &OsStr { + self.path.as_os_str() + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.dir); + } +} + +#[cfg(test)] +mod tests { + use super::Scratch; + + /// The guard's whole job, both shapes: what it hands back is usable, and + /// what it made is gone once it is -- including when a test panicked, which + /// is the case that leaked. + #[test] + fn a_scratch_takes_its_directory_with_it() { + let (dir, file) = (Scratch::dir("ve_scratch_dir"), Scratch::file("ve_scratch", "mp4")); + assert!(dir.is_dir(), "a directory to litter, ready to use"); + std::fs::write(dir.join("a.txt"), b"litter").expect("write into it"); + assert!(!file.exists(), "the file itself is the test's to write"); + assert!(file.parent().expect("inside a directory").is_dir()); + assert_eq!(file.extension().and_then(|e| e.to_str()), Some("mp4")); + + let (left, right) = (dir.to_path_buf(), file.parent().unwrap().to_path_buf()); + assert_ne!(left, right, "one directory each, never shared"); + drop((dir, file)); + assert!(!left.exists(), "the directory went with the guard"); + assert!(!right.exists(), "and so did the file's"); + + // Unwinding runs `Drop`, so a failing test cleans up like a passing one. + // The panic below is this test's own and is meant to reach stderr. + let seen = std::sync::Mutex::new(None); + let _ = std::panic::catch_unwind(|| { + let dir = Scratch::dir("ve_scratch_panic"); + std::fs::write(dir.join("a.txt"), b"litter").expect("write into it"); + *seen.lock().expect("the lock") = Some(dir.to_path_buf()); + panic!("a test failing with a scratch directory in hand"); + }); + let path = seen.lock().expect("the lock").clone().expect("it ran"); + assert!(!path.exists(), "a panicking test leaves nothing behind"); + } +} diff --git a/crates/engine/tests/audio_decode.rs b/crates/engine/tests/audio_decode.rs index 80e9199..d26e77e 100644 --- a/crates/engine/tests/audio_decode.rs +++ b/crates/engine/tests/audio_decode.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; use std::time::Instant; use engine::audio::AudioSession; +use engine::scratch::Scratch; const RATE: u32 = 44100; const SECONDS: u32 = 5; @@ -297,7 +298,7 @@ fn a_matroska_with_no_track_is_silent_and_a_broken_one_is_an_error() { // ...and a file that will not parse at all, under the same extension: an // `Err` a front-end can show, not a silent import. - let broken = std::env::temp_dir().join(format!("ve_broken_{}.mkv", std::process::id())); + let broken = Scratch::file("ve_broken", "mkv"); std::fs::write(&broken, b"\x1a\x45\xdf\xa3not a matroska file at all").expect("write"); let err = AudioSession::probe(&broken, 0) .expect_err("a broken file must not pass for a silent one") diff --git a/crates/engine/tests/audio_export.rs b/crates/engine/tests/audio_export.rs index 6e50101..c0a6cad 100644 --- a/crates/engine/tests/audio_export.rs +++ b/crates/engine/tests/audio_export.rs @@ -21,6 +21,7 @@ use mp4::{MediaType, Mp4Reader}; use engine::export::{ExportSettings, Format}; use engine::project::{Lane, LaneKind, Source, Speed}; use engine::scale::FitPolicy; +use engine::scratch::Scratch; use engine::{AudioSession, Clip, ExportHandle, Project}; const RATE: u32 = 44_100; @@ -31,13 +32,10 @@ fn asset(name: &str) -> PathBuf { .join(name) } -/// Per-run unique, like the mp4 suite's: two suites at once must not delete -/// each other's output. -fn out_path(name: &str, ext: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_audio_{name}_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&path); - let _ = std::fs::remove_file(part_path(&path)); - path +/// Its own directory, gone with the value: two suites at once must not delete +/// each other's output, and nothing may outlive the test that wrote it. +fn out_path(name: &str, ext: &str) -> Scratch { + Scratch::file(&format!("ve_audio_{name}"), ext) } fn part_path(out: &Path) -> PathBuf { @@ -900,7 +898,7 @@ fn a_mono_timeline_exports_as_dual_mono_ogg() { std::fs::write(&src, &wav).expect("write the mono fixture"); let project = Project::from_parts( - vec![Source::new(src.clone(), 0)], + vec![Source::new(src.to_path_buf(), 0)], vec![( LaneKind::Audio, vec![Clip { diff --git a/crates/engine/tests/audio_files.rs b/crates/engine/tests/audio_files.rs index d1d018f..beb27a1 100644 --- a/crates/engine/tests/audio_files.rs +++ b/crates/engine/tests/audio_files.rs @@ -12,6 +12,7 @@ use std::path::{Path, PathBuf}; use engine::export::{ExportSettings, Format}; use engine::project::Lane; +use engine::scratch::Scratch; use engine::{AudioSession, Clip, PlaybackSession}; /// The fixtures `scripts/gen_fixtures.sh` writes: 3 s of 440 Hz left / 880 Hz @@ -233,14 +234,11 @@ fn a_song_is_a_timeline_of_its_own() { /// timeline: refused at the door, in the words of whatever refused it. #[test] fn a_file_with_no_picture_and_no_sound_is_still_refused() { - let dir = std::env::temp_dir().join(format!("ve_audio_bad_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_audio_bad"); let fake = dir.join("not_really.mp3"); std::fs::write(&fake, b"this is not an mp3").expect("write"); let e = refusal(PlaybackSession::open(&fake)); assert!(e.starts_with(&fake.display().to_string()), "{e}"); - let _ = std::fs::remove_dir_all(&dir); } /// The whole audio-only round trip a user makes: open a song, place a second @@ -248,9 +246,7 @@ fn a_file_with_no_picture_and_no_sound_is_still_refused() { /// format that needs a picture cannot have one. #[test] fn an_audio_only_project_saves_reloads_and_exports_its_sound() { - let dir = std::env::temp_dir().join(format!("ve_audio_only_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_audio_only"); let mut session = open(asset("test_tone.mp3")); // A second song joins it, the same way one joins a timeline of video: no @@ -440,9 +436,7 @@ fn an_export_refuses_audio_it_cannot_copy() { #[test] fn a_song_survives_a_save_and_a_reload() { - let dir = std::env::temp_dir().join(format!("ve_audio_files_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_audio_files"); let copy = |name: &str| { let to = dir.join(name); std::fs::copy(asset(name), &to).expect("copy the fixture"); @@ -508,9 +502,7 @@ fn a_song_survives_a_save_and_a_reload() { /// hand-editing anywhere in it. #[test] fn a_pasted_song_never_lands_on_the_video_lane() { - let dir = std::env::temp_dir().join(format!("ve_audio_paste_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_audio_paste"); let copy = |name: &str| { let to = dir.join(name); std::fs::copy(asset(name), &to).expect("copy the fixture"); diff --git a/crates/engine/tests/audio_streams.rs b/crates/engine/tests/audio_streams.rs index c117b21..0f84933 100644 --- a/crates/engine/tests/audio_streams.rs +++ b/crates/engine/tests/audio_streams.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use engine::audio::{AudioSession, StreamInfo}; +use engine::scratch::Scratch; /// Three audio streams: 0 is AAC 44.1k stereo (440/880 Hz, und), 1 is AAC /// 22.05k mono (220 Hz, fra), 2 is AC-3, which we cannot decode. @@ -197,7 +198,7 @@ fn flag_default_on_a_later_track_is_still_stream_0() { "the fixture stopped carrying an explicit FlagDefault=0" ); bytes[at + 2] = 1; - let patched = std::env::temp_dir().join(format!("ve_flagdefault_{}.mkv", std::process::id())); + let patched = Scratch::file("ve_flagdefault", "mkv"); std::fs::write(&patched, &bytes).expect("write the patched file"); // The flagged track leads: it is what `AudioSession::open` played before @@ -215,7 +216,7 @@ fn flag_default_on_a_later_track_is_still_stream_0() { // track's tone, sample for sample what the wrapper hands out. let (wrapped, rx) = AudioSession::open(&patched).expect("opens").expect("audio"); let played: Vec = rx.into_iter().flat_map(|c| c.samples).collect(); - let sources = [(patched.clone(), 0)]; + let sources = [(patched.to_path_buf(), 0)]; let (meta, stream_0) = { let (meta, rx) = AudioSession::open_multi_streams(&sources, &[(Some(0), 0.0, f64::INFINITY)]) .expect("opens") diff --git a/crates/engine/tests/av1.rs b/crates/engine/tests/av1.rs index 531bfc6..d1b875b 100644 --- a/crates/engine/tests/av1.rs +++ b/crates/engine/tests/av1.rs @@ -20,6 +20,7 @@ use std::time::{Duration, Instant}; use engine::demux::{Codec, Demuxer}; use engine::export::ExportSettings; +use engine::scratch::Scratch; use engine::{AudioSession, DecodeSession, PlaybackSession, Project}; /// 1280x720@30, 2 s, keyframes 30 apart -- see `scripts/gen_fixtures.sh`. @@ -271,8 +272,7 @@ fn an_mp4_export_of_a_matroska_source_carries_its_sound() { // A short one: the picture is re-encoded frame by frame and what is under // test here is the sound reaching the file, not H.264 throughput. let project = Project::single(asset("test_av1.mkv"), 15); - let out = std::env::temp_dir().join(format!("ve_export_av1_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_export_av1", "mp4"); let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); let started = Instant::now(); @@ -322,9 +322,8 @@ fn exported( name: &str, settings: &ExportSettings, limit: Duration, -) -> PathBuf { - let out = std::env::temp_dir().join(format!("ve_av1_{name}_{}.mkv", std::process::id())); - let _ = std::fs::remove_file(&out); +) -> Scratch { + let out = Scratch::file(&format!("ve_av1_{name}"), "mkv"); let started = Instant::now(); let handle = session.export_to_with(&out, settings); while !handle.is_finished() { @@ -405,8 +404,7 @@ fn an_av1_export_carries_the_timelines_sound_in_either_container() { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av"); assert!(session.cut_at(2.0 / 30.0), "two frames of it"); assert!(session.delete_clip(engine::project::Lane::V1, 1)); - let out = std::env::temp_dir().join(format!("ve_av1_sound_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_av1_sound", ext); let settings = ExportSettings { format, force_sw: true, @@ -448,7 +446,7 @@ fn an_av1_export_carries_the_timelines_sound_in_either_container() { #[test] fn an_av1_export_of_an_audio_only_timeline_is_refused_by_name() { let session = PlaybackSession::open(asset("test_tone.wav")).expect("open the tone"); - let out = std::env::temp_dir().join(format!("ve_av1_refused_{}.mkv", std::process::id())); + let out = Scratch::file("ve_av1_refused", "mkv"); let handle = session.export_to_with(&out, &av1_settings()); while !handle.is_finished() { std::thread::sleep(Duration::from_millis(10)); diff --git a/crates/engine/tests/backends.rs b/crates/engine/tests/backends.rs index 45651b2..8547d6d 100644 --- a/crates/engine/tests/backends.rs +++ b/crates/engine/tests/backends.rs @@ -14,6 +14,7 @@ use std::time::{Duration, Instant}; use engine::Codec; use engine::decode::{Backend, probe}; use engine::export::{ExportSettings, Format}; +use engine::scratch::Scratch; use engine::{ExportHandle, PlaybackSession}; fn asset(name: &str) -> PathBuf { @@ -22,11 +23,8 @@ fn asset(name: &str) -> PathBuf { .join(name) } -fn out_path(name: &str, ext: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_backend_{name}_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&path); - let _ = std::fs::remove_file(format!("{}.part", path.display())); - path +fn out_path(name: &str, ext: &str) -> Scratch { + Scratch::file(&format!("ve_backend_{name}"), ext) } /// A still has no coded stream and no decoder to pick, and it must say so diff --git a/crates/engine/tests/color.rs b/crates/engine/tests/color.rs index 866a426..26c632b 100644 --- a/crates/engine/tests/color.rs +++ b/crates/engine/tests/color.rs @@ -17,6 +17,7 @@ use std::time::{Duration, Instant}; use engine::color::ColorParams; use engine::export::ExportSettings; use engine::project::Lane; +use engine::scratch::Scratch; use engine::{DecodeSession, ExportHandle, PlaybackSession}; const FPS: f64 = 30.0; @@ -154,8 +155,7 @@ fn an_exported_grade_is_the_one_that_was_watched() { } let mut session = graded(); session.pause(); - let out = std::env::temp_dir().join(format!("ve_color_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_color", "mp4"); let handle = session.export_to_with(&out, &ExportSettings::default()); wait(&handle, Duration::from_secs(180)).expect("export"); diff --git a/crates/engine/tests/eq_audio.rs b/crates/engine/tests/eq_audio.rs index f489532..e3576eb 100644 --- a/crates/engine/tests/eq_audio.rs +++ b/crates/engine/tests/eq_audio.rs @@ -25,6 +25,7 @@ use engine::eq::{Band, BandKind, EqParams}; use engine::export::{ExportSettings, Format}; use engine::project::{Lane, LaneKind, Source, Speed}; use engine::scale::FitPolicy; +use engine::scratch::Scratch; use engine::{AudioSession, Clip, ExportHandle, Project}; const RATE: u32 = 44_100; @@ -134,10 +135,8 @@ fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> { handle.result().expect("a finished export has an outcome") } -fn out_path(name: &str, ext: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_eq_{name}_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&path); - path +fn out_path(name: &str, ext: &str) -> Scratch { + Scratch::file(&format!("ve_eq_{name}"), ext) } fn meta() -> engine::VideoMeta { diff --git a/crates/engine/tests/export.rs b/crates/engine/tests/export.rs index 8a364e9..a973af9 100644 --- a/crates/engine/tests/export.rs +++ b/crates/engine/tests/export.rs @@ -24,6 +24,7 @@ use engine::hw::HwEncoder; use engine::mux::{Mp4Muxer, VideoParams, parameter_sets}; use engine::project::{Lane, Source, Speed}; use engine::scale::FitPolicy; +use engine::scratch::Scratch; use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; const FPS: f64 = 30.0; @@ -34,13 +35,11 @@ fn asset(name: &str) -> PathBuf { .join(name) } -/// Per-run unique: two suite runs at once (a release sweep beside a debug one, -/// or two agents) would otherwise write and delete each other's export. -fn out_path(name: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_export_{name}_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&path); - let _ = std::fs::remove_file(part_path(&path)); - path +/// Its own directory, gone with the value: two suite runs at once (a release +/// sweep beside a debug one, or two agents) would otherwise write and delete +/// each other's export, and nothing may outlive the test that wrote it. +fn out_path(name: &str) -> Scratch { + Scratch::file(&format!("ve_export_{name}"), "mp4") } /// What the worker actually writes to until it succeeds; the engine appends the @@ -486,7 +485,7 @@ fn a_higher_bitrate_writes_a_bigger_file() { fn a_vanished_source_fails_the_export() { pin_software(); let baseline = asset("test_baseline.mp4"); - let doomed = std::env::temp_dir().join("ve_export_vanished_source.mp4"); + let doomed = Scratch::file("ve_export_vanished_source", "mp4"); std::fs::copy(&baseline, &doomed).expect("copy a second source"); let (meta, _) = engine::demux::Demuxer::open(&baseline).unwrap(); diff --git a/crates/engine/tests/export_subs.rs b/crates/engine/tests/export_subs.rs index 052b146..f951e52 100644 --- a/crates/engine/tests/export_subs.rs +++ b/crates/engine/tests/export_subs.rs @@ -20,6 +20,7 @@ use std::time::{Duration, Instant}; use engine::export::{ExportSettings, Format}; use engine::project::Lane; +use engine::scratch::Scratch; use engine::subtitle::{self, Cue, SubtitleTrack}; use engine::{ExportHandle, Project}; @@ -38,11 +39,8 @@ fn asset(name: &str) -> PathBuf { .join(name) } -fn out_path(name: &str) -> PathBuf { - let path = - std::env::temp_dir().join(format!("ve_subs_export_{name}_{}.mkv", std::process::id())); - let _ = std::fs::remove_file(&path); - path +fn out_path(name: &str) -> Scratch { + Scratch::file(&format!("ve_subs_export_{name}"), "mkv") } fn pin_software() { @@ -110,7 +108,7 @@ fn project() -> Project { /// Exports `project` as an HEVC Matroska carrying subtitle track `pick`, and /// hands back what our own reader finds in the file. -fn exported(name: &str, project: Project, pick: Option) -> (PathBuf, Vec) { +fn exported(name: &str, project: Project, pick: Option) -> (Scratch, Vec) { pin_software(); let out = out_path(name); let (meta, _) = engine::demux::Demuxer::open(&asset(MEDIA)).expect("probe the media"); diff --git a/crates/engine/tests/hevc.rs b/crates/engine/tests/hevc.rs index c1576d7..38d2f8e 100644 --- a/crates/engine/tests/hevc.rs +++ b/crates/engine/tests/hevc.rs @@ -20,6 +20,7 @@ use std::time::{Duration, Instant}; use engine::demux::{Codec, Demuxer}; use engine::export::ExportSettings; +use engine::scratch::Scratch; use engine::{AudioSession, DecodeSession, PlaybackSession, Project}; /// 1280x720@30, 2 s -- see `scripts/gen_fixtures.sh`. @@ -217,8 +218,7 @@ fn an_h264_take_plays_and_exports_on_an_hevc_timeline() { // re-encodes both to one H.264 track. let frames = (session.timeline_duration() * f64::from(session.meta().frame_rate as f32)).round() as u32; - let out = std::env::temp_dir().join(format!("ve_export_mixed_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_export_mixed", "mp4"); let handle = session.export_to_with(&out, &ExportSettings::default()); let started = Instant::now(); while !handle.is_finished() { @@ -240,8 +240,7 @@ fn an_hevc_source_exports_as_h264() { let session = PlaybackSession::open(asset("test_hevc.mp4")).expect("open test_hevc.mp4"); let meta = *session.meta(); let project = Project::single(asset("test_hevc.mp4"), meta.frame_count); - let out = std::env::temp_dir().join(format!("ve_export_hevc_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_export_hevc", "mp4"); let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); let started = Instant::now(); diff --git a/crates/engine/tests/hevc_export.rs b/crates/engine/tests/hevc_export.rs index 0ae31bb..ad4a57d 100644 --- a/crates/engine/tests/hevc_export.rs +++ b/crates/engine/tests/hevc_export.rs @@ -26,6 +26,7 @@ use std::time::{Duration, Instant}; use engine::demux::{Codec, Demuxer}; use engine::export::{ExportSettings, Format}; +use engine::scratch::Scratch; use engine::{AudioSession, PlaybackSession}; fn asset(name: &str) -> PathBuf { @@ -34,11 +35,8 @@ fn asset(name: &str) -> PathBuf { .join(name) } -fn out_path(name: &str, ext: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_hevc_{name}_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&path); - let _ = std::fs::remove_file(format!("{}.part", path.display())); - path +fn out_path(name: &str, ext: &str) -> Scratch { + Scratch::file(&format!("ve_hevc_{name}"), ext) } /// Runs an export to completion, failing on the deadline rather than hanging. @@ -275,7 +273,7 @@ fn probe(path: &Path) -> Option { /// A two-second 1920x1080 H.264 fixture in the temp directory -- the assets are /// all 720p, which is a multiple of 16 in both directions and would leave the /// crop path untested. `None` where ffmpeg is not installed. -fn fixture_1080p() -> Option { +fn fixture_1080p() -> Option { let path = out_path("source1080", "mp4"); let made = Command::new("ffmpeg") .args([ diff --git a/crates/engine/tests/hevc_hvc1.rs b/crates/engine/tests/hevc_hvc1.rs index a278adf..d35e497 100644 --- a/crates/engine/tests/hevc_hvc1.rs +++ b/crates/engine/tests/hevc_hvc1.rs @@ -19,6 +19,7 @@ use std::time::{Duration, Instant}; use engine::demux::{Codec, Demuxer}; use engine::export::ExportSettings; +use engine::scratch::Scratch; use engine::{AudioSession, DecodeSession, PlaybackSession, Project}; /// 1280x720@30, 2 s -- see `scripts/gen_fixtures.sh`. @@ -124,8 +125,7 @@ fn an_hvc1_source_exports_as_h264() { let session = PlaybackSession::open(asset(HVC1)).expect("open the hvc1 fixture"); let meta = *session.meta(); let project = Project::single(asset(HVC1), meta.frame_count); - let out = std::env::temp_dir().join(format!("ve_export_hvc1_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_export_hvc1", "mp4"); let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); let started = Instant::now(); diff --git a/crates/engine/tests/images.rs b/crates/engine/tests/images.rs index 18de6d7..498145d 100644 --- a/crates/engine/tests/images.rs +++ b/crates/engine/tests/images.rs @@ -17,6 +17,7 @@ use std::time::{Duration, Instant}; use engine::export::{ExportSettings, Format}; use engine::project::{Edge, Lane, LaneKind}; +use engine::scratch::Scratch; use engine::{DecodeSession, ExportHandle, PlaybackSession}; /// test_av.mp4's rate, which every fixture here shares. @@ -117,7 +118,7 @@ fn importing_a_still_fills_the_library_and_places_nothing() { assert_eq!(session.file_frames(&asset("test_still.png")), CAP); // A file that is not a picture at all is refused at the same door, by name. - let broken = std::env::temp_dir().join(format!("ve_not_a_png_{}.png", std::process::id())); + let broken = Scratch::file("ve_not_a_png", "png"); std::fs::write(&broken, b"this is not a PNG").expect("write the decoy"); let refused = session.import(&broken).expect_err("not a picture"); assert!( @@ -125,7 +126,6 @@ fn importing_a_still_fills_the_library_and_places_nothing() { "the refusal names the file: {refused}" ); assert_eq!(session.sources().len(), 2, "a refusal registers nothing"); - let _ = std::fs::remove_file(&broken); } /// The picture itself: placed on a canvas twice its size, the still comes back @@ -241,9 +241,7 @@ fn a_still_trims_out_to_its_cap_and_no_further() { #[test] fn pasting_a_copied_still_never_reaches_the_audio_lane() { pin_software(); - let dir = std::env::temp_dir().join(format!("ve_images_paste_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_images_paste"); let media = dir.join("test_av.mp4"); let still = dir.join("test_still.png"); std::fs::copy(asset("test_av.mp4"), &media).expect("copy the video"); @@ -312,9 +310,7 @@ fn pasting_a_copied_still_never_reaches_the_audio_lane() { #[test] fn a_still_survives_the_project_round_trip() { pin_software(); - let dir = std::env::temp_dir().join(format!("ve_images_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_images"); let media = dir.join("test_av.mp4"); let still = dir.join("test_still.png"); std::fs::copy(asset("test_av.mp4"), &media).expect("copy the video"); @@ -371,9 +367,7 @@ fn a_still_survives_the_project_round_trip() { #[test] fn a_still_at_saved_source_zero_keeps_the_rate_and_the_sound() { pin_software(); - let dir = std::env::temp_dir().join(format!("ve_still_first_{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).expect("scratch dir"); + let dir = Scratch::dir("ve_still_first"); let media = dir.join("test_25fps.mp4"); let still = dir.join("test_still.png"); std::fs::copy(asset("test_25fps.mp4"), &media).expect("copy the video"); @@ -471,8 +465,7 @@ fn a_mixed_timeline_exports_the_still_at_the_cut() { assert_eq!(session.lane_clips(Lane::V1).len(), 2); assert_eq!(session.resolution(), (640, 360)); - let out = std::env::temp_dir().join(format!("ve_images_export_{}.mp4", std::process::id())); - let _ = std::fs::remove_file(&out); + let out = Scratch::file("ve_images_export", "mp4"); wait(&session.export_to(&out), Duration::from_secs(300)).expect("export"); let (meta, frames) = DecodeSession::open(&out).expect("reopen the export"); diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index 46ccf6f..9d6fbef 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -16,6 +16,7 @@ use std::time::{Duration, Instant}; use engine::export::{ExportSettings, Format}; use engine::project::{Clip, LaneKind, Source, Speed}; use engine::scale::FitPolicy; +use engine::scratch::Scratch; use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; const FPS: f64 = 30.0; @@ -41,12 +42,10 @@ fn pin_software() { }); } -/// Per-run unique, like the export suite's: two suites at once must not write -/// each other's files. -fn out_path(name: &str, ext: &str) -> PathBuf { - let path = std::env::temp_dir().join(format!("ve_lanes_{name}_{}.{ext}", std::process::id())); - let _ = std::fs::remove_file(&path); - path +/// Its own directory, gone with the value: two suites at once must not write +/// each other's files, and nothing may outlive the test that wrote it. +fn out_path(name: &str, ext: &str) -> Scratch { + Scratch::file(&format!("ve_lanes_{name}"), ext) } /// A hand-written v4 project: `V1` the whole of `test_av`, `V2` the middle @@ -57,7 +56,7 @@ fn out_path(name: &str, ext: &str) -> PathBuf { /// per test: two tests sharing one name run in parallel over one file, and the /// first to finish deletes it out from under the second -- which then fails on /// a file that is simply not there any more. -fn three_lane_file(name: &str) -> PathBuf { +fn three_lane_file(name: &str) -> Scratch { let path = out_path(name, "edith"); // `source