Skip to content

Commit

Permalink
change strip ansi to strip new lines and mutli spaces as well
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 committed Dec 6, 2023
1 parent c7d417f commit 264ef04
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ func (e2e *ZarfE2ETest) GetZarfVersion(t *testing.T) string {
return strings.Trim(stdOut, "\n")
}

// StripANSICodes strips any ANSI color codes from a given string
func (e2e *ZarfE2ETest) StripANSICodes(input string) string {
// StripMessageFormatting strips any ANSI color codes from a given string
func (e2e *ZarfE2ETest) StripMessageFormatting(input string) string {
// Regex to strip any color codes from the output - https://regex101.com/r/YFyIwC/2
ansiRegex := regexp.MustCompile(`\x1b\[(.*?)m`)
return ansiRegex.ReplaceAllString(input, "")
unAnsiInput := ansiRegex.ReplaceAllString(input, "")
// Regex to strip any more than two spaces or newline - https://regex101.com/r/wqQmys/1
multiSpaceRegex := regexp.MustCompile(`\s{2,}|\n`)
return multiSpaceRegex.ReplaceAllString(unAnsiInput, " ")
}

// NormalizeYAMLFilenames normalizes YAML filenames / paths across Operating Systems (i.e Windows vs Linux)
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func testHelmChartsExample(t *testing.T) {
evilChartDepsPath := filepath.Join("src", "test", "packages", "25-evil-chart-deps")
stdOut, stdErr, err = e2e.Zarf("package", "create", evilChartDepsPath, "--tmpdir", tmpdir, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripANSICodes(stdErr), "could not download\n https://charts.jetstack.io/charts/cert-manager-v1.11.1.tgz")
require.Contains(t, e2e.StripMessageFormatting(stdErr), "could not download https://charts.jetstack.io/charts/cert-manager-v1.11.1.tgz")
require.FileExists(t, filepath.Join(evilChartDepsPath, "good-chart", "charts", "gitlab-runner-0.55.0.tgz"))

// Create a package with a chart name that doesn't exist in a repo
evilChartLookupPath := filepath.Join("src", "test", "packages", "25-evil-chart-lookup")
stdOut, stdErr, err = e2e.Zarf("package", "create", evilChartLookupPath, "--tmpdir", tmpdir, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripANSICodes(stdErr), "chart \"asdf\" version \"6.4.0\" not found")
require.Contains(t, e2e.StripANSICodes(stdErr), "Available charts and versions from \"https://stefanprodan.github.io/podinfo\":")
require.Contains(t, e2e.StripMessageFormatting(stdErr), "chart \"asdf\" version \"6.4.0\" not found")
require.Contains(t, e2e.StripMessageFormatting(stdErr), "Available charts and versions from \"https://stefanprodan.github.io/podinfo\":")

// Create the package with a registry override
stdOut, stdErr, err = e2e.Zarf("package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io=docker.io", "--tmpdir", tmpdir, "--confirm")
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/29_mismatched_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestMismatchedArchitectures(t *testing.T) {
// Ensure zarf package deploy returns an error because of the mismatched architectures.
_, stdErr, err = e2e.Zarf("package", "deploy", mismatchedGamesPackage, "--confirm")
require.Error(t, err, stdErr)
require.Contains(t, stdErr, expectedErrorMessage)
require.Contains(t, e2e.StripMessageFormatting(stdErr), expectedErrorMessage)
}

// TestMismatchedVersions ensures that zarf produces a warning
Expand Down

0 comments on commit 264ef04

Please sign in to comment.