Skip to content

Commit c29c1c0

Browse files
authoredJan 17, 2022
fix: use ioutil for Go 1.15 and lower (#492)
Since Go 1.16 it has been recommended to no longer use ioutil in new code. We want Gomega to look modern so we removed references to ioutil, which also removes the chance of outdated code being copied. However we got feedback that some users were stuck on Go 1.15 and lower and were broken by this change. We have therefore introduces a "gutil" package that implements similar functions to ioutil and redirect to the right functions appropriate to the version of Go. This will hopefully: - make it look intentional that ioutil is still used (rather than looking like an omission) - reduce the chance of ioutil usage being propagated by copying - limit the possibility of deprecation warnings
1 parent 72e6040 commit c29c1c0

16 files changed

+164
-59
lines changed
 

Diff for: ‎gexec/build.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"runtime"
1414
"strings"
1515
"sync"
16+
17+
"github.com/onsi/gomega/internal/gutil"
1618
)
1719

1820
var (
@@ -221,11 +223,11 @@ func temporaryDirectory() (string, error) {
221223
mu.Lock()
222224
defer mu.Unlock()
223225
if tmpDir == "" {
224-
tmpDir, err = os.MkdirTemp("", "gexec_artifacts")
226+
tmpDir, err = gutil.MkdirTemp("", "gexec_artifacts")
225227
if err != nil {
226228
return "", err
227229
}
228230
}
229231

230-
return os.MkdirTemp(tmpDir, "g")
232+
return gutil.MkdirTemp(tmpDir, "g")
231233
}

Diff for: ‎gexec/build_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
. "github.com/onsi/ginkgo/v2"
99
. "github.com/onsi/gomega"
1010
"github.com/onsi/gomega/gexec"
11+
"github.com/onsi/gomega/internal/gutil"
1112
)
1213

