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

Function TestWriteConfigTOML

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

Source from the content-addressed store, hash-verified

1841}
1842
1843func TestWriteConfigTOML(t *testing.T) {
1844 fs := afero.NewMemMapFs()
1845
1846 testCases := map[string]struct {
1847 configName string
1848 configType string
1849 fileName string
1850 input []byte
1851 }{
1852 "with file extension": {
1853 configName: "c",
1854 configType: "toml",
1855 fileName: "c.toml",
1856 input: tomlExample,
1857 },
1858 "without file extension": {
1859 configName: "c",
1860 configType: "toml",
1861 fileName: "c",
1862 input: tomlExample,
1863 },
1864 }
1865 for name, tc := range testCases {
1866 t.Run(name, func(t *testing.T) {
1867 v := New()
1868 v.SetFs(fs)
1869 v.SetConfigName(tc.configName)
1870 v.SetConfigType(tc.configType)
1871 err := v.ReadConfig(bytes.NewBuffer(tc.input))
1872 require.NoError(t, err)
1873 err = v.WriteConfigAs(tc.fileName)
1874 require.NoError(t, err)
1875
1876 // The TOML String method does not order the contents.
1877 // Therefore, we must read the generated file and compare the data.
1878 v2 := New()
1879 v2.SetFs(fs)
1880 v2.SetConfigName(tc.configName)
1881 v2.SetConfigType(tc.configType)
1882 v2.SetConfigFile(tc.fileName)
1883 err = v2.ReadInConfig()
1884 require.NoError(t, err)
1885
1886 assert.Equal(t, v.GetString("title"), v2.GetString("title"))
1887 assert.Equal(t, v.GetString("owner.bio"), v2.GetString("owner.bio"))
1888 assert.Equal(t, v.GetString("owner.dob"), v2.GetString("owner.dob"))
1889 assert.Equal(t, v.GetString("owner.organization"), v2.GetString("owner.organization"))
1890 })
1891 }
1892}
1893
1894func TestWriteConfigDotEnv(t *testing.T) {
1895 fs := afero.NewMemMapFs()

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