From 0c55d2e5a7e4b383ab221d246f55bebe8336a22e Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 15 Sep 2022 16:10:35 +0300 Subject: [PATCH 1/2] Remove runtime and reflection from asserts --- assert.go | 203 +++++++++++++----------------------------------- go.mod | 2 +- go.sum | 1 - jsonschema.go | 11 +-- roundtripper.go | 8 +- step.go | 17 ++-- test.go | 4 +- 7 files changed, 71 insertions(+), 175 deletions(-) diff --git a/assert.go b/assert.go index 237a205..2a29005 100644 --- a/assert.go +++ b/assert.go @@ -2,12 +2,7 @@ package cute import ( "net/http" - "reflect" - "runtime" - "strings" - "github.com/ozontech/allure-go/pkg/allure" - "github.com/ozontech/allure-go/pkg/framework/provider" "github.com/ozontech/cute/errors" ) @@ -35,183 +30,98 @@ type AssertResponseT func(t T, response *http.Response) error func (it *Test) assertHeaders(t internalT, headers http.Header) []error { var ( - assertHeaders = it.Expect.AssertHeaders - assertHeadersT = it.Expect.AssertHeadersT - - errs = make([]error, 0) + asserts = it.Expect.AssertHeaders + assertT = it.Expect.AssertHeadersT ) - if len(assertHeaders) == 0 && len(assertHeadersT) == 0 { + if len(asserts) == 0 && len(assertT) == 0 { return nil } - t.WithNewStep("Assert headers", func(stepCtx provider.StepCtx) { - isOption := false - isOptionT := false - - // Execute assert only body - for _, f := range assertHeaders { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(headers) - if err != nil { - errs = append(errs, err) - - isOption = isOptionError(err) - - return []error{err} - } - - return nil - }, true) - } - - // Execute assert for body with TB - for _, f := range assertHeadersT { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(t, headers) - if err != nil { - errs = append(errs, err) - - isOptionT = isOptionError(err) - - return []error{err} - } - - return nil - }, true) + return executeWithStep(t, "Assert headers", func(t T) []error { + errs := make([]error, 0) + // Execute assert only response + for _, f := range asserts { + err := f(headers) + if err != nil { + errs = append(errs, err) + } } - if len(errs) > 0 { - if isOption && isOptionT { - stepCtx.CurrentStep().Status = allure.Skipped - } else { - stepCtx.CurrentStep().Status = allure.Failed + // Execute assert for response with TB + for _, f := range assertT { + err := f(t, headers) + if err != nil { + errs = append(errs, err) } } + return errs }) - - return errs } -func (it *Test) assertResponse(t internalT, response *http.Response) []error { +func (it *Test) assertResponse(t internalT, resp *http.Response) []error { var ( - assertResponse = it.Expect.AssertResponse - assertResponseT = it.Expect.AssertResponseT - - errs = make([]error, 0) + asserts = it.Expect.AssertResponse + assertT = it.Expect.AssertResponseT ) - if len(assertResponse) == 0 && len(assertResponseT) == 0 { + if len(asserts) == 0 && len(assertT) == 0 { return nil } - t.WithNewStep("Assert response", func(stepCtx provider.StepCtx) { - isOption := false - isOptionT := false - - // Execute assert only body - for _, f := range assertResponse { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(response) - if err != nil { - errs = append(errs, err) - - isOption = isOptionError(err) - - return []error{err} - } - - return nil - }, true) - } - - // Execute assert for body with TB - for _, f := range assertResponseT { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(t, response) - if err != nil { - errs = append(errs, err) - - isOptionT = isOptionError(err) - - return []error{err} - } - - return nil - }, true) + return executeWithStep(t, "Assert response", func(t T) []error { + errs := make([]error, 0) + // Execute assert only response + for _, f := range asserts { + err := f(resp) + if err != nil { + errs = append(errs, err) + } } - if len(errs) > 0 { - if isOption && isOptionT { - stepCtx.CurrentStep().Status = allure.Skipped - } else { - stepCtx.CurrentStep().Status = allure.Failed + // Execute assert for response with TB + for _, f := range assertT { + err := f(t, resp) + if err != nil { + errs = append(errs, err) } } - }) - return errs + return errs + }) } func (it *Test) assertBody(t internalT, body []byte) []error { var ( - assertBody = it.Expect.AssertBody - assertBodyT = it.Expect.AssertBodyT - - errs = make([]error, 0) + asserts = it.Expect.AssertBody + assertT = it.Expect.AssertBodyT ) - if len(assertBody) == 0 && len(assertBodyT) == 0 { + if len(asserts) == 0 && len(assertT) == 0 { return nil } - t.WithNewStep("Assert body", func(stepCtx provider.StepCtx) { - isOption := false - isOptionT := false - - // Execute assert only body - for _, f := range assertBody { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(body) - if err != nil { - errs = append(errs, err) - - isOption = isOptionError(err) - - return []error{err} - } - - return nil - }, true) - } - - // Execute assert for body with TB - for _, f := range assertBodyT { - executeWithStep(stepCtx, getFunctionName(f), func(t T) []error { - err := f(t, body) - if err != nil { - errs = append(errs, err) - - isOptionT = isOptionError(err) - - return []error{err} - } - - return nil - }, true) + return executeWithStep(t, "Assert body", func(t T) []error { + errs := make([]error, 0) + // Execute assert only response + for _, f := range asserts { + err := f(body) + if err != nil { + errs = append(errs, err) + } } - if len(errs) > 0 { - if isOption && isOptionT { - stepCtx.CurrentStep().Status = allure.Skipped - } else { - stepCtx.CurrentStep().Status = allure.Failed + // Execute assert for response with TB + for _, f := range assertT { + err := f(t, body) + if err != nil { + errs = append(errs, err) } } - }) - return errs + return errs + }) } func isOptionError(err error) bool { @@ -222,11 +132,6 @@ func isOptionError(err error) bool { return false } -func getFunctionName(temp interface{}) string { - strs := strings.Split(runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name(), ".") - return strs[len(strs)-2] -} - func optionalAssertHeaders(assert AssertHeaders) AssertHeaders { return func(headers http.Header) error { err := assert(headers) diff --git a/go.mod b/go.mod index d10ab36..5c2e0a2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ozontech/cute -go 1.17 +go 1.18 require ( github.com/PaesslerAG/jsonpath v0.1.1 diff --git a/go.sum b/go.sum index 15c4702..e031c56 100644 --- a/go.sum +++ b/go.sum @@ -27,7 +27,6 @@ github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/jsonschema.go b/jsonschema.go index 8f61b90..7eac1ad 100644 --- a/jsonschema.go +++ b/jsonschema.go @@ -11,7 +11,6 @@ import ( // Automatically add information about validation to allure. func (it *Test) validateJSONSchema(t internalT, body []byte) []error { var ( - scope = make([]error, 0) expect gojsonschema.JSONLoader ) @@ -26,13 +25,9 @@ func (it *Test) validateJSONSchema(t internalT, body []byte) []error { return nil } - executeWithStep(t, "Validate body by JSON schema", func(t T) []error { - scope = checkJSONSchema(expect, body) - - return scope - }, false) - - return scope + return executeWithStep(t, "Validate body by JSON schema", func(t T) []error { + return checkJSONSchema(expect, body) + }) } func checkJSONSchema(expect gojsonschema.JSONLoader, data []byte) []error { diff --git a/roundtripper.go b/roundtripper.go index 94b0b0a..cabdc47 100644 --- a/roundtripper.go +++ b/roundtripper.go @@ -39,7 +39,7 @@ func (it *Test) makeRequest(t internalT, req *http.Request) (*http.Response, []e } return nil - }, false) + }) if err == nil { break @@ -105,7 +105,9 @@ func addInformationRequest(t T, req *http.Request) error { return err } - t.Log("[Request]" + curl.String()) + if c := curl.String(); len(c) <= 2048 { + t.Log("[Request]" + c) + } headers, err := utils.ToJSON(req.Header) if err != nil { @@ -117,7 +119,7 @@ func addInformationRequest(t T, req *http.Request) error { "method", req.Method, "host", req.Host, "headers", headers, - "curl", curl.String(), + "curl", curl, )..., ) diff --git a/step.go b/step.go index d44bc95..b0ae88f 100644 --- a/step.go +++ b/step.go @@ -6,20 +6,20 @@ import ( "github.com/ozontech/cute/errors" ) -func executeWithStep(t internalT, stepName string, execute func(t T) []error, skipCreateNewStep bool) []error { +func executeWithStep(t internalT, stepName string, execute func(t T) []error) []error { var ( errs []error ) t.WithNewStep(stepName, func(stepCtx provider.StepCtx) { errs = execute(stepCtx) - processStepErrors(stepCtx, errs, skipCreateNewStep) + processStepErrors(stepCtx, errs) }) return errs } -func processStepErrors(stepCtx provider.StepCtx, errs []error, skipCreateNewStep bool) { +func processStepErrors(stepCtx provider.StepCtx, errs []error) { var ( step = stepCtx.CurrentStep() statuses = make([]allure.Status, 0) @@ -40,14 +40,9 @@ func processStepErrors(stepCtx provider.StepCtx, errs []error, skipCreateNewStep } if tErr, ok := err.(errors.WithNameError); ok { - if skipCreateNewStep { - currentStep.Name = tErr.GetName() - - } else { - currentStep = allure.NewSimpleStep(tErr.GetName()) - currentStep.Status = currentStatus - currentStep.WithParent(step) - } + currentStep = allure.NewSimpleStep(tErr.GetName()) + currentStep.Status = currentStatus + currentStep.WithParent(step) } if tErr, ok := err.(errors.WithFields); ok { diff --git a/test.go b/test.go index 411be68..fe5fd98 100644 --- a/test.go +++ b/test.go @@ -288,7 +288,7 @@ func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []erro } return scope - }, false) + }) } func (it *Test) beforeTest(t internalT, req *http.Request) []error { @@ -312,7 +312,7 @@ func (it *Test) beforeTest(t internalT, req *http.Request) []error { } return scope - }, false) + }) } func (it *Test) createRequest(ctx context.Context) (*http.Request, error) { From 2d473383fafb6220c4f9b5355539750bcff9b740 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 15 Sep 2022 16:11:50 +0300 Subject: [PATCH 2/2] Fix curl strings --- roundtripper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roundtripper.go b/roundtripper.go index cabdc47..beb2a9f 100644 --- a/roundtripper.go +++ b/roundtripper.go @@ -119,7 +119,7 @@ func addInformationRequest(t T, req *http.Request) error { "method", req.Method, "host", req.Host, "headers", headers, - "curl", curl, + "curl", curl.String(), )..., )