1314
var packagePath = "./_fixture/firefly"
@@ -98,7 +99,7 @@ var _ = Describe(".BuildIn", func() {
9899
BeforeEach(func() {
99100
var err error
100101
original = os.Getenv("GOPATH")
101-
gopath, err = os.MkdirTemp("", "")
102+
gopath, err = gutil.MkdirTemp("", "")
102103
Expect(err).NotTo(HaveOccurred())
103104
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
104105
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
@@ -234,7 +235,7 @@ var _ = Describe(".CompiledTestIn", func() {
234235
BeforeEach(func() {
235236
var err error
236237
original = os.Getenv("GOPATH")
237-
gopath, err = os.MkdirTemp("", "")
238+
gopath, err = gutil.MkdirTemp("", "")
238239
Expect(err).NotTo(HaveOccurred())
239240
copyFile(filepath.Join("_fixture", "firefly", "main.go"), filepath.Join(gopath, "src", target), "main.go")
240241
Expect(os.Setenv("GOPATH", filepath.Join(os.TempDir(), "emptyFakeGopath"))).To(Succeed())
@@ -277,7 +278,7 @@ var _ = Describe(".CompiledTestIn", func() {
277278

278279
func copyFile(source, directory, basename string) {
279280
Expect(os.MkdirAll(directory, 0755)).To(Succeed())
280-
content, err := os.ReadFile(source)
281+
content, err := gutil.ReadFile(source)
281282
Expect(err).NotTo(HaveOccurred())
282-
Expect(os.WriteFile(filepath.Join(directory, basename), content, 0644)).To(Succeed())
283+
Expect(gutil.WriteFile(filepath.Join(directory, basename), content)).To(Succeed())
283284
}

Diff for: ‎ghttp/handlers.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"io"
109
"net/http"
1110
"net/url"
1211
"reflect"
@@ -15,6 +14,7 @@ import (
1514
"github.com/golang/protobuf/proto"
1615
"github.com/onsi/gomega"
1716
. "github.com/onsi/gomega"
17+
"github.com/onsi/gomega/internal/gutil"
1818
"github.com/onsi/gomega/types"
1919
)
2020

@@ -117,7 +117,7 @@ func (g GHTTPWithGomega) VerifyHeaderKV(key string, values ...string) http.Handl
117117
func (g GHTTPWithGomega) VerifyBody(expectedBody []byte) http.HandlerFunc {
118118
return CombineHandlers(
119119
func(w http.ResponseWriter, req *http.Request) {
120-
body, err := io.ReadAll(req.Body)
120+
body, err := gutil.ReadAll(req.Body)
121121
req.Body.Close()
122122
g.gomega.Expect(err).ShouldNot(HaveOccurred())
123123
g.gomega.Expect(body).Should(Equal(expectedBody), "Body Mismatch")
@@ -133,7 +133,7 @@ func (g GHTTPWithGomega) VerifyJSON(expectedJSON string) http.HandlerFunc {
133133
return CombineHandlers(
134134
g.VerifyMimeType("application/json"),
135135
func(w http.ResponseWriter, req *http.Request) {
136-
body, err := io.ReadAll(req.Body)
136+
body, err := gutil.ReadAll(req.Body)
137137
req.Body.Close()
138138
g.gomega.Expect(err).ShouldNot(HaveOccurred())
139139
g.gomega.Expect(body).Should(MatchJSON(expectedJSON), "JSON Mismatch")
@@ -182,7 +182,7 @@ func (g GHTTPWithGomega) VerifyProtoRepresenting(expected proto.Message) http.Ha
182182
return CombineHandlers(
183183
g.VerifyContentType("application/x-protobuf"),
184184
func(w http.ResponseWriter, req *http.Request) {
185-
body, err := io.ReadAll(req.Body)
185+
body, err := gutil.ReadAll(req.Body)
186186
g.gomega.Expect(err).ShouldNot(HaveOccurred())
187187
req.Body.Close()
188188

Diff for: ‎ghttp/test_server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ import (
120120
"sync"
121121

122122
. "github.com/onsi/gomega"
123+
"github.com/onsi/gomega/internal/gutil"
123124
)
124125

125126
func new() *Server {
@@ -268,7 +269,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
268269
} else {
269270
s.rwMutex.Unlock()
270271
if s.GetAllowUnhandledRequests() {
271-
io.ReadAll(req.Body)
272+
gutil.ReadAll(req.Body)
272273
req.Body.Close()
273274
w.WriteHeader(s.GetUnhandledRequestStatusCode())
274275
} else {

Diff for: ‎ghttp/test_server_test.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/golang/protobuf/proto"
1111
"github.com/onsi/gomega/gbytes"
1212
"github.com/onsi/gomega/ghttp/protobuf"
13+
"github.com/onsi/gomega/internal/gutil"
1314

1415
. "github.com/onsi/ginkgo/v2"
1516
. "github.com/onsi/gomega"
@@ -59,7 +60,7 @@ var _ = Describe("TestServer", func() {
5960
Expect(err).ShouldNot(HaveOccurred())
6061
Expect(resp.StatusCode).Should(Equal(200))
6162

62-
body, err := io.ReadAll(resp.Body)
63+
body, err := gutil.ReadAll(resp.Body)
6364
resp.Body.Close()
6465
Expect(err).ShouldNot(HaveOccurred())
6566

@@ -69,7 +70,7 @@ var _ = Describe("TestServer", func() {
6970
Expect(err).ShouldNot(HaveOccurred())
7071
Expect(resp.StatusCode).Should(Equal(200))
7172

72-
body2, err := io.ReadAll(resp.Body)
73+
body2, err := gutil.ReadAll(resp.Body)
7374
resp.Body.Close()
7475
Expect(err).ShouldNot(HaveOccurred())
7576

@@ -101,7 +102,7 @@ var _ = Describe("TestServer", func() {
101102
Expect(err).ShouldNot(HaveOccurred())
102103
Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
103104

104-
data, err := io.ReadAll(resp.Body)
105+
data, err := gutil.ReadAll(resp.Body)
105106
Expect(err).ShouldNot(HaveOccurred())
106107
Expect(data).Should(BeEmpty())
107108
})
@@ -791,7 +792,7 @@ var _ = Describe("TestServer", func() {
791792

792793
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
793794

794-
body, err := io.ReadAll(resp.Body)
795+
body, err := gutil.ReadAll(resp.Body)
795796
Expect(err).ShouldNot(HaveOccurred())
796797
Expect(body).Should(Equal([]byte("sweet")))
797798

@@ -800,7 +801,7 @@ var _ = Describe("TestServer", func() {
800801

801802
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
802803

803-
body, err = io.ReadAll(resp.Body)
804+
body, err = gutil.ReadAll(resp.Body)
804805
Expect(err).ShouldNot(HaveOccurred())
805806
Expect(body).Should(Equal([]byte("sour")))
806807
})
@@ -819,7 +820,7 @@ var _ = Describe("TestServer", func() {
819820
Expect(err).ShouldNot(HaveOccurred())
820821

821822
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
822-
Expect(io.ReadAll(resp.Body)).Should(Equal([]byte("sweet")))
823+
Expect(gutil.ReadAll(resp.Body)).Should(Equal([]byte("sweet")))
823824
Expect(resp.Header.Get("X-Custom-Header")).Should(Equal("my header"))
824825
})
825826
})
@@ -853,7 +854,7 @@ var _ = Describe("TestServer", func() {
853854

854855
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
855856

856-
body, err := io.ReadAll(resp.Body)
857+
body, err := gutil.ReadAll(resp.Body)
857858
Expect(err).ShouldNot(HaveOccurred())
858859
Expect(body).Should(Equal([]byte("tasty")))
859860

@@ -862,7 +863,7 @@ var _ = Describe("TestServer", func() {
862863

863864
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
864865

865-
body, err = io.ReadAll(resp.Body)
866+
body, err = gutil.ReadAll(resp.Body)
866867
Expect(err).ShouldNot(HaveOccurred())
867868
Expect(body).Should(Equal([]byte("treat")))
868869
})
@@ -880,7 +881,7 @@ var _ = Describe("TestServer", func() {
880881

881882
Expect(err).ShouldNot(HaveOccurred())
882883
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
883-
body, err := io.ReadAll(resp.Body)
884+
body, err := gutil.ReadAll(resp.Body)
884885
Expect(err).ShouldNot(HaveOccurred())
885886
Expect(body).Should(BeEmpty())
886887

@@ -904,7 +905,7 @@ var _ = Describe("TestServer", func() {
904905

905906
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
906907

907-
body, err := io.ReadAll(resp.Body)
908+
body, err := gutil.ReadAll(resp.Body)
908909
Expect(err).ShouldNot(HaveOccurred())
909910
Expect(body).Should(MatchJSON("[1,2,3]"))
910911
})
@@ -989,7 +990,7 @@ var _ = Describe("TestServer", func() {
989990

990991
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
991992

992-
body, err := io.ReadAll(resp.Body)
993+
body, err := gutil.ReadAll(resp.Body)
993994
Expect(err).ShouldNot(HaveOccurred())
994995
Expect(body).Should(MatchJSON(`{"Key": "Jim", "Value": "Codes"}`))
995996
})
@@ -1070,7 +1071,7 @@ var _ = Describe("TestServer", func() {
10701071
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
10711072

10721073
var received protobuf.SimpleMessage
1073-
body, err := io.ReadAll(resp.Body)
1074+
body, err := gutil.ReadAll(resp.Body)
10741075
Expect(err).ShouldNot(HaveOccurred())
10751076
err = proto.Unmarshal(body, &received)
10761077
Expect(err).ShouldNot(HaveOccurred())

Diff for: ‎gmeasure/cache.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
10+
"github.com/onsi/gomega/internal/gutil"
911
)
1012

1113
const CACHE_EXT = ".gmeasure-cache"
@@ -66,15 +68,15 @@ List returns a list of all Cached Experiments found in the cache.
6668
*/
6769
func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
6870
var out []CachedExperimentHeader
69-
entries, err := os.ReadDir(cache.Path)
71+
names, err := gutil.ReadDir(cache.Path)
7072
if err != nil {
7173
return out, err
7274
}
73-
for _, entry := range entries {
74-
if filepath.Ext(entry.Name()) != CACHE_EXT {
75+
for _, name := range names {
76+
if filepath.Ext(name) != CACHE_EXT {
7577
continue
7678
}
77-
header, err := cache.readHeader(entry.Name())
79+
header, err := cache.readHeader(name)
7880
if err != nil {
7981
return out, err
8082
}
@@ -87,15 +89,15 @@ func (cache ExperimentCache) List() ([]CachedExperimentHeader, error) {
8789
Clear empties out the cache - this will delete any and all detected cache files in the cache directory. Use with caution!
8890
*/
8991
func (cache ExperimentCache) Clear() error {
90-
entries, err := os.ReadDir(cache.Path)
92+
names, err := gutil.ReadDir(cache.Path)
9193
if err != nil {
9294
return err
9395
}
94-
for _, entry := range entries {
95-
if filepath.Ext(entry.Name()) != CACHE_EXT {
96+
for _, name := range names {
97+
if filepath.Ext(name) != CACHE_EXT {
9698
continue
9799
}
98-
err := os.Remove(filepath.Join(cache.Path, entry.Name()))
100+
err := os.Remove(filepath.Join(cache.Path, name))
99101
if err != nil {
100102
return err
101103
}

Diff for: ‎internal/gutil/post_ioutil.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build go1.16
2+
// +build go1.16
3+
4+
// Package gutil is a replacement for ioutil, which should not be used in new
5+
// code as of Go 1.16. With Go 1.16 and higher, this implementation
6+
// uses the ioutil replacement functions in "io" and "os" with some
7+
// Gomega specifics. This means that we should not get deprecation warnings
8+
// for ioutil when they are added.
9+
package gutil
10+
11+
import (
12+
"io"
13+
"os"
14+
)
15+
16+
func NopCloser(r io.Reader) io.ReadCloser {
17+
return io.NopCloser(r)
18+
}
19+
20+
func ReadAll(r io.Reader) ([]byte, error) {
21+
return io.ReadAll(r)
22+
}
23+
24+
func ReadDir(dirname string) ([]string, error) {
25+
entries, err := os.ReadDir(dirname)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
var names []string
31+
for _, entry := range entries {
32+
names = append(names, entry.Name())
33+
}
34+
35+
return names, nil
36+
}
37+
38+
func ReadFile(filename string) ([]byte, error) {
39+
return os.ReadFile(filename)
40+
}
41+
42+
func MkdirTemp(dir, pattern string) (string, error) {
43+
return os.MkdirTemp(dir, pattern)
44+
}
45+
46+
func WriteFile(filename string, data []byte) error {
47+
return os.WriteFile(filename, data, 0644)
48+
}

Diff for: ‎internal/gutil/using_ioutil.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//go:build !go1.16
2+
// +build !go1.16
3+
4+
// Package gutil is a replacement for ioutil, which should not be used in new
5+
// code as of Go 1.16. With Go 1.15 and lower, this implementation
6+
// uses the ioutil functions, meaning that although Gomega is not officially
7+
// supported on these versions, it is still likely to work.
8+
package gutil
9+
10+
import (
11+
"io"
12+
"io/ioutil"
13+
)
14+
15+
func NopCloser(r io.Reader) io.ReadCloser {
16+
return ioutil.NopCloser(r)
17+
}
18+
19+
func ReadAll(r io.Reader) ([]byte, error) {
20+
return ioutil.ReadAll(r)
21+
}
22+
23+
func ReadDir(dirname string) ([]string, error) {
24+
files, err := ioutil.ReadDir(dirname)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
var names []string
30+
for _, file := range files {
31+
names = append(names, file.Name())
32+
}
33+
34+
return names, nil
35+
}
36+
37+
func ReadFile(filename string) ([]byte, error) {
38+
return ioutil.ReadFile(filename)
39+
}
40+
41+
func MkdirTemp(dir, pattern string) (string, error) {
42+
return ioutil.TempDir(dir, pattern)
43+
}
44+
45+
func WriteFile(filename string, data []byte) error {
46+
return ioutil.WriteFile(filename, data, 0644)
47+
}

Diff for: ‎matchers/be_a_directory_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8+
"github.com/onsi/gomega/internal/gutil"
89
. "github.com/onsi/gomega/matchers"
910
)
1011

@@ -18,7 +19,7 @@ var _ = Describe("BeADirectoryMatcher", func() {
1819
defer os.Remove(tmpFile.Name())
1920
Expect(tmpFile.Name()).ShouldNot(BeADirectory())
2021

21-
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
22+
tmpDir, err := gutil.MkdirTemp("", "gomega-test-tempdir")
2223
Expect(err).ShouldNot(HaveOccurred())
2324
defer os.Remove(tmpDir)
2425
Expect(tmpDir).Should(BeADirectory())

Diff for: ‎matchers/be_a_regular_file_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8+
"github.com/onsi/gomega/internal/gutil"
89
. "github.com/onsi/gomega/matchers"
910
)
1011

@@ -18,7 +19,7 @@ var _ = Describe("BeARegularFileMatcher", func() {
1819
defer os.Remove(tmpFile.Name())
1920
Expect(tmpFile.Name()).Should(BeARegularFile())
2021

21-
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
22+
tmpDir, err := gutil.MkdirTemp("", "gomega-test-tempdir")
2223
Expect(err).ShouldNot(HaveOccurred())
2324
defer os.Remove(tmpDir)
2425
Expect(tmpDir).ShouldNot(BeARegularFile())

Diff for: ‎matchers/be_an_existing_file_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8+
"github.com/onsi/gomega/internal/gutil"
89
. "github.com/onsi/gomega/matchers"
910
)
1011

@@ -18,7 +19,7 @@ var _ = Describe("BeAnExistingFileMatcher", func() {
1819
defer os.Remove(tmpFile.Name())
1920
Expect(tmpFile.Name()).Should(BeAnExistingFile())
2021

21-
tmpDir, err := os.MkdirTemp("", "gomega-test-tempdir")
22+
tmpDir, err := gutil.MkdirTemp("", "gomega-test-tempdir")
2223
Expect(err).ShouldNot(HaveOccurred())
2324
defer os.Remove(tmpDir)
2425
Expect(tmpDir).Should(BeAnExistingFile())

Diff for: ‎matchers/have_http_body_matcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package matchers
22

33
import (
44
"fmt"
5-
"io"
65
"net/http"
76
"net/http/httptest"
87

98
"github.com/onsi/gomega/format"
9+
"github.com/onsi/gomega/internal/gutil"
1010
"github.com/onsi/gomega/types"
1111
)
1212

@@ -81,7 +81,7 @@ func (matcher *HaveHTTPBodyMatcher) body(actual interface{}) ([]byte, error) {
8181
if a.Body != nil {
8282
defer a.Body.Close()
8383
var err error
84-
matcher.cachedBody, err = io.ReadAll(a.Body)
84+
matcher.cachedBody, err = gutil.ReadAll(a.Body)
8585
if err != nil {
8686
return nil, fmt.Errorf("error reading response body: %w", err)
8787
}

Diff for: ‎matchers/have_http_body_matcher_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ package matchers_test
22

33
import (
44
"bytes"
5-
"io"
65
"net/http"
76
"net/http/httptest"
87
"strings"
98

109
. "github.com/onsi/ginkgo/v2"
1110
. "github.com/onsi/gomega"
11+
"github.com/onsi/gomega/internal/gutil"
1212
)
1313

1414
var _ = Describe("HaveHTTPBody", func() {
1515
When("ACTUAL is *http.Response", func() {
1616
It("matches the body", func() {
1717
const body = "this is the body"
18-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
18+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
1919
Expect(resp).To(HaveHTTPBody(body))
2020
})
2121

2222
It("mismatches the body", func() {
2323
const body = "this is the body"
24-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
24+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
2525
Expect(resp).NotTo(HaveHTTPBody("something else"))
2626
})
2727
})
@@ -52,33 +52,33 @@ var _ = Describe("HaveHTTPBody", func() {
5252
When("EXPECTED is []byte", func() {
5353
It("matches the body", func() {
5454
const body = "this is the body"
55-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
55+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
5656
Expect(resp).To(HaveHTTPBody([]byte(body)))
5757
})
5858

5959
It("mismatches the body", func() {
6060
const body = "this is the body"
61-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
61+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
6262
Expect(resp).NotTo(HaveHTTPBody([]byte("something else")))
6363
})
6464
})
6565

6666
When("EXPECTED is a submatcher", func() {
6767
It("matches the body", func() {
68-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(`{"some":"json"}`))}
68+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
6969
Expect(resp).To(HaveHTTPBody(MatchJSON(`{ "some": "json" }`)))
7070
})
7171

7272
It("mismatches the body", func() {
73-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(`{"some":"json"}`))}
73+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
7474
Expect(resp).NotTo(HaveHTTPBody(MatchJSON(`{ "something": "different" }`)))
7575
})
7676
})
7777

7878
When("EXPECTED is something else", func() {
7979
It("errors", func() {
8080
failures := InterceptGomegaFailures(func() {
81-
resp := &http.Response{Body: io.NopCloser(strings.NewReader("body"))}
81+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("body"))}
8282
Expect(resp).To(HaveHTTPBody(map[int]bool{}))
8383
})
8484
Expect(failures).To(HaveLen(1))
@@ -90,7 +90,7 @@ var _ = Describe("HaveHTTPBody", func() {
9090
Context("EXPECTED is string", func() {
9191
It("returns a match failure message", func() {
9292
failures := InterceptGomegaFailures(func() {
93-
resp := &http.Response{Body: io.NopCloser(strings.NewReader("this is the body"))}
93+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("this is the body"))}
9494
Expect(resp).To(HaveHTTPBody("this is a different body"))
9595
})
9696
Expect(failures).To(HaveLen(1))
@@ -104,7 +104,7 @@ to equal
104104
Context("EXPECTED is []byte", func() {
105105
It("returns a match failure message", func() {
106106
failures := InterceptGomegaFailures(func() {
107-
resp := &http.Response{Body: io.NopCloser(strings.NewReader("this is the body"))}
107+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader("this is the body"))}
108108
Expect(resp).To(HaveHTTPBody([]byte("this is a different body")))
109109
})
110110
Expect(failures).To(HaveLen(1))
@@ -118,7 +118,7 @@ to equal
118118
Context("EXPECTED is submatcher", func() {
119119
It("returns a match failure message", func() {
120120
failures := InterceptGomegaFailures(func() {
121-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(`{"some":"json"}`))}
121+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
122122
Expect(resp).To(HaveHTTPBody(MatchJSON(`{"other":"stuff"}`)))
123123
})
124124
Expect(failures).To(HaveLen(1))
@@ -139,7 +139,7 @@ to match JSON of
139139
It("returns a negated failure message", func() {
140140
const body = "this is the body"
141141
failures := InterceptGomegaFailures(func() {
142-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
142+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
143143
Expect(resp).NotTo(HaveHTTPBody(body))
144144
})
145145
Expect(failures).To(HaveLen(1))
@@ -154,7 +154,7 @@ not to equal
154154
It("returns a match failure message", func() {
155155
const body = "this is the body"
156156
failures := InterceptGomegaFailures(func() {
157-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
157+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
158158
Expect(resp).NotTo(HaveHTTPBody([]byte(body)))
159159
})
160160
Expect(failures).To(HaveLen(1))
@@ -169,7 +169,7 @@ not to equal
169169
It("returns a match failure message", func() {
170170
const body = `{"some":"json"}`
171171
failures := InterceptGomegaFailures(func() {
172-
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
172+
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
173173
Expect(resp).NotTo(HaveHTTPBody(MatchJSON(body)))
174174
})
175175
Expect(failures).To(HaveLen(1))

Diff for: ‎matchers/have_http_status_matcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package matchers
22

33
import (
44
"fmt"
5-
"io"
65
"net/http"
76
"net/http/httptest"
87
"reflect"
98
"strings"
109

1110
"github.com/onsi/gomega/format"
11+
"github.com/onsi/gomega/internal/gutil"
1212
)
1313

1414
type HaveHTTPStatusMatcher struct {
@@ -78,7 +78,7 @@ func formatHttpResponse(input interface{}) string {
7878
body := "<nil>"
7979
if resp.Body != nil {
8080
defer resp.Body.Close()
81-
data, err := io.ReadAll(resp.Body)
81+
data, err := gutil.ReadAll(resp.Body)
8282
if err != nil {
8383
data = []byte("<error reading body>")
8484
}

Diff for: ‎matchers/have_http_status_matcher_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package matchers_test
22

33
import (
4-
"io"
54
"net/http"
65
"net/http/httptest"
76
"strings"
87

98
. "github.com/onsi/ginkgo/v2"
109
. "github.com/onsi/gomega"
10+
"github.com/onsi/gomega/internal/gutil"
1111
)
1212

1313
var _ = Describe("HaveHTTPStatus", func() {
@@ -121,7 +121,7 @@ var _ = Describe("HaveHTTPStatus", func() {
121121
resp := &http.Response{
122122
StatusCode: http.StatusBadGateway,
123123
Status: "502 Bad Gateway",
124-
Body: io.NopCloser(strings.NewReader("did not like it")),
124+
Body: gutil.NopCloser(strings.NewReader("did not like it")),
125125
}
126126
Expect(resp).To(HaveHTTPStatus(http.StatusOK))
127127
})
@@ -141,7 +141,7 @@ to have HTTP status
141141
resp := &http.Response{
142142
StatusCode: http.StatusBadGateway,
143143
Status: "502 Bad Gateway",
144-
Body: io.NopCloser(strings.NewReader("did not like it")),
144+
Body: gutil.NopCloser(strings.NewReader("did not like it")),
145145
}
146146
Expect(resp).To(HaveHTTPStatus(http.StatusOK, http.StatusNotFound, "204 No content"))
147147
})
@@ -165,7 +165,7 @@ to have HTTP status
165165
resp := &http.Response{
166166
StatusCode: http.StatusOK,
167167
Status: "200 OK",
168-
Body: io.NopCloser(strings.NewReader("got it!")),
168+
Body: gutil.NopCloser(strings.NewReader("got it!")),
169169
}
170170
Expect(resp).NotTo(HaveHTTPStatus(http.StatusOK))
171171
})
@@ -185,7 +185,7 @@ not to have HTTP status
185185
resp := &http.Response{
186186
StatusCode: http.StatusOK,
187187
Status: "200 OK",
188-
Body: io.NopCloser(strings.NewReader("got it!")),
188+
Body: gutil.NopCloser(strings.NewReader("got it!")),
189189
}
190190
Expect(resp).NotTo(HaveHTTPStatus(http.StatusOK, "204 No content", http.StatusGone))
191191
})

Diff for: ‎matchers/matcher_tests_suite_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package matchers_test
22

33
import (
44
"fmt"
5-
"io"
65
"os"
76
"testing"
87

98
. "github.com/onsi/ginkgo/v2"
109
. "github.com/onsi/gomega"
10+
"github.com/onsi/gomega/internal/gutil"
1111
)
1212

1313
type myStringer struct {
@@ -34,7 +34,7 @@ func Test(t *testing.T) {
3434

3535
func readFileContents(filePath string) []byte {
3636
f := openFile(filePath)
37-
b, err := io.ReadAll(f)
37+
b, err := gutil.ReadAll(f)
3838
if err != nil {
3939
panic(fmt.Errorf("failed to read file contents: %v", err))
4040
}

0 commit comments

Comments
 (0)
Please sign in to comment.