Skip to content

Commit

Permalink
remove deprecated "ioutil" and fix notes (#622)
Browse files Browse the repository at this point in the history
* remove deprecated "ioutil" and fix notes

* use os.CreateTemp instead of os.MkdirTemp

Co-authored-by: ktr <[email protected]>

---------

Co-authored-by: ktr <[email protected]>
  • Loading branch information
asjdf and ktr0731 authored Feb 6, 2023
1 parent 2856945 commit 5f3b4c5
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 35 deletions.
9 changes: 4 additions & 5 deletions app/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
Expand All @@ -22,9 +21,9 @@ import (
)

// checkUpdate checks whether an update exists. Update checking is instructed by following steps:
// 1. If install means is known, use it as an update means.
// If install means is unknown, checkUpdate selects an available means from candidates.
// 2. Check whether update exists. It it is found, cache the latest version.
// 1. If install means is known, use it as an update means.
// If install means is unknown, checkUpdate selects an available means from candidates.
// 2. Check whether update exists. If it is found, cache the latest version.
func checkUpdate(ctx context.Context, cfg *config.Config, c *cache.Cache) error {
var (
m updater.Means
Expand Down Expand Up @@ -109,7 +108,7 @@ func processUpdate(ctx context.Context, cfg *config.Config, w io.Writer, c *cach
// If auto update is enabled, do process update without user's confirmation.
if cfg.Meta.AutoUpdate {
// If canceled, ignore and return
err := update(ctx, ioutil.Discard, newUpdater(cfg, meta.Version, m), c)
err := update(ctx, io.Discard, newUpdater(cfg, meta.Version, m), c)
if errors.Is(err, context.Canceled) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions app/update_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"
"time"

Expand Down Expand Up @@ -324,7 +324,7 @@ func Test_processUpdate(t *testing.T) {

func Test_update(t *testing.T) {
updater := updater.New(meta.Version, &dummyMeans{dummyMeansBuilderOption: dummyMeansBuilderOption{version: "v1.0.0"}})
err := update(context.Background(), ioutil.Discard, updater, &cache.Cache{SaveFunc: func() error { return nil }})
err := update(context.Background(), io.Discard, updater, &cache.Cache{SaveFunc: func() error { return nil }})
if err != nil {
t.Errorf("update must not return an error, but got '%s'", err)
}
Expand Down
10 changes: 4 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -42,14 +41,13 @@ func init() {
//
// The directory structure is as follows:
//
// - (temp dir): dir
// - config: $XDG_CONFIG_HOME
// - evans: evansCfgDir
//
// ─ (temp dir): dir
// ─ config: $XDG_CONFIG_HOME
// ─ evans: evansCfgDir
func setupEnv(t *testing.T) (string, string, func()) {
cwd := getWorkDir(t)

dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("failed to create a temp dir to setup testing environment: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions config/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -21,12 +21,12 @@ func Test_migration(t *testing.T) {
_, cfgDir, cleanup := setupEnv(t)
defer cleanup()

b, err := ioutil.ReadFile(filepath.Join(oldCWD, "testdata", toFileName(oldVer)))
b, err := os.ReadFile(filepath.Join(oldCWD, "testdata", toFileName(oldVer)))
if err != nil {
t.Fatalf("failed to read a config file, but got '%s'", err)
}

err = ioutil.WriteFile(filepath.Join(cfgDir, "config.toml"), b, 0600)
err = os.WriteFile(filepath.Join(cfgDir, "config.toml"), b, 0600)
if err != nil {
t.Fatalf("failed to copy a config file to a temp config dir, but got '%s'", err)
}
Expand Down
11 changes: 5 additions & 6 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package e2e_test

import (
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -22,13 +22,12 @@ import (

// TestMain prepares the test environment for E2E testing. TestMain do following things for clean up the environment.
//
// - Set log output to ioutil.Discard.
// - Set log output to io.Discard.
// - Remove .evans.toml in this project root.
// - Change $XDG_CONFIG_HOME and $XDG_CACHE_HOME to ignore the root config and cache data.
// These envvars are reset at the end of E2E testing.
//
func TestMain(m *testing.M) {
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)

b, err := exec.Command("git", "rev-parse", "--show-cdup").Output()
if err != nil {
Expand Down Expand Up @@ -108,14 +107,14 @@ func compareWithGolden(t *testing.T, actual string) {
fname := normalizeFilename(name)

if *update {
if err := ioutil.WriteFile(fname, []byte(actual), 0600); err != nil {
if err := os.WriteFile(fname, []byte(actual), 0600); err != nil {
t.Fatalf("failed to update the golden file: %s", err)
}
return
}

// Load the golden file.
b, err := ioutil.ReadFile(fname)
b, err := os.ReadFile(fname)
if err != nil {
t.Fatalf("failed to load a golden file: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions e2e/e2egen/e2egen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// e2egen generates a new testcase stub from execution result. e2egen accepts following args.
//
// e2egen <file> <Evans flags>
// e2egen <file> <Evans flags>
//
// file is the source code of REPL E2E test. Updated code is written to it. Evans flags are options of Evans.
//
Expand All @@ -20,7 +20,6 @@ import (
"go/printer"
"go/token"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -45,7 +44,7 @@ func realMain() int {

fileName, args := os.Args[1], os.Args[2:]

src, err := ioutil.ReadFile(fileName)
src, err := os.ReadFile(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read the file: %s", err)
return 1
Expand Down
4 changes: 2 additions & 2 deletions fill/proto/interactive_filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -208,7 +208,7 @@ func (r *resolver) resolveField(f *desc.FieldDescriptor) error {
return b, nil
}
} else if r.opts.BytesFromFile {
b, err := ioutil.ReadFile(v)
b, err := os.ReadFile(v)
if err == nil {
return b, nil
}
Expand Down
5 changes: 3 additions & 2 deletions grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"

"crypto/tls"
"crypto/x509"
"io/ioutil"

"github.com/hashicorp/go-multierror"
"github.com/ktr0731/evans/grpc/grpcreflection"
Expand Down Expand Up @@ -121,7 +121,7 @@ func NewClient(addr, serverName string, useReflection, useTLS bool, cacert, cert
} else { // Enable TLS authentication
var tlsCfg tls.Config
if cacert != "" {
b, err := ioutil.ReadFile(cacert)
b, err := os.ReadFile(cacert)
if err != nil {
return nil, errors.Wrap(err, "failed to read the CA certificate")
}
Expand Down Expand Up @@ -308,6 +308,7 @@ func (c *client) NewBidiStream(ctx context.Context, streamDesc *grpc.StreamDesc,
// fqrnToEndpoint converts FullQualifiedRPCName to endpoint
//
// e.g.
//
// pkg_name.svc_name.rpc_name -> /pkg_name.svc_name/rpc_name
func fqrnToEndpoint(fqrn string) (string, error) {
sp := strings.Split(fqrn, ".")
Expand Down
3 changes: 1 addition & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package logger

import (
"io"
"io/ioutil"
"log"
)

Expand Down Expand Up @@ -60,5 +59,5 @@ func Scriptf(format string, f func() []interface{}) {
}

func newDefaultLogger() *log.Logger {
return log.New(ioutil.Discard, "evans: ", 0)
return log.New(io.Discard, "evans: ", 0)
}
3 changes: 1 addition & 2 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -181,7 +180,7 @@ func (r *REPL) printSplash(p string) {

_, err := os.Stat(abs)
if !os.IsNotExist(err) {
b, err := ioutil.ReadFile(abs)
b, err := os.ReadFile(abs)
if err == nil {
r.ui.Output(string(b))
}
Expand Down
3 changes: 1 addition & 2 deletions repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repl
import (
"bytes"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestREPL_printSplash(t *testing.T) {
t.Fatalf("failed to get user home dir: %s", err)
}

f, err := ioutil.TempFile(home, "")
f, err := os.CreateTemp(home, "")
if err != nil {
t.Fatalf("failed to create a temp file: %s", err)
}
Expand Down

0 comments on commit 5f3b4c5

Please sign in to comment.