Handle ioutil deprecations (#53526)

* replace ioutil.ReadFile -> os.ReadFile

* replace ioutil.ReadAll -> io.ReadAll

* replace ioutil.TempFile -> os.CreateTemp

* replace ioutil.NopCloser -> io.NopCloser

* replace ioutil.WriteFile -> os.WriteFile

* replace ioutil.TempDir -> os.MkdirTemp

* replace ioutil.Discard -> io.Discard
This commit is contained in:
Jo
2022-08-10 13:37:51 +00:00
committed by GitHub
parent 4926767737
commit 062d255124
140 changed files with 462 additions and 492 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
@@ -87,7 +86,7 @@ func runTests(createCases func() []fsTestCase, t *testing.T) {
setupLocalFs := func() {
commonSetup()
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
tempDir = tmpDir
if err != nil {
t.Fatal(err)
@@ -102,7 +101,7 @@ func runTests(createCases func() []fsTestCase, t *testing.T) {
setupLocalFsNestedPath := func() {
commonSetup()
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}

View File

@@ -1,7 +1,6 @@
package fs
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -12,16 +11,16 @@ import (
)
func TestCopyFile(t *testing.T) {
src, err := ioutil.TempFile("", "")
src, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(src.Name())
assert.NoError(t, err)
})
err = ioutil.WriteFile(src.Name(), []byte("Contents"), 0600)
err = os.WriteFile(src.Name(), []byte("Contents"), 0600)
require.NoError(t, err)
dst, err := ioutil.TempFile("", "")
dst, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(dst.Name())
@@ -35,18 +34,18 @@ func TestCopyFile(t *testing.T) {
func TestCopyFile_Permissions(t *testing.T) {
const perms = os.FileMode(0700)
src, err := ioutil.TempFile("", "")
src, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(src.Name())
assert.NoError(t, err)
})
err = ioutil.WriteFile(src.Name(), []byte("Contents"), 0600)
err = os.WriteFile(src.Name(), []byte("Contents"), 0600)
require.NoError(t, err)
err = os.Chmod(src.Name(), perms)
require.NoError(t, err)
dst, err := ioutil.TempFile("", "")
dst, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(dst.Name())
@@ -65,7 +64,7 @@ func TestCopyFile_Permissions(t *testing.T) {
// Test case where destination directory doesn't exist.
func TestCopyFile_NonExistentDestDir(t *testing.T) {
// nolint:gosec
src, err := ioutil.TempFile("", "")
src, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(src.Name())
@@ -82,7 +81,7 @@ func TestCopyRecursive_NonExistentDest(t *testing.T) {
err := os.MkdirAll(filepath.Join(src, "data"), 0750)
require.NoError(t, err)
// nolint:gosec
err = ioutil.WriteFile(filepath.Join(src, "data", "file.txt"), []byte("Test"), 0644)
err = os.WriteFile(filepath.Join(src, "data", "file.txt"), []byte("Test"), 0644)
require.NoError(t, err)
dstParent := t.TempDir()
@@ -101,7 +100,7 @@ func TestCopyRecursive_ExistentDest(t *testing.T) {
err := os.MkdirAll(filepath.Join(src, "data"), 0750)
require.NoError(t, err)
// nolint:gosec
err = ioutil.WriteFile(filepath.Join(src, "data", "file.txt"), []byte("Test"), 0644)
err = os.WriteFile(filepath.Join(src, "data", "file.txt"), []byte("Test"), 0644)
require.NoError(t, err)
dst := t.TempDir()
@@ -139,10 +138,10 @@ func compareDirs(t *testing.T, src, dst string) {
}
// nolint:gosec
srcData, err := ioutil.ReadFile(srcPath)
srcData, err := os.ReadFile(srcPath)
require.NoError(t, err)
// nolint:gosec
dstData, err := ioutil.ReadFile(dstPath)
dstData, err := os.ReadFile(dstPath)
require.NoError(t, err)
require.Equal(t, srcData, dstData)

View File

@@ -1,7 +1,6 @@
package fs
import (
"io/ioutil"
"os"
"testing"
@@ -17,7 +16,7 @@ func TestExists_NonExistent(t *testing.T) {
}
func TestExists_Existent(t *testing.T) {
f, err := ioutil.TempFile("", "")
f, err := os.CreateTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.Remove(f.Name())

View File

@@ -2,7 +2,7 @@ package httpclient
import (
"fmt"
"io/ioutil"
"io"
"strings"
"testing"
@@ -20,14 +20,14 @@ func TestCountBytesReader(t *testing.T) {
for index, tc := range tcs {
t.Run(fmt.Sprintf("Test CountBytesReader %d", index), func(t *testing.T) {
body := ioutil.NopCloser(strings.NewReader(tc.body))
body := io.NopCloser(strings.NewReader(tc.body))
var actualBytesRead int64
readCloser := CountBytesReader(body, func(bytesRead int64) {
actualBytesRead = bytesRead
})
bodyBytes, err := ioutil.ReadAll(readCloser)
bodyBytes, err := io.ReadAll(readCloser)
require.NoError(t, err)
err = readCloser.Close()
require.NoError(t, err)

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
@@ -27,7 +27,7 @@ func TestResponseLimitMiddleware(t *testing.T) {
for _, tc := range tcs {
t.Run(fmt.Sprintf("Test ResponseLimitMiddleware with limit: %d", tc.limit), func(t *testing.T) {
finalRoundTripper := httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req, Body: ioutil.NopCloser(strings.NewReader("dummy"))}, nil
return &http.Response{StatusCode: http.StatusOK, Request: req, Body: io.NopCloser(strings.NewReader("dummy"))}, nil
})
mw := ResponseLimitMiddleware(tc.limit)
@@ -46,7 +46,7 @@ func TestResponseLimitMiddleware(t *testing.T) {
require.NotNil(t, res.Body)
require.NoError(t, res.Body.Close())
bodyBytes, err := ioutil.ReadAll(res.Body)
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
require.EqualError(t, tc.err, err.Error())
} else {

View File

@@ -2,7 +2,7 @@ package httpclientprovider
import (
"bytes"
"io/ioutil"
"io"
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
@@ -18,7 +18,7 @@ func (c *testContext) createRoundTripper(name string) http.RoundTripper {
return &http.Response{
StatusCode: http.StatusOK,
Request: req,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
Body: io.NopCloser(bytes.NewBufferString("")),
}, nil
})
}

View File

@@ -3,7 +3,7 @@ package httpclient
import (
"errors"
"fmt"
"io/ioutil"
"io"
"strings"
"testing"
@@ -23,10 +23,10 @@ func TestMaxBytesReader(t *testing.T) {
}
for _, tc := range tcs {
t.Run(fmt.Sprintf("Test MaxBytesReader with limit: %d", tc.limit), func(t *testing.T) {
body := ioutil.NopCloser(strings.NewReader("dummy"))
body := io.NopCloser(strings.NewReader("dummy"))
readCloser := MaxBytesReader(body, tc.limit)
bodyBytes, err := ioutil.ReadAll(readCloser)
bodyBytes, err := io.ReadAll(readCloser)
if err != nil {
require.EqualError(t, tc.err, err.Error())
} else {

View File

@@ -7,7 +7,6 @@ package log
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
@@ -46,7 +45,7 @@ func init() {
// Use discard by default
format := func(w io.Writer) gokitlog.Logger {
return gokitlog.NewLogfmtLogger(gokitlog.NewSyncWriter(ioutil.Discard))
return gokitlog.NewLogfmtLogger(gokitlog.NewSyncWriter(io.Discard))
}
logger := level.NewFilter(format(os.Stderr), level.AllowInfo())
root = newManager(logger)
@@ -205,9 +204,12 @@ func (cl *ConcreteLogger) New(ctx ...interface{}) *ConcreteLogger {
// name plus additional contextual information, you must use the
// Logger interface New method for it to work as expected.
// Example creating a shared logger:
// requestLogger := log.New("request-logger")
//
// requestLogger := log.New("request-logger")
//
// Example creating a contextual logger:
// contextualLogger := requestLogger.New("username", "user123")
//
// contextualLogger := requestLogger.New("username", "user123")
func New(ctx ...interface{}) *ConcreteLogger {
if len(ctx) == 0 {
return root.New()

View File

@@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"runtime"
@@ -84,7 +84,7 @@ func TestMetrics(t *testing.T) {
ch := make(chan httpResp)
ticker := time.NewTicker(2 * time.Second)
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
buf, err := ioutil.ReadAll(r.Body)
buf, err := io.ReadAll(r.Body)
if err != nil {
t.Logf("Fake HTTP handler received an error: %s", err.Error())
ch <- httpResp{

View File

@@ -3,7 +3,7 @@ package statscollector
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"time"
@@ -107,7 +107,7 @@ func (s *Service) detectPrometheusVariant(ctx context.Context, ds *datasources.D
return "unknown", nil
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
s.log.Error("Failed to read Prometheus build info", "error", err)
return "", err