diff options
Diffstat (limited to 'internal/config/correct_empty_config_test.go')
| -rw-r--r-- | internal/config/correct_empty_config_test.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/config/correct_empty_config_test.go b/internal/config/correct_empty_config_test.go new file mode 100644 index 0000000..27d39da --- /dev/null +++ b/internal/config/correct_empty_config_test.go @@ -0,0 +1,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") + } +} |
