github.com/lingyao2333/mo-zero@v1.4.1/core/conf/config_test.go (about)

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