Skip to content

Commit

Permalink
update : add '--summary-only'
Browse files Browse the repository at this point in the history
  • Loading branch information
chunriyeqiongsaigao committed Aug 20, 2023
1 parent baed402 commit 73a825e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 75 deletions.
26 changes: 9 additions & 17 deletions commands/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@ 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() {
Verify.Flags().StringVarP(&query, "query", "q", "", "the query to get the actual data, the result of the query should in YAML format")
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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
}

Expand Down
35 changes: 18 additions & 17 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
Formats = []string{"yaml"}
)

type CaseInfo struct {
type YamlCaseResult struct {
Passed []string
Failed []string
Skipped []string
Expand All @@ -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))
}
55 changes: 14 additions & 41 deletions pkg/output/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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
}

Expand All @@ -76,51 +73,31 @@ 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
}

p.spinner.Success(msg)
}

func (p *printer) Warning(msg string) {
if p.batchOutput {
return
}

if p.outputInFormat {
if p.batchOutput || p.outputInFormat || p.summaryOnly {
return
}

p.spinner.Warning(msg)
}

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
}

Expand All @@ -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())

}
}
Expand Down

0 comments on commit 73a825e

Please sign in to comment.