Remove ioutil (#181)

In 1.16, it's now deprecated and replaced by `io` and `os`.  Let's
upgrade!

Test plan:
- make check
- git grep ioutil
This commit is contained in:
Ben Kraft
2022-03-22 12:10:24 -07:00
committed by GitHub
parent 36e86cf97f
commit 000f311254
7 changed files with 15 additions and 19 deletions
+1 -2
View File
@@ -3,7 +3,6 @@ package generate
import ( import (
_ "embed" _ "embed"
"go/token" "go/token"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -93,7 +92,7 @@ func (c *Config) ValidateAndFillDefaults(baseDir string) error {
// ReadAndValidateConfig reads the configuration from the given file, validates // ReadAndValidateConfig reads the configuration from the given file, validates
// it, and returns it. // it, and returns it.
func ReadAndValidateConfig(filename string) (*Config, error) { func ReadAndValidateConfig(filename string) (*Config, error) {
text, err := ioutil.ReadFile(filename) text, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, errorf(nil, "unreadable config file %v: %v", filename, err) return nil, errorf(nil, "unreadable config file %v: %v", filename, err)
} }
+4 -5
View File
@@ -2,7 +2,6 @@ package generate
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -24,7 +23,7 @@ const (
func buildGoFile(namePrefix string, content []byte) error { func buildGoFile(namePrefix string, content []byte) error {
// We need to put this within the current module, rather than in // We need to put this within the current module, rather than in
// /tmp, so that it can access internal/testutil. // /tmp, so that it can access internal/testutil.
f, err := ioutil.TempFile("./testdata/tmp", namePrefix+"_*.go") f, err := os.CreateTemp("./testdata/tmp", namePrefix+"_*.go")
if err != nil { if err != nil {
return err return err
} }
@@ -61,7 +60,7 @@ func buildGoFile(namePrefix string, content []byte) error {
// update the snapshots. Make sure to check that the output is sensible; the // update the snapshots. Make sure to check that the output is sensible; the
// snapshots don't even get compiled! // snapshots don't even get compiled!
func TestGenerate(t *testing.T) { func TestGenerate(t *testing.T) {
files, err := ioutil.ReadDir(dataDir) files, err := os.ReadDir(dataDir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -128,7 +127,7 @@ func getDefaultConfig(t *testing.T) *Config {
// Parse the config that `genqlient --init` generates, to make sure that // Parse the config that `genqlient --init` generates, to make sure that
// works. // works.
var config Config var config Config
b, err := ioutil.ReadFile("default_genqlient.yaml") b, err := os.ReadFile("default_genqlient.yaml")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -241,7 +240,7 @@ func TestGenerateWithConfig(t *testing.T) {
// line numbers, etc. We include both .go and .graphql tests, to make sure the // line numbers, etc. We include both .go and .graphql tests, to make sure the
// line numbers work in both cases. // line numbers work in both cases.
func TestGenerateErrors(t *testing.T) { func TestGenerateErrors(t *testing.T) {
files, err := ioutil.ReadDir(errorsDir) files, err := os.ReadDir(errorsDir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+1 -2
View File
@@ -5,7 +5,6 @@ package generate
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -41,7 +40,7 @@ func readConfigGenerateAndWrite(configFilename string) error {
filename, err) filename, err)
} }
err = ioutil.WriteFile(filename, content, 0o644) err = os.WriteFile(filename, content, 0o644)
if err != nil { if err != nil {
return errorf(nil, "could not write generated file %v: %v", return errorf(nil, "could not write generated file %v: %v",
filename, err) filename, err)
+3 -3
View File
@@ -5,7 +5,7 @@ import (
goAst "go/ast" goAst "go/ast"
goParser "go/parser" goParser "go/parser"
goToken "go/token" goToken "go/token"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@@ -25,7 +25,7 @@ func getSchema(globs StringList) (*ast.Schema, error) {
sources := make([]*ast.Source, len(filenames)) sources := make([]*ast.Source, len(filenames))
for i, filename := range filenames { for i, filename := range filenames {
text, err := ioutil.ReadFile(filename) text, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, errorf(nil, "unreadable schema file %v: %v", filename, err) return nil, errorf(nil, "unreadable schema file %v: %v", filename, err)
} }
@@ -105,7 +105,7 @@ func getQueries(basedir string, globs StringList) (*ast.QueryDocument, error) {
} }
for _, filename := range filenames { for _, filename := range filenames {
text, err := ioutil.ReadFile(filename) text, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, errorf(nil, "unreadable query-spec file %v: %v", filename, err) return nil, errorf(nil, "unreadable query-spec file %v: %v", filename, err)
} }
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"github.com/vektah/gqlparser/v2/gqlerror" "github.com/vektah/gqlparser/v2/gqlerror"
@@ -118,7 +118,7 @@ func (c *client) MakeRequest(ctx context.Context, opName string, query string, r
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
var respBody []byte var respBody []byte
respBody, err = ioutil.ReadAll(resp.Body) respBody, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
respBody = []byte(fmt.Sprintf("<unreadable: %v>", err)) respBody = []byte(fmt.Sprintf("<unreadable: %v>", err))
} }
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"testing" "testing"
@@ -29,13 +29,13 @@ func (t *lastResponseTransport) RoundTrip(req *http.Request) (*http.Response, er
return resp, err return resp, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return resp, fmt.Errorf("roundtrip failed: unreadable body: %w", err) return resp, fmt.Errorf("roundtrip failed: unreadable body: %w", err)
} }
t.lastResponseBody = body t.lastResponseBody = body
// Restore the body for the next reader: // Restore the body for the next reader:
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body)) resp.Body = io.NopCloser(bytes.NewBuffer(body))
return resp, err return resp, err
} }
+1 -2
View File
@@ -3,7 +3,6 @@ package integration
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -42,7 +41,7 @@ func RunGenerateTest(t *testing.T, relConfigFilename string) {
} }
for filename, content := range generated { for filename, content := range generated {
expectedContent, err := ioutil.ReadFile(filename) expectedContent, err := os.ReadFile(filename)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }