Skip to content

Commit

Permalink
all: remove ioutil package
Browse files Browse the repository at this point in the history
Closes #634

Signed-off-by: Tomás Senart <tsenart@gmail.com>
  • Loading branch information
fooofei authored and tsenart committed Jul 19, 2023
1 parent ca2362d commit 51bb5ee
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 33 deletions.
8 changes: 4 additions & 4 deletions attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/tsenart/vegeta/v12/internal/resolver"
Expand Down Expand Up @@ -137,7 +137,7 @@ func attack(opts *attackOpts) (err error) {

var body []byte
if bodyf, ok := files[opts.bodyf]; ok {
if body, err = ioutil.ReadAll(bodyf); err != nil {
if body, err = io.ReadAll(bodyf); err != nil {
return fmt.Errorf("error reading %s: %s", opts.bodyf, err)
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func attack(opts *attackOpts) (err error) {
res := atk.Attack(tr, opts.rate, opts.duration, opts.name)
enc := vegeta.NewEncoder(out)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)

return processAttack(atk, res, enc, sig)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func tlsConfig(insecure bool, certf, keyf string, rootCerts []string) (*tls.Conf
filenames := append([]string{certf, keyf}, rootCerts...)
for _, f := range filenames {
if f != "" {
if files[f], err = ioutil.ReadFile(f); err != nil {
if files[f], err = os.ReadFile(f); err != nil {
return nil, err
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/jsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -42,7 +41,7 @@ func main() {
case "stdout":
_, err = os.Stdout.Write(schema)
default:
err = ioutil.WriteFile(*out, schema, 0644)
err = os.WriteFile(*out, schema, 0644)
}

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package resolver
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestResolver(t *testing.T) {
if err != nil {
t.Fatalf("failed resolver round trip: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read respose body")
}
Expand Down
5 changes: 2 additions & 3 deletions lib/attack_fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package vegeta
import (
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand All @@ -21,7 +20,7 @@ func FuzzAttackerTCP(fuzz []byte) int {
}

// Start server
directory, err := ioutil.TempDir("/tmp", "fuzz")
directory, err := os.MkdirTemp("/tmp", "fuzz")
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -78,7 +77,7 @@ func FuzzAttackerHTTP(fuzz []byte) int {
}

// Start server
directory, err := ioutil.TempDir("/tmp", "fuzz")
directory, err := os.MkdirTemp("/tmp", "fuzz")
if err != nil {
panic(err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -297,7 +296,7 @@ func TestUnixSocket(t *testing.T) {
t.Parallel()
body := []byte("IT'S A UNIX SYSTEM, I KNOW THIS")

socketDir, err := ioutil.TempDir("", "vegata")
socketDir, err := os.MkdirTemp("", "vegata")
if err != nil {
t.Fatal("Failed to create socket dir", err)
}
Expand Down Expand Up @@ -352,10 +351,10 @@ func TestClient(t *testing.T) {
}

client := &http.Client{
Timeout: time.Duration(1 * time.Nanosecond),
Timeout: 1 * time.Nanosecond,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
DialContext: dialer.DialContext,
TLSClientConfig: DefaultTLSConfig,
MaxIdleConnsPerHost: DefaultConnections,
},
Expand Down
4 changes: 2 additions & 2 deletions lib/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vegeta

import (
"io/ioutil"
"io"
"math/rand"
"reflect"
"testing"
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestMetrics_EmptyMetricsCanBeReported(t *testing.T) {
m.Close()

reporter := NewJSONReporter(&m)
if err := reporter(ioutil.Discard); err != nil {
if err := reporter(io.Discard); err != nil {
t.Error(err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/plot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"math"
"sort"
"strconv"
Expand Down Expand Up @@ -167,7 +166,7 @@ func (p *Plot) Close() {
}

// WriteTo writes the HTML plot to the give io.Writer.
func (p Plot) WriteTo(w io.Writer) (n int64, err error) {
func (p *Plot) WriteTo(w io.Writer) (n int64, err error) {
type dygraphsOpts struct {
Title string `json:"title"`
Labels []string `json:"labels,omitempty"`
Expand Down Expand Up @@ -338,7 +337,7 @@ func asset(path string) ([]byte, error) {
if err != nil {
return nil, err
}
return ioutil.ReadAll(file)
return io.ReadAll(file)
}

type countingWriter struct {
Expand Down
9 changes: 5 additions & 4 deletions lib/plot/plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package plot
import (
"bytes"
"flag"
"io/ioutil"
"io"
"math/rand"
"os"
"path/filepath"
"sort"
"testing"
Expand Down Expand Up @@ -49,12 +50,12 @@ func TestPlot(t *testing.T) {
gp := filepath.Join("testdata", filepath.FromSlash(t.Name())+".golden.html")
if *update {
t.Logf("updating %q", gp)
if err := ioutil.WriteFile(gp, b.Bytes(), 0644); err != nil {
if err := os.WriteFile(gp, b.Bytes(), 0644); err != nil {
t.Fatalf("failed to update %q: %s", gp, err)
}
}

g, err := ioutil.ReadFile(gp)
g, err := os.ReadFile(gp)
if err != nil {
t.Fatalf("failed reading %q: %s", gp, err)
}
Expand Down Expand Up @@ -158,7 +159,7 @@ func BenchmarkPlot(b *testing.B) {

b.Run("WriteTo", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = plot.WriteTo(ioutil.Discard)
_, _ = plot.WriteTo(io.Discard)
}
})
}
3 changes: 1 addition & 2 deletions lib/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"math/rand"
"net/http"
"reflect"
Expand Down Expand Up @@ -170,7 +169,7 @@ func BenchmarkResultEncodings(b *testing.B) {
{"csv", NewCSVEncoder, NewCSVDecoder},
{"json", NewJSONEncoder, NewJSONDecoder},
} {
enc := tc.enc(ioutil.Discard)
enc := tc.enc(io.Discard)

b.Run(tc.encoding+"-encode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down
6 changes: 3 additions & 3 deletions lib/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -200,7 +200,7 @@ func (enc TargetEncoder) Encode(t *Target) error {
return enc(t)
}

// NewJSONTargetEncoder returns a TargetEncoder that encods Targets in the JSON format.
// NewJSONTargetEncoder returns a TargetEncoder that encodes Targets in the JSON format.
func NewJSONTargetEncoder(w io.Writer) TargetEncoder {
var jw jwriter.Writer
return func(t *Target) error {
Expand Down Expand Up @@ -310,7 +310,7 @@ func NewHTTPTargeter(src io.Reader, body []byte, hdr http.Header) Targeter {
} else if strings.HasPrefix(line, "#") {
continue
} else if strings.HasPrefix(line, "@") {
if tgt.Body, err = ioutil.ReadFile(line[1:]); err != nil {
if tgt.Body, err = os.ReadFile(line[1:]); err != nil {
return fmt.Errorf("bad body: %s", err)
}
break
Expand Down
7 changes: 3 additions & 4 deletions lib/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand All @@ -17,7 +16,7 @@ import (
func TestTargetRequest(t *testing.T) {
t.Parallel()

body, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 1024*512))
body, err := io.ReadAll(io.LimitReader(rand.Reader, 1024*512))
if err != nil {
t.Fatal(err)
}
Expand All @@ -35,7 +34,7 @@ func TestTargetRequest(t *testing.T) {
}
req, _ := tgt.Request()

reqBody, err := ioutil.ReadAll(req.Body)
reqBody, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -314,7 +313,7 @@ func TestNewHTTPTargeter(t *testing.T) {
}
}

bodyf, err := ioutil.TempFile("", "vegeta-")
bodyf, err := os.CreateTemp("", "vegeta-")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 51bb5ee

Please sign in to comment.