conf_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 TestAsset(t *testing.T) {
  14. // Make sure it does not blow up
  15. _, err := Asset("conf/app.ini")
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. }
  20. func TestAssetDir(t *testing.T) {
  21. // Make sure it does not blow up
  22. _, err := AssetDir("conf")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. }
  27. func TestMustAsset(t *testing.T) {
  28. // Make sure it does not blow up
  29. MustAsset("conf/app.ini")
  30. }
  31. func TestInit(t *testing.T) {
  32. ini.PrettyFormat = false
  33. defer func() {
  34. MustInit("")
  35. ini.PrettyFormat = true
  36. }()
  37. assert.Nil(t, Init(filepath.Join("testdata", "custom.ini")))
  38. cfg := ini.Empty()
  39. cfg.NameMapper = ini.SnackCase
  40. for _, v := range []struct {
  41. section string
  42. config interface{}
  43. }{
  44. {"", &App},
  45. {"server", &Server},
  46. {"server", &SSH},
  47. {"repository", &Repository},
  48. {"database", &Database},
  49. {"security", &Security},
  50. {"email", &Email},
  51. {"auth", &Auth},
  52. {"user", &User},
  53. {"session", &Session},
  54. {"attachment", &Attachment},
  55. {"time", &Time},
  56. {"picture", &Picture},
  57. {"mirror", &Mirror},
  58. {"i18n", &I18n},
  59. } {
  60. err := cfg.Section(v.section).ReflectFrom(v.config)
  61. if err != nil {
  62. t.Fatalf("%s: %v", v.section, err)
  63. }
  64. }
  65. buf := new(bytes.Buffer)
  66. _, err := cfg.WriteTo(buf)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. testutil.AssertGolden(t, filepath.Join("testdata", "TestInit.golden.ini"), testutil.Update("TestInit"), buf.String())
  71. }