diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9d9e4b1b46..22ea750052 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,16 +40,3 @@ repos: types: [go] language: golang pass_filenames: false - - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.14.0 - hooks: - - id: check-jsonschema - name: "Validate Zarf Configs Against Schema" - files: "zarf.yaml" - types: [yaml] - args: ["--schemafile", "zarf.schema.json"] - exclude: | - (?x)^( - src/test/packages/12-lint/.*| - src/pkg/lint/testdata/package-with-templates/zarf.yaml - )$ diff --git a/src/pkg/lint/lint.go b/src/pkg/lint/lint.go index 9f0259643e..724b6c214c 100644 --- a/src/pkg/lint/lint.go +++ b/src/pkg/lint/lint.go @@ -71,7 +71,7 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty node := chain.Head() for node != nil { component := node.ZarfComponent - compFindings, err := fillComponentTemplate(&component, createOpts) + compFindings, err := fillObjTemplate(&component, createOpts.SetVariables) if err != nil { return nil, err } @@ -87,12 +87,12 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty return findings, nil } -func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreateOptions) ([]PackageFinding, error) { +func fillObjTemplate(rawObj any, setVariables map[string]string) ([]PackageFinding, error) { var findings []PackageFinding templateMap := map[string]string{} setVarsAndWarn := func(templatePrefix string, deprecated bool) error { - yamlTemplates, err := utils.FindYamlTemplates(c, templatePrefix, "###") + yamlTemplates, err := utils.FindYamlTemplates(rawObj, templatePrefix, "###") if err != nil { return err } @@ -105,7 +105,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat Severity: SevWarn, }) } - if _, present := createOpts.SetVariables[key]; !present { + if _, present := setVariables[key]; !present { unSetTemplates = true } } @@ -115,7 +115,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat Severity: SevWarn, }) } - for key, value := range createOpts.SetVariables { + for key, value := range setVariables { templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value } return nil @@ -130,7 +130,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat return nil, err } - if err := utils.ReloadYamlTemplate(c, templateMap); err != nil { + if err := utils.ReloadYamlTemplate(rawObj, templateMap); err != nil { return nil, err } return findings, nil diff --git a/src/pkg/lint/lint_test.go b/src/pkg/lint/lint_test.go index d6ad24ad82..23f14c14c0 100644 --- a/src/pkg/lint/lint_test.go +++ b/src/pkg/lint/lint_test.go @@ -32,12 +32,10 @@ func TestLintComponents(t *testing.T) { require.Error(t, err) }) } -func TestFillComponentTemplate(t *testing.T) { - createOpts := types.ZarfCreateOptions{ - SetVariables: map[string]string{ - "KEY1": "value1", - "KEY2": "value2", - }, +func TestFillObjTemplate(t *testing.T) { + SetVariables := map[string]string{ + "KEY1": "value1", + "KEY2": "value2", } component := v1alpha1.ZarfComponent{ @@ -48,7 +46,7 @@ func TestFillComponentTemplate(t *testing.T) { }, } - findings, err := fillComponentTemplate(&component, createOpts) + findings, err := fillObjTemplate(&component, SetVariables) require.NoError(t, err) expectedFindings := []PackageFinding{ { diff --git a/src/pkg/lint/schema.go b/src/pkg/lint/schema.go index e5d69342a1..9c22ba7fa8 100644 --- a/src/pkg/lint/schema.go +++ b/src/pkg/lint/schema.go @@ -10,7 +10,6 @@ import ( "regexp" "github.com/xeipuuv/gojsonschema" - "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/utils" ) @@ -30,7 +29,7 @@ func ValidatePackageSchema(setVariables map[string]string) ([]PackageFinding, er return nil, err } - untypedZarfPackage, err = templateRawYaml(untypedZarfPackage, setVariables) + _, err = fillObjTemplate(&untypedZarfPackage, setVariables) if err != nil { return nil, err } @@ -38,23 +37,6 @@ func ValidatePackageSchema(setVariables map[string]string) ([]PackageFinding, er return getSchemaFindings(jsonSchema, untypedZarfPackage) } -func templateRawYaml(untypedPackage any, values map[string]string) (any, error) { - templateMap := map[string]string{} - - for key, value := range values { - templateMap[fmt.Sprintf("%s%s###", v1alpha1.ZarfPackageVariablePrefix, key)] = value - } - - for key, value := range values { - templateMap[fmt.Sprintf("%s%s###", v1alpha1.ZarfPackageTemplatePrefix, key)] = value - } - - if err := utils.ReloadYamlTemplate(&untypedPackage, templateMap); err != nil { - return nil, err - } - return untypedPackage, nil -} - func makeFieldPathYqCompat(field string) string { if field == "(root)" { return field diff --git a/src/pkg/lint/schema_test.go b/src/pkg/lint/schema_test.go index bd8b576a83..8ebcdc26bf 100644 --- a/src/pkg/lint/schema_test.go +++ b/src/pkg/lint/schema_test.go @@ -7,11 +7,13 @@ package lint import ( "fmt" "os" + "path/filepath" "testing" goyaml "github.com/goccy/go-yaml" "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/api/v1alpha1" + "github.com/zarf-dev/zarf/src/test/testutil" ) func TestZarfSchema(t *testing.T) { @@ -190,6 +192,24 @@ components: }) } +func TestValidatePackageSchema(t *testing.T) { + ZarfSchema = testutil.LoadSchema(t, "../../../zarf.schema.json") + setVariables := map[string]string{ + "PACKAGE_NAME": "test-package", + "MY_COMP_NAME": "test-comp", + } + cwd, err := os.Getwd() + require.NoError(t, err) + err = os.Chdir(filepath.Join("testdata", "package-with-templates")) + require.NoError(t, err) + defer func() { + require.NoError(t, os.Chdir(cwd)) + }() + findings, err := ValidatePackageSchema(setVariables) + require.Empty(t, findings) + require.NoError(t, err) +} + func TestYqCompat(t *testing.T) { t.Parallel() t.Run("Wrap standalone numbers in bracket", func(t *testing.T) { diff --git a/src/pkg/lint/testdata/package-with-templates/zarf.yaml b/src/pkg/lint/testdata/package-with-templates/zarf.yaml index 1b5cee0f71..f32ec32bab 100644 --- a/src/pkg/lint/testdata/package-with-templates/zarf.yaml +++ b/src/pkg/lint/testdata/package-with-templates/zarf.yaml @@ -1,5 +1,5 @@ kind: ZarfPackageConfig metadata: - name: "###ZARF_PKG_TMPL_PACKAGE_NAME###" + name: "###ZARF_PKG_VAR_PACKAGE_NAME###" components: - - name: ok + - name: "###ZARF_PKG_TMPL_MY_COMP_NAME###"