1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package config
import (
"os"
"path/filepath"
"testing"
)
// Regression: Load used yaml.Decoder.Decode for KnownFields, which returns
// io.EOF for an empty or comment-only config.yaml. cmd/thoi treats any non-
// IsNotExist error as fatal, so an empty config aborted startup instead of
// booting on defaults (yaml.Unmarshal had returned nil).
func writeEmptyConfig(t *testing.T, body string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestLoadEmptyFileBootsOnDefaults(t *testing.T) {
cfg, err := Load(writeEmptyConfig(t, ""))
if err != nil {
t.Fatalf("empty config.yaml must load on defaults, got: %v", err)
}
if cfg.TickLength != Default().TickLength {
t.Errorf("TickLength = %d, want default %d", cfg.TickLength, Default().TickLength)
}
if cfg.Telnet.Port != Default().Telnet.Port {
t.Errorf("Telnet.Port = %d, want default %d", cfg.Telnet.Port, Default().Telnet.Port)
}
}
func TestLoadCommentOnlyFileBootsOnDefaults(t *testing.T) {
cfg, err := Load(writeEmptyConfig(t, "# nothing configured yet\n\n# ports come from defaults\n"))
if err != nil {
t.Fatalf("comment-only config.yaml must load on defaults, got: %v", err)
}
if cfg.StartingRoom != Default().StartingRoom {
t.Errorf("StartingRoom = %d, want default %d", cfg.StartingRoom, Default().StartingRoom)
}
}
func TestLoadEmptyFileStillRejectsUnknownKey(t *testing.T) {
if _, err := Load(writeEmptyConfig(t, "not_a_real_key: 1\n")); err == nil {
t.Fatal("unknown key must still be rejected after the io.EOF fix")
}
}
|