all: replace uses of ioutil with io and os (#24869)

This commit is contained in:
JukLee0ira 2024-09-27 11:00:33 +08:00 committed by Daniel Liu
parent 4f9501f12c
commit 47d27fed3b
6 changed files with 12 additions and 16 deletions

View file

@ -20,7 +20,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"errors" "errors"
"io" "io"
"io/ioutil"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/accounts" "github.com/XinFinOrg/XDPoSChain/accounts"
@ -80,7 +79,7 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
// NewTransactorWithChainID is a utility method to easily create a transaction signer from // NewTransactorWithChainID is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase. // an encrypted json key stream and the associated passphrase.
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
json, err := ioutil.ReadAll(keyin) json, err := io.ReadAll(keyin)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -19,8 +19,8 @@ package rawdb
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"io/ioutil"
"math/big" "math/big"
"os"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -214,7 +214,7 @@ func TestDeriveLogFields(t *testing.T) {
func BenchmarkDecodeRLPLogs(b *testing.B) { func BenchmarkDecodeRLPLogs(b *testing.B) {
// Encoded receipts from block 0x14ee094309fbe8f70b65f45ebcc08fb33f126942d97464aad5eb91cfd1e2d269 // Encoded receipts from block 0x14ee094309fbe8f70b65f45ebcc08fb33f126942d97464aad5eb91cfd1e2d269
buf, err := ioutil.ReadFile("testdata/stored_receipts.bin") buf, err := os.ReadFile("testdata/stored_receipts.bin")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }

View file

@ -26,7 +26,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"io/ioutil"
"math/big" "math/big"
"os" "os"
@ -250,7 +249,7 @@ func checkKeyFileEnd(r *bufio.Reader) error {
// restrictive permissions. The key data is saved hex-encoded. // restrictive permissions. The key data is saved hex-encoded.
func SaveECDSA(file string, key *ecdsa.PrivateKey) error { func SaveECDSA(file string, key *ecdsa.PrivateKey) error {
k := hex.EncodeToString(FromECDSA(key)) k := hex.EncodeToString(FromECDSA(key))
return ioutil.WriteFile(file, []byte(k), 0600) return os.WriteFile(file, []byte(k), 0600)
} }
// GenerateKey generates a new private key. // GenerateKey generates a new private key.

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"reflect" "reflect"
@ -175,7 +174,7 @@ func TestLoadECDSA(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
f, err := ioutil.TempFile("", "loadecdsa_test.*.txt") f, err := os.CreateTemp("", "loadecdsa_test.*.txt")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -196,7 +195,7 @@ func TestLoadECDSA(t *testing.T) {
} }
func TestSaveECDSA(t *testing.T) { func TestSaveECDSA(t *testing.T) {
f, err := ioutil.TempFile("", "saveecdsa_test.*.txt") f, err := os.CreateTemp("", "saveecdsa_test.*.txt")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -20,7 +20,6 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -474,7 +473,7 @@ func RestoreAsset(dir, name string) error {
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) err = os.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil { if err != nil {
return err return err
} }

View file

@ -2,8 +2,8 @@ package testing
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"math/big" "math/big"
"os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
@ -66,7 +66,7 @@ func TestCallTracer(t *testing.T) {
} }
func testCallTracer(tracerName string, dirPath string, t *testing.T) { func testCallTracer(tracerName string, dirPath string, t *testing.T) {
files, err := ioutil.ReadDir(filepath.Join("..", "testdata", dirPath)) files, err := os.ReadDir(filepath.Join("..", "testdata", dirPath))
if err != nil { if err != nil {
t.Fatalf("failed to retrieve tracer test suite: %v", err) t.Fatalf("failed to retrieve tracer test suite: %v", err)
} }
@ -83,7 +83,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
tx = new(types.Transaction) tx = new(types.Transaction)
) )
// Call tracer test found, read if from disk // Call tracer test found, read if from disk
if blob, err := ioutil.ReadFile(filepath.Join("..", "testdata", dirPath, file.Name())); err != nil { if blob, err := os.ReadFile(filepath.Join("..", "testdata", dirPath, file.Name())); err != nil {
t.Fatalf("failed to read testcase: %v", err) t.Fatalf("failed to read testcase: %v", err)
} else if err := json.Unmarshal(blob, test); err != nil { } else if err := json.Unmarshal(blob, test); err != nil {
t.Fatalf("failed to parse testcase: %v", err) t.Fatalf("failed to parse testcase: %v", err)
@ -171,7 +171,7 @@ func camel(str string) string {
return strings.Join(pieces, "") return strings.Join(pieces, "")
} }
func BenchmarkTracers(b *testing.B) { func BenchmarkTracers(b *testing.B) {
files, err := ioutil.ReadDir(filepath.Join("..", "testdata", "call_tracer")) files, err := os.ReadDir(filepath.Join("..", "testdata", "call_tracer"))
if err != nil { if err != nil {
b.Fatalf("failed to retrieve tracer test suite: %v", err) b.Fatalf("failed to retrieve tracer test suite: %v", err)
} }
@ -181,7 +181,7 @@ func BenchmarkTracers(b *testing.B) {
} }
file := file // capture range variable file := file // capture range variable
b.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(b *testing.B) { b.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(b *testing.B) {
blob, err := ioutil.ReadFile(filepath.Join("..", "testdata", "call_tracer", file.Name())) blob, err := os.ReadFile(filepath.Join("..", "testdata", "call_tracer", file.Name()))
if err != nil { if err != nil {
b.Fatalf("failed to read testcase: %v", err) b.Fatalf("failed to read testcase: %v", err)
} }