harden ffmpeg pipeline failure handling and stream mapping

This commit is contained in:
2026-04-21 23:07:48 +02:00
parent beb6ce6cbb
commit d65dc182f8
6 changed files with 173 additions and 14 deletions

View File

@@ -1,6 +1,8 @@
package convert
import (
"os"
"path/filepath"
"strings"
"testing"
@@ -59,3 +61,43 @@ func TestBuildFFmpegArgsNoCoverForOpus(t *testing.T) {
t.Fatalf("unexpected cover map args=%s", joined)
}
}
func TestConvertKeepsSourceWhenRenameFails(t *testing.T) {
tmp := t.TempDir()
in := filepath.Join(tmp, "song.flac")
if err := os.WriteFile(in, []byte("src"), 0o644); err != nil {
t.Fatalf("write input: %v", err)
}
fakeBin := filepath.Join(tmp, "bin")
if err := os.MkdirAll(fakeBin, 0o755); err != nil {
t.Fatalf("mkdir bin: %v", err)
}
fakeFFmpeg := filepath.Join(fakeBin, "ffmpeg")
script := "#!/bin/sh\nout=\"\"\nfor arg in \"$@\"; do out=\"$arg\"; done\n: > \"$out\"\n"
if err := os.WriteFile(fakeFFmpeg, []byte(script), 0o755); err != nil {
t.Fatalf("write fake ffmpeg: %v", err)
}
t.Setenv("PATH", fakeBin)
finalPath := strings.TrimSuffix(in, filepath.Ext(in)) + ".m4a"
if err := os.Mkdir(finalPath, 0o755); err != nil {
t.Fatalf("mkdir final path: %v", err)
}
cfg := config.ConversionConfig{Enabled: true, Codec: "ALAC"}
out, err := Convert(in, cfg)
if err == nil {
t.Fatalf("expected rename failure")
}
if out != in {
t.Fatalf("returned path = %q, want %q", out, in)
}
if _, statErr := os.Stat(in); statErr != nil {
t.Fatalf("expected source to remain, stat err=%v", statErr)
}
tmpPath := finalPath + ".tmp.m4a"
if _, statErr := os.Stat(tmpPath); !os.IsNotExist(statErr) {
t.Fatalf("expected temp output cleanup, stat err=%v", statErr)
}
}