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

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

@@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -25,7 +24,7 @@ func (ae *archiveExtractor) Match(filename string) bool {
}
func (ae *archiveExtractor) Extract(name string, r io.ReadSeeker) (string, error) {
dir, err := ioutil.TempDir(os.TempDir(), "archiver")
dir, err := os.MkdirTemp(os.TempDir(), "archiver")
if err != nil {
return "", fmt.Errorf("error creating temporary file: %v", err)
}
@@ -49,7 +48,7 @@ func (ae *archiveExtractor) Extract(name string, r io.ReadSeeker) (string, error
filename = strings.ReplaceAll(filename, "-", " ")
filename = strings.ReplaceAll(filename, ".", " ")
filename = strings.ReplaceAll(filename, ",", " ")
data, err2 := ioutil.ReadAll(file)
data, err2 := io.ReadAll(file)
if err2 != nil {
return err2
}

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

@@ -10,7 +10,6 @@ package docextractor
import (
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"path"
@@ -63,7 +62,7 @@ func (mpe *mmPreviewExtractor) Extract(filename string, file io.ReadSeeker) (str
if resp.StatusCode != 200 {
return "", errors.New("Unable to generate file preview using mmpreview (The server has replied with an error)")
}
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "unable to read the response from mmpreview")
}

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

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
@@ -33,7 +32,7 @@ func (pe *pdfExtractor) Extract(filename string, r io.ReadSeeker) (out string, o
outErr = errors.New("error extracting pdf text")
}
}()
f, err := ioutil.TempFile(os.TempDir(), "pdflib")
f, err := os.CreateTemp(os.TempDir(), "pdflib")
if err != nil {
return "", fmt.Errorf("error creating temporary file: %v", err)
}

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

@@ -5,7 +5,6 @@ package docextractor
import (
"io"
"io/ioutil"
"unicode"
"unicode/utf8"
)
@@ -47,6 +46,6 @@ func (pe *plainExtractor) Extract(filename string, r io.ReadSeeker) (string, err
}
}
text, _ := ioutil.ReadAll(r)
text, _ := io.ReadAll(r)
return string(runes[0:total]) + string(text), nil
}

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

@@ -6,7 +6,7 @@ package httpservice
import (
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
@@ -108,7 +108,7 @@ func TestHTTPClientWithProxy(t *testing.T) {
require.NoError(t, err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "proxy", string(body))
}

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

@@ -4,7 +4,7 @@
package imageproxy
import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
@@ -85,7 +85,7 @@ func TestAtmosCamoBackend_GetImageDirect(t *testing.T) {
assert.Equal(t, "image/png", contentType)
require.NotNil(t, body)
respBody, _ := ioutil.ReadAll(body)
respBody, _ := io.ReadAll(body)
assert.Equal(t, []byte("1111111111"), respBody)
}

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

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net"
"net/http"
@@ -134,7 +133,7 @@ func (backend *LocalBackend) GetImageDirect(imageURL string) (io.ReadCloser, str
return nil, "", ErrLocalRequestFailed
}
return ioutil.NopCloser(recorder.Body), recorder.Header().Get("Content-Type"), nil
return io.NopCloser(recorder.Body), recorder.Header().Get("Content-Type"), nil
}
func (backend *LocalBackend) ServeImage(w http.ResponseWriter, req *http.Request) {
@@ -176,7 +175,7 @@ func (backend *LocalBackend) ServeImage(w http.ResponseWriter, req *http.Request
if contentType == "" || contentType == "application/octet-stream" || contentType == "binary/octet-stream" {
// try to detect content type
b := bufio.NewReader(resp.Body)
resp.Body = ioutil.NopCloser(b)
resp.Body = io.NopCloser(b)
contentType = peekContentType(b)
}
if resp.ContentLength != 0 && !contentTypeMatches(imageContentTypes, contentType) {

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

@@ -4,7 +4,7 @@
package imageproxy
import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
@@ -60,7 +60,7 @@ func TestLocalBackend_GetImage(t *testing.T) {
assert.Equal(t, "max-age=2592000, private", resp.Header.Get("Cache-Control"))
assert.Equal(t, "10", resp.Header.Get("Content-Length"))
respBody, _ := ioutil.ReadAll(resp.Body)
respBody, _ := io.ReadAll(resp.Body)
assert.Equal(t, []byte("1111111111"), respBody)
})
@@ -190,7 +190,7 @@ func TestLocalBackend_GetImage(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "attachment;filename=\"test.svg\"", resp.Header.Get("Content-Disposition"))
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
require.NoError(t, err)
})
@@ -247,7 +247,7 @@ func TestLocalBackend_GetImageDirect(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "image/png", contentType)
respBody, _ := ioutil.ReadAll(body)
respBody, _ := io.ReadAll(body)
assert.Equal(t, []byte("1111111111"), respBody)
})

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

