Skip to content

Commit

Permalink
go-require: require-error: readme & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom committed May 18, 2024
1 parent ff5c42a commit ef0060e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,13 @@ Also a bad solution would be to simply replace all `require` in goroutines with

The checker is enabled by default, because `testinggoroutine` is enabled by default in `go vet`.

P.S. Related `testify`'s [thread](https://github.com/stretchr/testify/issues/772).
In addition, the checker warns about `require` in HTTP handlers (functions and methods whose signature matches
[http.HandlerFunc](https://pkg.go.dev/net/http#HandlerFunc)), because handlers run in a separate
[service goroutine](https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/net/http/server.go;l=2782;drc=1d45a7ef560a76318ed59dfdb178cecd58caf948) that
services the HTTP connection. Terminating these goroutines can lead to undefined behaviour and difficulty debugging tests.
You can turn off the check using the `--go-require.ignore-http-handlers` flag.

P.S. Look at [testify's issue](https://github.com/stretchr/testify/issues/772), related to assertions in the goroutines.

---

Expand Down
15 changes: 10 additions & 5 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ func TestTestifyLint(t *testing.T) {
"expected-actual.pattern": "goldenValue",
},
},
{dir: "ginkgo"},
{
dir: "ginkgo",
},
{
dir: "go-require-http-handlers",
flags: map[string]string{
"enable": checkers.NewGoRequire().Name() + "," + // https://github.com/Antonboom/testifylint/issues/66
checkers.NewRequireError().Name(), // https://github.com/Antonboom/testifylint/issues/73
},
},
{
dir: "go-require-ignore-http-handlers",
flags: map[string]string{
Expand All @@ -58,10 +67,6 @@ func TestTestifyLint(t *testing.T) {
"go-require.ignore-http-handlers": "true",
},
},
{
dir: "go-require-issue66-issue73",
flags: map[string]string{"enable-all": "true"},
},
{
dir: "go-require-issue67",
flags: map[string]string{"disable-all": "true", "enable": checkers.NewGoRequire().Name()},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ func TestServerSuite(t *testing.T) {

func (s *ServerSuite) TestServer() {
httptest.NewServer(http.HandlerFunc(s.handler))
httptest.NewServer(s)
}

func (s *ServerSuite) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
s.T().Helper()

file, err := os.Open("some file.json")
s.Require().NoError(err) // want "go-require: do not use require in http handlers"

data, err := io.ReadAll(file)
if !s.NoError(err) {
return
}

w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if !s.NoError(err) {
s.FailNow(err.Error()) // want "go-require: do not use s\\.FailNow in http handlers"
}
}

func (s *ServerSuite) handler(w http.ResponseWriter, _ *http.Request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorequireignorehttphandlers_test

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -10,6 +11,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

func TestServer_Require(t *testing.T) {
Expand Down Expand Up @@ -46,3 +48,34 @@ func TestServer_Require(t *testing.T) {

require.Equal(t, http.StatusOK, <-statusCode)
}

type SomeServerSuite struct {
suite.Suite
}

func TestSomeServerSuite(t *testing.T) {
suite.Run(t, &SomeServerSuite{})
}

func (s *SomeServerSuite) TestServer() {
httptest.NewServer(http.HandlerFunc(s.handler))
httptest.NewServer(s)
}

func (s *SomeServerSuite) ServeHTTP(hres http.ResponseWriter, hreq *http.Request) {
var req MyRequest
err := json.NewDecoder(hreq.Body).Decode(&req)
s.Require().NoError(err)
s.Equal("42", req.ID)
}

func (s *SomeServerSuite) handler(hres http.ResponseWriter, hreq *http.Request) {
var req MyRequest
err := json.NewDecoder(hreq.Body).Decode(&req)
s.Require().NoError(err)
s.Equal("42", req.ID)
}

type MyRequest struct {
ID string
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func TestSomeServerSuite(t *testing.T) {

func (s *SomeServerSuite) TestServer() {
httptest.NewServer(http.HandlerFunc(s.handler))
httptest.NewServer(s)
}

func (s *SomeServerSuite) ServeHTTP(hres http.ResponseWriter, hreq *http.Request) {
var req MyRequest
err := json.NewDecoder(hreq.Body).Decode(&req)
s.Require().NoError(err)
s.Equal("42", req.ID)
}

func (s *SomeServerSuite) handler(hres http.ResponseWriter, hreq *http.Request) {
Expand Down

0 comments on commit ef0060e

Please sign in to comment.