Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from datreeio/DAT-3257-formatted-output
Browse files Browse the repository at this point in the history
fix: add evaluation summary to json and yaml output
  • Loading branch information
Yishay Mendelsohn authored May 30, 2021
2 parents 1e36e8e + 153b9be commit 2661f69
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
17 changes: 11 additions & 6 deletions bl/evaluation/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ type Printer interface {
PrintEvaluationSummary(summary printer.EvaluationSummary, k8sVersion string)
}

type FormattedOutput struct {
Results *EvaluationResults
EvaluationSummary printer.EvaluationSummary
}

func PrintResults(results *EvaluationResults, invalidFiles []*validation.InvalidFile, evaluationSummary printer.EvaluationSummary, loginURL string, outputFormat string, printer Printer, k8sVersion string) error {
switch {
case outputFormat == "json":
return jsonOutput(results)
return jsonOutput(&FormattedOutput{Results: results, EvaluationSummary: evaluationSummary})
case outputFormat == "yaml":
return yamlOutput(results)
return yamlOutput(&FormattedOutput{Results: results, EvaluationSummary: evaluationSummary})
default:
return textOutput(results, invalidFiles, evaluationSummary, loginURL, printer, k8sVersion)
}
}

func jsonOutput(results *EvaluationResults) error {
jsonOutput, err := json.Marshal(results)
func jsonOutput(formattedOutput *FormattedOutput) error {
jsonOutput, err := json.Marshal(formattedOutput)
if err != nil {
fmt.Println(err)
return err
Expand All @@ -40,8 +45,8 @@ func jsonOutput(results *EvaluationResults) error {
return nil
}

func yamlOutput(results *EvaluationResults) error {
yamlOutput, err := yaml.Marshal(results)
func yamlOutput(formattedOutput *FormattedOutput) error {
yamlOutput, err := yaml.Marshal(formattedOutput)
if err != nil {
fmt.Println(err)
return err
Expand Down
47 changes: 40 additions & 7 deletions bl/evaluation/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ func (c *mockPrinter) PrintSummaryTable(summary printer.Summary) {
c.Called(summary)
}

func (c *mockPrinter) PrintEvaluationSummary(summary printer.EvaluationSummary, k8sVersion string) {
c.Called(summary, k8sVersion)
}

type printResultsTestCaseArgs struct {
results *EvaluationResults
invalidFiles []*validation.InvalidFile
evaluationSummary printer.EvaluationSummary
loginURL string
outputFormat string
printer Printer
}

type printResultsTestCase struct {
Expand All @@ -37,29 +40,59 @@ type printResultsTestCase struct {
expected error
}

// TODO: write actual tests, what is this even?
// TODO: fill missing call assertions
func TestPrintResults(t *testing.T) {
tests := []*printResultsTestCase{}
tests := []*printResultsTestCase{
print_resultst(""),
print_resultst("json"),
print_resultst("yaml"),
}
for _, tt := range tests {
mockedPrinter := &mockPrinter{}
mockedPrinter.On("PrintWarnings", mock.Anything)
mockedPrinter.On("PrintSummaryTable", mock.Anything)
mockedPrinter.On("PrintEvaluationSummary", mock.Anything, mock.Anything)

t.Run(tt.name, func(t *testing.T) {
PrintResults(tt.args.results, tt.args.invalidFiles, tt.args.evaluationSummary, tt.args.loginURL, tt.args.outputFormat, tt.args.printer, "1.18.0")
_ = PrintResults(tt.args.results, tt.args.invalidFiles, tt.args.evaluationSummary, tt.args.loginURL, tt.args.outputFormat, mockedPrinter, "1.18.0")

if tt.args.outputFormat == "json" {
mockedPrinter.AssertNotCalled(t, "PrintWarnings")
mockedPrinter.AssertCalled(t, "PrintSummaryTable")
} else if tt.args.outputFormat == "yaml" {
mockedPrinter.AssertNotCalled(t, "PrintWarnings")
mockedPrinter.AssertCalled(t, "PrintSummaryTable")

} else {
pwd, _ := os.Getwd()
warnings, _ := parseToPrinterWarnings(tt.args.results, tt.args.invalidFiles, pwd, "1.18.0")
mockedPrinter.AssertCalled(t, "PrintWarnings", warnings)
mockedPrinter.AssertCalled(t, "PrintSummaryTable", tt.args.results, tt.args.loginURL)
}
})
}
}

func print_resultst(outputFormat string) *printResultsTestCase {
return &printResultsTestCase{
name: "Print Results Text",
args: &printResultsTestCaseArgs{
results: &EvaluationResults{
FileNameRuleMapper: map[string]map[int]*Rule{},
Summary: struct {
RulesCount int
TotalFailedRules int
FilesCount int
TotalPassedCount int
}{
RulesCount: 0,
TotalFailedRules: 0,
FilesCount: 0,
TotalPassedCount: 0,
},
},
invalidFiles: []*validation.InvalidFile{},
evaluationSummary: printer.EvaluationSummary{},
loginURL: "login/url",
outputFormat: outputFormat,
},
expected: nil,
}
}

0 comments on commit 2661f69

Please sign in to comment.