Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usages of deprecated io/ioutil; simplify viper tests #1631

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 30 additions & 61 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -237,7 +236,7 @@ func initIni() {
}

// make directories for testing
func initDirs(t *testing.T) (string, string, func()) {
func initDirs(t *testing.T) (string, string) {
var (
testDirs = []string{`a a`, `b`, `C_`}
config = `improbable`
Expand All @@ -247,38 +246,21 @@ func initDirs(t *testing.T) (string, string, func()) {
testDirs = append(testDirs, `d\d`)
}

root, err := ioutil.TempDir("", "")
require.NoError(t, err, "Failed to create temporary directory")

cleanup := true
defer func() {
if cleanup {
os.Chdir("..")
os.RemoveAll(root)
}
}()

assert.Nil(t, err)

err = os.Chdir(root)
require.Nil(t, err)
root := t.TempDir()

for _, dir := range testDirs {
err = os.Mkdir(dir, 0o750)
assert.Nil(t, err)
innerDir := filepath.Join(root, dir)
err := os.Mkdir(innerDir, 0o750)
require.NoError(t, err)

err = ioutil.WriteFile(
path.Join(dir, config+".toml"),
[]byte("key = \"value is "+dir+"\"\n"),
err = os.WriteFile(
filepath.Join(innerDir, config+".toml"),
[]byte(`key = "value is `+dir+`"`+"\n"),
0o640)
assert.Nil(t, err)
require.NoError(t, err)
}

cleanup = false
return root, config, func() {
os.Chdir("..")
os.RemoveAll(root)
}
return root, config
}

// stubs for PFlag Values
Expand Down Expand Up @@ -1515,30 +1497,28 @@ func TestIsSet(t *testing.T) {
}

func TestDirsSearch(t *testing.T) {
root, config, cleanup := initDirs(t)
defer cleanup()
root, config := initDirs(t)

v := New()
v.SetConfigName(config)
v.SetDefault(`key`, `default`)

entries, err := ioutil.ReadDir(root)
assert.Nil(t, err)
entries, err := os.ReadDir(root)
require.NoError(t, err)
for _, e := range entries {
if e.IsDir() {
v.AddConfigPath(e.Name())
v.AddConfigPath(filepath.Join(root, e.Name()))
}
}

err = v.ReadInConfig()
assert.Nil(t, err)
require.NoError(t, err)

assert.Equal(t, `value is `+filepath.Base(v.configPaths[0]), v.GetString(`key`))
}

func TestWrongDirsSearchNotFound(t *testing.T) {
_, config, cleanup := initDirs(t)
defer cleanup()
_, config := initDirs(t)

v := New()
v.SetConfigName(config)
Expand All @@ -1556,8 +1536,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
}

func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
_, config, cleanup := initDirs(t)
defer cleanup()
_, config := initDirs(t)

v := New()
v.SetConfigName(config)
Expand Down Expand Up @@ -2438,36 +2417,28 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) {
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
}

func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) {
watchDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
func newViperWithConfigFile(t *testing.T) (*Viper, string) {
watchDir := t.TempDir()
configFile := path.Join(watchDir, "config.yaml")
err = ioutil.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
require.Nil(t, err)
cleanup := func() {
os.RemoveAll(watchDir)
}
v := New()
v.SetConfigFile(configFile)
err = v.ReadInConfig()
require.Nil(t, err)
require.Equal(t, "bar", v.Get("foo"))
return v, configFile, cleanup
return v, configFile
}

func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func()) {
watchDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
watchDir := t.TempDir()
dataDir1 := path.Join(watchDir, "data1")
err = os.Mkdir(dataDir1, 0o777)
err := os.Mkdir(dataDir1, 0o777)
require.Nil(t, err)
realConfigFile := path.Join(dataDir1, "config.yaml")
t.Logf("Real config file location: %s\n", realConfigFile)
err = ioutil.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
err = os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0o640)
require.Nil(t, err)
cleanup := func() {
os.RemoveAll(watchDir)
}
// now, symlink the tm `data1` dir to `data` in the baseDir
os.Symlink(dataDir1, path.Join(watchDir, "data"))
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
Expand All @@ -2480,7 +2451,7 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func
err = v.ReadInConfig()
require.Nil(t, err)
require.Equal(t, "bar", v.Get("foo"))
return v, watchDir, configFile, cleanup
return v, watchDir, configFile
}

func TestWatchFile(t *testing.T) {
Expand All @@ -2491,8 +2462,7 @@ func TestWatchFile(t *testing.T) {

t.Run("file content changed", func(t *testing.T) {
// given a `config.yaml` file being watched
v, configFile, cleanup := newViperWithConfigFile(t)
defer cleanup()
v, configFile := newViperWithConfigFile(t)
_, err := os.Stat(configFile)
require.NoError(t, err)
t.Logf("test config file: %s\n", configFile)
Expand All @@ -2507,7 +2477,7 @@ func TestWatchFile(t *testing.T) {
})
v.WatchConfig()
// when overwriting the file and waiting for the custom change notification handler to be triggered
err = ioutil.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
err = os.WriteFile(configFile, []byte("foo: baz\n"), 0o640)
wg.Wait()
// then the config value should have changed
require.Nil(t, err)
Expand All @@ -2519,8 +2489,7 @@ func TestWatchFile(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...")
}
v, watchDir, _, _ := newViperWithSymlinkedConfigFile(t)
// defer cleanup()
v, watchDir, _ := newViperWithSymlinkedConfigFile(t)
wg := sync.WaitGroup{}
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
Expand All @@ -2533,7 +2502,7 @@ func TestWatchFile(t *testing.T) {
err := os.Mkdir(dataDir2, 0o777)
require.Nil(t, err)
configFile2 := path.Join(dataDir2, "config.yaml")
err = ioutil.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
err = os.WriteFile(configFile2, []byte("foo: baz\n"), 0o640)
require.Nil(t, err)
// change the symlink using the `ln -sfn` command
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()
Expand Down