Replace deprecated ioutil with io and os (#20776)
* Replace ioutil with io and os * Replace ioutil in utils/file.go * Minor fix to tests and excluded files Co-authored-by: Tim Scheuermann <tim.scheuermann@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
15b6046a62
Коммит
b4570afa90
@@ -6,7 +6,6 @@ package utils
|
||||
import (
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -90,7 +89,7 @@ func TestUnzipToPath(t *testing.T) {
|
||||
testDir, _ := fileutils.FindDir("tests")
|
||||
require.NotEmpty(t, testDir)
|
||||
|
||||
dir, err := ioutil.TempDir("", "unzip")
|
||||
dir, err := os.MkdirTemp("", "unzip")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
@@ -85,7 +84,7 @@ func CopyDir(src string, dst string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
items, err := ioutil.ReadDir(src)
|
||||
items, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -100,7 +99,12 @@ func CopyDir(src string, dst string) (err error) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if item.Mode()&os.ModeSymlink != 0 {
|
||||
info, ierr := item.Info()
|
||||
if ierr != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -17,18 +16,18 @@ import (
|
||||
)
|
||||
|
||||
func TestCopyDir(t *testing.T) {
|
||||
srcDir, err := ioutil.TempDir("", "src")
|
||||
srcDir, err := os.MkdirTemp("", "src")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(srcDir)
|
||||
|
||||
dstParentDir, err := ioutil.TempDir("", "dstparent")
|
||||
dstParentDir, err := os.MkdirTemp("", "dstparent")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dstParentDir)
|
||||
|
||||
dstDir := filepath.Join(dstParentDir, "dst")
|
||||
|
||||
tempFile := "temp.txt"
|
||||
err = ioutil.WriteFile(filepath.Join(srcDir, tempFile), []byte("test file"), 0655)
|
||||
err = os.WriteFile(filepath.Join(srcDir, tempFile), []byte("test file"), 0655)
|
||||
require.NoError(t, err)
|
||||
|
||||
childDir := "child"
|
||||
@@ -36,7 +35,7 @@ func TestCopyDir(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
childTempFile := "childtemp.txt"
|
||||
err = ioutil.WriteFile(filepath.Join(srcDir, childDir, childTempFile), []byte("test file"), 0755)
|
||||
err = os.WriteFile(filepath.Join(srcDir, childDir, childTempFile), []byte("test file"), 0755)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = CopyDir(srcDir, dstDir)
|
||||
@@ -46,7 +45,7 @@ func TestCopyDir(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, uint32(0655), uint32(stat.Mode()))
|
||||
assert.False(t, stat.IsDir())
|
||||
data, err := ioutil.ReadFile(filepath.Join(dstDir, tempFile))
|
||||
data, err := os.ReadFile(filepath.Join(dstDir, tempFile))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test file", string(data))
|
||||
|
||||
@@ -58,7 +57,7 @@ func TestCopyDir(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, uint32(0755), uint32(stat.Mode()))
|
||||
assert.False(t, stat.IsDir())
|
||||
data, err = ioutil.ReadFile(filepath.Join(dstDir, childDir, childTempFile))
|
||||
data, err = os.ReadFile(filepath.Join(dstDir, childDir, childTempFile))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test file", string(data))
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ package fileutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -26,23 +25,23 @@ func TestFindFile(t *testing.T) {
|
||||
// tmpDir3/
|
||||
// tmpDir4/
|
||||
// tmpDir5/
|
||||
tmpDir1, err := ioutil.TempDir("", "")
|
||||
tmpDir1, err := os.MkdirTemp("", "")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tmpDir1)
|
||||
|
||||
tmpDir2, err := ioutil.TempDir(tmpDir1, "")
|
||||
tmpDir2, err := os.MkdirTemp(tmpDir1, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.Mkdir(filepath.Join(tmpDir2, "other.txt"), 0700)
|
||||
require.NoError(t, err)
|
||||
|
||||
tmpDir3, err := ioutil.TempDir(tmpDir2, "")
|
||||
tmpDir3, err := os.MkdirTemp(tmpDir2, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
tmpDir4, err := ioutil.TempDir(tmpDir3, "")
|
||||
tmpDir4, err := os.MkdirTemp(tmpDir3, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
tmpDir5, err := ioutil.TempDir(tmpDir4, "")
|
||||
tmpDir5, err := os.MkdirTemp(tmpDir4, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
type testCase struct {
|
||||
@@ -56,7 +55,7 @@ func TestFindFile(t *testing.T) {
|
||||
|
||||
for _, fileName := range []string{"file1.json", "file2.xml", "other.txt"} {
|
||||
filePath := filepath.Join(tmpDir1, fileName)
|
||||
require.NoError(t, ioutil.WriteFile(filePath, []byte("{}"), 0600))
|
||||
require.NoError(t, os.WriteFile(filePath, []byte("{}"), 0600))
|
||||
|
||||
// Relative paths end up getting symlinks fully resolved, so use this below as necessary.
|
||||
filePathResolved, err := filepath.EvalSymlinks(filePath)
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -141,7 +141,7 @@ func GetLicenseFileFromDisk(fileName string) []byte {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
licenseBytes, err := ioutil.ReadAll(file)
|
||||
licenseBytes, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
mlog.Error("Failed to read license key from disk at", mlog.String("filename", fileName), mlog.Err(err))
|
||||
return nil
|
||||
|
||||
@@ -6,7 +6,6 @@ package utils
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -80,10 +79,10 @@ func TestGetLicenseFileFromDisk(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("not a license file", func(t *testing.T) {
|
||||
f, err := ioutil.TempFile("", "TestGetLicenseFileFromDisk")
|
||||
f, err := os.CreateTemp("", "TestGetLicenseFileFromDisk")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(f.Name())
|
||||
ioutil.WriteFile(f.Name(), []byte("not a license"), 0777)
|
||||
os.WriteFile(f.Name(), []byte("not a license"), 0777)
|
||||
|
||||
fileBytes := GetLicenseFileFromDisk(f.Name())
|
||||
require.NotEmpty(t, fileBytes, "should have read the file")
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
@@ -63,7 +62,7 @@ func UpdateAssetsSubpathInDir(subpath, directory string) error {
|
||||
}
|
||||
|
||||
rootHTMLPath := filepath.Join(staticDir, "root.html")
|
||||
oldRootHTML, err := ioutil.ReadFile(rootHTMLPath)
|
||||
oldRootHTML, err := os.ReadFile(rootHTMLPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to open root.html")
|
||||
}
|
||||
@@ -113,19 +112,19 @@ func UpdateAssetsSubpathInDir(subpath, directory string) error {
|
||||
}
|
||||
|
||||
// Write out the updated root.html.
|
||||
if err = ioutil.WriteFile(rootHTMLPath, []byte(newRootHTML), 0); err != nil {
|
||||
if err = os.WriteFile(rootHTMLPath, []byte(newRootHTML), 0); err != nil {
|
||||
return errors.Wrapf(err, "failed to update root.html with subpath %s", subpath)
|
||||
}
|
||||
|
||||
// Rewrite the manifest.json and *.css references to `/static/*` (or a previously rewritten subpath).
|
||||
err = filepath.Walk(staticDir, func(walkPath string, info os.FileInfo, err error) error {
|
||||
if filepath.Base(walkPath) == "manifest.json" || filepath.Ext(walkPath) == ".css" {
|
||||
old, err := ioutil.ReadFile(walkPath)
|
||||
old, err := os.ReadFile(walkPath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to open %s", walkPath)
|
||||
}
|
||||
new := strings.Replace(string(old), pathToReplace, newPath, -1)
|
||||
if err = ioutil.WriteFile(walkPath, []byte(new), 0); err != nil {
|
||||
if err = os.WriteFile(walkPath, []byte(new), 0); err != nil {
|
||||
return errors.Wrapf(err, "failed to update %s with subpath %s", walkPath, subpath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ package utils_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -41,7 +40,7 @@ func TestUpdateAssetsSubpathFromConfig(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("no config", func(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "test_update_assets_subpath")
|
||||
tempDir, err := os.MkdirTemp("", "test_update_assets_subpath")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
os.Chdir(tempDir)
|
||||
@@ -53,7 +52,7 @@ func TestUpdateAssetsSubpathFromConfig(t *testing.T) {
|
||||
|
||||
func TestUpdateAssetsSubpath(t *testing.T) {
|
||||
t.Run("no client dir", func(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "test_update_assets_subpath")
|
||||
tempDir, err := os.MkdirTemp("", "test_update_assets_subpath")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
os.Chdir(tempDir)
|
||||
@@ -63,7 +62,7 @@ func TestUpdateAssetsSubpath(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "test_update_assets_subpath")
|
||||
tempDir, err := os.MkdirTemp("", "test_update_assets_subpath")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
os.Chdir(tempDir)
|
||||
@@ -163,9 +162,9 @@ func TestUpdateAssetsSubpath(t *testing.T) {
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.Description, func(t *testing.T) {
|
||||
ioutil.WriteFile(filepath.Join(tempDir, model.ClientDir, "root.html"), []byte(testCase.RootHTML), 0700)
|
||||
ioutil.WriteFile(filepath.Join(tempDir, model.ClientDir, "main.css"), []byte(testCase.MainCSS), 0700)
|
||||
ioutil.WriteFile(filepath.Join(tempDir, model.ClientDir, "manifest.json"), []byte(testCase.ManifestJSON), 0700)
|
||||
os.WriteFile(filepath.Join(tempDir, model.ClientDir, "root.html"), []byte(testCase.RootHTML), 0700)
|
||||
os.WriteFile(filepath.Join(tempDir, model.ClientDir, "main.css"), []byte(testCase.MainCSS), 0700)
|
||||
os.WriteFile(filepath.Join(tempDir, model.ClientDir, "manifest.json"), []byte(testCase.ManifestJSON), 0700)
|
||||
err := utils.UpdateAssetsSubpath(testCase.Subpath)
|
||||
if testCase.ExpectedError != nil {
|
||||
require.Equal(t, testCase.ExpectedError, err)
|
||||
@@ -173,7 +172,7 @@ func TestUpdateAssetsSubpath(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
contents, err := ioutil.ReadFile(filepath.Join(tempDir, model.ClientDir, "root.html"))
|
||||
contents, err := os.ReadFile(filepath.Join(tempDir, model.ClientDir, "root.html"))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Rewrite the expected and contents for simpler diffs when failed.
|
||||
@@ -181,11 +180,11 @@ func TestUpdateAssetsSubpath(t *testing.T) {
|
||||
contentsStr := strings.Replace(string(contents), ">", ">\n", -1)
|
||||
require.Equal(t, expectedRootHTML, contentsStr)
|
||||
|
||||
contents, err = ioutil.ReadFile(filepath.Join(tempDir, model.ClientDir, "main.css"))
|
||||
contents, err = os.ReadFile(filepath.Join(tempDir, model.ClientDir, "main.css"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testCase.ExpectedMainCSS, string(contents))
|
||||
|
||||
contents, err = ioutil.ReadFile(filepath.Join(tempDir, model.ClientDir, "manifest.json"))
|
||||
contents, err = os.ReadFile(filepath.Join(tempDir, model.ClientDir, "manifest.json"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testCase.ExpectedManifestJSON, string(contents))
|
||||
})
|
||||
|
||||
@@ -5,7 +5,6 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -16,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func CompileGo(t *testing.T, sourceCode, outputPath string) {
|
||||
dir, err := ioutil.TempDir(".", "")
|
||||
dir, err := os.MkdirTemp(".", "")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
@@ -25,7 +24,7 @@ func CompileGo(t *testing.T, sourceCode, outputPath string) {
|
||||
|
||||
// Write out main.go given the source code.
|
||||
main := filepath.Join(dir, "main.go")
|
||||
err = ioutil.WriteFile(main, []byte(sourceCode), 0600)
|
||||
err = os.WriteFile(main, []byte(sourceCode), 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, sourceFile, _, ok := runtime.Caller(0)
|
||||
@@ -45,7 +44,7 @@ func CompileGo(t *testing.T, sourceCode, outputPath string) {
|
||||
}
|
||||
|
||||
func CompileGoTest(t *testing.T, sourceCode, outputPath string) {
|
||||
dir, err := ioutil.TempDir(".", "")
|
||||
dir, err := os.MkdirTemp(".", "")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
@@ -54,7 +53,7 @@ func CompileGoTest(t *testing.T, sourceCode, outputPath string) {
|
||||
|
||||
// Write out main.go given the source code.
|
||||
main := filepath.Join(dir, "main_test.go")
|
||||
err = ioutil.WriteFile(main, []byte(sourceCode), 0600)
|
||||
err = os.WriteFile(main, []byte(sourceCode), 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, sourceFile, _, ok := runtime.Caller(0)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -165,7 +165,7 @@ func GetURLWithCache(url string, cache *RequestCache, skip bool) ([]byte, error)
|
||||
return nil, errors.Errorf("Fetching notices failed with status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
cache.Data, err = ioutil.ReadAll(resp.Body)
|
||||
cache.Data, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
cache.Data = nil
|
||||
return nil, err
|
||||
|
||||
Ссылка в новой задаче
Block a user