83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func writeConfig(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "applesync.toml")
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
path := writeConfig(t, `
|
|
[global]
|
|
downloads_dir = "/music"
|
|
m3u_dir = "/playlists"
|
|
|
|
[[playlist]]
|
|
url = "https://music.apple.com/us/playlist/x/pl.abc"
|
|
`)
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Global.Mode != "replace" || cfg.Global.M3UPaths != "relative" || !cfg.Global.ExtendedM3U {
|
|
t.Fatalf("unexpected defaults: %+v", cfg.Global)
|
|
}
|
|
if cfg.Global.Concurrency != 4 || cfg.Rip.Quality != 3 {
|
|
t.Fatalf("unexpected defaults: %+v %+v", cfg.Global, cfg.Rip)
|
|
}
|
|
if cfg.Playlists[0].Mode != "replace" {
|
|
t.Fatalf("playlist mode = %q", cfg.Playlists[0].Mode)
|
|
}
|
|
}
|
|
|
|
func TestExampleConfigLoads(t *testing.T) {
|
|
cfg, err := Load("../../applesync.example.toml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cfg.Playlists) != 1 {
|
|
t.Fatalf("playlists = %d", len(cfg.Playlists))
|
|
}
|
|
}
|
|
|
|
func TestValidation(t *testing.T) {
|
|
cases := []string{
|
|
`
|
|
[global]
|
|
m3u_dir = "/playlists"
|
|
[[playlist]]
|
|
url = "https://music.apple.com/us/playlist/x/pl.abc"
|
|
`,
|
|
`
|
|
[global]
|
|
downloads_dir = "/music"
|
|
m3u_dir = "/playlists"
|
|
mode = "mirror"
|
|
[[playlist]]
|
|
url = "https://music.apple.com/us/playlist/x/pl.abc"
|
|
`,
|
|
`
|
|
[global]
|
|
downloads_dir = "/music"
|
|
m3u_dir = "/playlists"
|
|
[[playlist]]
|
|
url = ""
|
|
`,
|
|
}
|
|
for _, content := range cases {
|
|
path := writeConfig(t, content)
|
|
if _, err := Load(path); err == nil {
|
|
t.Fatalf("expected validation error for:\n%s", content)
|
|
}
|
|
}
|
|
}
|