77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveConfigPathExplicit(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "custom.json")
|
|
if err := os.WriteFile(path, []byte("{}"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := ResolveConfigPath(path, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want, _ := filepath.Abs(path)
|
|
if got != want {
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestResolveConfigPathEnvironmentIsAuthoritative(t *testing.T) {
|
|
_, err := ResolveConfigPath("", filepath.Join(t.TempDir(), "missing.json"))
|
|
if err == nil || !strings.Contains(err.Error(), "ENV_CONTROLLER_CONFIG") {
|
|
t.Fatalf("expected environment-path error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestResolveConfigPathCurrentDirectory(t *testing.T) {
|
|
oldWD, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.Chdir(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = os.Chdir(oldWD) })
|
|
|
|
path := filepath.Join(dir, "controller.json")
|
|
if err := os.WriteFile(path, []byte("{}"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := ResolveConfigPath("", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != path {
|
|
t.Fatalf("got %q, want %q", got, path)
|
|
}
|
|
}
|
|
|
|
func TestResolveConfigPathMentionsExample(t *testing.T) {
|
|
oldWD, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.Chdir(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = os.Chdir(oldWD) })
|
|
|
|
example := filepath.Join(dir, "controller.example.json")
|
|
if err := os.WriteFile(example, []byte("{}"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = ResolveConfigPath("", "")
|
|
if err == nil || !strings.Contains(err.Error(), example) {
|
|
t.Fatalf("expected hint for %q, got %v", example, err)
|
|
}
|
|
}
|