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>
Этот коммит содержится в:
Tim Scheuermann
2022-08-09 14:25:46 +03:00
коммит произвёл GitHub
родитель 15b6046a62
Коммит b4570afa90
115 изменённых файлов: 408 добавлений и 475 удалений

Просмотреть файл

@@ -13,7 +13,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/rpc"
@@ -444,7 +443,7 @@ func (s *hooksRPCServer) ServeHTTP(args *Z_ServeHTTPArgs, returns *struct{}) err
}
r.Body = connectIOReader(connection)
} else {
r.Body = ioutil.NopCloser(&bytes.Buffer{})
r.Body = io.NopCloser(&bytes.Buffer{})
}
defer r.Body.Close()
@@ -487,7 +486,7 @@ func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response {
}
if request.Body != nil {
requestBody, err := ioutil.ReadAll(request.Body)
requestBody, err := io.ReadAll(request.Body)
if err != nil {
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
return nil
@@ -504,20 +503,20 @@ func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response {
return nil
}
_returns.Response.Body = ioutil.NopCloser(bytes.NewBuffer(_returns.ResponseBody))
_returns.Response.Body = io.NopCloser(bytes.NewBuffer(_returns.ResponseBody))
return _returns.Response
}
func (s *apiRPCServer) PluginHTTP(args *Z_PluginHTTPArgs, returns *Z_PluginHTTPReturns) error {
args.Request.Body = ioutil.NopCloser(bytes.NewBuffer(args.RequestBody))
args.Request.Body = io.NopCloser(bytes.NewBuffer(args.RequestBody))
if hook, ok := s.impl.(interface {
PluginHTTP(request *http.Request) *http.Response
}); ok {
response := hook.PluginHTTP(args.Request)
responseBody, err := ioutil.ReadAll(response.Body)
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return encodableError(fmt.Errorf("RPC call to PluginHTTP API failed: %s", err.Error()))
}

Просмотреть файл

@@ -6,7 +6,6 @@ package plugin
import (
"fmt"
"hash/fnv"
"io/ioutil"
"os"
"path/filepath"
"sync"
@@ -86,7 +85,7 @@ func NewEnvironment(newAPIImpl apiImplCreatorFunc,
//
// Plugins are found non-recursively and paths beginning with a dot are always ignored.
func scanSearchPath(path string) ([]*model.BundleInfo, error) {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
@@ -468,7 +467,7 @@ func (env *Environment) UnpackWebappBundle(id string) (*model.Manifest, error) {
sourceBundleFilepath := filepath.Join(destinationPath, filepath.Base(bundlePath))
sourceBundleFileContents, err := ioutil.ReadFile(sourceBundleFilepath)
sourceBundleFileContents, err := os.ReadFile(sourceBundleFilepath)
if err != nil {
return nil, errors.Wrapf(err, "unable to read webapp bundle: %v", id)
}

Просмотреть файл

@@ -5,7 +5,6 @@ package plugin
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -16,7 +15,7 @@ import (
)
func TestAvailablePlugins(t *testing.T) {
dir, err1 := ioutil.TempDir("", "mm-plugin-test")
dir, err1 := os.MkdirTemp("", "mm-plugin-test")
require.NoError(t, err1)
t.Cleanup(func() {
os.RemoveAll(dir)
@@ -40,7 +39,7 @@ func TestAvailablePlugins(t *testing.T) {
path := filepath.Join(dir, "plugin1", "plugin.json")
manifestJSON, jsonErr := json.Marshal(bundle1.Manifest)
require.NoError(t, jsonErr)
err = ioutil.WriteFile(path, manifestJSON, 0644)
err = os.WriteFile(path, manifestJSON, 0644)
require.NoError(t, err)
bundles, err := env.Available()
@@ -54,7 +53,7 @@ func TestAvailablePlugins(t *testing.T) {
defer os.RemoveAll(filepath.Join(dir, "plugin2"))
path := filepath.Join(dir, "plugin2", "manifest.json")
err = ioutil.WriteFile(path, []byte("{}"), 0644)
err = os.WriteFile(path, []byte("{}"), 0644)
require.NoError(t, err)
bundles, err := env.Available()

Просмотреть файл

@@ -4,7 +4,6 @@
package plugin
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -27,7 +26,7 @@ func TestPluginHealthCheck(t *testing.T) {
}
func testPluginHealthCheckSuccess(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
@@ -48,7 +47,7 @@ func testPluginHealthCheckSuccess(t *testing.T) {
}
`, backend)
err = ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
err = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
require.NoError(t, err)
bundle := model.BundleInfoForPath(dir)
@@ -65,7 +64,7 @@ func testPluginHealthCheckSuccess(t *testing.T) {
}
func testPluginHealthCheckPanic(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
@@ -91,7 +90,7 @@ func testPluginHealthCheckPanic(t *testing.T) {
}
`, backend)
err = ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
err = os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
require.NoError(t, err)
bundle := model.BundleInfoForPath(dir)

Просмотреть файл

@@ -10,8 +10,8 @@ import (
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
@@ -580,7 +580,7 @@ func generateHooksGlue(info *PluginInterfaceInfo) {
panic(err)
}
if err := ioutil.WriteFile(filepath.Join(getPluginPackageDir(), "client_rpc_generated.go"), formatted, 0664); err != nil {
if err := os.WriteFile(filepath.Join(getPluginPackageDir(), "client_rpc_generated.go"), formatted, 0664); err != nil {
panic(err)
}
}
@@ -613,7 +613,7 @@ func generateProductHooksInterfaces(info *PluginInterfaceInfo) {
panic(err)
}
if err := ioutil.WriteFile(filepath.Join(getPluginPackageDir(), "product_hooks_generated.go"), formatted, 0664); err != nil {
if err := os.WriteFile(filepath.Join(getPluginPackageDir(), "product_hooks_generated.go"), formatted, 0664); err != nil {
panic(err)
}
}
@@ -667,7 +667,7 @@ func generatePluginTimerLayer(info *PluginInterfaceInfo) {
panic(err)
}
if err := ioutil.WriteFile(filepath.Join(getPluginPackageDir(), fileName), formatted, 0664); err != nil {
if err := os.WriteFile(filepath.Join(getPluginPackageDir(), fileName), formatted, 0664); err != nil {
panic(err)
}
}

Просмотреть файл

@@ -5,7 +5,7 @@ package plugintest_test
import (
"fmt"
"io/ioutil"
io "io"
"net/http"
"net/http/httptest"
"testing"
@@ -52,7 +52,7 @@ func Example() {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Add("Mattermost-User-Id", user.Id)
p.ServeHTTP(&plugin.Context{}, w, r)
body, err := ioutil.ReadAll(w.Result().Body)
body, err := io.ReadAll(w.Result().Body)
require.NoError(t, err)
assert.Equal(t, "Welcome back, billybob!", string(body))
}

Просмотреть файл

@@ -4,7 +4,6 @@
package plugin
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -28,11 +27,11 @@ func TestSupervisor(t *testing.T) {
}
func testSupervisorInvalidExecutablePath(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "/foo/../../backend.exe"}}`), 0600)
os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "/foo/../../backend.exe"}}`), 0600)
bundle := model.BundleInfoForPath(dir)
log := mlog.CreateConsoleTestLogger(true, mlog.LvlError)
@@ -43,11 +42,11 @@ func testSupervisorInvalidExecutablePath(t *testing.T) {
}
func testSupervisorNonExistentExecutablePath(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "thisfileshouldnotexist"}}`), 0600)
os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "thisfileshouldnotexist"}}`), 0600)
bundle := model.BundleInfoForPath(dir)
log := mlog.CreateConsoleTestLogger(true, mlog.LvlError)
@@ -59,7 +58,7 @@ func testSupervisorNonExistentExecutablePath(t *testing.T) {
// If plugin development goes really wrong, let's make sure plugin activation won't block forever.
func testSupervisorStartTimeout(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
@@ -73,7 +72,7 @@ func testSupervisorStartTimeout(t *testing.T) {
}
`, backend)
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
os.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "server": {"executable": "backend.exe"}}`), 0600)
bundle := model.BundleInfoForPath(dir)
log := mlog.CreateConsoleTestLogger(true, mlog.LvlError)