150 lines
3.9 KiB
Go
150 lines
3.9 KiB
Go
package main_test
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"slices"
|
|
"testing"
|
|
|
|
main "gitea/show-ingestion"
|
|
)
|
|
|
|
func TestRunChoosesOne(t *testing.T) {
|
|
ind := t.TempDir()
|
|
outd := t.TempDir()
|
|
|
|
for _, given := range []string{
|
|
"Survivor.AU.S12E11.1080p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
"Survivor.AU.S12E11.720p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
"Survivor.AU.S12E12.720p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
} {
|
|
ioutil.WriteFile(path.Join(ind, given), []byte{}, os.ModePerm)
|
|
}
|
|
|
|
want := map[string]bool{
|
|
"Australian_Survivor_S12E11.mkv": false,
|
|
"Australian_Survivor_S12E12.mkv": false,
|
|
}
|
|
|
|
if err := main.Run(context.Background(),
|
|
outd,
|
|
ind,
|
|
[]string{
|
|
".urvivor.[Aa][Uu].*[sS](?P<season>[0-9]+)[eE](?P<episode>[0-9]*).*1080.*MeGusta",
|
|
".urvivor.[Aa][Uu].*[sS](?P<season>[0-9]+)[eE](?P<episode>[0-9]*).*720.*MeGusta",
|
|
},
|
|
main.Fields{
|
|
Title: "Australian_Survivor",
|
|
},
|
|
func(outf, inf string) error {
|
|
want[path.Base(outf)] = true
|
|
return nil
|
|
},
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for k, v := range want {
|
|
if !v {
|
|
t.Errorf("did not mv_n_ln(outf=%s)", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRun(t *testing.T) {
|
|
cases := map[string]struct {
|
|
given []string
|
|
patterns []string
|
|
overrides main.Fields
|
|
want []string
|
|
}{
|
|
"empty": {},
|
|
"fallback survivor": {
|
|
given: []string{
|
|
"Survivor.AU.S12E11.1080p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
"Survivor.AU.S12E11.720p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
"Survivor.AU.S12E12.720p.HEVC.x265-MeGusta[EZTVx.to].mkv",
|
|
},
|
|
patterns: []string{
|
|
".urvivor.[Aa][Uu].*[sS](?P<season>[0-9]+)[eE](?P<episode>[0-9]*).*1080.*MeGusta",
|
|
".urvivor.[Aa][Uu].*[sS](?P<season>[0-9]+)[eE](?P<episode>[0-9]*).*720.*MeGusta",
|
|
},
|
|
overrides: main.Fields{
|
|
Title: "Australian_Survivor",
|
|
},
|
|
want: []string{
|
|
"Australian_Survivor_S12E11.mkv",
|
|
"Australian_Survivor_S12E12.mkv",
|
|
},
|
|
},
|
|
"easy w group": {
|
|
given: []string{
|
|
"[SubsPlease] Tokidoki Bosotto Russia-go de Dereru Tonari no Alya-san - 01 (720p) [A12844D5].mkv",
|
|
"[SubsPlease] Tokidoki Bosotto Russia-go de Dereru Tonari no Alya-san - 02 (720p) [2608F490].mkv",
|
|
},
|
|
patterns: []string{
|
|
`^\[[^\]]*\] (?P<title>.*) - (?<episode>[0-9]*)`,
|
|
},
|
|
overrides: main.Fields{
|
|
Season: "01",
|
|
},
|
|
want: []string{
|
|
"Tokidoki_Bosotto_Russia-go_de_Dereru_Tonari_no_Alya-san_S01E01.mkv",
|
|
"Tokidoki_Bosotto_Russia-go_de_Dereru_Tonari_no_Alya-san_S01E02.mkv",
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
ind := t.TempDir()
|
|
for _, f := range c.given {
|
|
ioutil.WriteFile(path.Join(ind, f), []byte{}, os.ModePerm)
|
|
}
|
|
outd := t.TempDir()
|
|
|
|
if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
|
t.Fatal("err on first run:", err)
|
|
} else if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
|
t.Fatal("err on second run:", err)
|
|
}
|
|
|
|
for _, f := range c.want {
|
|
if stat, err := os.Stat(path.Join(outd, f)); os.IsNotExist(err) {
|
|
t.Errorf("expected %s", f)
|
|
} else if !stat.Mode().IsRegular() {
|
|
t.Errorf("%s not a regular file: %v", f, stat.Mode())
|
|
}
|
|
}
|
|
|
|
if entries, err := os.ReadDir(outd); err != nil {
|
|
t.Error("failed to list outdir: %w", err)
|
|
} else {
|
|
for _, entry := range entries {
|
|
t.Logf("%s", entry.Name())
|
|
if !slices.Contains(c.want, path.Base(entry.Name())) {
|
|
t.Errorf("unexpected %s", entry.Name())
|
|
}
|
|
if !entry.Type().IsRegular() {
|
|
t.Errorf("non-regular file %s in out: %v", entry.Name(), entry.Type())
|
|
}
|
|
}
|
|
}
|
|
|
|
if entries, err := os.ReadDir(ind); err != nil {
|
|
t.Error("failed to list indir: %w", err)
|
|
} else {
|
|
for _, entry := range entries {
|
|
inf := path.Join(ind, entry.Name())
|
|
if _, err := os.Stat(inf); err != nil {
|
|
t.Errorf("%s no longer in ind: %v", inf, err)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|