core/configcmd: "<program> config create|validate|upgrade|path" from a Spec (name, application, minimal/full templates, Validate, Schema), taken over from yamase-core/pkg/configcmd and generalized: Filename, Optional (programs that run on defaults) and None (no configuration). core/version: "<program> version [--json]" over the values each program stamps into its internal/version; JSON keys match yasta's existing output (yavebu reads it), VCS fallback for unstamped builds. paths: macOS CLI/Service system-scope RuntimeDir is /var/run/<app> instead of /run/<app>, which macOS does not have. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
package configcmd
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type testInner struct {
|
|
Driver string `yaml:"driver"`
|
|
Root string `yaml:"root"`
|
|
}
|
|
|
|
type testThrottle struct {
|
|
Enabled bool `yaml:"enabled"`
|
|
}
|
|
|
|
type testThrottleSection struct {
|
|
Config testThrottle `yaml:",inline"`
|
|
Storage testInner `yaml:"storage"`
|
|
}
|
|
|
|
type testListener struct {
|
|
Address string `yaml:"address"`
|
|
TLS string `yaml:"tls"`
|
|
}
|
|
|
|
type selfDecoding struct{ v map[string]string }
|
|
|
|
func (s *selfDecoding) UnmarshalYAML(n *yaml.Node) error { return n.Decode(&s.v) }
|
|
|
|
type testConfig struct {
|
|
LogLevel string `yaml:"log-level"`
|
|
Listen []testListener `yaml:"listen"`
|
|
Storage struct {
|
|
Mail *testInner `yaml:"mailstore"`
|
|
} `yaml:"storage"`
|
|
Backends map[string]testInner `yaml:"backends"`
|
|
Throttle testThrottleSection `yaml:"throttle"`
|
|
Timeout time.Duration `yaml:"timeout"`
|
|
Custom selfDecoding `yaml:"custom"`
|
|
Ignored string `yaml:"-"`
|
|
}
|
|
|
|
func TestUnknownKeys(t *testing.T) {
|
|
raw := []byte(`log-level: info
|
|
log-levle: debug
|
|
listen:
|
|
- address: ":25"
|
|
tsl: auto
|
|
storage:
|
|
mailstore:
|
|
driver: filesystem
|
|
rot: /x
|
|
backends:
|
|
inbound: {driver: filesystem, root: /q, extra: 1}
|
|
throttle:
|
|
enabled: true
|
|
storage: {driver: filesystem, root: /t}
|
|
enabeld: false
|
|
timeout: 5s
|
|
custom: {anything: goes}
|
|
Ignored: x
|
|
`)
|
|
got, err := UnknownKeys(raw, testConfig{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []UnknownKey{
|
|
{"log-levle", 2},
|
|
{"listen[0].tsl", 5},
|
|
{"storage.mailstore.rot", 9},
|
|
{"backends.inbound.extra", 11},
|
|
{"throttle.enabeld", 15},
|
|
{"Ignored", 18},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("UnknownKeys =\n%v\nwant\n%v", got, want)
|
|
}
|
|
if got, _ := UnknownKeys([]byte("log-level: info\n"), testConfig{}); len(got) != 0 {
|
|
t.Fatalf("a clean file has unknown keys: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestWarnUnknownAndCheck(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "c.yaml")
|
|
if err := os.WriteFile(path, []byte("log-level: info\nlog-levle: debug\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var buf bytes.Buffer
|
|
WarnUnknown(slog.New(slog.NewTextHandler(&buf, nil)), path, testConfig{})
|
|
if !strings.Contains(buf.String(), "level=WARN") || !strings.Contains(buf.String(), "option=log-levle") || !strings.Contains(buf.String(), "line=2") {
|
|
t.Errorf("warning = %q", buf.String())
|
|
}
|
|
if ok, note := UnknownCheck(path, testConfig{}); ok || !strings.Contains(note, "log-levle (line 2)") {
|
|
t.Errorf("check = %v %q", ok, note)
|
|
}
|
|
if err := os.WriteFile(path, []byte("log-level: info\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, note := UnknownCheck(path, testConfig{}); !ok || note != "" {
|
|
t.Errorf("clean file: %v %q", ok, note)
|
|
}
|
|
}
|