diff options
author | Oleksandr Redko <oleksandr.red+github@gmail.com> | 2023-02-19 00:43:26 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-03-01 16:28:43 +0100 |
commit | d453c12742e992d672fcf3e61b7a5ed35391c4b0 (patch) | |
tree | 1299a1f5a44bda1cf6449a8257f393350b2cc012 /helpers/path_test.go | |
parent | 97b010f521e592b5fc29daace225476b64543643 (diff) |
Replace deprecated ioutil with io and os
https://pkg.go.dev/io/ioutil is deprecated since Go 1.16.
Diffstat (limited to 'helpers/path_test.go')
-rw-r--r-- | helpers/path_test.go | 32 |
1 files changed, 7 insertions, 25 deletions
diff --git a/helpers/path_test.go b/helpers/path_test.go index b8739f37a..1f206a881 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -15,7 +15,6 @@ package helpers import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -259,7 +258,7 @@ func TestIsDir(t *testing.T) { func createZeroSizedFileInTempDir() (*os.File, error) { filePrefix := "_path_test_" - f, e := ioutil.TempFile("", filePrefix) // dir is os.TempDir() + f, e := os.CreateTemp("", filePrefix) // dir is os.TempDir() if e != nil { // if there was an error no file was created. // => no requirement to delete the file @@ -275,7 +274,7 @@ func createNonZeroSizedFileInTempDir() (*os.File, error) { return nil, err } byteString := []byte("byteString") - err = ioutil.WriteFile(f.Name(), byteString, 0644) + err = os.WriteFile(f.Name(), byteString, 0644) if err != nil { // delete the file deleteFileInTempDir(f) @@ -288,27 +287,12 @@ func deleteFileInTempDir(f *os.File) { _ = os.Remove(f.Name()) } -func createEmptyTempDir() (string, error) { - dirPrefix := "_dir_prefix_" - d, e := ioutil.TempDir("", dirPrefix) // will be in os.TempDir() - if e != nil { - // no directory to delete - it was never created - return "", e - } - return d, nil -} - -func deleteTempDir(d string) { - _ = os.RemoveAll(d) -} - func TestExists(t *testing.T) { zeroSizedFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(zeroSizedFile) nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir() defer deleteFileInTempDir(nonZeroSizedFile) - emptyDirectory, _ := createEmptyTempDir() - defer deleteTempDir(emptyDirectory) + emptyDirectory := t.TempDir() nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt" nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/" @@ -455,8 +439,7 @@ func TestFindCWD(t *testing.T) { func TestSafeWriteToDisk(t *testing.T) { emptyFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(emptyFile) - tmpDir, _ := createEmptyTempDir() - defer deleteTempDir(tmpDir) + tmpDir := t.TempDir() randomString := "This is a random string!" reader := strings.NewReader(randomString) @@ -485,7 +468,7 @@ func TestSafeWriteToDisk(t *testing.T) { if d.expectedErr != e { t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e) } - contents, _ := ioutil.ReadFile(d.filename) + contents, _ := os.ReadFile(d.filename) if randomString != string(contents) { t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents)) } @@ -497,8 +480,7 @@ func TestSafeWriteToDisk(t *testing.T) { func TestWriteToDisk(t *testing.T) { emptyFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(emptyFile) - tmpDir, _ := createEmptyTempDir() - defer deleteTempDir(tmpDir) + tmpDir := t.TempDir() randomString := "This is a random string!" reader := strings.NewReader(randomString) @@ -520,7 +502,7 @@ func TestWriteToDisk(t *testing.T) { if d.expectedErr != e { t.Errorf("Test %d failed. WriteToDisk Error Expected %q but got %q", i, d.expectedErr, e) } - contents, e := ioutil.ReadFile(d.filename) + contents, e := os.ReadFile(d.filename) if e != nil { t.Errorf("Test %d failed. Could not read file %s. Reason: %s\n", i, d.filename, e) } |