Skip to content

Commit

Permalink
chore(ioutil): move from io/ioutil to io and os packages (ChainSafe#2030
Browse files Browse the repository at this point in the history
)

* refactor: move from io/ioutil to io and os packages

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* ci: use Go 1.17 for check-description

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* refactor(test): use t.tempDir() for auto cleanup

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored and timwu20 committed Dec 6, 2021
1 parent 57cb6b6 commit 73daafb
Show file tree
Hide file tree
Showing 45 changed files with 104 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
if: ${{ github.actor != 'dependabot[bot]' }}
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: "1.17.x"
- uses: actions/checkout@v2
- name: Checks PR has title and body description
run: |
Expand Down
4 changes: 2 additions & 2 deletions cmd/gossamer/import_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/ChainSafe/gossamer/lib/genesis"
Expand All @@ -15,7 +15,7 @@ import (
var defaultGenesisSpecPath = "./chain/gssmr/genesis-spec.json"

func createGenesisWithRuntime(fp string) (string, error) {
runtime, err := ioutil.ReadFile(filepath.Clean(fp))
runtime, err := os.ReadFile(filepath.Clean(fp))
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/gossamer/import_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

Expand All @@ -20,11 +19,11 @@ func TestCreateGenesisWithRuntime(t *testing.T) {

testCode := []byte("somecode")
testHex := common.BytesToHex(testCode)
testFile, err := ioutil.TempFile("", "testcode-*.wasm")
testFile, err := os.CreateTemp("", "testcode-*.wasm")
require.NoError(t, err)
defer os.Remove(testFile.Name())

err = ioutil.WriteFile(testFile.Name(), testCode, 0777)
err = os.WriteFile(testFile.Name(), testCode, 0777)
require.NoError(t, err)

out, err := createGenesisWithRuntime(testFile.Name())
Expand Down
10 changes: 3 additions & 7 deletions cmd/gossamer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sync"
Expand Down Expand Up @@ -91,7 +90,7 @@ func (tt *TestExecCommand) Run(name string, args ...string) {
func (tt *TestExecCommand) ExpectExit() {
var output []byte
tt.withKillTimeout(func() {
output, _ = ioutil.ReadAll(tt.stdout)
output, _ = io.ReadAll(tt.stdout)
})
tt.WaitExit()
if tt.Cleanup != nil {
Expand All @@ -104,7 +103,7 @@ func (tt *TestExecCommand) ExpectExit() {

func (tt *TestExecCommand) GetOutput() (stdout []byte, stderr []byte) {
tt.withSigTimeout(func() {
stdout, _ = ioutil.ReadAll(tt.stdout)
stdout, _ = io.ReadAll(tt.stdout)
stderr = tt.stderr.buf.Bytes()
})
tt.WaitExit()
Expand Down Expand Up @@ -220,8 +219,7 @@ func TestInvalidCommand(t *testing.T) {
func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
genesisPath := utils.GetGssmrGenesisRawPath()

tempDir, err := ioutil.TempDir("", "gossamer-maintest-")
require.Nil(t, err)
tempDir := t.TempDir()

nodeName := dot.RandomNodeName()
init := runTestGossamer(t,
Expand All @@ -234,7 +232,6 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
)

stdout, stderr := init.GetOutput()
require.Nil(t, err)

t.Log("init gossamer output, ", "stdout", string(stdout), "stderr", string(stderr))

Expand All @@ -250,7 +247,6 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
)

stdout, stderr = init.GetOutput()
require.Nil(t, err)

t.Log("init gossamer output, ", "stdout", string(stdout), "stderr", string(stderr))

Expand Down
3 changes: 1 addition & 2 deletions cmd/gossamer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -110,7 +109,7 @@ func newTestConfig(t *testing.T) *dot.Config {
func newTestConfigWithFile(t *testing.T) (*dot.Config, *os.File) {
cfg := newTestConfig(t)

file, err := ioutil.TempFile(cfg.Global.BasePath, "config-")
file, err := os.CreateTemp(cfg.Global.BasePath, "config-")
require.NoError(t, err)

tomlCfg := dotConfigToToml(cfg)
Expand Down
6 changes: 3 additions & 3 deletions dot/build_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dot
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -62,7 +62,7 @@ func TestBuildFromGenesis_WhenGenesisDoesNotExists(t *testing.T) {
}

func TestWriteGenesisSpecFileWhenFileAlreadyExists(t *testing.T) {
f, err := ioutil.TempFile("", "existing file data")
f, err := os.CreateTemp("", "existing file data")
require.NoError(t, err)
defer os.Remove(f.Name())

Expand Down Expand Up @@ -105,7 +105,7 @@ func TestWriteGenesisSpecFile(t *testing.T) {
require.NoError(t, err)
defer file.Close()

genesisBytes, err := ioutil.ReadAll(file)
genesisBytes, err := io.ReadAll(file)
require.NoError(t, err)

gen := new(genesis.Genesis)
Expand Down
7 changes: 3 additions & 4 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package core
import (
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"sort"
Expand Down Expand Up @@ -549,7 +548,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
err = s.blockState.HandleRuntimeChanges(ts, parentRt, bhash1)
require.NoError(t, err)

testRuntime, err := ioutil.ReadFile(updateNodeRuntimeWasmPath)
testRuntime, err := os.ReadFile(updateNodeRuntimeWasmPath)
require.NoError(t, err)

ts.Set(common.CodeKey, testRuntime)
Expand Down Expand Up @@ -578,7 +577,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
func TestService_HandleCodeSubstitutes(t *testing.T) {
s := NewTestService(t, nil)

testRuntime, err := ioutil.ReadFile(runtime.POLKADOT_RUNTIME_FP)
testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP)
require.NoError(t, err)

blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution
Expand Down Expand Up @@ -626,7 +625,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
require.NoError(t, err)
require.Equal(t, codeHashBefore, parentRt.GetCodeHash()) // codeHash should remain unchanged after code substitute

testRuntime, err := ioutil.ReadFile(runtime.POLKADOT_RUNTIME_FP)
testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP)
require.NoError(t, err)

ts, err = s.storageState.TrieState(nil)
Expand Down
7 changes: 3 additions & 4 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package core

import (
"io/ioutil"
"path/filepath"
"testing"

Expand Down Expand Up @@ -45,8 +44,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
cfg.LogLvl = 3

var stateSrvc *state.Service
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)
testDatadirPath := t.TempDir()

gen, genTrie, genHeader := genesis.NewTestGenesisWithTrieAndHeader(t)

Expand All @@ -58,7 +56,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
stateSrvc = state.NewService(config)
stateSrvc.UseMemDB()

err = stateSrvc.Initialise(gen, genHeader, genTrie)
err := stateSrvc.Initialise(gen, genHeader, genTrie)
require.Nil(t, err)

err = stateSrvc.Start()
Expand Down Expand Up @@ -88,6 +86,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
if cfg.Runtime == nil {
rtCfg := &wasmer.Config{}

var err error
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)

Expand Down
6 changes: 2 additions & 4 deletions dot/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package digest

import (
"io/ioutil"
"math/big"
"testing"
"time"
Expand All @@ -23,8 +22,7 @@ import (
)

func newTestHandler(t *testing.T) *Handler {
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)
testDatadirPath := t.TempDir()

config := state.Config{
Path: testDatadirPath,
Expand All @@ -34,7 +32,7 @@ func newTestHandler(t *testing.T) *Handler {
stateSrvc.UseMemDB()

gen, genTrie, genHeader := genesis.NewTestGenesisWithTrieAndHeader(t)
err = stateSrvc.Initialise(gen, genHeader, genTrie)
err := stateSrvc.Initialise(gen, genHeader, genTrie)
require.NoError(t, err)

err = stateSrvc.Start()
Expand Down
6 changes: 3 additions & 3 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dot
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"

"github.com/ChainSafe/gossamer/dot/state"
Expand Down Expand Up @@ -41,7 +41,7 @@ func ImportState(basepath, stateFP, headerFP string, firstSlot uint64) error {
}

func newTrieFromPairs(filename string) (*trie.Trie, error) {
data, err := ioutil.ReadFile(filepath.Clean(filename))
data, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func newTrieFromPairs(filename string) (*trie.Trie, error) {
}

func newHeaderFromFile(filename string) (*types.Header, error) {
data, err := ioutil.ReadFile(filepath.Clean(filename))
data, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, err
}
Expand Down
13 changes: 6 additions & 7 deletions dot/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package dot

import (
"encoding/json"
"io/ioutil"
"math/big"
"os"
"testing"

"github.com/ChainSafe/gossamer/dot/types"
Expand All @@ -20,7 +20,7 @@ import (
func setupStateFile(t *testing.T) string {
filename := "../lib/runtime/test_data/kusama/block1482002.out"

data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
require.NoError(t, err)

rpcPairs := make(map[string]interface{})
Expand All @@ -32,7 +32,7 @@ func setupStateFile(t *testing.T) string {
require.NoError(t, err)

fp := "./test_data/state.json"
err = ioutil.WriteFile(fp, bz, 0777)
err = os.WriteFile(fp, bz, 0777)
require.NoError(t, err)

return fp
Expand All @@ -41,7 +41,7 @@ func setupStateFile(t *testing.T) string {
func setupHeaderFile(t *testing.T) string {
headerStr := "{\"digest\":{\"logs\":[\"0x0642414245b501013c0000009659bd0f0000000070edad1c9064fff78cb18435223d8adaf5ea04c24b1a8766e3dc01eb03cc6a0c11b79793d4e31cc0990838229c44fed1669a7c7c79e1e6d0a96374d6496728069d1ef739e290497a0e3b728fa88fcbdd3a5504e0efde0242e7a806dd4fa9260c\",\"0x054241424501019e7f28dddcf27c1e6b328d5694c368d5b2ec5dbe0e412ae1c98f88d53be4d8502fac571f3f19c9caaf281a673319241e0c5095a683ad34316204088a36a4bd86\"]},\"extrinsicsRoot\":\"0xda26dc8c1455f8f81cae12e4fc59e23ce961b2c837f6d3f664283af906d344e0\",\"number\":\"0x169d12\",\"parentHash\":\"0x3b45c9c22dcece75a30acc9c2968cb311e6b0557350f83b430f47559db786975\",\"stateRoot\":\"0x09f9ca28df0560c2291aa16b56e15e07d1e1927088f51356d522722aa90ca7cb\"}"
fp := "./test_data/header.json"
err := ioutil.WriteFile(fp, []byte(headerStr), 0777)
err := os.WriteFile(fp, []byte(headerStr), 0777)
require.NoError(t, err)
return fp
}
Expand Down Expand Up @@ -78,8 +78,7 @@ func TestNewHeaderFromFile(t *testing.T) {
}

func TestImportState(t *testing.T) {
basepath, err := ioutil.TempDir("", "gossamer-test-*")
require.NoError(t, err)
basepath := t.TempDir()

cfg := NewTestConfig(t)
require.NotNil(t, cfg)
Expand All @@ -92,7 +91,7 @@ func TestImportState(t *testing.T) {
cfg.Init.Genesis = genFile.Name()

cfg.Global.BasePath = basepath
err = InitNode(cfg)
err := InitNode(cfg)
require.NoError(t, err)

stateFP := setupStateFile(t)
Expand Down
3 changes: 1 addition & 2 deletions dot/network/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"os"
"path"
Expand Down Expand Up @@ -78,7 +77,7 @@ func loadKey(fp string) (crypto.PrivKey, error) {
if _, err := os.Stat(pth); os.IsNotExist(err) {
return nil, nil
}
keyData, err := ioutil.ReadFile(filepath.Clean(pth))
keyData, err := os.ReadFile(filepath.Clean(pth))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"testing"
Expand Down Expand Up @@ -279,7 +278,7 @@ func PostRequest(t *testing.T, url string, data io.Reader) (int, []byte) {

defer res.Body.Close()

resBody, err := ioutil.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
require.NoError(t, err)

responseData := new(bytes.Buffer)
Expand Down
6 changes: 2 additions & 4 deletions dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package modules

import (
"io/ioutil"
"math/big"
"path/filepath"
"testing"
Expand Down Expand Up @@ -338,8 +337,7 @@ func TestChainGetFinalizedHeadByRound(t *testing.T) {
}

func newTestStateService(t *testing.T) *state.Service {
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)
testDatadirPath := t.TempDir()

config := state.Config{
Path: testDatadirPath,
Expand All @@ -350,7 +348,7 @@ func newTestStateService(t *testing.T) *state.Service {

gen, genTrie, genesisHeader := genesis.NewTestGenesisWithTrieAndHeader(t)

err = stateSrvc.Initialise(gen, genesisHeader, genTrie)
err := stateSrvc.Initialise(gen, genesisHeader, genTrie)
require.NoError(t, err)

err = stateSrvc.Start()
Expand Down
Loading

0 comments on commit 73daafb

Please sign in to comment.