From f2181ebb21ff319d198ec3cbd13af50150a74944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E9=94=B4?= Date: Sat, 14 Jan 2023 02:37:06 +0800 Subject: [PATCH 1/2] remove deprecated "ioutil" and fix notes --- app/update.go | 9 ++++----- app/update_test.go | 4 ++-- config/config_test.go | 10 ++++------ config/migrate_test.go | 6 +++--- e2e/e2e_test.go | 11 +++++------ e2e/e2egen/e2egen.go | 5 ++--- fill/proto/interactive_filler.go | 4 ++-- grpc/grpc.go | 5 +++-- logger/logger.go | 3 +-- repl/repl.go | 3 +-- repl/repl_test.go | 3 +-- 11 files changed, 28 insertions(+), 35 deletions(-) diff --git a/app/update.go b/app/update.go index b0873355..f7ee23a8 100644 --- a/app/update.go +++ b/app/update.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "os/signal" "syscall" @@ -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 @@ -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 } diff --git a/app/update_test.go b/app/update_test.go index 49ef400f..e2410187 100644 --- a/app/update_test.go +++ b/app/update_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "testing" "time" @@ -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) } diff --git a/config/config_test.go b/config/config_test.go index 55540b7a..8c65ff27 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -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) } diff --git a/config/migrate_test.go b/config/migrate_test.go index b72ce994..2c951ba5 100644 --- a/config/migrate_test.go +++ b/config/migrate_test.go @@ -1,7 +1,7 @@ package config import ( - "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -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) } diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 3998a1c5..af17d83b 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -2,7 +2,7 @@ package e2e_test import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -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 { @@ -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) } diff --git a/e2e/e2egen/e2egen.go b/e2e/e2egen/e2egen.go index 4b4b3324..ebea4eff 100644 --- a/e2e/e2egen/e2egen.go +++ b/e2e/e2egen/e2egen.go @@ -2,7 +2,7 @@ // // e2egen generates a new testcase stub from execution result. e2egen accepts following args. // -// e2egen +// e2egen // // file is the source code of REPL E2E test. Updated code is written to it. Evans flags are options of Evans. // @@ -20,7 +20,6 @@ import ( "go/printer" "go/token" "io" - "io/ioutil" "os" "strconv" "strings" @@ -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 diff --git a/fill/proto/interactive_filler.go b/fill/proto/interactive_filler.go index fda0f8d4..c7265401 100644 --- a/fill/proto/interactive_filler.go +++ b/fill/proto/interactive_filler.go @@ -3,7 +3,7 @@ package proto import ( "fmt" "io" - "io/ioutil" + "os" "strconv" "strings" @@ -202,7 +202,7 @@ func (r *resolver) resolveField(f *desc.FieldDescriptor) error { case descriptorpb.FieldDescriptorProto_TYPE_BYTES: converter = func(v string) (interface{}, error) { if r.opts.BytesFromFile { - b, err := ioutil.ReadFile(v) + b, err := os.ReadFile(v) if err == nil { return b, nil } diff --git a/grpc/grpc.go b/grpc/grpc.go index 4d2f3d46..f576de2b 100644 --- a/grpc/grpc.go +++ b/grpc/grpc.go @@ -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" @@ -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") } @@ -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, ".") diff --git a/logger/logger.go b/logger/logger.go index e269ec48..68e33178 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -4,7 +4,6 @@ package logger import ( "io" - "io/ioutil" "log" ) @@ -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) } diff --git a/repl/repl.go b/repl/repl.go index 8c182bbf..bd91a893 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "path/filepath" "sort" @@ -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)) } diff --git a/repl/repl_test.go b/repl/repl_test.go index bf5927e6..268a5284 100644 --- a/repl/repl_test.go +++ b/repl/repl_test.go @@ -3,7 +3,6 @@ package repl import ( "bytes" "io" - "io/ioutil" "os" "runtime" "strings" @@ -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.MkdirTemp(home, "") if err != nil { t.Fatalf("failed to create a temp file: %s", err) } From e9be9c412dfff7607370feca89ae24f10fefdaac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=90=E9=94=B4?= Date: Sun, 5 Feb 2023 17:14:21 +0800 Subject: [PATCH 2/2] use os.CreateTemp instead of os.MkdirTemp Co-authored-by: ktr --- repl/repl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repl/repl_test.go b/repl/repl_test.go index 268a5284..8c7e1384 100644 --- a/repl/repl_test.go +++ b/repl/repl_test.go @@ -55,7 +55,7 @@ func TestREPL_printSplash(t *testing.T) { t.Fatalf("failed to get user home dir: %s", err) } - f, err := os.MkdirTemp(home, "") + f, err := os.CreateTemp(home, "") if err != nil { t.Fatalf("failed to create a temp file: %s", err) }