Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package (#1746)
Browse files Browse the repository at this point in the history
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 <[email protected]>

Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
Juneezee and Pantani authored Oct 29, 2021
1 parent 37e44a9 commit d59603e
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 65 deletions.
3 changes: 1 addition & 2 deletions integration/app/cmd_serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package app_test

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -88,7 +87,7 @@ func TestServeStargateWithConfigHome(t *testing.T) {
}

func TestServeStargateWithCustomConfigFile(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "starporttest")
tmpDir, err := os.MkdirTemp("", "starporttest")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

Expand Down
7 changes: 3 additions & 4 deletions integration/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -115,8 +114,8 @@ func ExecRetry() ExecOption {
func (e Env) Exec(msg string, steps step.Steps, options ...ExecOption) (ok bool) {
opts := &execOptions{
ctx: e.ctx,
stdout: ioutil.Discard,
stderr: ioutil.Discard,
stdout: io.Discard,
stderr: io.Discard,
}
for _, o := range options {
o(opts)
Expand Down Expand Up @@ -241,7 +240,7 @@ func (e Env) IsAppServed(ctx context.Context, host chainconfig.Host) error {

// TmpDir creates a new temporary directory.
func (e Env) TmpDir() (path string) {
path, err := ioutil.TempDir("", "integration")
path, err := os.MkdirTemp("", "integration")
require.NoError(e.t, err, "create a tmp dir")
e.t.Cleanup(func() { os.RemoveAll(path) })
return path
Expand Down
4 changes: 2 additions & 2 deletions starport/cmd/network_account_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package starportcmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,7 +66,7 @@ func networkAccountExportHandler(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(path, []byte(privateKey), 0755); err != nil {
if err := os.WriteFile(path, []byte(privateKey), 0755); err != nil {
return err
}
privateKeyPathAbs, err := filepath.Abs(path)
Expand Down
4 changes: 2 additions & 2 deletions starport/cmd/network_account_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package starportcmd

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

"github.com/spf13/cobra"
)
Expand All @@ -25,7 +25,7 @@ func networkAccountImportHandler(cmd *cobra.Command, args []string) error {
return err
}
var name, password, privateKeyPath = args[0], args[1], args[2]
privateKey, err := ioutil.ReadFile(privateKeyPath)
privateKey, err := os.ReadFile(privateKeyPath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions starport/cmd/network_proposal_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package starportcmd
import (
"errors"
"fmt"
"io/ioutil"
"io"

"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/services/networkbuilder"
Expand Down Expand Up @@ -71,7 +71,7 @@ func networkProposalApproveHandler(cmd *cobra.Command, args []string) error {
s.SetText("Verifying proposals...")
s.Start()

err := nb.VerifyProposals(cmd.Context(), chainID, ids, ioutil.Discard)
err := nb.VerifyProposals(cmd.Context(), chainID, ids, io.Discard)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions starport/cmd/network_proposal_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package starportcmd
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,7 +56,7 @@ func networkProposalVerifyHandler(cmd *cobra.Command, args []string) error {
s.Start()

// Check verbose flag
out := ioutil.Discard
out := io.Discard
debugSet, err := cmd.Flags().GetBool(flagDebug)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions starport/pkg/chaincmd/runner/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -225,7 +224,7 @@ func (r Runner) Export(ctx context.Context, exportedFile string) error {
}

// Save the new state
return ioutil.WriteFile(exportedFile, exportedState, 0644)
return os.WriteFile(exportedFile, exportedState, 0644)
}

// EventSelector is used to query events.
Expand Down
5 changes: 2 additions & 3 deletions starport/pkg/chaincmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
Expand Down Expand Up @@ -58,8 +57,8 @@ func Stderr(w io.Writer) Option {
func New(ctx context.Context, chainCmd chaincmd.ChainCmd, options ...Option) (Runner, error) {
runner := Runner{
chainCmd: chainCmd,
stdout: ioutil.Discard,
stderr: ioutil.Discard,
stdout: io.Discard,
stderr: io.Discard,
}

applyOptions(&runner, options)
Expand Down
3 changes: 1 addition & 2 deletions starport/pkg/confile/confile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package confile

import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -28,7 +27,7 @@ func TestAll(t *testing.T) {
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {

file, err := ioutil.TempFile("", "")
file, err := os.CreateTemp("", "")
require.NoError(t, err)
defer func() {
file.Close()
Expand Down
3 changes: 1 addition & 2 deletions starport/pkg/cosmosgen/generate_go.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cosmosgen

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

Expand All @@ -27,7 +26,7 @@ func (g *generator) generateGo() error {
// created a temporary dir to locate generated code under which later only some of them will be moved to the
// app's source code. this also prevents having leftover files in the app's source code or its parent dir -when
// command executed directly there- in case of an interrupt.
tmp, err := ioutil.TempDir("", "")
tmp, err := os.MkdirTemp("", "")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions starport/pkg/cosmosgen/generate_javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cosmosgen

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (g *jsGenerator) generateModule(ctx context.Context, tsprotoPluginPath, app
}

// generate OpenAPI spec.
oaitemp, err := ioutil.TempDir("", "gen-js-openapi-module-spec")
oaitemp, err := os.MkdirTemp("", "gen-js-openapi-module-spec")
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions starport/pkg/cosmosgen/generate_openapi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cosmosgen

import (
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -43,7 +42,7 @@ func generateOpenAPISpec(g *generator) error {
return err
}

dir, err := ioutil.TempDir("", "gen-openapi-module-spec")
dir, err := os.MkdirTemp("", "gen-openapi-module-spec")
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions starport/pkg/dirchange/dirchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"crypto/md5"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
Expand All @@ -27,7 +26,7 @@ func SaveDirChecksum(workdir string, paths []string, checksumSavePath string, ch

// save checksum
checksumFilePath := filepath.Join(checksumSavePath, checksumName)
return ioutil.WriteFile(checksumFilePath, checksum, 0644)
return os.WriteFile(checksumFilePath, checksum, 0644)
}

// HasDirChecksumChanged computes the md5 checksum of the provided paths (directories or files)
Expand Down Expand Up @@ -57,7 +56,7 @@ func HasDirChecksumChanged(workdir string, paths []string, checksumSavePath stri
}

// Compare checksums
savedChecksum, err := ioutil.ReadFile(checksumFilePath)
savedChecksum, err := os.ReadFile(checksumFilePath)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -103,7 +102,7 @@ func checksumFromPaths(workdir string, paths []string) ([]byte, error) {
noFile = false

// write file content
content, err := ioutil.ReadFile(subPath)
content, err := os.ReadFile(subPath)
if err != nil {
return err
}
Expand Down
34 changes: 17 additions & 17 deletions starport/pkg/dirchange/dirchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package dirchange

import (
"crypto/rand"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -37,25 +37,25 @@ func TestHasDirChecksumChanged(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir3)

dir11, err := ioutil.TempDir(dir1, TmpPattern)
dir11, err := os.MkdirTemp(dir1, TmpPattern)
require.NoError(t, err)
dir12, err := ioutil.TempDir(dir1, TmpPattern)
dir12, err := os.MkdirTemp(dir1, TmpPattern)
require.NoError(t, err)
dir21, err := ioutil.TempDir(dir2, TmpPattern)
dir21, err := os.MkdirTemp(dir2, TmpPattern)
require.NoError(t, err)

// Create files
err = ioutil.WriteFile(filepath.Join(dir1, "foo"), []byte("some bytes"), 0644)
err = os.WriteFile(filepath.Join(dir1, "foo"), []byte("some bytes"), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir11, "foo"), randomBytes(15), 0644)
err = os.WriteFile(filepath.Join(dir11, "foo"), randomBytes(15), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir12, "foo"), randomBytes(20), 0644)
err = os.WriteFile(filepath.Join(dir12, "foo"), randomBytes(20), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir21, "foo"), randomBytes(20), 0644)
err = os.WriteFile(filepath.Join(dir21, "foo"), randomBytes(20), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir3, "foo1"), randomBytes(10), 0644)
err = os.WriteFile(filepath.Join(dir3, "foo1"), randomBytes(10), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir3, "foo2"), randomBytes(10), 0644)
err = os.WriteFile(filepath.Join(dir3, "foo2"), randomBytes(10), 0644)
require.NoError(t, err)

// Check checksum
Expand All @@ -68,7 +68,7 @@ func TestHasDirChecksumChanged(t *testing.T) {
// Checksum remains the same if a file is deleted and recreated with the same content
err = os.Remove(filepath.Join(dir1, "foo"))
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(dir1, "foo"), []byte("some bytes"), 0644)
err = os.WriteFile(filepath.Join(dir1, "foo"), []byte("some bytes"), 0644)
require.NoError(t, err)
tmpChecksum, err := checksumFromPaths("", paths)
require.NoError(t, err)
Expand All @@ -92,7 +92,7 @@ func TestHasDirChecksumChanged(t *testing.T) {
require.NotEqual(t, checksum, tmpChecksum)

// Checksum changes if a file is modified
err = ioutil.WriteFile(filepath.Join(dir3, "foo1"), randomBytes(10), 0644)
err = os.WriteFile(filepath.Join(dir3, "foo1"), randomBytes(10), 0644)
require.NoError(t, err)
newChecksum, err := checksumFromPaths("", paths)
require.NoError(t, err)
Expand All @@ -111,13 +111,13 @@ func TestHasDirChecksumChanged(t *testing.T) {
require.Error(t, err)

// SaveDirChecksum saves the checksum in the specified dir
saveDir, err := ioutil.TempDir(tempDir, TmpPattern)
saveDir, err := os.MkdirTemp(tempDir, TmpPattern)
require.NoError(t, err)
defer os.RemoveAll(saveDir)
err = SaveDirChecksum("", paths, saveDir, ChecksumFile)
require.NoError(t, err)
require.FileExists(t, filepath.Join(saveDir, ChecksumFile))
fileContent, err := ioutil.ReadFile(filepath.Join(saveDir, ChecksumFile))
fileContent, err := os.ReadFile(filepath.Join(saveDir, ChecksumFile))
require.NoError(t, err)
require.Equal(t, newChecksum, fileContent)

Expand All @@ -131,7 +131,7 @@ func TestHasDirChecksumChanged(t *testing.T) {
require.False(t, changed)

// Return true if checksum file doesn't exist
newSaveDir, err := ioutil.TempDir(tempDir, TmpPattern)
newSaveDir, err := os.MkdirTemp(tempDir, TmpPattern)
require.NoError(t, err)
defer os.RemoveAll(newSaveDir)
changed, err = HasDirChecksumChanged("", paths, newSaveDir, ChecksumFile)
Expand All @@ -144,7 +144,7 @@ func TestHasDirChecksumChanged(t *testing.T) {
require.True(t, changed)

// Return true if it has been changed
err = ioutil.WriteFile(filepath.Join(dir21, "bar"), randomBytes(20), 0644)
err = os.WriteFile(filepath.Join(dir21, "bar"), randomBytes(20), 0644)
require.NoError(t, err)
changed, err = HasDirChecksumChanged("", paths, saveDir, ChecksumFile)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions starport/pkg/gomodule/gomodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"

"github.com/tendermint/starport/starport/pkg/cmdrunner"
Expand All @@ -22,7 +22,7 @@ var ErrGoModNotFound = errors.New("go.mod not found")

// ParseAt finds and parses go.mod at app's path.
func ParseAt(path string) (*modfile.File, error) {
gomod, err := ioutil.ReadFile(filepath.Join(path, "go.mod"))
gomod, err := os.ReadFile(filepath.Join(path, "go.mod"))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, ErrGoModNotFound
Expand Down
4 changes: 2 additions & 2 deletions starport/pkg/xhttp/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package xhttp
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -20,7 +20,7 @@ func TestResponseJSON(t *testing.T) {
require.Equal(t, http.StatusCreated, resp.StatusCode)
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))

body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
dataJSON, _ := json.Marshal(data)
require.Equal(t, dataJSON, body)
}
Expand Down
Loading

0 comments on commit d59603e

Please sign in to comment.