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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bin/validator
Binary file not shown.
1 change: 1 addition & 0 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func cleanString(command string) string {

func mainInit() int {
validatorConfig, err := getFlags()

if err != nil {
return 1
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func Test_flags(t *testing.T) {
{"wrong output set", []string{"--output", "/path/not/exist", "--reporter", "json", "."}, 1},
{"incorrect group", []string{"-groupby=badgroup", "."}, 1},
{"correct group", []string{"-groupby=directory", "."}, 0},
{"grouped junit", []string{"-groupby=directory", "--reporter=junit", "."}, 1},
{"groupby duplicate", []string{"--groupby=directory,directory", "."}, 1},
{"quiet flag", []string{"--quiet=true", "."}, 0},
}
for _, tc := range cases {
// this call is required because otherwise flags panics,
Expand Down
114 changes: 76 additions & 38 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// store the group by options that the user specifies
var GroupOutput []string
var Quiet bool
var errorFound bool

type CLI struct {
// FileFinder interface to search for the files
Expand Down Expand Up @@ -77,7 +78,7 @@ func Init(opts ...CLIOption) *CLI {
// - Calls the Validate method from the Validator interface to validate the file
// - Outputs the results using the Reporter
func (c CLI) Run() (int, error) {
errorFound := false
errorFound = false
var reports []reporter.Report
foundFiles, err := c.Finder.Find()

Expand All @@ -101,56 +102,93 @@ func (c CLI) Run() (int, error) {
FilePath: fileToValidate.Path,
IsValid: isValid,
ValidationError: err,
IsQuiet: Quiet,
}
reports = append(reports, report)

}

err = c.printReports(reports)
if err != nil {
return 1, err
}

if errorFound {
return 1, nil
} else {
return 0, nil
}
}

// Group the output if the user specified a group by option
// Length is equal to one when empty as it contains an empty string
// 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 len(GroupOutput) == 1 && GroupOutput[0] != "" {
reportGroup, err := GroupBySingle(reports, GroupOutput[0])
if err != nil {
return 1, fmt.Errorf("unable to group by single value: %v", err)
}
// Check reporter type to determine how to print
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintSingleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintSingleGroupStdout(reportGroup)
}
return c.printGroupSingle(reports)
} else if len(GroupOutput) == 2 {
reportGroup, err := GroupByDouble(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by double value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintDoubleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintDoubleGroupStdout(reportGroup)
}

return c.printGroupDouble(reports)
} else if len(GroupOutput) == 3 {
reportGroup, err := GroupByTriple(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by triple value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintTripleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintTripleGroupStdout(reportGroup)
}
return c.printGroupTriple(reports)
} else {
if !Quiet {
err = c.Reporter.Print(reports)
}
err := c.Reporter.Print(reports)

if err != nil {
fmt.Println("failed to report:", err)
errorFound = true
}

return nil
}
if errorFound {
return 1, nil

}

func (c CLI) printGroupSingle(reports []reporter.Report) error {

reportGroup, err := GroupBySingle(reports, GroupOutput[0])
if err != nil {
return fmt.Errorf("unable to group by single value: %v", err)
}

// Check reporter type to determine how to print
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintSingleGroupJson(reportGroup)
} else {
return 0, nil
reporter.PrintSingleGroupStdout(reportGroup)
}

return nil

}

func (c CLI) printGroupDouble(reports []reporter.Report) error {
reportGroup, err := GroupByDouble(reports, GroupOutput)
if err != nil {
return fmt.Errorf("unable to group by double value: %v", err)
}

// Check reporter type to determine how to print
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintDoubleGroupJson(reportGroup)
} else {
reporter.PrintDoubleGroupStdout(reportGroup)
}

return nil

}

func (c CLI) printGroupTriple(reports []reporter.Report) error {
reportGroup, err := GroupByTriple(reports, GroupOutput)
if err != nil {
return fmt.Errorf("unable to group by triple value: %v", err)
}

if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintTripleGroupJson(reportGroup)
} else {

reporter.PrintTripleGroupStdout(reportGroup)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Test_CLIRepoertErr(t *testing.T) {
exitStatus, err := cli.Run()

if err != nil {
t.Errorf("An error was returned: %v", err)
t.Errorf("An error returned: %v", err)
}

if exitStatus == 0 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/finder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package finder

import (
"fmt"
"github.com/Boeing/config-file-validator/pkg/misc"
"path/filepath"
"testing"

"github.com/Boeing/config-file-validator/pkg/misc"

HakanVardarr marked this conversation as resolved.
Show resolved Hide resolved
"github.com/Boeing/config-file-validator/pkg/filetype"
"github.com/Boeing/config-file-validator/pkg/validator"
)
Expand Down Expand Up @@ -208,7 +209,7 @@ func Test_FileSystemFinderAbsPath(t *testing.T) {

func Test_FileSystemFinderUpperCaseExtension(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures/uppercase-extention"),
WithPathRoots("../../test/fixtures/uppercase-extension"),
)

files, err := fsFinder.Find()
Expand All @@ -224,7 +225,7 @@ func Test_FileSystemFinderUpperCaseExtension(t *testing.T) {

func Test_FileSystemFinderMixedCaseExtension(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures/mixedcase-extention"),
WithPathRoots("../../test/fixtures/mixedcase-extension"),
)

files, err := fsFinder.Find()
Expand Down
8 changes: 7 additions & 1 deletion pkg/reporter/json_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ type tripleGroupReportJSON struct {
// if outputDest flag is provided, output results to a file.
func (jr JsonReporter) Print(reports []Report) error {
report, err := createJsonReport(reports)
if err != nil {
return err
}

jsonBytes, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}

jsonBytes = append(jsonBytes, '\n')
fmt.Print(string(jsonBytes))

if len(reports) > 0 && !reports[0].IsQuiet {
fmt.Print(string(jsonBytes))
}

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

// Reporter is the interface that wraps the Print method
Expand Down
Loading
Loading