Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Go Report Card Issues #131

Merged
merged 15 commits into from
Apr 10, 2024
Prev Previous commit
Next Next commit
Fix file output on quiet flag
HakanVardarr committed Mar 30, 2024
commit 621af20236c451a708be1664f63354938b1f18ee
9 changes: 3 additions & 6 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -101,6 +101,7 @@ func (c CLI) Run() (int, error) {
FilePath: fileToValidate.Path,
IsValid: isValid,
ValidationError: err,
IsQuiet: Quiet,
}
reports = append(reports, report)
}
@@ -121,10 +122,6 @@ func (c CLI) Run() (int, error) {
// printReports prints the reports based on the specified grouping and reporter type.
// It returns any error encountered during the printing process.
func (c CLI) printReports(reports []reporter.Report) error {
if Quiet {
return nil
}

switch len(GroupOutput) {
case 1:
return c.printGroupSingle(reports)
@@ -151,8 +148,8 @@ func (c CLI) printGroupSingle(reports []reporter.Report) error {
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
return reporter.PrintSingleGroupJson(reportGroup)
}

return reporter.PrintSingleGroupStdout(reportGroup)

}

func (c CLI) printGroupDouble(reports []reporter.Report) error {
@@ -165,8 +162,8 @@ func (c CLI) printGroupDouble(reports []reporter.Report) error {
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
return reporter.PrintDoubleGroupJson(reportGroup)
}

return reporter.PrintDoubleGroupStdout(reportGroup)

}

func (c CLI) printGroupTriple(reports []reporter.Report) error {
4 changes: 3 additions & 1 deletion pkg/reporter/json_reporter.go
Original file line number Diff line number Diff line change
@@ -68,7 +68,9 @@ func (jr JsonReporter) Print(reports []Report) error {
}

jsonBytes = append(jsonBytes, '\n')
fmt.Print(string(jsonBytes))
if !reports[0].IsQuiet {
fmt.Print(string(jsonBytes))
}

if jr.outputDest != "" {
return outputBytesToFile(jr.outputDest, "result", "json", jsonBytes)
1 change: 1 addition & 0 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ type Report struct {
FilePath string
IsValid bool
ValidationError error
IsQuiet bool
}

// Reporter is the interface that wraps the Print method
29 changes: 29 additions & 0 deletions pkg/reporter/reporter_test.go
Original file line number Diff line number Diff line change
@@ -16,20 +16,23 @@ func Test_stdoutReport(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -47,20 +50,23 @@ func Test_jsonReport(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithBackslashPath := Report{
"good.xml",
"\\fake\\path\\good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithBackslashPath}
@@ -111,20 +117,23 @@ func Test_junitReport(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithBackslashPath := Report{
"good.xml",
"\\fake\\path\\good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reports := []Report{reportNoValidationError, reportWithBackslashPath, reportWithValidationError}
@@ -143,6 +152,7 @@ func Test_jsonReporterWriter(t *testing.T) {
"test/output/example/good.json",
true,
nil,
false,
}
)
deleteFiles(t)
@@ -250,6 +260,7 @@ func Test_JunitReporter_OutputBytesToFile(t *testing.T) {
"test/output/example/good.json",
true,
nil,
false,
}
)
deleteFiles(t)
@@ -398,20 +409,23 @@ func Test_stdoutReportSingleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -430,20 +444,23 @@ func Test_stdoutReportDoubleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -462,20 +479,23 @@ func Test_stdoutReportTripleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -497,20 +517,23 @@ func Test_jsonReportSingleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -529,20 +552,23 @@ func Test_jsonReportDoubleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
@@ -561,20 +587,23 @@ func Test_jsonReportTripleGroup(t *testing.T) {
"/fake/path/good.xml",
true,
nil,
false,
}

reportWithValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse bad.xml file"),
false,
}

reportWithMultiLineValidationError := Report{
"bad.xml",
"/fake/path/bad.xml",
false,
errors.New("Unable to parse keys:\nkey1\nkey2"),
false,
}

reports := []Report{reportNoValidationError, reportWithValidationError, reportWithMultiLineValidationError}
5 changes: 4 additions & 1 deletion pkg/reporter/stdout_reporter.go
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@ type StdoutReporter struct{}
// Print implements the Reporter interface by outputting
// the report content to stdout
func (sr StdoutReporter) Print(reports []Report) error {
if reports[0].IsQuiet {
return nil
}

var results string
var successCount = 0
var failureCount = 0
@@ -32,7 +36,6 @@ func (sr StdoutReporter) Print(reports []Report) error {
successCount = successCount + 1
}
}
fmt.Printf("Summary: %d succeeded, %d failed\n", successCount, failureCount)

return nil
}
40 changes: 40 additions & 0 deletions test/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"files": [
{
"path": "test/fixtures/uppercase-extention/good.CSV",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.HCL",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.INI",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.JSON",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.PLIST",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.PROPERTIES",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.TOML",
"status": "passed"
},
{
"path": "test/fixtures/uppercase-extention/good.YAML",
"status": "passed"
}
],
"summary": {
"passed": 8,
"failed": 0
}
}