Skip to content

Commit e274ad6

Browse files
Bump Go and change CI/CD.
Also fix golang/go#65051 and resolve obvious linting problems.
1 parent 895b231 commit e274ad6

File tree

13 files changed

+124
-139
lines changed

13 files changed

+124
-139
lines changed

.github/workflows/ci.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Build and test
5+
6+
on:
7+
push:
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: '1.19'
20+
21+
- name: Build
22+
run: go build -v ./...
23+
24+
- name: Test
25+
run: go test -v ./...

.travis.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"net/http"
1413
"net/url"
1514
"reflect"
@@ -251,7 +250,7 @@ func UnmarshalJSONResponse(resp *http.Response, x interface{}) error {
251250
// connection, but don't try *too* hard. Note that the
252251
// usual number of additional bytes is 1 (a single newline
253252
// after the JSON).
254-
defer io.Copy(ioutil.Discard, io.LimitReader(resp.Body, 8*1024))
253+
defer io.Copy(io.Discard, io.LimitReader(resp.Body, 8*1024))
255254

256255
if err := dec.Decode(x); err != nil {
257256
return newDecodeResponseError(resp, bodyData, err)

client_test.go

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
109
"reflect"
@@ -86,7 +85,7 @@ var callTests = []struct {
8685
err1, ok := errgo.Cause(err).(*httprequest.DecodeResponseError)
8786
c.Assert(ok, qt.Equals, true, qt.Commentf("error not of type *httprequest.DecodeResponseError (%T)", errgo.Cause(err)))
8887
c.Assert(err1.Response, qt.Not(qt.IsNil))
89-
data, err := ioutil.ReadAll(err1.Response.Body)
88+
data, err := io.ReadAll(err1.Response.Body)
9089
c.Assert(err, qt.Equals, nil)
9190
c.Assert(string(data), qt.Equals, "bad response")
9291
},
@@ -99,7 +98,7 @@ var callTests = []struct {
9998
err1, ok := errgo.Cause(err).(*httprequest.DecodeResponseError)
10099
c.Assert(ok, qt.Equals, true, qt.Commentf("error not of type *httprequest.DecodeResponseError (%T)", errgo.Cause(err)))
101100
c.Assert(err1.Response, qt.Not(qt.IsNil))
102-
data, err := ioutil.ReadAll(err1.Response.Body)
101+
data, err := io.ReadAll(err1.Response.Body)
103102
c.Assert(err, qt.Equals, nil)
104103
c.Assert(string(data), qt.Equals, "bad error value")
105104
c.Assert(err1.Response.StatusCode, qt.Equals, http.StatusTeapot)
@@ -152,10 +151,11 @@ var callTests = []struct {
152151

153152
func TestCall(t *testing.T) {
154153
c := qt.New(t)
155-
defer c.Done()
156154

157155
srv := newServer()
158-
c.Defer(srv.Close)
156+
c.Cleanup(func() {
157+
srv.Close()
158+
})
159159

160160
for _, test := range callTests {
161161
c.Run(test.about, func(c *qt.C) {
@@ -183,10 +183,11 @@ func TestCall(t *testing.T) {
183183

184184
func TestCallURLNoRequestPath(t *testing.T) {
185185
c := qt.New(t)
186-
defer c.Done()
187186

188187
srv := newServer()
189-
c.Defer(srv.Close)
188+
c.Cleanup(func() {
189+
srv.Close()
190+
})
190191

191192
var client httprequest.Client
192193
req := struct {
@@ -263,16 +264,13 @@ var doTests = []struct {
263264
expectResp: &chM2Resp{"foo", 999},
264265
}}
265266

266-
func newInt64(i int64) *int64 {
267-
return &i
268-
}
269-
270267
func TestDo(t *testing.T) {
271268
c := qt.New(t)
272-
defer c.Done()
273269

274270
srv := newServer()
275-
c.Defer(srv.Close)
271+
c.Cleanup(func() {
272+
srv.Close()
273+
})
276274

277275
for _, test := range doTests {
278276
test := test
@@ -302,10 +300,11 @@ func TestDo(t *testing.T) {
302300

303301
func TestDoWithHTTPReponse(t *testing.T) {
304302
c := qt.New(t)
305-
defer c.Done()
306303

307304
srv := newServer()
308-
c.Defer(srv.Close)
305+
c.Cleanup(func() {
306+
srv.Close()
307+
})
309308

310309
client := &httprequest.Client{
311310
BaseURL: srv.URL,
@@ -314,17 +313,18 @@ func TestDoWithHTTPReponse(t *testing.T) {
314313
err := client.Get(context.Background(), "/m1/foo", &resp)
315314
c.Assert(err, qt.Equals, nil)
316315
defer resp.Body.Close()
317-
data, err := ioutil.ReadAll(resp.Body)
316+
data, err := io.ReadAll(resp.Body)
318317
c.Assert(err, qt.Equals, nil)
319318
c.Assert(string(data), qt.Equals, `{"P":"foo"}`)
320319
}
321320

322321
func TestDoWithHTTPReponseAndError(t *testing.T) {
323322
c := qt.New(t)
324-
defer c.Done()
325323

326324
srv := newServer()
327-
c.Defer(srv.Close)
325+
c.Cleanup(func() {
326+
srv.Close()
327+
})
328328

329329
var doer closeCountingDoer // Also check the body is closed.
330330
client := &httprequest.Client{
@@ -341,10 +341,11 @@ func TestDoWithHTTPReponseAndError(t *testing.T) {
341341

342342
func TestCallWithHTTPResponse(t *testing.T) {
343343
c := qt.New(t)
344-
defer c.Done()
345344

346345
srv := newServer()
347-
c.Defer(srv.Close)
346+
c.Cleanup(func() {
347+
srv.Close()
348+
})
348349

349350
client := &httprequest.Client{
350351
BaseURL: srv.URL,
@@ -353,18 +354,20 @@ func TestCallWithHTTPResponse(t *testing.T) {
353354
err := client.Call(context.Background(), &chM1Req{
354355
P: "foo",
355356
}, &resp)
357+
c.Assert(err, qt.IsNil)
356358
defer resp.Body.Close()
357-
data, err := ioutil.ReadAll(resp.Body)
359+
data, err := io.ReadAll(resp.Body)
358360
c.Assert(err, qt.Equals, nil)
359361
c.Assert(string(data), qt.Equals, `{"P":"foo"}`)
360362
}
361363

362364
func TestCallClosesResponseBodyOnSuccess(t *testing.T) {
363365
c := qt.New(t)
364-
defer c.Done()
365366

366367
srv := newServer()
367-
c.Defer(srv.Close)
368+
c.Cleanup(func() {
369+
srv.Close()
370+
})
368371

369372
var doer closeCountingDoer
370373
client := &httprequest.Client{
@@ -383,10 +386,11 @@ func TestCallClosesResponseBodyOnSuccess(t *testing.T) {
383386

384387
func TestCallClosesResponseBodyOnError(t *testing.T) {
385388
c := qt.New(t)
386-
defer c.Done()
387389

388390
srv := newServer()
389-
c.Defer(srv.Close)
391+
c.Cleanup(func() {
392+
srv.Close()
393+
})
390394

391395
var doer closeCountingDoer
392396
client := &httprequest.Client{
@@ -401,10 +405,11 @@ func TestCallClosesResponseBodyOnError(t *testing.T) {
401405

402406
func TestDoClosesResponseBodyOnSuccess(t *testing.T) {
403407
c := qt.New(t)
404-
defer c.Done()
405408

406409
srv := newServer()
407-
c.Defer(srv.Close)
410+
c.Cleanup(func() {
411+
srv.Close()
412+
})
408413

409414
var doer closeCountingDoer
410415
client := &httprequest.Client{
@@ -423,10 +428,11 @@ func TestDoClosesResponseBodyOnSuccess(t *testing.T) {
423428

424429
func TestDoClosesResponseBodyOnError(t *testing.T) {
425430
c := qt.New(t)
426-
defer c.Done()
427431

428432
srv := newServer()
429-
c.Defer(srv.Close)
433+
c.Cleanup(func() {
434+
srv.Close()
435+
})
430436

431437
var doer closeCountingDoer
432438
client := &httprequest.Client{
@@ -443,10 +449,11 @@ func TestDoClosesResponseBodyOnError(t *testing.T) {
443449

444450
func TestGet(t *testing.T) {
445451
c := qt.New(t)
446-
defer c.Done()
447452

448453
srv := newServer()
449-
c.Defer(srv.Close)
454+
c.Cleanup(func() {
455+
srv.Close()
456+
})
450457

451458
client := httprequest.Client{
452459
BaseURL: srv.URL,
@@ -459,10 +466,11 @@ func TestGet(t *testing.T) {
459466

460467
func TestGetNoBaseURL(t *testing.T) {
461468
c := qt.New(t)
462-
defer c.Done()
463469

464470
srv := newServer()
465-
c.Defer(srv.Close)
471+
c.Cleanup(func() {
472+
srv.Close()
473+
})
466474

467475
client := httprequest.Client{}
468476
var resp chM1Resp
@@ -479,7 +487,7 @@ func TestUnmarshalJSONResponseWithBodyReadError(t *testing.T) {
479487
"Content-Type": {"application/json"},
480488
},
481489
StatusCode: http.StatusOK,
482-
Body: ioutil.NopCloser(io.MultiReader(
490+
Body: io.NopCloser(io.MultiReader(
483491
strings.NewReader(`{"one": "two"}`),
484492
errorReader("some bad read"),
485493
)),
@@ -535,7 +543,7 @@ func TestUnmarshalJSONResponseWithVariedJSONContentTypes(t *testing.T) {
535543
"Content-Type": {test.contentType},
536544
},
537545
StatusCode: http.StatusTeapot,
538-
Body: ioutil.NopCloser(strings.NewReader(`{}`)),
546+
Body: io.NopCloser(strings.NewReader(`{}`)),
539547
}
540548
var val map[string]string
541549
err := httprequest.UnmarshalJSONResponse(resp, &val)
@@ -552,7 +560,6 @@ func TestUnmarshalJSONResponseWithVariedJSONContentTypes(t *testing.T) {
552560

553561
func TestUnmarshalJSONResponseWithErrorAndLargeBody(t *testing.T) {
554562
c := qt.New(t)
555-
defer c.Done()
556563

557564
c.Patch(httprequest.MaxErrorBodySize, 11)
558565

@@ -561,7 +568,7 @@ func TestUnmarshalJSONResponseWithErrorAndLargeBody(t *testing.T) {
561568
"Content-Type": {"foo/bar"},
562569
},
563570
StatusCode: http.StatusOK,
564-
Body: ioutil.NopCloser(strings.NewReader(`123456789 123456789`)),
571+
Body: io.NopCloser(strings.NewReader(`123456789 123456789`)),
565572
}
566573
var val map[string]string
567574
err := httprequest.UnmarshalJSONResponse(resp, &val)
@@ -572,7 +579,6 @@ func TestUnmarshalJSONResponseWithErrorAndLargeBody(t *testing.T) {
572579

573580
func TestUnmarshalJSONResponseWithLargeBody(t *testing.T) {
574581
c := qt.New(t)
575-
defer c.Done()
576582

577583
c.Patch(httprequest.MaxErrorBodySize, 11)
578584

@@ -581,7 +587,7 @@ func TestUnmarshalJSONResponseWithLargeBody(t *testing.T) {
581587
"Content-Type": {"application/json"},
582588
},
583589
StatusCode: http.StatusOK,
584-
Body: ioutil.NopCloser(strings.NewReader(`"23456789 123456789"`)),
590+
Body: io.NopCloser(strings.NewReader(`"23456789 123456789"`)),
585591
}
586592
var val string
587593
err := httprequest.UnmarshalJSONResponse(resp, &val)
@@ -597,7 +603,7 @@ func TestUnmarshalJSONWithDecodeError(t *testing.T) {
597603
"Content-Type": {"application/json"},
598604
},
599605
StatusCode: http.StatusOK,
600-
Body: ioutil.NopCloser(strings.NewReader(`{"one": "two"}`)),
606+
Body: io.NopCloser(strings.NewReader(`{"one": "two"}`)),
601607
}
602608
var val chan string
603609
err := httprequest.UnmarshalJSONResponse(resp, &val)
@@ -608,7 +614,6 @@ func TestUnmarshalJSONWithDecodeError(t *testing.T) {
608614

609615
func TestUnmarshalJSONWithDecodeErrorAndLargeBody(t *testing.T) {
610616
c := qt.New(t)
611-
defer c.Done()
612617

613618
c.Patch(httprequest.MaxErrorBodySize, 11)
614619

@@ -617,7 +622,7 @@ func TestUnmarshalJSONWithDecodeErrorAndLargeBody(t *testing.T) {
617622
"Content-Type": {"application/json"},
618623
},
619624
StatusCode: http.StatusOK,
620-
Body: ioutil.NopCloser(strings.NewReader(`"23456789 123456789"`)),
625+
Body: io.NopCloser(strings.NewReader(`"23456789 123456789"`)),
621626
}
622627
var val chan string
623628
err := httprequest.UnmarshalJSONResponse(resp, &val)
@@ -629,7 +634,7 @@ func TestUnmarshalJSONWithDecodeErrorAndLargeBody(t *testing.T) {
629634
func assertDecodeResponseError(c *qt.C, err error, status int, body string) {
630635
err1, ok := errgo.Cause(err).(*httprequest.DecodeResponseError)
631636
c.Assert(ok, qt.Equals, true, qt.Commentf("error not of type *httprequest.DecodeResponseError (%T)", errgo.Cause(err)))
632-
data, err := ioutil.ReadAll(err1.Response.Body)
637+
data, err := io.ReadAll(err1.Response.Body)
633638
c.Assert(err, qt.Equals, nil)
634639
c.Assert(err1.Response.StatusCode, qt.Equals, status)
635640
c.Assert(string(data), qt.Equals, body)
@@ -849,39 +854,6 @@ func (r *closeCountingReader) Close() error {
849854
return r.ReadCloser.Close()
850855
}
851856

852-
// largeReader implements a reader that produces up to total bytes
853-
// in 1 byte reads.
854-
type largeReader struct {
855-
byte byte
856-
total int
857-
n int
858-
}
859-
860-
func (r *largeReader) Read(buf []byte) (int, error) {
861-
if r.n >= r.total {
862-
return 0, io.EOF
863-
}
864-
r.n++
865-
return copy(buf, []byte{r.byte}), nil
866-
}
867-
868-
func (r *largeReader) Seek(offset int64, whence int) (int64, error) {
869-
if offset != 0 || whence != 0 {
870-
panic("unexpected seek")
871-
}
872-
r.n = 0
873-
return 0, nil
874-
}
875-
876-
func (r *largeReader) Close() error {
877-
// By setting n to zero, we ensure that if there's
878-
// a concurrent read, it will also read from n
879-
// and so the race detector should pick up the
880-
// problem.
881-
r.n = 0
882-
return nil
883-
}
884-
885857
func isRemoteError(err error) bool {
886858
_, ok := err.(*httprequest.RemoteError)
887859
return ok

0 commit comments

Comments
 (0)