Skip to content

Commit 3c5b71b

Browse files
authored
Merge pull request #282 from atoato88/change-deprecated-ioutil-funcs
Replace deprecated funcs on ioutil package
2 parents cf4f629 + 7663603 commit 3c5b71b

File tree

8 files changed

+29
-23
lines changed

8 files changed

+29
-23
lines changed

applylib/testutils/harness.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"flag"
2323
"io"
24-
"io/ioutil"
2524
"os"
2625
"strings"
2726
"testing"
@@ -205,12 +204,12 @@ func (h *Harness) RESTMapper() *restmapper.DeferredDiscoveryRESTMapper {
205204
func (h *Harness) AssertMatchesFile(p string, got string) {
206205
if os.Getenv("WRITE_GOLDEN_OUTPUT") != "" {
207206
// Short-circuit when the output is correct
208-
b, err := ioutil.ReadFile(p)
207+
b, err := os.ReadFile(p)
209208
if err == nil && bytes.Equal(b, []byte(got)) {
210209
return
211210
}
212211

213-
if err := ioutil.WriteFile(p, []byte(got), 0644); err != nil {
212+
if err := os.WriteFile(p, []byte(got), 0644); err != nil {
214213
h.Fatalf("failed to write golden output %s: %v", p, err)
215214
}
216215
h.Errorf("wrote output to %s", p)

mockkubeapiserver/patchresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
23+
"io"
2424
"net/http"
2525
"strconv"
2626

@@ -53,7 +53,7 @@ func (req *patchResource) Run(ctx context.Context, s *MockKubeAPIServer) error {
5353
existingObj = nil
5454
}
5555

56-
bodyBytes, err := ioutil.ReadAll(req.r.Body)
56+
bodyBytes, err := io.ReadAll(req.r.Body)
5757
if err != nil {
5858
return err
5959
}

mockkubeapiserver/postresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package mockkubeapiserver
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424

2525
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -48,7 +48,7 @@ func (req *postResource) Run(ctx context.Context, s *MockKubeAPIServer) error {
4848
return req.writeErrorResponse(http.StatusNotFound)
4949
}
5050

51-
bodyBytes, err := ioutil.ReadAll(req.r.Body)
51+
bodyBytes, err := io.ReadAll(req.r.Body)
5252
if err != nil {
5353
return err
5454
}

mockkubeapiserver/putresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package mockkubeapiserver
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424
"reflect"
2525

@@ -52,7 +52,7 @@ func (req *putResource) Run(ctx context.Context, s *MockKubeAPIServer) error {
5252
return req.writeErrorResponse(http.StatusNotFound)
5353
}
5454

55-
bodyBytes, err := ioutil.ReadAll(req.r.Body)
55+
bodyBytes, err := io.ReadAll(req.r.Body)
5656
if err != nil {
5757
return err
5858
}

pkg/patterns/declarative/pkg/applier/applylib_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package applier
33
import (
44
"bytes"
55
"context"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"path/filepath"
99
"testing"
@@ -125,12 +125,12 @@ func (h *logKubeRequestsHook) BeforeHTTPOperation(op *mockkubeapiserver.HTTPOper
125125
}
126126

127127
if req.Body != nil {
128-
requestBody, err := ioutil.ReadAll(req.Body)
128+
requestBody, err := io.ReadAll(req.Body)
129129
if err != nil {
130130
panic("failed to read request body")
131131
}
132132
entry.Request.Body = string(requestBody)
133-
req.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
133+
req.Body = io.NopCloser(bytes.NewReader(requestBody))
134134
}
135135
h.log.Entries = append(h.log.Entries, entry)
136136
}

pkg/patterns/declarative/pkg/applier/exec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"os/exec"
2625
"strconv"
@@ -117,7 +116,7 @@ func (c *ExecKubectl) Apply(ctx context.Context, opt ApplierOptions) error {
117116
return fmt.Errorf("error building kubeconfig: %w", err)
118117
}
119118

120-
f, err := ioutil.TempFile("", "kubeconfig")
119+
f, err := os.CreateTemp("", "kubeconfig")
121120
if err != nil {
122121
return fmt.Errorf("error creating temp file: %w", err)
123122
}

pkg/test/httprecorder/http_recorder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package httprecorder
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"strings"
99
)
@@ -27,12 +27,12 @@ func (m *HTTPRecorder) RoundTrip(request *http.Request) (*http.Response, error)
2727
}
2828

2929
if request.Body != nil {
30-
requestBody, err := ioutil.ReadAll(request.Body)
30+
requestBody, err := io.ReadAll(request.Body)
3131
if err != nil {
3232
panic("failed to read request body")
3333
}
3434
entry.Request.Body = string(requestBody)
35-
request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
35+
request.Body = io.NopCloser(bytes.NewReader(requestBody))
3636
}
3737

3838
response, err := m.inner.RoundTrip(request)
@@ -54,12 +54,12 @@ func (m *HTTPRecorder) RoundTrip(request *http.Request) (*http.Response, error)
5454
}
5555

5656
if response.Body != nil {
57-
requestBody, err := ioutil.ReadAll(response.Body)
57+
requestBody, err := io.ReadAll(response.Body)
5858
if err != nil {
5959
entry.Response.Body = fmt.Sprintf("<error reading response:%v>", err)
6060
} else {
6161
entry.Response.Body = string(requestBody)
62-
response.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
62+
response.Body = io.NopCloser(bytes.NewReader(requestBody))
6363
}
6464
}
6565
}

pkg/test/testharness/golden.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package testharness
22

33
import (
44
"bytes"
5-
"io/ioutil"
5+
"io/fs"
66
"os"
77
"path/filepath"
88
"testing"
@@ -11,10 +11,18 @@ import (
1111
)
1212

1313
func RunGoldenTests(t *testing.T, basedir string, fn func(h *Harness, dir string)) {
14-
files, err := ioutil.ReadDir(basedir)
14+
entries, err := os.ReadDir(basedir)
1515
if err != nil {
1616
t.Fatalf("ReadDir(%q) failed: %v", basedir, err)
1717
}
18+
files := make([]fs.FileInfo, 0, len(entries))
19+
for _, entry := range entries {
20+
info, err := entry.Info()
21+
if err != nil {
22+
t.Fatalf("failed to get FileInfo %v: %v", info, err)
23+
}
24+
files = append(files, info)
25+
}
1826
count := 0
1927
for _, file := range files {
2028
name := file.Name()
@@ -34,12 +42,12 @@ func RunGoldenTests(t *testing.T, basedir string, fn func(h *Harness, dir string
3442
func (h *Harness) CompareGoldenFile(p string, got string) {
3543
if os.Getenv("WRITE_GOLDEN_OUTPUT") != "" {
3644
// Short-circuit when the output is correct
37-
b, err := ioutil.ReadFile(p)
45+
b, err := os.ReadFile(p)
3846
if err == nil && bytes.Equal(b, []byte(got)) {
3947
return
4048
}
4149

42-
if err := ioutil.WriteFile(p, []byte(got), 0644); err != nil {
50+
if err := os.WriteFile(p, []byte(got), 0644); err != nil {
4351
h.Fatalf("failed to write golden output %s: %v", p, err)
4452
}
4553
h.Errorf("wrote output to %s", p)

0 commit comments

Comments
 (0)