diff --git a/src/pkg/packager/lint/lint.go b/src/pkg/packager/lint/lint.go index c9574e16b5..b729dcf51d 100644 --- a/src/pkg/packager/lint/lint.go +++ b/src/pkg/packager/lint/lint.go @@ -33,7 +33,7 @@ var ZarfSchema FileLoader // Validate validates a zarf file func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) ([]types.PackageFinding, error) { var pkg types.ZarfPackage - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding if err := utils.ReadYaml(layout.ZarfYAML, &pkg); err != nil { return nil, err } @@ -41,7 +41,7 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) ([]types. if err != nil { return nil, err } - pkgErrs = append(pkgErrs, compFindings...) + findings = append(findings, compFindings...) jsonSchema, err := ZarfSchema.ReadFile("zarf.schema.json") if err != nil { @@ -57,13 +57,13 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) ([]types. if err != nil { return nil, err } - pkgErrs = append(pkgErrs, schemaFindings...) + findings = append(findings, schemaFindings...) - return pkgErrs, nil + return findings, nil } func lintComponents(ctx context.Context, pkg types.ZarfPackage, createOpts types.ZarfCreateOptions) ([]types.PackageFinding, error) { - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding for i, component := range pkg.Components { arch := config.GetArch(pkg.Metadata.Architecture) @@ -80,26 +80,26 @@ func lintComponents(ctx context.Context, pkg types.ZarfPackage, createOpts types node := chain.Head() for node != nil { component := node.ZarfComponent - nodeErrs := fillComponentTemplate(&component, &createOpts) - nodeErrs = append(nodeErrs, checkComponent(component, node.Index())...) - for i := range nodeErrs { - nodeErrs[i].PackagePathOverride = node.ImportLocation() - nodeErrs[i].PackageNameOverride = node.OriginalPackageName() + compFindings := fillComponentTemplate(&component, &createOpts) + compFindings = append(compFindings, checkComponent(component, node.Index())...) + for i := range compFindings { + compFindings[i].PackagePathOverride = node.ImportLocation() + compFindings[i].PackageNameOverride = node.OriginalPackageName() } - pkgErrs = append(pkgErrs, nodeErrs...) + findings = append(findings, compFindings...) node = node.Next() } } - return pkgErrs, nil + return findings, nil } func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateOptions) []types.PackageFinding { err := creator.ReloadComponentTemplate(c) - var nodeErrs []types.PackageFinding + var findings []types.PackageFinding if err != nil { - nodeErrs = append(nodeErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ Description: err.Error(), - Category: types.SevWarn, + Severity: types.SevWarn, }) } templateMap := map[string]string{} @@ -107,24 +107,24 @@ func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateO setVarsAndWarn := func(templatePrefix string, deprecated bool) { yamlTemplates, err := utils.FindYamlTemplates(c, templatePrefix, "###") if err != nil { - nodeErrs = append(nodeErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ Description: err.Error(), - Category: types.SevWarn, + Severity: types.SevWarn, }) } for key := range yamlTemplates { if deprecated { - nodeErrs = append(nodeErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ Description: fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key), - Category: types.SevWarn, + Severity: types.SevWarn, }) } _, present := createOpts.SetVariables[key] if !present { - nodeErrs = append(nodeErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ Description: lang.UnsetVarLintWarning, - Category: types.SevWarn, + Severity: types.SevWarn, }) } } @@ -140,7 +140,7 @@ func fillComponentTemplate(c *types.ZarfComponent, createOpts *types.ZarfCreateO //nolint: errcheck // This error should bubble up utils.ReloadYamlTemplate(c, templateMap) - return nodeErrs + return findings } func isPinnedImage(image string) (bool, error) { @@ -161,69 +161,69 @@ func isPinnedRepo(repo string) bool { // checkComponent runs lint rules against a component func checkComponent(c types.ZarfComponent, i int) []types.PackageFinding { - var pkgErrs []types.PackageFinding - pkgErrs = append(pkgErrs, checkForUnpinnedRepos(c, i)...) - pkgErrs = append(pkgErrs, checkForUnpinnedImages(c, i)...) - pkgErrs = append(pkgErrs, checkForUnpinnedFiles(c, i)...) - return pkgErrs + var findings []types.PackageFinding + findings = append(findings, checkForUnpinnedRepos(c, i)...) + findings = append(findings, checkForUnpinnedImages(c, i)...) + findings = append(findings, checkForUnpinnedFiles(c, i)...) + return findings } func checkForUnpinnedRepos(c types.ZarfComponent, i int) []types.PackageFinding { - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding for j, repo := range c.Repos { repoYqPath := fmt.Sprintf(".components.[%d].repos.[%d]", i, j) if !isPinnedRepo(repo) { - pkgErrs = append(pkgErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ YqPath: repoYqPath, Description: "Unpinned repository", Item: repo, - Category: types.SevWarn, + Severity: types.SevWarn, }) } } - return pkgErrs + return findings } func checkForUnpinnedImages(c types.ZarfComponent, i int) []types.PackageFinding { - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding for j, image := range c.Images { imageYqPath := fmt.Sprintf(".components.[%d].images.[%d]", i, j) pinnedImage, err := isPinnedImage(image) if err != nil { - pkgErrs = append(pkgErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ YqPath: imageYqPath, Description: "Failed to parse image reference", Item: image, - Category: types.SevWarn, + Severity: types.SevWarn, }) continue } if !pinnedImage { - pkgErrs = append(pkgErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ YqPath: imageYqPath, Description: "Image not pinned with digest", Item: image, - Category: types.SevWarn, + Severity: types.SevWarn, }) } } - return pkgErrs + return findings } func checkForUnpinnedFiles(c types.ZarfComponent, i int) []types.PackageFinding { - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding for j, file := range c.Files { fileYqPath := fmt.Sprintf(".components.[%d].files.[%d]", i, j) if file.Shasum == "" && helpers.IsURL(file.Source) { - pkgErrs = append(pkgErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ YqPath: fileYqPath, Description: "No shasum for remote file", Item: file.Source, - Category: types.SevWarn, + Severity: types.SevWarn, }) } } - return pkgErrs + return findings } func makeFieldPathYqCompat(field string) string { @@ -240,7 +240,7 @@ func makeFieldPathYqCompat(field string) string { } func validateSchema(jsonSchema []byte, untypedZarfPackage interface{}) ([]types.PackageFinding, error) { - var pkgErrs []types.PackageFinding + var findings []types.PackageFinding schemaErrors, err := runSchema(jsonSchema, untypedZarfPackage) if err != nil { @@ -249,15 +249,15 @@ func validateSchema(jsonSchema []byte, untypedZarfPackage interface{}) ([]types. if len(schemaErrors) != 0 { for _, schemaErr := range schemaErrors { - pkgErrs = append(pkgErrs, types.PackageFinding{ + findings = append(findings, types.PackageFinding{ YqPath: makeFieldPathYqCompat(schemaErr.Field()), Description: schemaErr.Description(), - Category: types.SevErr, + Severity: types.SevErr, }) } } - return pkgErrs, err + return findings, err } func runSchema(jsonSchema []byte, pkg interface{}) ([]gojsonschema.ResultError, error) { diff --git a/src/pkg/packager/lint/lint_test.go b/src/pkg/packager/lint/lint_test.go index c16ee90873..8311218c6b 100644 --- a/src/pkg/packager/lint/lint_test.go +++ b/src/pkg/packager/lint/lint_test.go @@ -152,14 +152,14 @@ func TestValidateSchema(t *testing.T) { }, }, } - for _, tc := range tests { - tt := tc + for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - schemaErrs, err := runSchema(getZarfSchema(t), tt.pkg) + findings, err := runSchema(getZarfSchema(t), tt.pkg) require.NoError(t, err) var schemaStrings []string - for _, schemaErr := range schemaErrs { + for _, schemaErr := range findings { schemaStrings = append(schemaStrings, schemaErr.String()) } require.ElementsMatch(t, tt.expectedSchemaStrings, schemaStrings) @@ -195,16 +195,16 @@ func TestValidateComponent(t *testing.T) { unpinnedRepo, "https://dev.azure.com/defenseunicorns/zarf-public-test/_git/zarf-public-test@v0.0.1", }} - pkgErrs := checkForUnpinnedRepos(component, 0) + findings := checkForUnpinnedRepos(component, 0) expected := []types.PackageFinding{ { Item: unpinnedRepo, Description: "Unpinned repository", - Category: types.SevWarn, + Severity: types.SevWarn, YqPath: ".components.[0].repos.[0]", }, } - require.Equal(t, expected, pkgErrs) + require.Equal(t, expected, findings) }) t.Run("Unpinnned image warning", func(t *testing.T) { @@ -216,22 +216,22 @@ func TestValidateComponent(t *testing.T) { "busybox:latest@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79", badImage, }} - pkgErrs := checkForUnpinnedImages(component, 0) + findings := checkForUnpinnedImages(component, 0) expected := []types.PackageFinding{ { Item: unpinnedImage, Description: "Image not pinned with digest", - Category: types.SevWarn, + Severity: types.SevWarn, YqPath: ".components.[0].images.[0]", }, { Item: badImage, Description: "Failed to parse image reference", - Category: types.SevWarn, + Severity: types.SevWarn, YqPath: ".components.[0].images.[2]", }, } - require.Equal(t, expected, pkgErrs) + require.Equal(t, expected, findings) }) t.Run("Unpinnned file warning", func(t *testing.T) { @@ -251,17 +251,17 @@ func TestValidateComponent(t *testing.T) { }, } component := types.ZarfComponent{Files: zarfFiles} - pkgErrs := checkForUnpinnedFiles(component, 0) + findings := checkForUnpinnedFiles(component, 0) expectedErr := []types.PackageFinding{ { Item: fileURL, Description: "No shasum for remote file", - Category: types.SevWarn, + Severity: types.SevWarn, YqPath: ".components.[0].files.[0]", }, } - require.Equal(t, expectedErr, pkgErrs) - require.Len(t, pkgErrs, 1) + require.Equal(t, expectedErr, findings) + require.Len(t, findings, 1) }) t.Run("Wrap standalone numbers in bracket", func(t *testing.T) { diff --git a/src/pkg/packager/lint/validator.go b/src/pkg/packager/lint/validator.go index 67fa4464b2..769f0b583b 100644 --- a/src/pkg/packager/lint/validator.go +++ b/src/pkg/packager/lint/validator.go @@ -51,7 +51,7 @@ func PrintFindings(findings []types.PackageFinding, severity types.Severity, bas lintData := [][]string{} for _, finding := range findings { lintData = append(lintData, []string{ - colorWrapSev(finding.Category), + colorWrapSev(finding.Severity), pathColorWrap(finding.YqPath), itemizedDescription(finding.Description, finding.Item), }) @@ -64,7 +64,7 @@ func PrintFindings(findings []types.PackageFinding, severity types.Severity, bas func groupFindingsByPath(findings []types.PackageFinding, severity types.Severity, packageName string) map[string][]types.PackageFinding { findings = helpers.RemoveMatches(findings, func(finding types.PackageFinding) bool { - return finding.Category > severity + return finding.Severity > severity }) for i := range findings { if findings[i].PackageNameOverride == "" { @@ -91,7 +91,7 @@ func pathColorWrap(path string) string { func hasSeverity(findings []types.PackageFinding, category types.Severity) bool { for _, finding := range findings { - if finding.Category <= category { + if finding.Severity <= category { return true } } diff --git a/src/pkg/packager/lint/validator_test.go b/src/pkg/packager/lint/validator_test.go index 6217a0b0fb..4c925ea7ed 100644 --- a/src/pkg/packager/lint/validator_test.go +++ b/src/pkg/packager/lint/validator_test.go @@ -23,41 +23,41 @@ func TestGroupFindingsByPath(t *testing.T) { { name: "same package multiple findings", findings: []types.PackageFinding{ - {Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, - {Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, + {Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, + {Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, }, severity: types.SevWarn, packageName: "testPackage", want: map[string][]types.PackageFinding{ "path": { - {Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, - {Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, + {Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, + {Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, }, }, }, { name: "different packages single finding", findings: []types.PackageFinding{ - {Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, - {Category: types.SevErr, PackageNameOverride: "", PackagePathOverride: ""}, + {Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}, + {Severity: types.SevErr, PackageNameOverride: "", PackagePathOverride: ""}, }, severity: types.SevWarn, packageName: "testPackage", want: map[string][]types.PackageFinding{ - "path": {{Category: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}}, - ".": {{Category: types.SevErr, PackageNameOverride: "testPackage", PackagePathOverride: "."}}, + "path": {{Severity: types.SevWarn, PackageNameOverride: "import", PackagePathOverride: "path"}}, + ".": {{Severity: types.SevErr, PackageNameOverride: "testPackage", PackagePathOverride: "."}}, }, }, { name: "Multiple findings, mixed severity", findings: []types.PackageFinding{ - {Category: types.SevWarn, PackageNameOverride: "", PackagePathOverride: ""}, - {Category: types.SevErr, PackageNameOverride: "", PackagePathOverride: ""}, + {Severity: types.SevWarn, PackageNameOverride: "", PackagePathOverride: ""}, + {Severity: types.SevErr, PackageNameOverride: "", PackagePathOverride: ""}, }, severity: types.SevErr, packageName: "testPackage", want: map[string][]types.PackageFinding{ - ".": {{Category: types.SevErr, PackageNameOverride: "testPackage", PackagePathOverride: "."}}, + ".": {{Severity: types.SevErr, PackageNameOverride: "testPackage", PackagePathOverride: "."}}, }, }, } @@ -73,46 +73,50 @@ func TestGroupFindingsByPath(t *testing.T) { func TestHasSeverity(t *testing.T) { t.Parallel() tests := []struct { + name string severity types.Severity expected bool findings []types.PackageFinding }{ { + name: "error severity present", findings: []types.PackageFinding{ { - Category: types.SevErr, + Severity: types.SevErr, }, }, severity: types.SevErr, expected: true, }, { + name: "error severity not present", findings: []types.PackageFinding{ { - Category: types.SevWarn, + Severity: types.SevWarn, }, }, - severity: types.SevWarn, - expected: true, + severity: types.SevErr, + expected: false, }, { + name: "err and warning severity present", findings: []types.PackageFinding{ { - Category: types.SevWarn, + Severity: types.SevWarn, }, { - Category: types.SevErr, + Severity: types.SevErr, }, }, severity: types.SevErr, expected: true, }, } - for _, tc := range tests { - tc := tc - t.Run("test has severity", func(t *testing.T) { + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { t.Parallel() - require.Equal(t, tc.expected, hasSeverity(tc.findings, tc.severity)) + require.Equal(t, tt.expected, hasSeverity(tt.findings, tt.severity)) }) } } diff --git a/src/types/package.go b/src/types/package.go index 2eac2bf3e2..62f936557d 100644 --- a/src/types/package.go +++ b/src/types/package.go @@ -86,7 +86,7 @@ type PackageFinding struct { // PackagePathOverride shows the path to the package that the error originated from // If it is not set the base package will be used when displaying the error PackagePathOverride string - Category Severity + Severity Severity } // Severity is the type of package error