A non-flag word no longer stops parsing, and a missing home directory refuses to create or rewrite a config instead of writing an empty log path. Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package station
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestMissingHomeDoesNotCreateConfig(t *testing.T) {
|
|
orig := userHomeDir
|
|
t.Cleanup(func() { userHomeDir = orig })
|
|
userHomeDir = func() (string, error) {
|
|
return "", errors.New("no home")
|
|
}
|
|
|
|
path := filepath.Join(t.TempDir(), "lab.json")
|
|
_, err := New(Options{ConfigPath: path})
|
|
if err == nil || !strings.HasPrefix(err.Error(), "config path:") {
|
|
t.Fatalf("New() error = %v, want a config path failure", err)
|
|
}
|
|
if _, statErr := os.Stat(path); !errors.Is(statErr, fs.ErrNotExist) {
|
|
t.Fatalf("stat = %v, want the file absent", statErr)
|
|
}
|
|
}
|
|
|
|
func TestMissingHomeDoesNotRewriteConfig(t *testing.T) {
|
|
orig := userHomeDir
|
|
t.Cleanup(func() { userHomeDir = orig })
|
|
userHomeDir = func() (string, error) {
|
|
return "", errors.New("no home")
|
|
}
|
|
|
|
path := filepath.Join(t.TempDir(), "lab.json")
|
|
original := []byte("{\"host\":\"127.0.0.1\",\"port\":5308,\"callsign\":\"DL1ABC\",\"authKey\":\"key\",\"talkGroup\":20}\n")
|
|
if err := os.WriteFile(path, original, 0o640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := New(Options{ConfigPath: path})
|
|
if err == nil || !strings.HasPrefix(err.Error(), "config path:") {
|
|
t.Fatalf("New() error = %v, want a config path failure", err)
|
|
}
|
|
got, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != string(original) {
|
|
t.Fatalf("file = %q, want %q", got, original)
|
|
}
|
|
}
|