Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package (#10341)
Browse files Browse the repository at this point in the history
## Description

The `io/ioutil` package has been deprecated in Go 1.16 (See https://golang.org/doc/go1.16#ioutil). Since cosmos-sdk has upgraded to Go 1.17 (#9987), this PR replaces the existing `io/ioutil` functions with their new definitions in `io` and `os` packages.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
Juneezee committed Oct 13, 2021
1 parent 33f3b69 commit 40a92a2
Show file tree
Hide file tree
Showing 44 changed files with 97 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9699](https://github.com/cosmos/cosmos-sdk/pull/9699) Add `:`, `.`, `-`, and `_` as allowed characters in the default denom regular expression.
* (genesis) [\#9697](https://github.com/cosmos/cosmos-sdk/pull/9697) Ensure `InitGenesis` returns with non-empty validator set.
* [\#10262](https://github.com/cosmos/cosmos-sdk/pull/10262) Remove unnecessary logging in `x/feegrant` simulation.
* [\#10341](https://github.com/cosmos/cosmos-sdk/pull/10341) Move from `io/ioutil` to `io` and `os` packages.

### Bug Fixes

Expand Down
5 changes: 2 additions & 3 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
Expand Down Expand Up @@ -162,7 +161,7 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options

snapshotInterval := uint64(2)
snapshotTimeout := 1 * time.Minute
snapshotDir, err := ioutil.TempDir("", "baseapp")
snapshotDir, err := os.MkdirTemp("", "baseapp")
require.NoError(t, err)
snapshotStore, err := snapshots.NewStore(dbm.NewMemDB(), snapshotDir)
require.NoError(t, err)
Expand Down Expand Up @@ -934,7 +933,7 @@ func incrementingCounter(t *testing.T, store sdk.KVStore, counterKey []byte, cou
return &sdk.Result{}, nil
}

//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Tx processing - CheckTx, DeliverTx, SimulateTx.
// These tests use the serialized tx as input, while most others will use the
// Check(), Deliver(), Simulate() methods directly.
Expand Down
4 changes: 2 additions & 2 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config_test
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -58,7 +58,7 @@ func TestConfigCmd(t *testing.T) {
cmd.SetOut(b)
cmd.SetArgs([]string{"node"})
cmd.Execute()
out, err := ioutil.ReadAll(b)
out, err := io.ReadAll(b)
require.NoError(t, err)
require.Equal(t, string(out), testNode1+"\n")
}
Expand Down
3 changes: 1 addition & 2 deletions client/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"bytes"
"io/ioutil"
"os"
"text/template"

Expand Down Expand Up @@ -43,7 +42,7 @@ func writeConfigToFile(configFilePath string, config *ClientConfig) error {
return err
}

return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600)
return os.WriteFile(configFilePath, buffer.Bytes(), 0600)
}

// ensureConfigPath creates a directory configPath if it does not exist
Expand Down
4 changes: 2 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -188,7 +188,7 @@ func Test_runAddCmdLedgerDryRun(t *testing.T) {
_, err = kb.Key("testkey")
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
out, err := io.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
} else {
Expand Down
6 changes: 3 additions & 3 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -18,7 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
bip39 "github.com/cosmos/go-bip39"
"github.com/cosmos/go-bip39"
)

func Test_runAddCmdBasic(t *testing.T) {
Expand Down Expand Up @@ -220,7 +220,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
_, err := kb.Key("testkey")
require.NoError(t, err)

out, err := ioutil.ReadAll(b)
out, err := io.ReadAll(b)
require.NoError(t, err)
require.Contains(t, string(out), "name: testkey")
} else {
Expand Down
4 changes: 2 additions & 2 deletions client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package keys

import (
"bufio"
"io/ioutil"
"os"

"github.com/spf13/cobra"

Expand All @@ -24,7 +24,7 @@ func ImportKeyCommand() *cobra.Command {
}
buf := bufio.NewReader(clientCtx.Input)

bz, err := ioutil.ReadFile(args[1])
bz, err := os.ReadFile(args[1])
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keys
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -98,7 +97,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO

keyfile := filepath.Join(kbHome, "key.asc")

require.NoError(t, ioutil.WriteFile(keyfile, []byte(armoredKey), 0644))
require.NoError(t, os.WriteFile(keyfile, []byte(armoredKey), 0644))

defer func() {
_ = os.RemoveAll(kbHome)
Expand Down
4 changes: 2 additions & 2 deletions codec/unknownproto/unknown_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"compress/gzip"
"errors"
"fmt"
"io/ioutil"
"io"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -358,7 +358,7 @@ func extractFileDescMessageDesc(desc descriptorIface) (*descriptor.FileDescripto
if err != nil {
return nil, nil, err
}
protoBlob, err := ioutil.ReadAll(gzr)
protoBlob, err := io.ReadAll(gzr)
if err != nil {
return nil, nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package container_test

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -506,14 +505,14 @@ func TestLogging(t *testing.T) {
var logOut string
var dotGraph string

outfile, err := ioutil.TempFile("", "out")
outfile, err := os.CreateTemp("", "out")
require.NoError(t, err)
stdout := os.Stdout
os.Stdout = outfile
defer func() { os.Stdout = stdout }()
defer os.Remove(outfile.Name())

graphfile, err := ioutil.TempFile("", "graph")
graphfile, err := os.CreateTemp("", "graph")
require.NoError(t, err)
defer os.Remove(graphfile.Name())

Expand All @@ -533,11 +532,11 @@ func TestLogging(t *testing.T) {
require.Contains(t, logOut, "digraph")
require.Contains(t, dotGraph, "digraph")

outfileContents, err := ioutil.ReadFile(outfile.Name())
outfileContents, err := os.ReadFile(outfile.Name())
require.NoError(t, err)
require.Contains(t, string(outfileContents), "digraph")

graphfileContents, err := ioutil.ReadFile(graphfile.Name())
graphfileContents, err := os.ReadFile(graphfile.Name())
require.NoError(t, err)
require.Contains(t, string(graphfileContents), "<svg")
}
3 changes: 1 addition & 2 deletions cosmovisor/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -246,7 +245,7 @@ func (cfg *Config) UpgradeInfo() UpgradeInfo {
if err != nil { // no current directory
goto returnError
}
if bz, err = ioutil.ReadFile(filename); err != nil {
if bz, err = os.ReadFile(filename); err != nil {
goto returnError
}
if err = json.Unmarshal(bz, &u); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cosmovisor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -113,7 +112,7 @@ func doBackup(cfg *Config) error {
if !cfg.UnsafeSkipBackup {
// check if upgrade-info.json is not empty.
var uInfo UpgradeInfo
upgradeInfoFile, err := ioutil.ReadFile(filepath.Join(cfg.Home, "data", "upgrade-info.json"))
upgradeInfoFile, err := os.ReadFile(filepath.Join(cfg.Home, "data", "upgrade-info.json"))
if err != nil {
return fmt.Errorf("error while reading upgrade-info.json: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cosmovisor/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -108,7 +107,7 @@ func GetDownloadURL(info UpgradeInfo) (string, error) {
doc := strings.TrimSpace(info.Info)
// if this is a url, then we download that and try to get a new doc with the real info
if _, err := url.Parse(doc); err == nil {
tmpDir, err := ioutil.TempDir("", "upgrade-manager-reference")
tmpDir, err := os.MkdirTemp("", "upgrade-manager-reference")
if err != nil {
return "", fmt.Errorf("create tempdir for reference file: %w", err)
}
Expand All @@ -119,7 +118,7 @@ func GetDownloadURL(info UpgradeInfo) (string, error) {
return "", fmt.Errorf("downloading reference link %s: %w", doc, err)
}

refBytes, err := ioutil.ReadFile(refPath)
refBytes, err := os.ReadFile(refPath)
if err != nil {
return "", fmt.Errorf("reading downloaded reference: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/hd/fundraiser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -35,7 +35,7 @@ func initFundraiserTestVectors(t *testing.T) []addrData {
// var hdPath string = "m/44'/118'/0'/0/0"
var hdToAddrTable []addrData

b, err := ioutil.ReadFile("testdata/test.json")
b, err := os.ReadFile("testdata/test.json")
if err != nil {
t.Fatalf("could not read fundraiser test vector file (testdata/test.json): %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -682,7 +681,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {

switch {
case err == nil:
keyhash, err = ioutil.ReadFile(keyhashFilePath)
keyhash, err = os.ReadFile(keyhashFilePath)
if err != nil {
return "", fmt.Errorf("failed to read %s: %v", keyhashFilePath, err)
}
Expand Down Expand Up @@ -746,7 +745,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
continue
}

if err := ioutil.WriteFile(dir+"/keyhash", passwordHash, 0555); err != nil {
if err := os.WriteFile(dir+"/keyhash", passwordHash, 0555); err != nil {
return "", err
}

Expand Down
3 changes: 1 addition & 2 deletions server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package server

import (
"fmt"
"io/ioutil"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +48,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
return err
}

genesis, err := ioutil.ReadFile(config.GenesisFile())
genesis, err := os.ReadFile(config.GenesisFile())
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions server/grpc/gogoreflection/serverreflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"reflect"
"sort"
Expand Down Expand Up @@ -219,7 +218,7 @@ func decompress(b []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
}
out, err := ioutil.ReadAll(r)
out, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions server/grpc/grpc_web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/textproto"
"strconv"
Expand Down Expand Up @@ -197,7 +196,7 @@ func (s *GRPCWebTestSuite) makeGrpcRequest(
return nil, Trailer{}, nil, err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
contents, err := io.ReadAll(resp.Body)
if err != nil {
return nil, Trailer{}, nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions server/mock/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mock

import (
"fmt"
"io/ioutil"
"os"

abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -14,7 +13,7 @@ import (
func SetupApp() (abci.Application, func(), error) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "mock")
rootDir, err := ioutil.TempDir("", "mock-sdk")
rootDir, err := os.MkdirTemp("", "mock-sdk")
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit 40a92a2

Please sign in to comment.