1
1

config_test.go 1003 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestConfigFile(t *testing.T) {
  8. dir := t.TempDir()
  9. path := filepath.Join(dir, "site.json")
  10. c := newConfigFile(path)
  11. steps := []struct {
  12. name, json string // json "" means delete the file
  13. want string
  14. }{
  15. {"missing file", "", "Home"},
  16. {"value set", `{"homeText": "Start"}`, "Start"},
  17. {"field left out", `{}`, "Home"},
  18. {"blank value", `{"homeText": " "}`, "Home"},
  19. {"set again", `{"homeText": "Posts"}`, "Posts"},
  20. {"broken JSON keeps last good", `{"homeText": `, "Posts"},
  21. {"unknown field keeps last good", `{"homeTxt": "Typo"}`, "Posts"},
  22. {"fixed", `{"homeText": "Index page"}`, "Index page"},
  23. {"deleted again", "", "Home"},
  24. }
  25. for _, s := range steps {
  26. if s.json == "" {
  27. os.Remove(path)
  28. } else if err := os.WriteFile(path, []byte(s.json), 0o644); err != nil {
  29. t.Fatal(err)
  30. }
  31. if got := c.Get().HomeText; got != s.want {
  32. t.Errorf("%s: HomeText = %q, want %q", s.name, got, s.want)
  33. }
  34. }
  35. }