conf_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package conf
  5. import (
  6. "bytes"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "gopkg.in/ini.v1"
  11. "gogs.io/gogs/internal/testutil"
  12. )
  13. func TestInit(t *testing.T) {
  14. ini.PrettyFormat = false
  15. defer func() {
  16. MustInit("")
  17. ini.PrettyFormat = true
  18. }()
  19. assert.Nil(t, Init(filepath.Join("testdata", "custom.ini")))
  20. cfg := ini.Empty()
  21. cfg.NameMapper = ini.SnackCase
  22. for _, v := range []struct {
  23. section string
  24. config any
  25. }{
  26. {"", &App},
  27. {"server", &Server},
  28. {"server", &SSH},
  29. {"repository", &Repository},
  30. {"database", &Database},
  31. {"security", &Security},
  32. {"email", &Email},
  33. {"auth", &Auth},
  34. {"user", &User},
  35. {"session", &Session},
  36. {"attachment", &Attachment},
  37. {"time", &Time},
  38. {"picture", &Picture},
  39. {"mirror", &Mirror},
  40. {"i18n", &I18n},
  41. } {
  42. err := cfg.Section(v.section).ReflectFrom(v.config)
  43. if err != nil {
  44. t.Fatalf("%s: %v", v.section, err)
  45. }
  46. }
  47. buf := new(bytes.Buffer)
  48. _, err := cfg.WriteTo(buf)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. testutil.AssertGolden(t, filepath.Join("testdata", "TestInit.golden.ini"), testutil.Update("TestInit"), buf.String())
  53. }