From 0199b5f7643d766166e9274dfb85cbcde3706189 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:34:24 +0800 Subject: [PATCH] fix checker for empty array case (#1153) --- pkg/policy/checker.go | 2 +- pkg/policy/checker_test.go | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/policy/checker.go b/pkg/policy/checker.go index 2023ecea..71e09761 100644 --- a/pkg/policy/checker.go +++ b/pkg/policy/checker.go @@ -13,7 +13,7 @@ import ( var ( EnvironmentsAndReleasesWithinSameYamlPartErr = errors.New("environments and releases cannot be defined within the same YAML part. Use --- to extract the environments into a dedicated part") - topConfigKeysRegex = regexp.MustCompile(`^[a-zA-Z]+: *$`) + topConfigKeysRegex = regexp.MustCompile(`^[a-zA-Z]+:`) separatorRegex = regexp.MustCompile(`^--- *$`) topkeysPriority = map[string]int{ "bases": 0, diff --git a/pkg/policy/checker_test.go b/pkg/policy/checker_test.go index 3bdbb6a1..45d3a558 100644 --- a/pkg/policy/checker_test.go +++ b/pkg/policy/checker_test.go @@ -19,7 +19,7 @@ func TestForbidEnvironmentsWithReleases(t *testing.T) { }{ { name: "no error when only releases", - filePath: "helmfile.yaml.gotmpl", + filePath: "helmfile.yaml", content: []byte("releases:\n"), v1mode: false, expectedErr: false, @@ -27,7 +27,7 @@ func TestForbidEnvironmentsWithReleases(t *testing.T) { }, { name: "no error when only environments", - filePath: "helmfile.yaml.gotmpl", + filePath: "helmfile.yaml", content: []byte("environments:\n"), v1mode: false, expectedErr: false, @@ -35,7 +35,7 @@ func TestForbidEnvironmentsWithReleases(t *testing.T) { }, { name: "no error when has --- between releases and environments", - filePath: "helmfile.yaml.gotmpl", + filePath: "helmfile.yaml", content: []byte("environments:\n---\nreleases:\n"), v1mode: false, expectedErr: false, @@ -43,7 +43,7 @@ func TestForbidEnvironmentsWithReleases(t *testing.T) { }, { name: "no error when has --- between releases and environments, and --- on top of helmfile.yaml.gotmpl", - filePath: "helmfile.yaml.gotmpl", + filePath: "helmfile.yaml", content: []byte("---\nenvironments:\n---\nreleases:\n"), v1mode: false, expectedErr: false, @@ -51,7 +51,7 @@ func TestForbidEnvironmentsWithReleases(t *testing.T) { }, { name: "error when both releases and environments", - filePath: "helmfile.yaml.gotmpl", + filePath: "helmfile.yaml", content: []byte("environments:\nreleases:\n"), v1mode: false, expectedErr: true, @@ -214,6 +214,12 @@ func TestTopKeys(t *testing.T) { hasSeparator: true, want: []string{"bases", "---", "releases"}, }, + { + name: "get top keys with empty array", + helmfileContent: []byte("bases: []\n---\nreleases: []\n"), + hasSeparator: true, + want: []string{"bases", "---", "releases"}, + }, { name: "get empty keys", helmfileContent: []byte(""),