config_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package conf
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/zeromicro/go-zero/core/fs"
  7. "github.com/zeromicro/go-zero/core/hash"
  8. )
  9. func TestLoadConfig_notExists(t *testing.T) {
  10. assert.NotNil(t, Load("not_a_file", nil))
  11. }
  12. func TestLoadConfig_notRecogFile(t *testing.T) {
  13. filename, err := fs.TempFilenameWithText("hello")
  14. assert.Nil(t, err)
  15. defer os.Remove(filename)
  16. assert.NotNil(t, Load(filename, nil))
  17. }
  18. func TestConfigJson(t *testing.T) {
  19. tests := []string{
  20. ".json",
  21. ".yaml",
  22. ".yml",
  23. }
  24. text := `{
  25. "a": "foo",
  26. "b": 1,
  27. "c": "${FOO}",
  28. "d": "abcd!@#$112"
  29. }`
  30. for _, test := range tests {
  31. test := test
  32. t.Run(test, func(t *testing.T) {
  33. os.Setenv("FOO", "2")
  34. defer os.Unsetenv("FOO")
  35. tmpfile, err := createTempFile(test, text)
  36. assert.Nil(t, err)
  37. defer os.Remove(tmpfile)
  38. var val struct {
  39. A string `json:"a"`
  40. B int `json:"b"`
  41. C string `json:"c"`
  42. D string `json:"d"`
  43. }
  44. MustLoad(tmpfile, &val)
  45. assert.Equal(t, "foo", val.A)
  46. assert.Equal(t, 1, val.B)
  47. assert.Equal(t, "${FOO}", val.C)
  48. assert.Equal(t, "abcd!@#$112", val.D)
  49. })
  50. }
  51. }
  52. func TestConfigToml(t *testing.T) {
  53. text := `a = "foo"
  54. b = 1
  55. c = "${FOO}"
  56. d = "abcd!@#$112"
  57. `
  58. os.Setenv("FOO", "2")
  59. defer os.Unsetenv("FOO")
  60. tmpfile, err := createTempFile(".toml", text)
  61. assert.Nil(t, err)
  62. defer os.Remove(tmpfile)
  63. var val struct {
  64. A string `json:"a"`
  65. B int `json:"b"`
  66. C string `json:"c"`
  67. D string `json:"d"`
  68. }
  69. MustLoad(tmpfile, &val)
  70. assert.Equal(t, "foo", val.A)
  71. assert.Equal(t, 1, val.B)
  72. assert.Equal(t, "${FOO}", val.C)
  73. assert.Equal(t, "abcd!@#$112", val.D)
  74. }
  75. func TestConfigTomlEnv(t *testing.T) {
  76. text := `a = "foo"
  77. b = 1
  78. c = "${FOO}"
  79. d = "abcd!@#112"
  80. `
  81. os.Setenv("FOO", "2")
  82. defer os.Unsetenv("FOO")
  83. tmpfile, err := createTempFile(".toml", text)
  84. assert.Nil(t, err)
  85. defer os.Remove(tmpfile)
  86. var val struct {
  87. A string `json:"a"`
  88. B int `json:"b"`
  89. C string `json:"c"`
  90. D string `json:"d"`
  91. }
  92. MustLoad(tmpfile, &val, UseEnv())
  93. assert.Equal(t, "foo", val.A)
  94. assert.Equal(t, 1, val.B)
  95. assert.Equal(t, "2", val.C)
  96. assert.Equal(t, "abcd!@#112", val.D)
  97. }
  98. func TestConfigJsonEnv(t *testing.T) {
  99. tests := []string{
  100. ".json",
  101. ".yaml",
  102. ".yml",
  103. }
  104. text := `{
  105. "a": "foo",
  106. "b": 1,
  107. "c": "${FOO}",
  108. "d": "abcd!@#$a12 3"
  109. }`
  110. for _, test := range tests {
  111. test := test
  112. t.Run(test, func(t *testing.T) {
  113. os.Setenv("FOO", "2")
  114. defer os.Unsetenv("FOO")
  115. tmpfile, err := createTempFile(test, text)
  116. assert.Nil(t, err)
  117. defer os.Remove(tmpfile)
  118. var val struct {
  119. A string `json:"a"`
  120. B int `json:"b"`
  121. C string `json:"c"`
  122. D string `json:"d"`
  123. }
  124. MustLoad(tmpfile, &val, UseEnv())
  125. assert.Equal(t, "foo", val.A)
  126. assert.Equal(t, 1, val.B)
  127. assert.Equal(t, "2", val.C)
  128. assert.Equal(t, "abcd!@# 3", val.D)
  129. })
  130. }
  131. }
  132. func createTempFile(ext, text string) (string, error) {
  133. tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
  134. if err != nil {
  135. return "", err
  136. }
  137. if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
  138. return "", err
  139. }
  140. filename := tmpfile.Name()
  141. if err = tmpfile.Close(); err != nil {
  142. return "", err
  143. }
  144. return filename, nil
  145. }