🌱 Hide maintainer annotation implementation details (#4167)

* make validation func private

Signed-off-by: Spencer Schrock <sschrock@google.com>

* hide config validation sentinel errors

Signed-off-by: Spencer Schrock <sschrock@google.com>

---------

Signed-off-by: Spencer Schrock <sschrock@google.com>
This commit is contained in:
Spencer Schrock 2024-06-12 11:16:13 -07:00 committed by GitHub
parent 1faca4943d
commit 157948d4f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 16 deletions

View File

@ -71,8 +71,9 @@ func (r *Reason) Doc() string {
}
}
// IsValidReason validates if a reason exists.
func IsValidReason(r Reason) bool {
// isValidReason checks if a reason can be used by a config file.
func isValidReason(r Reason) bool {
// the reason must be one of the preselected options
switch r {
case TestData, Remediated, NotApplicable, NotSupported, NotDetected:
return true

View File

@ -26,8 +26,8 @@ import (
)
var (
ErrInvalidCheck = errors.New("check is not valid")
ErrInvalidReason = errors.New("reason is not valid")
errInvalidCheck = errors.New("check is not valid")
errInvalidReason = errors.New("reason is not valid")
)
// Config contains configurations defined by maintainers.
@ -58,12 +58,12 @@ func validate(c Config, checks []string) error {
for _, annotation := range c.Annotations {
for _, check := range annotation.Checks {
if !isValidCheck(check, checks) {
return fmt.Errorf("%w: %s", ErrInvalidCheck, check)
return fmt.Errorf("%w: %s", errInvalidCheck, check)
}
}
for _, reasonGroup := range annotation.Reasons {
if !IsValidReason(reasonGroup.Reason) {
return fmt.Errorf("%w: %s", ErrInvalidReason, reasonGroup.Reason)
if !isValidReason(reasonGroup.Reason) {
return fmt.Errorf("%w: %s", errInvalidReason, reasonGroup.Reason)
}
}
}

View File

@ -17,7 +17,6 @@
package config_test
import (
"errors"
"os"
"testing"
@ -32,8 +31,8 @@ func Test_Parse_Checks(t *testing.T) {
tests := []struct {
name string
configPath string
wantErr error
want config.Config
wantErr bool
}{
{
name: "Annotation on a single check",
@ -115,12 +114,12 @@ func Test_Parse_Checks(t *testing.T) {
{
name: "Invalid check",
configPath: "testdata/invalid_check.yml",
wantErr: config.ErrInvalidCheck,
wantErr: true,
},
{
name: "Invalid reason",
configPath: "testdata/invalid_reason.yml",
wantErr: config.ErrInvalidReason,
wantErr: true,
},
}
for _, tt := range tests {
@ -136,11 +135,8 @@ func Test_Parse_Checks(t *testing.T) {
t.Fatalf("Could not open config test file: %s", tt.configPath)
}
result, err := config.Parse(r, allChecks)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Fatalf("Unexpected error during Parse: got %v, wantErr %v", err, tt.wantErr)
}
return
if (err != nil) != tt.wantErr {
t.Fatalf("Unexpected error during Parse: got %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.want, result); diff != "" {
t.Errorf("Config mismatch (-want +got):\n%s", diff)