Skip to content

Commit

Permalink
chore: remove deprecated usage of "io/ioutil" (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarvspr committed Dec 5, 2022
1 parent 8241797 commit 7ca331a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 39 deletions.
3 changes: 1 addition & 2 deletions examples/nats-bench/main.go
Expand Up @@ -16,7 +16,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
Expand Down Expand Up @@ -145,7 +144,7 @@ func main() {

if len(*csvFile) > 0 {
csv := benchmark.CSV()
ioutil.WriteFile(*csvFile, []byte(csv), 0644)
os.WriteFile(*csvFile, []byte(csv), 0644)
fmt.Printf("Saved metric data in csv file %s\n", *csvFile)
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/nats-echo/main.go
Expand Up @@ -17,7 +17,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -208,7 +208,7 @@ func lookupGeo() string {
log.Fatalf("Could not retrieve geo location data: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
g := geo{}
if err := json.Unmarshal(body, &g); err != nil {
log.Fatalf("Error unmarshalling geo: %v", err)
Expand Down
5 changes: 2 additions & 3 deletions js_test.go
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"reflect"
Expand Down Expand Up @@ -69,13 +68,13 @@ func RunServerWithConfig(configFile string) (*server.Server, *server.Options) {

func createConfFile(t *testing.T, content []byte) string {
t.Helper()
conf, err := ioutil.TempFile("", "")
conf, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
fName := conf.Name()
conf.Close()
if err := ioutil.WriteFile(fName, content, 0666); err != nil {
if err := os.WriteFile(fName, content, 0666); err != nil {
os.Remove(fName)
t.Fatalf("Error writing conf file: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions nats.go
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
Expand Down Expand Up @@ -838,7 +837,7 @@ func RootCAs(file ...string) Option {
return func(o *Options) error {
pool := x509.NewCertPool()
for _, f := range file {
rootPEM, err := ioutil.ReadFile(f)
rootPEM, err := os.ReadFile(f)
if err != nil || rootPEM == nil {
return fmt.Errorf("nats: error loading or parsing rootCA file: %v", err)
}
Expand Down Expand Up @@ -5386,7 +5385,7 @@ func userFromFile(userFile string) (string, error) {
return _EMPTY_, fmt.Errorf("nats: %v", err)
}

contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
return _EMPTY_, fmt.Errorf("nats: %v", err)
}
Expand Down Expand Up @@ -5435,7 +5434,7 @@ func expandPath(p string) (string, error) {
}

func nkeyPairFromSeedFile(seedFile string) (nkeys.KeyPair, error) {
contents, err := ioutil.ReadFile(seedFile)
contents, err := os.ReadFile(seedFile)
if err != nil {
return nil, fmt.Errorf("nats: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions nats_test.go
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -1868,13 +1867,13 @@ func TestNkeyAuth(t *testing.T) {

func createTmpFile(t *testing.T, content []byte) string {
t.Helper()
conf, err := ioutil.TempFile("", "")
conf, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
fName := conf.Name()
conf.Close()
if err := ioutil.WriteFile(fName, content, 0666); err != nil {
if err := os.WriteFile(fName, content, 0666); err != nil {
os.Remove(fName)
t.Fatalf("Error writing conf file: %v", err)
}
Expand Down Expand Up @@ -1966,7 +1965,7 @@ func TestNKeyOptionFromSeed(t *testing.T) {
checkErrChannel(t, errCh)

// Now that option is already created, change content of file
ioutil.WriteFile(seedFile, []byte(`xxxxx`), 0666)
os.WriteFile(seedFile, []byte(`xxxxx`), 0666)
ch = make(chan bool, 1)
go rs(ch)

Expand Down
4 changes: 2 additions & 2 deletions test/conn_test.go
Expand Up @@ -19,8 +19,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestServerSecureConnections(t *testing.T) {
// Let's be more TLS correct and verify servername, endpoint etc.
// Now do more advanced checking, verifying servername and using rootCA.
// Setup our own TLSConfig using RootCA from our self signed cert.
rootPEM, err := ioutil.ReadFile("./configs/certs/ca.pem")
rootPEM, err := os.ReadFile("./configs/certs/ca.pem")
if err != nil || rootPEM == nil {
t.Fatalf("failed to read root certificate")
}
Expand Down
5 changes: 2 additions & 3 deletions test/helper_test.go
Expand Up @@ -16,7 +16,6 @@ package test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -135,13 +134,13 @@ func RunBasicJetStreamServer() *server.Server {

func createConfFile(t *testing.T, content []byte) string {
t.Helper()
conf, err := ioutil.TempFile("", "")
conf, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
fName := conf.Name()
conf.Close()
if err := ioutil.WriteFile(fName, content, 0666); err != nil {
if err := os.WriteFile(fName, content, 0666); err != nil {
os.Remove(fName)
t.Fatalf("Error writing conf file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions test/js_test.go
Expand Up @@ -18,7 +18,6 @@ import (
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
mrand "math/rand"
"net"
"net/url"
Expand Down Expand Up @@ -5049,7 +5048,7 @@ func setupJSClusterWithSize(t *testing.T, clusterName string, size int) []*jsSer
o := natsserver.DefaultTestOptions
o.JetStream = true
o.ServerName = fmt.Sprintf("NODE_%d", i)
tdir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("%s_%s-", o.ServerName, clusterName))
tdir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("%s_%s-", o.ServerName, clusterName))
if err != nil {
t.Fatal(err)
}
Expand Down
28 changes: 14 additions & 14 deletions test/object_test.go
Expand Up @@ -19,7 +19,7 @@ import (
"crypto/rand"
"crypto/sha256"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestObjectBasics(t *testing.T) {
}

// Check result.
copy, err := ioutil.ReadAll(result)
copy, err := io.ReadAll(result)
expectOk(t, err)
if !bytes.Equal(copy, blob) {
t.Fatalf("Result not the same")
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestGetObjectDigestMismatch(t *testing.T) {
res, err := obs.Get("A")
expectOk(t, err)
// first read should be successful
data, err := ioutil.ReadAll(res)
data, err := io.ReadAll(res)
expectOk(t, err)
if string(data) != "abc" {
t.Fatalf("Expected result: 'abc'; got: %s", string(data))
Expand All @@ -158,7 +158,7 @@ func TestGetObjectDigestMismatch(t *testing.T) {

res, err = obs.Get("A")
expectOk(t, err)
_, err = ioutil.ReadAll(res)
_, err = io.ReadAll(res)
expectErr(t, err, nats.ErrDigestMismatch)
expectErr(t, res.Error(), nats.ErrDigestMismatch)
}
Expand Down Expand Up @@ -205,27 +205,27 @@ func TestObjectFileBasics(t *testing.T) {
blob := make([]byte, 8*1024*1024+33)
rand.Read(blob)

tmpFile, err := ioutil.TempFile("", "objfile")
tmpFile, err := os.CreateTemp("", "objfile")
expectOk(t, err)
defer os.Remove(tmpFile.Name()) // clean up
err = ioutil.WriteFile(tmpFile.Name(), blob, 0600)
err = os.WriteFile(tmpFile.Name(), blob, 0600)
expectOk(t, err)

_, err = obs.PutFile(tmpFile.Name())
expectOk(t, err)

tmpResult, err := ioutil.TempFile("", "objfileresult")
tmpResult, err := os.CreateTemp("", "objfileresult")
expectOk(t, err)
defer os.Remove(tmpResult.Name()) // clean up

err = obs.GetFile(tmpFile.Name(), tmpResult.Name())
expectOk(t, err)

// Make sure they are the same.
original, err := ioutil.ReadFile(tmpFile.Name())
original, err := os.ReadFile(tmpFile.Name())
expectOk(t, err)

restored, err := ioutil.ReadFile(tmpResult.Name())
restored, err := os.ReadFile(tmpResult.Name())
expectOk(t, err)

if !bytes.Equal(original, restored) {
Expand All @@ -244,7 +244,7 @@ func TestObjectMulti(t *testing.T) {
expectOk(t, err)

numFiles := 0
fis, _ := ioutil.ReadDir(".")
fis, _ := os.ReadDir(".")
for _, fi := range fis {
fn := fi.Name()
// Just grab clean test files.
Expand All @@ -268,10 +268,10 @@ func TestObjectMulti(t *testing.T) {
_, err = result.Info()
expectOk(t, err)

copy, err := ioutil.ReadAll(result)
copy, err := io.ReadAll(result)
expectOk(t, err)

orig, err := ioutil.ReadFile(path.Join(".", "object_test.go"))
orig, err := os.ReadFile(path.Join(".", "object_test.go"))
expectOk(t, err)

if !bytes.Equal(orig, copy) {
Expand Down Expand Up @@ -929,7 +929,7 @@ func TestGetObjectDigestValue(t *testing.T) {

for _, test := range tests {
t.Run(test.inputFile, func(t *testing.T) {
data, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", test.inputFile))
data, err := os.ReadFile(fmt.Sprintf("./testdata/%s", test.inputFile))
expectOk(t, err)
h := sha256.New()
h.Write(data)
Expand Down Expand Up @@ -966,7 +966,7 @@ func TestDecodeObjectDigest(t *testing.T) {

for _, test := range tests {
t.Run(test.expectedFile, func(t *testing.T) {
expected, err := ioutil.ReadFile(fmt.Sprintf("./testdata/%s", test.expectedFile))
expected, err := os.ReadFile(fmt.Sprintf("./testdata/%s", test.expectedFile))
h := sha256.New()
h.Write(expected)
expected = h.Sum(nil)
Expand Down
4 changes: 1 addition & 3 deletions ws.go
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -170,8 +169,7 @@ func (d *wsDecompressor) decompress() ([]byte, error) {
} else {
d.flate.(flate.Resetter).Reset(d, nil)
}
// TODO: When Go 1.15 support is dropped, replace with io.ReadAll()
b, err := ioutil.ReadAll(d.flate)
b, err := io.ReadAll(d.flate)
// Now reset the compressed buffers list
d.bufs = nil
return b, err
Expand Down

0 comments on commit 7ca331a

Please sign in to comment.