From 205657588e44f251f0c136f77d46411e8f707a7e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 28 Mar 2022 16:47:15 +0800 Subject: [PATCH] refactor: change TStep API/TestCase type --- hrp/convert.go | 42 +++++++++++++++++++++++++++++------------- hrp/models.go | 28 +++++++++++++--------------- hrp/runner.go | 4 ++-- hrp/step.go | 24 ++++++++++++++++-------- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/hrp/convert.go b/hrp/convert.go index 3148ab56..3d09b025 100644 --- a/hrp/convert.go +++ b/hrp/convert.go @@ -103,35 +103,51 @@ func (tc *TCase) ToTestCase() (*TestCase, error) { log.Info().Str("dir", projectRootDir).Msg("located project root dir") for _, step := range tc.TestSteps { - if step.APIPath != "" { - path := filepath.Join(projectRootDir, step.APIPath) - if !builtin.IsFilePathExists(path) { - return nil, errors.New("referenced api file not found: " + path) + if step.API != nil { + if apiContent, ok := step.API.(*API); ok { + step.API = apiContent + testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{ + step: step, + }) + return testCase, nil } - refAPI := APIPath(path) - step.APIContent = &refAPI - apiContent, err := step.APIContent.ToAPI() + // reference api path + var apiFullPath string + if apiPath, ok := step.API.(string); ok { + apiFullPath = filepath.Join(projectRootDir, apiPath) + } else if apiPath, ok := step.API.(APIPath); ok { + apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath()) + } else { + return nil, errors.New("invalid api format") + } + + if !builtin.IsFilePathExists(apiFullPath) { + return nil, errors.New("referenced api file not found: " + apiFullPath) + } + + refAPI := APIPath(apiFullPath) + apiContent, err := refAPI.ToAPI() if err != nil { return nil, err } - step.APIContent = apiContent + step.API = apiContent + testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{ step: step, }) - } else if step.TestCasePath != "" { - path := filepath.Join(projectRootDir, step.TestCasePath) + } else if step.TestCase != nil { + path := filepath.Join(projectRootDir, step.TestCase.(string)) if !builtin.IsFilePathExists(path) { return nil, errors.New("referenced testcase file not found: " + path) } refTestCase := TestCasePath(path) - step.TestCaseContent = &refTestCase - tc, err := step.TestCaseContent.ToTestCase() + tc, err := refTestCase.ToTestCase() if err != nil { return nil, err } - step.TestCaseContent = tc + step.TestCase = tc testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{ step: step, }) diff --git a/hrp/models.go b/hrp/models.go index 4b5068cc..e3b7d6d4 100644 --- a/hrp/models.go +++ b/hrp/models.go @@ -222,21 +222,19 @@ type IAPI interface { // TStep represents teststep data structure. // Each step maybe two different type: make one HTTP request or reference another testcase. type TStep struct { - Name string `json:"name" yaml:"name"` // required - Request *Request `json:"request,omitempty" yaml:"request,omitempty"` - APIPath string `json:"api,omitempty" yaml:"api,omitempty"` - APIContent IAPI `json:"api_content,omitempty" yaml:"api_content,omitempty"` - TestCasePath string `json:"testcase,omitempty" yaml:"testcase,omitempty"` - TestCaseContent ITestCase `json:"testcase_content,omitempty" yaml:"testcase_content,omitempty"` - Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"` - Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"` - ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"` - Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` - SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"` - TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"` - Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"` - Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"` - Export []string `json:"export,omitempty" yaml:"export,omitempty"` + Name string `json:"name" yaml:"name"` // required + Request *Request `json:"request,omitempty" yaml:"request,omitempty"` + API interface{} `json:"api,omitempty" yaml:"api,omitempty"` // *APIPath or *API + TestCase interface{} `json:"testcase,omitempty" yaml:"testcase,omitempty"` // *TestCasePath or *TestCase + Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"` + Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"` + ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"` + Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` + SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"` + TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"` + Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"` + Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"` + Export []string `json:"export,omitempty" yaml:"export,omitempty"` } type stepType string diff --git a/hrp/runner.go b/hrp/runner.go index 828cf8d8..34d030ce 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -345,7 +345,7 @@ func (r *caseRunner) runStep(index int, caseConfig *TConfig) (stepResult *stepDa if _, ok := step.(*StepAPIWithOptionalArgs); ok { // run referenced API log.Info().Str("api", copiedStep.Name).Msg("run referenced api") - api, _ := copiedStep.APIContent.ToAPI() + api, _ := copiedStep.API.(*API) extendWithAPI(copiedStep, api) } // override headers @@ -967,7 +967,7 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err StepType: stepTypeTestCase, Success: false, } - testcase := step.TestCaseContent + testcase := step.TestCase.(*TestCase) // copy testcase to avoid data racing copiedTestCase := &TestCase{} diff --git a/hrp/step.go b/hrp/step.go index f0dd63f9..72da0818 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -169,7 +169,7 @@ func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs { // CallRefCase calls a referenced testcase. func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs { var err error - s.step.TestCaseContent, err = tc.ToTestCase() + s.step.TestCase, err = tc.ToTestCase() if err != nil { log.Error().Err(err).Msg("failed to load testcase") os.Exit(1) @@ -182,7 +182,7 @@ func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs { // CallRefAPI calls a referenced api. func (s *StepRequest) CallRefAPI(api IAPI) *StepAPIWithOptionalArgs { var err error - s.step.APIContent, err = api.ToAPI() + s.step.API, err = api.ToAPI() if err != nil { log.Error().Err(err).Msg("failed to load api") os.Exit(1) @@ -332,8 +332,10 @@ func (s *StepAPIWithOptionalArgs) TeardownHook(hook string) *StepAPIWithOptional // Export specifies variable names to export from referenced api for current step. func (s *StepAPIWithOptionalArgs) Export(names ...string) *StepAPIWithOptionalArgs { - api, _ := s.step.APIContent.ToAPI() - s.step.Export = append(api.Export, names...) + api, ok := s.step.API.(*API) + if ok { + s.step.Export = append(api.Export, names...) + } return s } @@ -341,8 +343,11 @@ func (s *StepAPIWithOptionalArgs) Name() string { if s.step.Name != "" { return s.step.Name } - api, _ := s.step.APIContent.ToAPI() - return api.Name + api, ok := s.step.API.(*API) + if ok { + return api.Name + } + return "" } func (s *StepAPIWithOptionalArgs) Type() string { @@ -374,8 +379,11 @@ func (s *StepTestCaseWithOptionalArgs) Name() string { if s.step.Name != "" { return s.step.Name } - ts, _ := s.step.TestCaseContent.ToTestCase() - return ts.Config.Name + ts, ok := s.step.TestCase.(*TestCase) + if ok { + return ts.Config.Name + } + return "" } func (s *StepTestCaseWithOptionalArgs) Type() string {