MCPcopy
hub / github.com/spf13/viper / TestWriteConfigDotEnv

Function TestWriteConfigDotEnv

viper_test.go:1894–1941  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1892}
1893
1894func TestWriteConfigDotEnv(t *testing.T) {
1895 fs := afero.NewMemMapFs()
1896 testCases := map[string]struct {
1897 configName string
1898 configType string
1899 fileName string
1900 input []byte
1901 }{
1902 "with file extension": {
1903 configName: "c",
1904 configType: "env",
1905 fileName: "c.env",
1906 input: dotenvExample,
1907 },
1908 "without file extension": {
1909 configName: "c",
1910 configType: "env",
1911 fileName: "c",
1912 input: dotenvExample,
1913 },
1914 }
1915 for name, tc := range testCases {
1916 t.Run(name, func(t *testing.T) {
1917 v := New()
1918 v.SetFs(fs)
1919 v.SetConfigName(tc.configName)
1920 v.SetConfigType(tc.configType)
1921 err := v.ReadConfig(bytes.NewBuffer(tc.input))
1922 require.NoError(t, err)
1923 err = v.WriteConfigAs(tc.fileName)
1924 require.NoError(t, err)
1925
1926 // The TOML String method does not order the contents.
1927 // Therefore, we must read the generated file and compare the data.
1928 v2 := New()
1929 v2.SetFs(fs)
1930 v2.SetConfigName(tc.configName)
1931 v2.SetConfigType(tc.configType)
1932 v2.SetConfigFile(tc.fileName)
1933 err = v2.ReadInConfig()
1934 require.NoError(t, err)
1935
1936 assert.Equal(t, v.GetString("title_dotenv"), v2.GetString("title_dotenv"))
1937 assert.Equal(t, v.GetString("type_dotenv"), v2.GetString("type_dotenv"))
1938 assert.Equal(t, v.GetString("kind_dotenv"), v2.GetString("kind_dotenv"))
1939 })
1940 }
1941}
1942
1943func TestSafeWriteConfig(t *testing.T) {
1944 v := New()

Callers

nothing calls this directly

Calls 9

NewFunction · 0.85
SetFsMethod · 0.80
SetConfigNameMethod · 0.80
SetConfigTypeMethod · 0.80
ReadConfigMethod · 0.80
WriteConfigAsMethod · 0.80
SetConfigFileMethod · 0.80
ReadInConfigMethod · 0.80
GetStringMethod · 0.80

Tested by

no test coverage detected