test recursive and run as go run ./ r
This commit is contained in:
102
main_test.go
102
main_test.go
@@ -28,7 +28,7 @@ func TestRunChoosesOne(t *testing.T) {
|
||||
"Australian_Survivor_S12E12.mkv": false,
|
||||
}
|
||||
|
||||
if err := main.Run(context.Background(),
|
||||
if err := main.RunWith(context.Background(),
|
||||
outd,
|
||||
ind,
|
||||
[]string{
|
||||
@@ -53,7 +53,7 @@ func TestRunChoosesOne(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
func TestRunWith(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
given []string
|
||||
patterns []string
|
||||
@@ -106,9 +106,9 @@ func TestRun(t *testing.T) {
|
||||
}
|
||||
outd := t.TempDir()
|
||||
|
||||
if err := main.Run(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
||||
if err := main.RunWith(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 {
|
||||
} else if err := main.RunWith(context.Background(), outd, ind, c.patterns, c.overrides, main.RealMvNLn); err != nil {
|
||||
t.Fatal("err on second run:", err)
|
||||
}
|
||||
|
||||
@@ -147,3 +147,97 @@ func TestRun(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecursive(t *testing.T) {
|
||||
was, _ := os.Getwd()
|
||||
t.Cleanup(func() { os.Chdir(was) })
|
||||
os.Chdir(t.TempDir())
|
||||
|
||||
outd := t.TempDir()
|
||||
os.MkdirAll(path.Join(outd, "A"), os.ModePerm)
|
||||
|
||||
// use config
|
||||
write("./showA/.show-ingestion.yaml", `{
|
||||
"c": {
|
||||
"title": "A",
|
||||
"season": "A",
|
||||
"episode": "A"
|
||||
},
|
||||
"p": [".*"],
|
||||
"o": "`+outd+`/A"
|
||||
}`)
|
||||
write("./showA/file.a")
|
||||
|
||||
// parse files
|
||||
write("./showB/.show-ingestion.yaml", `{
|
||||
"o": "`+outd+`/B",
|
||||
"p": []
|
||||
}`)
|
||||
write("./showB/title S01E02.b")
|
||||
|
||||
// use file pattern
|
||||
write("./dirA/showC/.show-ingestion.yaml", `{
|
||||
"o": "`+outd+`/C",
|
||||
"p": ["^(?P<title>.) (?P<season>.) (?P<episode>.)"]
|
||||
}`)
|
||||
write("./dirA/showC/t s e.c")
|
||||
|
||||
// dry run
|
||||
write("./dirA/showD/.show-ingestion.yaml", `{
|
||||
"o": "`+outd+`/D",
|
||||
"d": true
|
||||
}`)
|
||||
write("./dirA/showD/title S02E04.d")
|
||||
|
||||
// not configured
|
||||
os.MkdirAll("./dirB/showE", os.ModePerm)
|
||||
write("./dirB/showE/title S03E06.e")
|
||||
|
||||
if err := main.Recursive(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
exists(t, path.Join(outd, "A", "A_SAEA.a"))
|
||||
exists(t, path.Join(outd, "B", "title_S01E02.b"))
|
||||
exists(t, path.Join(outd, "C", "t_SsEe.c"))
|
||||
notExists(t, path.Join(outd, "D", "title_S02E04.d"))
|
||||
notExists(t, path.Join(outd, "title_S03E06.e"))
|
||||
}
|
||||
|
||||
func write(f string, b ...string) {
|
||||
if len(b) == 0 {
|
||||
b = append(b, "")
|
||||
}
|
||||
|
||||
os.MkdirAll(path.Dir(f), os.ModePerm)
|
||||
os.WriteFile(f, []byte(b[0]), os.ModePerm)
|
||||
}
|
||||
|
||||
func exists(t *testing.T, p string) {
|
||||
if _, err := os.Stat(p); os.IsNotExist(err) {
|
||||
d := path.Dir(path.Dir(p))
|
||||
t.Errorf("expected %s of %s (%+v)", path.Base(p), d, ls(d))
|
||||
}
|
||||
}
|
||||
|
||||
func notExists(t *testing.T, p string) {
|
||||
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
||||
d := path.Dir(path.Dir(p))
|
||||
t.Errorf("unexpected %s of %s (%+v)", path.Base(p), d, ls(d))
|
||||
}
|
||||
}
|
||||
|
||||
func ls(d string) []string {
|
||||
result := []string{}
|
||||
entries, _ := os.ReadDir(d)
|
||||
for _, entry := range entries {
|
||||
p := path.Join(d, entry.Name())
|
||||
if entry.IsDir() {
|
||||
result = append(result, ls(p)...)
|
||||
} else {
|
||||
result = append(result, p)
|
||||
}
|
||||
}
|
||||
slices.Sort(result)
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user