Skip to content

Commit

Permalink
improve test coverge for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed Oct 2, 2018
1 parent c71aff5 commit 849132f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 19 deletions.
41 changes: 22 additions & 19 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,6 +69,14 @@ type (
Debug *bool
Args []string
}

// ReportWriter formats and displays a ValidationReport
ReportWriter interface {
WriteReport(assertion.ValidationReport, LinterOptions)
}

// DefaultReportWriter writes the report to Stdout
DefaultReportWriter struct{}
)

//go:generate go-bindata -pkg $GOPACKAGE -o assets.go assets/
Expand All @@ -85,8 +94,7 @@ func main() {
}

if *commandLineOptions.Validate {
validateRules(commandLineOptions.Args)
return
os.Exit(validateRules(commandLineOptions.Args, DefaultReportWriter{}))
}

profileOptions, err := loadProfile(*commandLineOptions.ProfileFilename)
Expand Down Expand Up @@ -124,7 +132,7 @@ func main() {
}
ruleSets = append(ruleSets, builtInRuleSet)
}
applyRules(ruleSets, configFilenames, linterOptions)
os.Exit(applyRules(ruleSets, configFilenames, linterOptions, DefaultReportWriter{}))
}

func addExceptions(ruleSets []assertion.RuleSet, exceptions []RuleException) []assertion.RuleSet {
Expand All @@ -151,17 +159,17 @@ func addExceptionsToRuleSet(ruleSet assertion.RuleSet, exceptions []RuleExceptio
return ruleSet
}

func validateRules(filenames []string) {
func validateRules(filenames []string, w ReportWriter) int {
builtInRuleSet, err := loadBuiltInRuleSet("assets/lint-rules.yml")
if err != nil {
fmt.Printf("Unable to load build-in rules for validation: %v\n", err)
return
return -1
}
ruleSets := []assertion.RuleSet{builtInRuleSet}
linterOptions := LinterOptions{
QueryExpression: "Violations[]",
}
applyRules(ruleSets, filenames, linterOptions)
return applyRules(ruleSets, filenames, linterOptions, w)
}

func loadRuleSets(args arrayFlags) ([]assertion.RuleSet, error) {
Expand Down Expand Up @@ -214,7 +222,7 @@ func loadBuiltInRuleSet(name string) (assertion.RuleSet, error) {
return ruleSet, nil
}

func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options LinterOptions) {
func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options LinterOptions, w ReportWriter) int {

report := assertion.ValidationReport{
Violations: []assertion.Violation{},
Expand All @@ -229,7 +237,7 @@ func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options LinterOpt
l, err := linter.NewLinter(ruleSet, vs, filenames)
if err != nil {
fmt.Println(err)
return
return -1
}
if l != nil {
if options.SearchExpression != "" {
Expand All @@ -248,16 +256,11 @@ func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options LinterOpt
}
}
}
if options.SearchExpression == "" {
err := printReport(report, options.QueryExpression)
if err != nil {
fmt.Println(err.Error())
}
}
os.Exit(generateExitCode(report))
w.WriteReport(report, options)
return generateExitCode(report)
}

func printReport(report assertion.ValidationReport, queryExpression string) error {
func printReport(w io.Writer, report assertion.ValidationReport, queryExpression string) error {
jsonData, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
Expand All @@ -274,10 +277,10 @@ func printReport(report assertion.ValidationReport, queryExpression string) erro
}
s, err := assertion.JSONStringify(v)
if err == nil && s != "null" {
fmt.Println(s)
fmt.Fprintln(w, s)
}
} else {
fmt.Println(string(jsonData))
fmt.Fprintln(w, string(jsonData))
}
return nil
}
Expand All @@ -299,7 +302,7 @@ func (i *arrayFlags) Set(value string) error {
func generateExitCode(report assertion.ValidationReport) int {
for _, v := range report.Violations {
if v.Status == "FAILURE" {
return 1
return -1
}
}
return 0
Expand Down
56 changes: 56 additions & 0 deletions cli/app_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"bytes"
"github.com/stelligent/config-lint/assertion"
"github.com/stelligent/config-lint/linter"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down Expand Up @@ -132,3 +134,57 @@ func TestBuiltInLinterRules(t *testing.T) {
t.Errorf("Expecting Validate for built in lint-rules to not report any violations: %v", report.Violations)
}
}

func TestPrintReport(t *testing.T) {
r := assertion.ValidationReport{}
var b bytes.Buffer
err := printReport(&b, r, "")
assert.Nil(t, err, "Expecting printReport to run without error")
assert.Contains(t, b.String(), "FilesScanned\": null")
assert.Contains(t, b.String(), "ResourcesScanned\": null")
assert.Contains(t, b.String(), "Violations\": null")
}

func TestPrintReportWithQueryString(t *testing.T) {
r := assertion.ValidationReport{
Violations: []assertion.Violation{
assertion.Violation{RuleMessage: "Houston, we have a problem"},
},
}
var b bytes.Buffer
err := printReport(&b, r, "Violations[]")
assert.Nil(t, err, "Expecting printReport to run without error")
assert.Contains(t, b.String(), "RuleMessage")
assert.NotContains(t, b.String(), "Violations")
assert.NotContains(t, b.String(), "FilesScanned")
assert.NotContains(t, b.String(), "ResourcesScanned")
}

type TestReportWriter struct {
Report assertion.ValidationReport
}

func (w TestReportWriter) WriteReport(r assertion.ValidationReport, o LinterOptions) {
w.Report = r
}

func TestApplyRules(t *testing.T) {
ruleSets := []assertion.RuleSet{
assertion.RuleSet{
Type: "JSON",
},
}
args := arrayFlags{}
options := LinterOptions{}
w := TestReportWriter{}
exitCode := applyRules(ruleSets, args, options, w)
assert.Equal(t, exitCode, 0, "Expecting applyRules to return 0")
assert.Empty(t, w.Report.Violations, "Expecting empty report")
}

func TestValidateRules(t *testing.T) {
filenames := []string{"./testdata/has-properties.yml"}
w := TestReportWriter{}
validateRules(filenames, w)
assert.Empty(t, w.Report.Violations, "Expecting empty report for validateRules")
}
16 changes: 16 additions & 0 deletions cli/report_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"github.com/stelligent/config-lint/assertion"
"os"
)

func (w DefaultReportWriter) WriteReport(report assertion.ValidationReport, options LinterOptions) {
if options.SearchExpression == "" {
err := printReport(os.Stdout, report, options.QueryExpression)
if err != nil {
fmt.Println(err.Error())
}
}
}

0 comments on commit 849132f

Please sign in to comment.