From 73a825e3339b7fe98eae801df113e216b5abac35 Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Sun, 20 Aug 2023 17:02:05 +0800 Subject: [PATCH] update : add '--summary-only' --- commands/verify/verify.go | 26 +++++++----------- pkg/output/output.go | 35 +++++++++++++------------ pkg/output/printer.go | 55 ++++++++++----------------------------- 3 files changed, 41 insertions(+), 75 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index c41e0c2..eb97a45 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -33,12 +33,11 @@ import ( ) var ( - query string - actual string - expected string - outputDiff bool - printer output.Printer - caseInfo output.CaseInfo + query string + actual string + expected string + summaryOnly bool + printer output.Printer ) func init() { @@ -46,7 +45,7 @@ func init() { Verify.Flags().StringVarP(&actual, "actual", "a", "", "the actual data file, only YAML file format is supported") Verify.Flags().StringVarP(&expected, "expected", "e", "", "the expected data file, only YAML file format is supported") Verify.Flags().StringVarP(&output.Format, "output", "o", "", "output the verify summary in which format") - Verify.Flags().BoolVarP(&outputDiff, "no-diff", "", false, "") + Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -195,7 +194,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err defer func() { if output.Format != "" { - output.PrintTheOutput(caseInfo) + output.PrintResult(res) } else { _, errNum, _ := printer.PrintResult(res) if errNum > 0 { @@ -207,6 +206,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err for idx := range verify.Cases { printer.Start() v := &verify.Cases[idx] + res[idx].CaseName = caseName(v) if v.GetExpected() == "" { res[idx].Skip = false @@ -230,8 +230,6 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } res[idx].Skip = false printer.Success(res[idx].Msg) - // add the passed cases to caseInfo - caseInfo.AddPassedCase(caseName(v)) break } else if current != verifyInfo.retryCount { if current == 0 { @@ -247,13 +245,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err printer.UpdateText(fmt.Sprintf("failed to verify %v, retry [%d/%d]", caseName(v), current, verifyInfo.retryCount)) printer.Warning(res[idx].Msg) printer.Fail(res[idx].Err.Error()) - // add the failed cases to verifyResult - caseInfo.AddFailedCase(caseName(v)) if verifyInfo.failFast { - // add the skipped cases to caseInfo - for j := idx + 1; j < len(verify.Cases); j++ { - caseInfo.AddSkippedCase(caseName(&verify.Cases[j])) - } return } } @@ -307,7 +299,7 @@ func DoVerifyAccordingConfig() error { return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(util.BatchMode, output.Format != "", outputDiff) + printer = output.NewPrinter(util.BatchMode, output.Format != "", summaryOnly) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/pkg/output/output.go b/pkg/output/output.go index acaab0d..e1b5ba5 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -10,7 +10,7 @@ var ( Formats = []string{"yaml"} ) -type CaseInfo struct { +type YamlCaseResult struct { Passed []string Failed []string Skipped []string @@ -26,26 +26,27 @@ func FormatIsNotExist() bool { return true } -func PrintTheOutput(caseInfo CaseInfo) { +func PrintResult(caseRes []*CaseResult) { switch Format { case "yaml": - caseInfo.PrintInYAML() + PrintResultInYAML(caseRes) } } -func (caseInfo *CaseInfo) PrintInYAML() { - yaml, _ := yaml.Marshal(caseInfo) - fmt.Print(string(yaml)) -} - -func (caseInfo *CaseInfo) AddPassedCase(caseName string) { - caseInfo.Passed = append(caseInfo.Passed, caseName) -} - -func (caseInfo *CaseInfo) AddFailedCase(caseName string) { - caseInfo.Failed = append(caseInfo.Failed, caseName) -} +func PrintResultInYAML(caseRes []*CaseResult) { + var yamlCaseResult YamlCaseResult + for _, cr := range caseRes { + if !cr.Skip { + if cr.Err == nil { + yamlCaseResult.Passed = append(yamlCaseResult.Passed, cr.CaseName) + } else { + yamlCaseResult.Failed = append(yamlCaseResult.Failed, cr.CaseName) + } + } else { + yamlCaseResult.Skipped = append(yamlCaseResult.Skipped, cr.CaseName) + } + } -func (caseInfo *CaseInfo) AddSkippedCase(caseName string) { - caseInfo.Skipped = append(caseInfo.Skipped, caseName) + yaml, _ := yaml.Marshal(yamlCaseResult) + fmt.Print(string(yaml)) } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index 3b8d461..5224b58 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -24,9 +24,10 @@ import ( // CaseResult represents the result of a verification case. type CaseResult struct { - Msg string - Err error - Skip bool + CaseName string + Msg string + Err error + Skip bool } type Printer interface { @@ -42,12 +43,12 @@ type printer struct { spinner *pterm.SpinnerPrinter batchOutput bool outputInFormat bool - outputDiff bool + summaryOnly bool } var _ Printer = &printer{} -func NewPrinter(batchOutput bool, outputInFormat bool, outputDiff bool) Printer { +func NewPrinter(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", @@ -58,16 +59,12 @@ func NewPrinter(batchOutput bool, outputInFormat bool, outputDiff bool) Printer spinner: spinner, batchOutput: batchOutput, outputInFormat: outputInFormat, - outputDiff: outputDiff, + summaryOnly: summaryOnly, } } func (p *printer) Start(msg ...string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -76,11 +73,7 @@ func (p *printer) Start(msg ...string) { func (p *printer) Success(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -88,11 +81,7 @@ func (p *printer) Success(msg string) { } func (p *printer) Warning(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -100,27 +89,15 @@ func (p *printer) Warning(msg string) { } func (p *printer) Fail(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } - if !p.outputDiff { - p.spinner.Fail(msg) - } else { - println() - } + p.spinner.Fail(msg) } func (p *printer) UpdateText(text string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -143,11 +120,7 @@ func (p *printer) PrintResult(caseRes []*CaseResult) (passNum, failNum, skipNum failNum++ if p.batchOutput { p.spinner.Warning(cr.Msg) - if !p.outputDiff { - p.spinner.Fail(cr.Err.Error()) - } else { - fmt.Println() - } + p.spinner.Fail(cr.Err.Error()) } }