From 72e08d03ca1e4ffe93019fa9fff6babee2a1779a Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 17 May 2022 12:21:03 +0800 Subject: [PATCH 1/3] Update license header Signed-off-by: tison --- adatper.go | 1 + error.go | 1 + error_test.go | 1 + form_helper.go | 1 + go.mod | 7 ++++++- go.sum | 16 ++++++++++++++++ go113.go | 4 +++- group.go | 1 + interface.go | 1 + plugin.go | 1 + pre_go113.go | 4 +++- types.go | 1 + types_test.go | 1 + wrapper.go | 1 + wrapper_test.go | 1 + 15 files changed, 39 insertions(+), 3 deletions(-) diff --git a/adatper.go b/adatper.go index 41ab2ab..c4184f1 100644 --- a/adatper.go +++ b/adatper.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/error.go b/error.go index b9d1c9f..8ba56d6 100644 --- a/error.go +++ b/error.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/error_test.go b/error_test.go index 090dda8..4912a04 100644 --- a/error_test.go +++ b/error_test.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/form_helper.go b/form_helper.go index 3d40e46..4cfe752 100644 --- a/form_helper.go +++ b/form_helper.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/go.mod b/go.mod index 8b2277d..b904f3b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module github.com/pingcap/fn go 1.11 -require github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 + github.com/stretchr/testify v1.7.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect +) diff --git a/go.sum b/go.sum index 177fd0b..93630ac 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,18 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 h1:USx2/E1bX46VG32FIw034Au6seQ2fY9NEILmNh/UlQg= github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc= +gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go113.go b/go113.go index 957ac8c..af0d190 100644 --- a/go113.go +++ b/go113.go @@ -1,4 +1,5 @@ -//+build go1.13 +//go:build go1.13 +// +build go1.13 // Copyright 2020 PingCAP, Inc. // @@ -10,6 +11,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/group.go b/group.go index 3dc4c48..8e2cfc0 100644 --- a/group.go +++ b/group.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/interface.go b/interface.go index 635e9de..5a3932c 100644 --- a/interface.go +++ b/interface.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/plugin.go b/plugin.go index 5023f3b..45aca40 100644 --- a/plugin.go +++ b/plugin.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/pre_go113.go b/pre_go113.go index 291df71..aaadcba 100644 --- a/pre_go113.go +++ b/pre_go113.go @@ -1,4 +1,5 @@ -//+build !go1.13 +//go:build !go1.13 +// +build !go1.13 // Copyright 2020 PingCAP, Inc. // @@ -10,6 +11,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/types.go b/types.go index c603ca1..f11ed48 100644 --- a/types.go +++ b/types.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/types_test.go b/types_test.go index 8462c34..1dbe01b 100644 --- a/types_test.go +++ b/types_test.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/wrapper.go b/wrapper.go index ada125f..5a02348 100644 --- a/wrapper.go +++ b/wrapper.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. diff --git a/wrapper_test.go b/wrapper_test.go index 7b0ed48..6e5d669 100644 --- a/wrapper_test.go +++ b/wrapper_test.go @@ -8,6 +8,7 @@ // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. From 4868f705d2ef880e0fb1d7652131ee9e128767f9 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 17 May 2022 12:38:00 +0800 Subject: [PATCH 2/3] migrate to testify Signed-off-by: tison --- error_test.go | 27 ++++++------------- wrapper_test.go | 72 +++++++++++++++++++++---------------------------- 2 files changed, 38 insertions(+), 61 deletions(-) diff --git a/error_test.go b/error_test.go index 4912a04..c895255 100644 --- a/error_test.go +++ b/error_test.go @@ -19,12 +19,10 @@ import ( "net/http" "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -var ( - errTest = errors.New("test") -) +var errTest = errors.New("test") type withError struct { err error @@ -38,27 +36,18 @@ func (w *withError) Unwrap() error { return w.err } -type errSuite struct{} - -func TestError(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&errSuite{}) - // TestWithError test UnwrapErrorStatusCode method -func (e *errSuite) TestWithError(c *C) { +func TestWithError(t *testing.T) { var err error = &withError{ err: ErrorWithStatusCode(errTest, http.StatusInternalServerError), } code, ok := UnwrapErrorStatusCode(err) - c.Assert(ok, IsTrue) - c.Assert(code == http.StatusInternalServerError, IsTrue) + require.True(t, ok) + require.Equal(t, http.StatusInternalServerError, code) } -// TestOriginError ErrorWithStatusCode Unwrap -func (e *errSuite) TestOriginError(c *C) { +// TestOriginError test ErrorWithStatusCode Unwrap +func TestOriginError(t *testing.T) { err := ErrorWithStatusCode(errTest, http.StatusInternalServerError) - err = Unwrap(err) - c.Assert(err == errTest, IsTrue) + require.Equal(t, errTest, Unwrap(err)) } diff --git a/wrapper_test.go b/wrapper_test.go index 6e5d669..a3a80b9 100644 --- a/wrapper_test.go +++ b/wrapper_test.go @@ -25,20 +25,11 @@ import ( "net/http" "net/http/httptest" "net/url" - "reflect" "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -type fnSuite struct{} - -func TestFn(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&fnSuite{}) - type testRequest struct { Foo string `json:"foo"` Bar int `json:"bar"` @@ -91,7 +82,7 @@ func withAll(io.ReadCloser, *testRequest, Form, PostForm, http.Header, *multipar return nil, nil } -func (s *fnSuite) TestHandler(c *C) { +func TestHandler(t *testing.T) { Wrap(withNone) Wrap(withBody) Wrap(withReq) @@ -109,23 +100,23 @@ func (s *fnSuite) TestHandler(c *C) { Wrap(withInContextAndPayload) } -func (s *fnSuite) TestPlugin(c *C) { +func TestPlugin(t *testing.T) { logic := func(ctx context.Context) (*testResponse, error) { - c.Assert(ctx.Value("key").(string) == "value", IsTrue) - c.Assert(ctx.Value("key2").(string) == "value2", IsTrue) + require.Equal(t, "value", ctx.Value("key").(string)) + require.Equal(t, "value2", ctx.Value("key2").(string)) return &testResponse{}, nil } plugin1 := func(ctx context.Context, request *http.Request) (context.Context, error) { - c.Assert(ctx.Value("global1").(string) == "globalvalue1", IsTrue) - c.Assert(ctx.Value("global2").(string) == "globalvalue2", IsTrue) + require.Equal(t, "globalvalue1", ctx.Value("global1").(string)) + require.Equal(t, "globalvalue2", ctx.Value("global2").(string)) return context.WithValue(ctx, "key", "value"), nil } plugin2 := func(ctx context.Context, request *http.Request) (context.Context, error) { - c.Assert(ctx.Value("global1").(string) == "globalvalue1", IsTrue) - c.Assert(ctx.Value("global2").(string) == "globalvalue2", IsTrue) - c.Assert(ctx.Value("key").(string) == "value", IsTrue) + require.Equal(t, "globalvalue1", ctx.Value("global1").(string)) + require.Equal(t, "globalvalue2", ctx.Value("global2").(string)) + require.Equal(t, "value", ctx.Value("key").(string)) return context.WithValue(ctx, "key2", "value2"), nil } @@ -133,31 +124,31 @@ func (s *fnSuite) TestPlugin(c *C) { recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) } -func (s *fnSuite) TestGroupPlugin(c *C) { +func TestGroupPlugin(t *testing.T) { group := NewGroup() group.Plugin(func(ctx context.Context, request *http.Request) (context.Context, error) { return context.WithValue(ctx, "key", "value"), nil }) logic := func(ctx context.Context) (*testResponse, error) { - c.Assert(ctx.Value("key").(string) == "value", IsTrue) + require.Equal(t, "value", ctx.Value("key").(string)) return &testResponse{}, nil } handler := group.Wrap(logic) recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) } -func (s *fnSuite) TestSetResponseEncoder(c *C) { +func TestSetResponseEncoder(t *testing.T) { handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) { - return nil, nil + return context.TODO(), nil }) testResp := &testResponse{ @@ -170,14 +161,14 @@ func (s *fnSuite) TestSetResponseEncoder(c *C) { recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) respMsg := &testResponse{} _ = json.Unmarshal(recorder.Body.Bytes(), &respMsg) - c.Assert(reflect.DeepEqual(respMsg, testResp), IsTrue) + require.Equal(t, testResp, respMsg) } -func (s *fnSuite) TestSetErrorEncoder(c *C) { +func TestSetErrorEncoder(t *testing.T) { handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) { return nil, errors.New("") }) @@ -192,53 +183,50 @@ func (s *fnSuite) TestSetErrorEncoder(c *C) { recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) respMsg := &testErrorResponse{} _ = json.Unmarshal(recorder.Body.Bytes(), &respMsg) - c.Assert(reflect.DeepEqual(respMsg, testErrorResp), IsTrue) + require.Equal(t, testErrorResp, respMsg) } -func (s *fnSuite) TestGenericAdapter_Invoke(c *C) { +func TestGenericAdapter_Invoke(t *testing.T) { type CustomForm testRequest handler := Wrap(func(ctx context.Context, form *CustomForm) (context.Context, error) { return nil, nil }) - recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err == nil, IsTrue) + require.NoError(t, err) payload := []byte(`{"for":"hello", "bar":10000}`) request.Body = ioutil.NopCloser(bytes.NewBuffer(payload)) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) } -func (s *fnSuite) TestSimpleUnaryAdapter_Invoke(c *C) { +func TestSimpleUnaryAdapter_Invoke(t *testing.T) { handler := Wrap(withReq) recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - if err != nil { - c.Fatal(err) - } + require.NoError(t, err) payload := []byte(`{"for":"hello", "bar":10000}`) request.Body = ioutil.NopCloser(bytes.NewBuffer(payload)) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) } -func (s *fnSuite) TestErrorWithStatusCode(c *C) { +func TestErrorWithStatusCode(t *testing.T) { handler := Wrap(func(ctx context.Context, request *http.Request) (context.Context, error) { return nil, ErrorWithStatusCode(errors.New("not found"), http.StatusNotFound) }) recorder := httptest.NewRecorder() request, err := http.NewRequest(http.MethodGet, "", nil) - c.Assert(err, IsNil) + require.NoError(t, err) handler.ServeHTTP(recorder, request) - c.Assert(recorder.Code == http.StatusNotFound, IsTrue) + require.Equal(t, http.StatusNotFound, recorder.Code) } func BenchmarkSimplePlainAdapter_Invoke(b *testing.B) { From 95186e315f2d0c9a1eb8f02d6904447e268a4de0 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 17 May 2022 12:38:34 +0800 Subject: [PATCH 3/3] go mod tidy Signed-off-by: tison --- go.mod | 3 +-- go.sum | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index b904f3b..fa2d9ef 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.11 require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 - github.com/stretchr/testify v1.7.1 // indirect + github.com/stretchr/testify v1.7.1 gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect ) diff --git a/go.sum b/go.sum index 93630ac..5507b1d 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,13 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 h1:USx2/E1bX46VG32FIw034Au6seQ2fY9NEILmNh/UlQg= -github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 h1:dbuHpmKjkDzSOMKAWl10QNlgaZUd3V1q99xc81tt2Kc= gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=