config_test.go 3.2 KB

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