conf_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if IsWindowsRuntime() {
  33. return
  34. }
  35. ini.PrettyFormat = false
  36. defer func() {
  37. MustInit("")
  38. ini.PrettyFormat = true
  39. }()
  40. assert.Nil(t, Init(filepath.Join("testdata", "custom.ini")))
  41. cfg := ini.Empty()
  42. cfg.NameMapper = ini.SnackCase
  43. for _, v := range []struct {
  44. section string
  45. config interface{}
  46. }{
  47. {"", &App},
  48. {"server", &Server},
  49. {"server", &SSH},
  50. {"repository", &Repository},
  51. {"database", &Database},
  52. {"security", &Security},
  53. {"email", &Email},
  54. {"auth", &Auth},
  55. {"user", &User},
  56. {"session", &Session},
  57. {"attachment", &Attachment},
  58. {"time", &Time},
  59. {"picture", &Picture},
  60. {"mirror", &Mirror},
  61. {"i18n", &I18n},
  62. } {
  63. err := cfg.Section(v.section).ReflectFrom(v.config)
  64. if err != nil {
  65. t.Fatalf("%s: %v", v.section, err)
  66. }
  67. }
  68. buf := new(bytes.Buffer)
  69. _, err := cfg.WriteTo(buf)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. testutil.AssertGolden(t, filepath.Join("testdata", "TestInit.golden.ini"), testutil.Update("TestInit"), buf.String())
  74. }