Remove usages of deprecated io/ioutil; simplify tests

This commit is contained in:
Oleksandr Redko 2023-09-13 17:56:49 +03:00 committed by Márk Sági-Kazár
parent f683416bfb
commit 4aeec5882c
1 changed files with 22 additions and 51 deletions

View File

@ -10,7 +10,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -237,7 +236,7 @@ func initIni() {
} }
// make directories for testing // make directories for testing
func initDirs(t *testing.T) (string, string, func()) { func initDirs(t *testing.T) (string, string) {
var ( var (
testDirs = []string{`a a`, `b`, `C_`} testDirs = []string{`a a`, `b`, `C_`}
config = `improbable` config = `improbable`
@ -247,38 +246,23 @@ func initDirs(t *testing.T) (string, string, func()) {
testDirs = append(testDirs, `d\d`) testDirs = append(testDirs, `d\d`)
} }
root, err := ioutil.TempDir("", "") root := t.TempDir()
require.NoError(t, err, "Failed to create temporary directory")
cleanup := true err := os.Chdir(root)
defer func() {
if cleanup {
os.Chdir("..")
os.RemoveAll(root)
}
}()
assert.Nil(t, err)
err = os.Chdir(root)
require.Nil(t, err) require.Nil(t, err)
for _, dir := range testDirs { for _, dir := range testDirs {
err = os.Mkdir(dir, 0o750) err = os.Mkdir(dir, 0o750)
assert.Nil(t, err) assert.Nil(t, err)
err = ioutil.WriteFile( err = os.WriteFile(
path.Join(dir, config+".toml"), path.Join(dir, config+".toml"),
[]byte("key = \"value is "+dir+"\"\n"), []byte("key = \"value is "+dir+"\"\n"),
0o640) 0o640)
assert.Nil(t, err) assert.Nil(t, err)
} }
cleanup = false return root, config
return root, config, func() {
os.Chdir("..")
os.RemoveAll(root)
}
} }
// stubs for PFlag Values // stubs for PFlag Values
@ -1515,14 +1499,13 @@ func TestIsSet(t *testing.T) {
} }
func TestDirsSearch(t *testing.T) { func TestDirsSearch(t *testing.T) {
root, config, cleanup := initDirs(t) root, config := initDirs(t)
defer cleanup()
v := New() v := New()
v.SetConfigName(config) v.SetConfigName(config)
v.SetDefault(`key`, `default`) v.SetDefault(`key`, `default`)
entries, err := ioutil.ReadDir(root) entries, err := os.ReadDir(root)
assert.Nil(t, err) assert.Nil(t, err)
for _, e := range entries { for _, e := range entries {
if e.IsDir() { if e.IsDir() {
@ -1537,8 +1520,7 @@ func TestDirsSearch(t *testing.T) {
} }
func TestWrongDirsSearchNotFound(t *testing.T) { func TestWrongDirsSearchNotFound(t *testing.T) {
_, config, cleanup := initDirs(t) _, config := initDirs(t)
defer cleanup()
v := New() v := New()
v.SetConfigName(config) v.SetConfigName(config)
@ -1556,8 +1538,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
} }
func TestWrongDirsSearchNotFoundForMerge(t *testing.T) { func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
_, config, cleanup := initDirs(t) _, config := initDirs(t)
defer cleanup()
v := New() v := New()
v.SetConfigName(config) v.SetConfigName(config)
@ -2438,36 +2419,28 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) {
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q"))) assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
} }
func newViperWithConfigFile(t *testing.T) (*Viper, string, func()) { func newViperWithConfigFile(t *testing.T) (*Viper, string) {
watchDir, err := ioutil.TempDir("", "") watchDir := t.TempDir()
require.Nil(t, err)
configFile := path.Join(watchDir, "config.yaml") 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) require.Nil(t, err)
cleanup := func() {
os.RemoveAll(watchDir)
}
v := New() v := New()
v.SetConfigFile(configFile) v.SetConfigFile(configFile)
err = v.ReadInConfig() err = v.ReadInConfig()
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, "bar", v.Get("foo")) require.Equal(t, "bar", v.Get("foo"))
return v, configFile, cleanup return v, configFile
} }
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func()) { func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
watchDir, err := ioutil.TempDir("", "") watchDir := t.TempDir()
require.Nil(t, err)
dataDir1 := path.Join(watchDir, "data1") dataDir1 := path.Join(watchDir, "data1")
err = os.Mkdir(dataDir1, 0o777) err := os.Mkdir(dataDir1, 0o777)
require.Nil(t, err) require.Nil(t, err)
realConfigFile := path.Join(dataDir1, "config.yaml") realConfigFile := path.Join(dataDir1, "config.yaml")
t.Logf("Real config file location: %s\n", realConfigFile) 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) require.Nil(t, err)
cleanup := func() {
os.RemoveAll(watchDir)
}
// now, symlink the tm `data1` dir to `data` in the baseDir // now, symlink the tm `data1` dir to `data` in the baseDir
os.Symlink(dataDir1, path.Join(watchDir, "data")) os.Symlink(dataDir1, path.Join(watchDir, "data"))
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml` // and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
@ -2480,7 +2453,7 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string, func
err = v.ReadInConfig() err = v.ReadInConfig()
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, "bar", v.Get("foo")) require.Equal(t, "bar", v.Get("foo"))
return v, watchDir, configFile, cleanup return v, watchDir, configFile
} }
func TestWatchFile(t *testing.T) { func TestWatchFile(t *testing.T) {
@ -2491,8 +2464,7 @@ func TestWatchFile(t *testing.T) {
t.Run("file content changed", func(t *testing.T) { t.Run("file content changed", func(t *testing.T) {
// given a `config.yaml` file being watched // given a `config.yaml` file being watched
v, configFile, cleanup := newViperWithConfigFile(t) v, configFile := newViperWithConfigFile(t)
defer cleanup()
_, err := os.Stat(configFile) _, err := os.Stat(configFile)
require.NoError(t, err) require.NoError(t, err)
t.Logf("test config file: %s\n", configFile) t.Logf("test config file: %s\n", configFile)
@ -2507,7 +2479,7 @@ func TestWatchFile(t *testing.T) {
}) })
v.WatchConfig() v.WatchConfig()
// when overwriting the file and waiting for the custom change notification handler to be triggered // 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() wg.Wait()
// then the config value should have changed // then the config value should have changed
require.Nil(t, err) require.Nil(t, err)
@ -2519,8 +2491,7 @@ func TestWatchFile(t *testing.T) {
if runtime.GOOS != "linux" { if runtime.GOOS != "linux" {
t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...") t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...")
} }
v, watchDir, _, _ := newViperWithSymlinkedConfigFile(t) v, watchDir, _ := newViperWithSymlinkedConfigFile(t)
// defer cleanup()
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
v.WatchConfig() v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) { v.OnConfigChange(func(in fsnotify.Event) {
@ -2533,7 +2504,7 @@ func TestWatchFile(t *testing.T) {
err := os.Mkdir(dataDir2, 0o777) err := os.Mkdir(dataDir2, 0o777)
require.Nil(t, err) require.Nil(t, err)
configFile2 := path.Join(dataDir2, "config.yaml") 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) require.Nil(t, err)
// change the symlink using the `ln -sfn` command // change the symlink using the `ln -sfn` command
err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run() err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run()