@@ -7,7 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"path"
@@ -117,7 +117,7 @@ func (rcs *Service) sendFileToRemote(timeout time.Duration, task sendFileTask) (
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

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

@@ -8,7 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
@@ -163,7 +163,7 @@ func (rcs *Service) sendFrameToRemote(timeout time.Duration, rc *model.RemoteClu
return nil, err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

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

@@ -8,7 +8,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
@@ -134,7 +133,7 @@ func (rcs *Service) sendProfileImageToRemote(timeout time.Duration, task sendPro
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
if err != nil {
return err
}

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

@@ -4,7 +4,6 @@
package bleveengine
import (
"io/ioutil"
"os"
"testing"
@@ -37,7 +36,7 @@ func TestBleveEngineTestSuite(t *testing.T) {
}
func (s *BleveEngineTestSuite) setupIndexes() {
indexDir, err := ioutil.TempDir("", "mmbleve")
indexDir, err := os.MkdirTemp("", "mmbleve")
if err != nil {
s.Require().FailNow("Cannot setup bleveengine tests: %s", err.Error())
}

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

@@ -5,7 +5,6 @@ package indexer
import (
"errors"
"io/ioutil"
"os"
"testing"
@@ -33,7 +32,7 @@ func TestBleveIndexer(t *testing.T) {
mockStore.JobStore.On("UpdateOptimistically", job, model.JobStatusInProgress).Return(true, nil)
mockStore.PostStore.On("GetOldestEntityCreationTime").Return(int64(1), errors.New("")) // intentionally return error to return from function
tempDir, err := ioutil.TempDir("", "setupConfigFile")
tempDir, err := os.MkdirTemp("", "setupConfigFile")
require.NoError(t, err)
t.Cleanup(func() {

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

@@ -9,7 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
@@ -108,7 +108,7 @@ func makeTelemetryServiceAndReceiver(t *testing.T, cloudLicense bool) (*Telemetr
pchan := make(chan testTelemetryPayload, 100)
receiver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
var p testTelemetryPayload
@@ -154,8 +154,8 @@ func initializeMocks(cfg *model.Config, cloudLicense bool) (*mocks.ServerIface,
serverIfaceMock.On("Config").Return(cfg)
serverIfaceMock.On("IsLeader").Return(true)
pluginDir, _ := ioutil.TempDir("", "")
webappPluginDir, _ := ioutil.TempDir("", "")
pluginDir, _ := os.MkdirTemp("", "")
webappPluginDir, _ := os.MkdirTemp("", "")
cleanUp := func() {
os.RemoveAll(pluginDir)
os.RemoveAll(webappPluginDir)

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

@@ -10,7 +10,6 @@ import (
_ "embed"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/user"
@@ -257,7 +256,7 @@ func download(url string, limit int64) (string, error) {
}
defer resp.Body.Close()
out, err := ioutil.TempFile("", "*_mattermost.tar.gz")
out, err := os.CreateTemp("", "*_mattermost.tar.gz")
if err != nil {
return "", err
}
@@ -310,7 +309,7 @@ func extractBinary(executablePath string, filename string) error {
if header.Typeflag == tar.TypeReg && header.Name == "mattermost/bin/mattermost" {
permissions := getFilePermissionsOrDefault(executablePath, 0755)
tmpFile, err := ioutil.TempFile(path.Dir(executablePath), "*")
tmpFile, err := os.CreateTemp(path.Dir(executablePath), "*")
if err != nil {
return err
}

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

@@ -6,7 +6,7 @@ package upgrader
import (
"archive/tar"
"compress/gzip"
"io/ioutil"
"io"
"os"
"testing"
@@ -75,12 +75,12 @@ func TestGetCurrentVersionTgzURL(t *testing.T) {
func TestExtractBinary(t *testing.T) {
t.Run("extract from empty file", func(t *testing.T) {
tmpMockTarGz, err := ioutil.TempFile("", "mock_tgz")
tmpMockTarGz, err := os.CreateTemp("", "mock_tgz")
require.NoError(t, err)
defer os.Remove(tmpMockTarGz.Name())
tmpMockTarGz.Close()
tmpMockExecutable, err := ioutil.TempFile("", "mock_exe")
tmpMockExecutable, err := os.CreateTemp("", "mock_exe")
require.NoError(t, err)
defer os.Remove(tmpMockExecutable.Name())
tmpMockExecutable.Close()
@@ -89,7 +89,7 @@ func TestExtractBinary(t *testing.T) {
})
t.Run("extract from empty tar.gz file", func(t *testing.T) {
tmpMockTarGz, err := ioutil.TempFile("", "mock_tgz")
tmpMockTarGz, err := os.CreateTemp("", "mock_tgz")
require.NoError(t, err)
defer os.Remove(tmpMockTarGz.Name())
gz := gzip.NewWriter(tmpMockTarGz)
@@ -98,7 +98,7 @@ func TestExtractBinary(t *testing.T) {
gz.Close()
tmpMockTarGz.Close()
tmpMockExecutable, err := ioutil.TempFile("", "mock_exe")
tmpMockExecutable, err := os.CreateTemp("", "mock_exe")
require.NoError(t, err)
defer os.Remove(tmpMockExecutable.Name())
tmpMockExecutable.Close()
@@ -107,7 +107,7 @@ func TestExtractBinary(t *testing.T) {
})
t.Run("extract from tar.gz without mattermost/bin/mattermost file", func(t *testing.T) {
tmpMockTarGz, err := ioutil.TempFile("", "mock_tgz")
tmpMockTarGz, err := os.CreateTemp("", "mock_tgz")
require.NoError(t, err)
defer os.Remove(tmpMockTarGz.Name())
gz := gzip.NewWriter(tmpMockTarGz)
@@ -123,7 +123,7 @@ func TestExtractBinary(t *testing.T) {
gz.Close()
tmpMockTarGz.Close()
tmpMockExecutable, err := ioutil.TempFile("", "mock_exe")
tmpMockExecutable, err := os.CreateTemp("", "mock_exe")
require.NoError(t, err)
defer os.Remove(tmpMockExecutable.Name())
tmpMockExecutable.Close()
@@ -132,7 +132,7 @@ func TestExtractBinary(t *testing.T) {
})
t.Run("extract from tar.gz with mattermost/bin/mattermost file", func(t *testing.T) {
tmpMockTarGz, err := ioutil.TempFile("", "mock_tgz")
tmpMockTarGz, err := os.CreateTemp("", "mock_tgz")
require.NoError(t, err)
defer os.Remove(tmpMockTarGz.Name())
gz := gzip.NewWriter(tmpMockTarGz)
@@ -148,7 +148,7 @@ func TestExtractBinary(t *testing.T) {
gz.Close()
tmpMockTarGz.Close()
tmpMockExecutable, err := ioutil.TempFile("", "mock_exe")
tmpMockExecutable, err := os.CreateTemp("", "mock_exe")
require.NoError(t, err)
defer os.Remove(tmpMockExecutable.Name())
tmpMockExecutable.Close()
@@ -157,7 +157,7 @@ func TestExtractBinary(t *testing.T) {
tmpMockExecutableAfter, err := os.Open(tmpMockExecutable.Name())
require.NoError(t, err)
defer tmpMockExecutableAfter.Close()
bytes, err := ioutil.ReadAll(tmpMockExecutableAfter)
bytes, err := io.ReadAll(tmpMockExecutableAfter)
require.NoError(t, err)
require.Equal(t, []byte("test"), bytes)
})