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 удалений

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

@@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
@@ -84,7 +83,7 @@ func SetMainHelper(mh *testlib.MainHelper) {
func setupTestHelper(dbStore store.Store, searchEngine *searchengine.Broker, enterprise bool, includeCache bool,
updateConfig func(*model.Config), options []app.Option) *TestHelper {
tempWorkspace, err := ioutil.TempDir("", "apptest")
tempWorkspace, err := os.MkdirTemp("", "apptest")
if err != nil {
panic(err)
}

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

@@ -5,7 +5,7 @@ package api4
import (
"encoding/json"
"io/ioutil"
"io"
"strings"
"testing"
@@ -461,7 +461,7 @@ func TestPatchBot(t *testing.T) {
r, err := th.Client.DoAPIPut("/bots/"+createdBot.UserId, `{"creator_id":"`+th.BasicUser2.Id+`"}`)
require.NoError(t, err)
defer func() {
_, _ = ioutil.ReadAll(r.Body)
_, _ = io.ReadAll(r.Body)
_ = r.Body.Close()
}()
var patchedBot *model.Bot

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

@@ -7,7 +7,7 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"time"
@@ -123,7 +123,7 @@ func changeSubscription(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.changeSubscription", "api.cloud.app_error", nil, err.Error(), http.StatusBadRequest)
return
@@ -174,7 +174,7 @@ func requestCloudTrial(c *Context, w http.ResponseWriter, r *http.Request) {
}
// check if the email needs to be set
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return
@@ -221,7 +221,7 @@ func validateBusinessEmail(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.requestCloudTrial", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return
@@ -394,7 +394,7 @@ func updateCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.updateCloudCustomer", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return
@@ -432,7 +432,7 @@ func updateCloudCustomerAddress(c *Context, w http.ResponseWriter, r *http.Reque
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.updateCloudCustomerAddress", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return
@@ -504,7 +504,7 @@ func confirmCustomerPayment(c *Context, w http.ResponseWriter, r *http.Request)
auditRec := c.MakeAuditRecord("confirmCustomerPayment", audit.Fail)
defer c.LogAuditRec(auditRec)
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.confirmCustomerPayment", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return
@@ -594,7 +594,7 @@ func handleCWSWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.handleCWSWebhook", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError)
return

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

@@ -6,7 +6,7 @@ package api4
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
@@ -533,7 +533,7 @@ func TestUpdateConfigRestrictSystemAdmin(t *testing.T) {
}
func TestUpdateConfigDiffInAuditRecord(t *testing.T) {
logFile, err := ioutil.TempFile("", "adv.log")
logFile, err := os.CreateTemp("", "adv.log")
require.NoError(t, err)
defer os.Remove(logFile.Name())
@@ -569,7 +569,7 @@ func TestUpdateConfigDiffInAuditRecord(t *testing.T) {
require.NoError(t, logFile.Sync())
data, err := ioutil.ReadAll(logFile)
data, err := io.ReadAll(logFile)
require.NoError(t, err)
require.NotEmpty(t, data)
@@ -955,7 +955,7 @@ func TestMigrateConfig(t *testing.T) {
file, err := json.MarshalIndent(cfg, "", " ")
require.NoError(t, err)
err = ioutil.WriteFile("from.json", file, 0644)
err = os.WriteFile("from.json", file, 0644)
require.NoError(t, err)
defer os.Remove("from.json")

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

@@ -7,7 +7,6 @@ import (
"bytes"
"image"
_ "image/gif"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -100,7 +99,7 @@ func TestCreateEmoji(t *testing.T) {
}
path, _ := fileutils.FindDir("tests")
bytes, err := ioutil.ReadFile(filepath.Join(path, "testwebp.webp"))
bytes, err := os.ReadFile(filepath.Join(path, "testwebp.webp"))
require.NoError(t, err)
newEmoji, _, err = client.CreateEmoji(emoji, bytes, "image.webp")
require.NoError(t, err)

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

@@ -6,7 +6,6 @@ package api4
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -151,7 +150,7 @@ func TestDownloadExport(t *testing.T) {
data := randomBytes(t, 1024*1024)
var buf bytes.Buffer
exportName := "export.zip"
err = ioutil.WriteFile(filepath.Join(exportDir, exportName), data, 0600)
err = os.WriteFile(filepath.Join(exportDir, exportName), data, 0600)
require.NoError(t, err)
n, _, err := c.DownloadExport(exportName, &buf, 0)
@@ -168,7 +167,7 @@ func TestDownloadExport(t *testing.T) {
data := randomBytes(t, 1024*1024)
var buf bytes.Buffer
exportName := "export.zip"
err = ioutil.WriteFile(filepath.Join(exportDir, exportName), data, 0600)
err = os.WriteFile(filepath.Join(exportDir, exportName), data, 0600)
require.NoError(t, err)
offset := 1024 * 512

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

@@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
@@ -55,7 +54,7 @@ func fileBytes(t *testing.T, path string) []byte {
f, err := os.Open(path)
require.NoError(t, err)
defer f.Close()
bb, err := ioutil.ReadAll(f)
bb, err := io.ReadAll(f)
require.NoError(t, err)
return bb
}
@@ -701,10 +700,10 @@ func TestUploadFiles(t *testing.T) {
data, _, err := get(ri.Id)
require.NoError(t, err)
expected, err := ioutil.ReadFile(filepath.Join(testDir, name))
expected, err := os.ReadFile(filepath.Join(testDir, name))
require.NoError(t, err)
if !bytes.Equal(data, expected) {
tf, err := ioutil.TempFile("", fmt.Sprintf("test_%v_*_%s", i, name))
tf, err := os.CreateTemp("", fmt.Sprintf("test_%v_*_%s", i, name))
require.NoError(t, err)
defer tf.Close()
_, err = io.Copy(tf, bytes.NewReader(data))

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

@@ -6,7 +6,7 @@ package api4
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
@@ -293,7 +293,7 @@ func linkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
}
syncableType := c.Params.SyncableType
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.createGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
return
@@ -464,7 +464,7 @@ func patchGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
}
syncableType := c.Params.SyncableType
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
return
@@ -1109,8 +1109,8 @@ func deleteGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
// licensedAndConfiguredForGroupBySource returns an app error if not properly license or configured for the given group type. The returned app error
// will have a blank 'Where' field, which should be subsequently set by the caller, for example:
//
// err := licensedAndConfiguredForGroupBySource(c.App, group.Source)
// err.Where = "Api4.getGroup"
// err := licensedAndConfiguredForGroupBySource(c.App, group.Source)
// err.Where = "Api4.getGroup"
//
// Temporarily, this function also checks for the CustomGroups feature flag.
func licensedAndConfiguredForGroupBySource(app app.AppIface, source model.GroupSource) *model.AppError {

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

@@ -4,7 +4,7 @@
package api4
import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
@@ -89,7 +89,7 @@ func TestGetImage(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "success", string(respBody))

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

@@ -5,7 +5,7 @@ package api4
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
@@ -21,7 +21,7 @@ type testHandler struct {
}
func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
bb, err := ioutil.ReadAll(r.Body)
bb, err := io.ReadAll(r.Body)
assert.NoError(th.t, err)
assert.NotEmpty(th.t, string(bb))
var poir model.PostActionIntegrationRequest

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

@@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
@@ -203,7 +202,7 @@ func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
ReceiveEmailsAccepted bool `json:"receive_emails_accepted"`
}
b, readErr := ioutil.ReadAll(r.Body)
b, readErr := io.ReadAll(r.Body)
if readErr != nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest)
return

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

@@ -8,7 +8,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
@@ -44,7 +44,7 @@ func TestPlugin(t *testing.T) {
})
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
// Install from URL
@@ -295,7 +295,7 @@ func TestNotifyClusterPluginEvent(t *testing.T) {
})
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
testCluster.ClearMessages()
@@ -378,7 +378,7 @@ func TestNotifyClusterPluginEvent(t *testing.T) {
func TestDisableOnRemove(t *testing.T) {
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
testCases := []struct {
@@ -723,7 +723,7 @@ func TestGetInstalledMarketplacePlugins(t *testing.T) {
}
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
t.Run("marketplace client returns not-installed plugin", func(t *testing.T) {
@@ -752,7 +752,7 @@ func TestGetInstalledMarketplacePlugins(t *testing.T) {
manifest, _, err := th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData))
require.NoError(t, err)
testIcon, err := ioutil.ReadFile(filepath.Join(path, "test.svg"))
testIcon, err := os.ReadFile(filepath.Join(path, "test.svg"))
require.NoError(t, err)
require.True(t, svg.Is(testIcon))
testIconData := fmt.Sprintf("data:image/svg+xml;base64,%s", base64.StdEncoding.EncodeToString(testIcon))
@@ -860,13 +860,13 @@ func TestSearchGetMarketplacePlugins(t *testing.T) {
}
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
tarDataV2, err := ioutil.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
tarDataV2, err := os.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
require.NoError(t, err)
testIcon, err := ioutil.ReadFile(filepath.Join(path, "test.svg"))
testIcon, err := os.ReadFile(filepath.Join(path, "test.svg"))
require.NoError(t, err)
require.True(t, svg.Is(testIcon))
testIconData := fmt.Sprintf("data:image/svg+xml;base64,%s", base64.StdEncoding.EncodeToString(testIcon))
@@ -1021,7 +1021,7 @@ func TestGetLocalPluginInMarketplace(t *testing.T) {
// Upload one local plugin
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
manifest, _, err := th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData))
@@ -1050,13 +1050,13 @@ func TestGetLocalPluginInMarketplace(t *testing.T) {
// Upload one local plugin
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
manifest, _, err := th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData))
require.NoError(t, err)
testIcon, err := ioutil.ReadFile(filepath.Join(path, "test.svg"))
testIcon, err := os.ReadFile(filepath.Join(path, "test.svg"))
require.NoError(t, err)
require.True(t, svg.Is(testIcon))
testIconData := fmt.Sprintf("data:image/svg+xml;base64,%s", base64.StdEncoding.EncodeToString(testIcon))
@@ -1090,13 +1090,13 @@ func TestGetLocalPluginInMarketplace(t *testing.T) {
// Upload one local plugin
path, _ := fileutils.FindDir("tests")
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin.tar.gz"))
require.NoError(t, err)
manifest, _, err := th.SystemAdminClient.UploadPlugin(bytes.NewReader(tarData))
require.NoError(t, err)
testIcon, err := ioutil.ReadFile(filepath.Join(path, "test.svg"))
testIcon, err := os.ReadFile(filepath.Join(path, "test.svg"))
require.NoError(t, err)
require.True(t, svg.Is(testIcon))
testIconData := fmt.Sprintf("data:image/svg+xml;base64,%s", base64.StdEncoding.EncodeToString(testIcon))
@@ -1262,11 +1262,11 @@ func TestInstallMarketplacePlugin(t *testing.T) {
signatureFilename := "testplugin2.tar.gz.sig"
signatureFileReader, err := os.Open(filepath.Join(path, signatureFilename))
require.NoError(t, err)
sigFile, err := ioutil.ReadAll(signatureFileReader)
sigFile, err := io.ReadAll(signatureFileReader)
require.NoError(t, err)
pluginSignature := base64.StdEncoding.EncodeToString(sigFile)
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
require.NoError(t, err)
pluginServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
@@ -1622,7 +1622,7 @@ func TestInstallMarketplacePlugin(t *testing.T) {
th2.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
pluginSignatureFile, err := os.Open(filepath.Join(path, "testplugin.tar.gz.asc"))
require.NoError(t, err)
pluginSignatureData, err := ioutil.ReadAll(pluginSignatureFile)
pluginSignatureData, err := io.ReadAll(pluginSignatureFile)
require.NoError(t, err)
key, err := os.Open(filepath.Join(path, "development-private-key.asc"))

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

@@ -5,7 +5,7 @@ package api4
import (
"encoding/json"
"io/ioutil"
"io"
"mime"
"mime/multipart"
"net/http"
@@ -139,7 +139,7 @@ func addSamlIdpCertificate(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec.AddMeta("type", d)
if d == "application/x-pem-file" {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
c.Err = model.NewAppError("addSamlIdpCertificate", "api.admin.saml.set_certificate_from_metadata.invalid_body.app_error", nil, err.Error(), http.StatusBadRequest)
return

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

@@ -8,7 +8,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
@@ -64,7 +64,7 @@ func TestGetPing(t *testing.T) {
resp, err := client.DoAPIGet("/system/ping", "")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respString := string(respBytes)
require.NotContains(t, respString, "TestFeatureFlag")
@@ -77,7 +77,7 @@ func TestGetPing(t *testing.T) {
resp, err = client.DoAPIGet("/system/ping", "")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
respBytes, err = ioutil.ReadAll(resp.Body)
respBytes, err = io.ReadAll(resp.Body)
require.NoError(t, err)
respString = string(respBytes)
require.Contains(t, respString, "testvalue")
@@ -130,7 +130,7 @@ func TestEmailTest(t *testing.T) {
defer th.TearDown()
client := th.Client
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(dir)
@@ -817,7 +817,7 @@ func TestPushNotificationAck(t *testing.T) {
resp := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/api/v4/notifications/ack", nil)
req.Header.Set(model.HeaderAuth, "Bearer "+session.Token)
req.Body = ioutil.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"id":"123", "is_id_loaded":true, "post_id":"%s", "type": "%s"}`, privatePost.Id, model.PushTypeMessage)))
req.Body = io.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"id":"123", "is_id_loaded":true, "post_id":"%s", "type": "%s"}`, privatePost.Id, model.PushTypeMessage)))
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusForbidden, resp.Code)
@@ -833,11 +833,11 @@ func TestCompleteOnboarding(t *testing.T) {
signatureFilename := "testplugin2.tar.gz.sig"
signatureFileReader, err := os.Open(filepath.Join(path, signatureFilename))
require.NoError(t, err)
sigFile, err := ioutil.ReadAll(signatureFileReader)
sigFile, err := io.ReadAll(signatureFileReader)
require.NoError(t, err)
pluginSignature := base64.StdEncoding.EncodeToString(sigFile)
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
tarData, err := os.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
require.NoError(t, err)
pluginServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)