Files
core/configcmd/unknown_test.go
T

126 lines
3.3 KiB
Go
Raw Normal View History

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)
}
}
func TestUnknownKeysNode(t *testing.T) {
var doc yaml.Node
if err := yaml.Unmarshal([]byte("other: 1\nsection:\n log-level: info\n log-levle: debug\n"), &doc); err != nil {
t.Fatal(err)
}
section := doc.Content[0].Content[3]
got := UnknownKeysNode(section, testConfig{})
if len(got) != 1 || got[0].Key != "log-levle" || got[0].Line != 4 {
t.Errorf("UnknownKeysNode = %v, want log-levle on line 4 (relative key, document line)", got)
}
if got := UnknownKeysNode(nil, testConfig{}); got != nil {
t.Errorf("UnknownKeysNode(nil) = %v", got)
}
}