count empty files in the number of parsed files in text output

This commit is contained in:
Yann Hamon 2020-10-18 13:52:40 +02:00
parent 54d899f8f6
commit 51732e6a21
5 changed files with 24 additions and 5 deletions

View file

@ -19,7 +19,7 @@
} }
@test "Pass when parsing a valid Kubernetes config YAML file with generate name" { @test "Pass when parsing a valid Kubernetes config YAML file with generate name" {
run bin/kubeconform fixtures/generate_name.yaml run bin/kubeconform -verbose fixtures/generate_name.yaml
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
[ "$output" = "fixtures/generate_name.yaml - Job pi-{{ generateName }} is valid" ] [ "$output" = "fixtures/generate_name.yaml - Job pi-{{ generateName }} is valid" ]
} }
@ -58,6 +58,12 @@
[ "$output" = "fixtures/not-here - failed validation: open fixtures/not-here: no such file or directory" ] [ "$output" = "fixtures/not-here - failed validation: open fixtures/not-here: no such file or directory" ]
} }
@test "Pass when parsing a blank config file" {
run bin/kubeconform -summary fixtures/blank.yaml
[ "$status" -eq 0 ]
[ "$output" = "Summary: 0 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0 Skipped: 0" ]
}
@test "Fail when parsing a config with additional properties and strict set" { @test "Fail when parsing a config with additional properties and strict set" {
run bin/kubeconform -strict -k8sversion 1.16.0 fixtures/extra_property.yaml run bin/kubeconform -strict -k8sversion 1.16.0 fixtures/extra_property.yaml
[ "$status" -eq 1 ] [ "$status" -eq 1 ]

View file

@ -65,7 +65,12 @@ func ValidateStream(r io.Reader, regs []registry.Registry, k8sVersion string, c
return []validationResult{{err: fmt.Errorf("failed reading file: %s", err)}} return []validationResult{{err: fmt.Errorf("failed reading file: %s", err)}}
} }
validationResults := []validationResult{} validationResults := []validationResult{}
if len(rawResources) == 0 {
// In case a file has no resources, we want to capture that the file was parsed - and therefore send a message with an empty resource and no error
validationResults = append(validationResults, validationResult{kind: "", version: "", Name: "", err: nil, skipped: false})
}
for _, rawResource := range rawResources { for _, rawResource := range rawResources {
var sig resource.Signature var sig resource.Signature
@ -75,6 +80,7 @@ func ValidateStream(r io.Reader, regs []registry.Registry, k8sVersion string, c
} }
if sig.Kind == "" { if sig.Kind == "" {
validationResults = append(validationResults, validationResult{kind: "", version: "", Name: "", err: nil, skipped: false})
continue // We skip resoures that don't have a Kind defined continue // We skip resoures that don't have a Kind defined
} }

View file

@ -41,7 +41,7 @@ func JSON(w io.Writer, withSummary bool, verbose bool) Output {
func (o *jsono) Write(filename, kind, name, version string, err error, skipped bool) error { func (o *jsono) Write(filename, kind, name, version string, err error, skipped bool) error {
msg, st := "", "" msg, st := "", ""
s := status(err, skipped) s := status(kind, name, err, skipped)
switch s { switch s {
case VALID: case VALID:
@ -58,9 +58,10 @@ func (o *jsono) Write(filename, kind, name, version string, err error, skipped b
case SKIPPED: case SKIPPED:
st = "SKIPPED" st = "SKIPPED"
o.nSkipped++ o.nSkipped++
case EMPTY:
} }
if o.verbose || (s != VALID && s != SKIPPED) { if o.verbose || (s != VALID && s != SKIPPED && s != EMPTY ) {
o.results = append(o.results, result{Filename: filename, Kind: kind, Name: name, Version: version, Status: st, Msg: msg}) o.results = append(o.results, result{Filename: filename, Kind: kind, Name: name, Version: version, Status: st, Msg: msg})
} }

View file

@ -10,6 +10,7 @@ const (
INVALID INVALID
ERROR ERROR
SKIPPED SKIPPED
EMPTY
) )
type Output interface { type Output interface {
@ -17,7 +18,11 @@ type Output interface {
Flush() error Flush() error
} }
func status(err error, skipped bool) int { func status(kind, name string, err error, skipped bool) int {
if name == "" && kind == "" && err == nil && skipped == false {
return EMPTY
}
if skipped { if skipped {
return SKIPPED return SKIPPED
} }

View file

@ -36,7 +36,7 @@ func (o *text) Write(filename, kind, name, version string, reserr error, skipped
var err error var err error
o.files[filename] = true o.files[filename] = true
switch status(reserr, skipped) { switch status(kind, name, reserr, skipped) {
case VALID: case VALID:
if o.verbose { if o.verbose {
_, err = fmt.Fprintf(o.w, "%s - %s %s is valid\n", filename, kind, name) _, err = fmt.Fprintf(o.w, "%s - %s %s is valid\n", filename, kind, name)
@ -57,6 +57,7 @@ func (o *text) Write(filename, kind, name, version string, reserr error, skipped
_, err = fmt.Fprintf(o.w, "%s - %s %s skipped\n", filename, name, kind) _, err = fmt.Fprintf(o.w, "%s - %s %s skipped\n", filename, name, kind)
} }
o.nSkipped++ o.nSkipped++
case EMPTY:
} }
return err return err