Skip to content

Commit

Permalink
shorten test to focus on what we're testing
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Dec 16, 2024
1 parent 44a0cbe commit 1ed51df
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
15 changes: 7 additions & 8 deletions src/pkg/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func templateZarfObj(zarfObj any, setVariables map[string]string) ([]PackageFind
return err
}

var unSetTemplates bool
for key := range yamlTemplates {
if deprecated {
findings = append(findings, PackageFinding{
Expand All @@ -128,15 +127,15 @@ func templateZarfObj(zarfObj any, setVariables map[string]string) ([]PackageFind
})
}
if _, present := setVariables[key]; !present {
unSetTemplates = true
findings = append(findings, PackageFinding{
Description: fmt.Sprintf("package template %s is not set and won't be evaluated during lint", key),
Severity: SevWarn,
})
}
}
if unSetTemplates {
findings = append(findings, PackageFinding{
Description: lang.UnsetVarLintWarning,
Severity: SevWarn,
})
}
// if unSetTemplates {

// }
for key, value := range setVariables {
templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value
}
Expand Down
40 changes: 12 additions & 28 deletions src/pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package lint
import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestFillObjTemplate(t *testing.T) {
expectedFindings := []PackageFinding{
{
Severity: SevWarn,
Description: "There are templates that are not set and won't be evaluated during lint",
Description: "package template KEY3 is not set and won't be evaluated during lint",
},
{
Severity: SevWarn,
Expand All @@ -104,7 +105,7 @@ func TestLintPackageWithImports(t *testing.T) {
findings := []PackageFinding{
{
YqPath: "",
Description: "There are templates that are not set and won't be evaluated during lint",
Description: "package template UNSET is not set and won't be evaluated during lint",
Item: "",
PackageNameOverride: "linted-import",
PackagePathOverride: "linted-import",
Expand Down Expand Up @@ -136,23 +137,7 @@ func TestLintPackageWithImports(t *testing.T) {
},
{
YqPath: "",
Description: "There are templates that are not set and won't be evaluated during lint",
Item: "",
PackageNameOverride: "lint",
PackagePathOverride: ".",
Severity: SevWarn,
},
{
YqPath: "",
Description: `Package template "WHATEVER_IMAGE" is using the deprecated syntax ###ZARF_PKG_VAR_WHATEVER_IMAGE###. This will be removed in Zarf v1.0.0. Please update to ###ZARF_PKG_TMPL_WHATEVER_IMAGE###.`,
Item: "",
PackageNameOverride: "lint",
PackagePathOverride: ".",
Severity: SevWarn,
},
{
YqPath: "",
Description: "There are templates that are not set and won't be evaluated during lint",
Description: "package template UBUNTU_IMAGE is not set and won't be evaluated during lint",
Item: "",
PackageNameOverride: "lint",
PackagePathOverride: ".",
Expand Down Expand Up @@ -183,7 +168,7 @@ func TestLintPackageWithImports(t *testing.T) {
Severity: SevWarn,
},
{
YqPath: ".components.[2].images.[3]",
YqPath: ".components.[2].images.[2]",
Description: "Image not pinned with digest",
Item: "busybox:latest",
PackageNameOverride: "lint",
Expand Down Expand Up @@ -223,14 +208,13 @@ func TestLintPackageWithImports(t *testing.T) {
Severity: SevErr,
},
}
// cwd, err := os.Getwd()
// require.NoError(t, err)
// err = os.Chdir(filepath.Join("testdata", "lint-with-templates"))
// require.NoError(t, err)
// defer func() {
// require.NoError(t, os.Chdir(cwd))
// }()
err := Validate(ctx, "testdata/lint-with-imports", "good-flavor", setVariables)
cwd, err := os.Getwd()
require.NoError(t, err)
// TODO @austinabro321: remove this and parallelize the test once changing the working directory is no longer required
defer func() {
require.NoError(t, os.Chdir(cwd))
}()
err = Validate(ctx, "testdata/lint-with-imports", "good-flavor", setVariables)
var lintErr *LintError
require.ErrorAs(t, err, &lintErr)
require.ElementsMatch(t, findings, lintErr.Findings)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/lint/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestValidatePackageSchema(t *testing.T) {
}
cwd, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(filepath.Join("testdata", "lint-with-templates"))
err = os.Chdir(filepath.Join("testdata", "package-with-templates"))
require.NoError(t, err)
defer func() {
require.NoError(t, os.Chdir(cwd))
Expand Down
1 change: 0 additions & 1 deletion src/pkg/lint/testdata/lint-with-imports/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ components:
images:
- registry.com:9001/whatever/image:1.0.0
- busybox@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
- busybox:###ZARF_PKG_VAR_WHATEVER_IMAGE###
- busybox:###ZARF_PKG_TMPL_BUSYBOX_IMAGE###
- ubuntu:###ZARF_PKG_TMPL_UBUNTU_IMAGE###
files:
Expand Down
10 changes: 9 additions & 1 deletion src/test/testutil/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package testutil
import (
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -27,7 +29,13 @@ func (m *schemaFS) Open(_ string) (fs.File, error) {
func LoadSchema(t *testing.T, path string) fs.ReadFileFS {
t.Helper()

b, err := os.ReadFile(path)
_, testFilePath, _, ok := runtime.Caller(1)
require.True(t, ok, "failed to determine the test file path")

// Resolve the schema file's absolute path
schemaPath := filepath.Join(filepath.Dir(testFilePath), path)

b, err := os.ReadFile(schemaPath)
require.NoError(t, err)
return &schemaFS{b: b}
}

0 comments on commit 1ed51df

Please sign in to comment.