Skip to content

Commit

Permalink
Remove ioutil (#181)
Browse files Browse the repository at this point in the history
In 1.16, it's now deprecated and replaced by `io` and `os`.  Let's
upgrade!

Test plan:
- make check
- git grep ioutil
  • Loading branch information
benjaminjkraft authored Mar 22, 2022
1 parent 36e86cf commit 000f311
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package generate
import (
_ "embed"
"go/token"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -93,7 +92,7 @@ func (c *Config) ValidateAndFillDefaults(baseDir string) error {
// ReadAndValidateConfig reads the configuration from the given file, validates
// it, and returns it.
func ReadAndValidateConfig(filename string) (*Config, error) {
text, err := ioutil.ReadFile(filename)
text, err := os.ReadFile(filename)
if err != nil {
return nil, errorf(nil, "unreadable config file %v: %v", filename, err)
}
Expand Down
9 changes: 4 additions & 5 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package generate

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -24,7 +23,7 @@ const (
func buildGoFile(namePrefix string, content []byte) error {
// We need to put this within the current module, rather than in
// /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 {
return err
}
Expand Down Expand Up @@ -61,7 +60,7 @@ func buildGoFile(namePrefix string, content []byte) error {
// update the snapshots. Make sure to check that the output is sensible; the
// snapshots don't even get compiled!
func TestGenerate(t *testing.T) {
files, err := ioutil.ReadDir(dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -128,7 +127,7 @@ func getDefaultConfig(t *testing.T) *Config {
// Parse the config that `genqlient --init` generates, to make sure that
// works.
var config Config
b, err := ioutil.ReadFile("default_genqlient.yaml")
b, err := os.ReadFile("default_genqlient.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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 work in both cases.
func TestGenerateErrors(t *testing.T) {
files, err := ioutil.ReadDir(errorsDir)
files, err := os.ReadDir(errorsDir)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package generate

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -41,7 +40,7 @@ func readConfigGenerateAndWrite(configFilename string) error {
filename, err)
}

err = ioutil.WriteFile(filename, content, 0o644)
err = os.WriteFile(filename, content, 0o644)
if err != nil {
return errorf(nil, "could not write generated file %v: %v",
filename, err)
Expand Down
6 changes: 3 additions & 3 deletions generate/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
goAst "go/ast"
goParser "go/parser"
goToken "go/token"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -25,7 +25,7 @@ func getSchema(globs StringList) (*ast.Schema, error) {

sources := make([]*ast.Source, len(filenames))
for i, filename := range filenames {
text, err := ioutil.ReadFile(filename)
text, err := os.ReadFile(filename)
if err != nil {
return nil, errorf(nil, "unreadable schema file %v: %v", filename, err)
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func getQueries(basedir string, globs StringList) (*ast.QueryDocument, error) {
}

for _, filename := range filenames {
text, err := ioutil.ReadFile(filename)
text, err := os.ReadFile(filename)
if err != nil {
return nil, errorf(nil, "unreadable query-spec file %v: %v", filename, err)
}
Expand Down
4 changes: 2 additions & 2 deletions graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/vektah/gqlparser/v2/gqlerror"
Expand Down Expand Up @@ -118,7 +118,7 @@ func (c *client) MakeRequest(ctx context.Context, opName string, query string, r

if resp.StatusCode != http.StatusOK {
var respBody []byte
respBody, err = ioutil.ReadAll(resp.Body)
respBody, err = io.ReadAll(resp.Body)
if err != nil {
respBody = []byte(fmt.Sprintf("<unreadable: %v>", err))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/integration/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -29,13 +29,13 @@ func (t *lastResponseTransport) RoundTrip(req *http.Request) (*http.Response, er
return resp, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("roundtrip failed: unreadable body: %w", err)
}
t.lastResponseBody = body
// Restore the body for the next reader:
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
resp.Body = io.NopCloser(bytes.NewBuffer(body))
return resp, err
}

Expand Down
3 changes: 1 addition & 2 deletions internal/integration/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package integration
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -42,7 +41,7 @@ func RunGenerateTest(t *testing.T, relConfigFilename string) {
}

for filename, content := range generated {
expectedContent, err := ioutil.ReadFile(filename)
expectedContent, err := os.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 000f311

Please sign in to comment.