Keep a later config flag when extra words are present.

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>
This commit is contained in:
2026-10-07 12:42:01 +02:00
co-authored by Cursor
parent 465a26911e
commit 87e366e36c
6 changed files with 317 additions and 14 deletions
+52
View File
@@ -0,0 +1,52 @@
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)
}
}