prfeedback(lint): added a new isHelperFile function in the lint flow to skip helperFiles

Signed-off-by: Daksh Sangal <134773611+Qu-Ack@users.noreply.github.com>
pull/31306/head
Daksh Sangal 7 days ago
parent 5cea37f75f
commit bd602d27d1

@ -123,6 +123,11 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string
fileName := template.Name fileName := template.Name
fpath = fileName fpath = fileName
// skip the linting rules if the file starts with _ and is not a yml/yaml file
if isHelperFile(fileName) {
continue
}
linter.RunLinterRule(support.ErrorSev, fpath, validateAllowedExtension(fileName)) linter.RunLinterRule(support.ErrorSev, fpath, validateAllowedExtension(fileName))
// We only apply the following lint rules to yaml files // We only apply the following lint rules to yaml files
@ -219,14 +224,19 @@ func validateTemplatesDir(templatesPath string) error {
return nil return nil
} }
func validateAllowedExtension(fileName string) error { // checks if the file starts with '_'.
func isHelperFile(fileName string) bool {
baseName := filepath.Base(fileName) baseName := filepath.Base(fileName)
ext := filepath.Ext(fileName)
if strings.HasPrefix(baseName, "_") { if strings.HasPrefix(baseName, "_") && ext != ".yaml" && ext != ".yml" {
fmt.Println("skipping _ files") return true
return nil
} }
return false
}
func validateAllowedExtension(fileName string) error {
ext := filepath.Ext(fileName) ext := filepath.Ext(fileName)
validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"} validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"}

@ -32,14 +32,14 @@ import (
const templateTestBasedir = "./testdata/albatross" const templateTestBasedir = "./testdata/albatross"
func TestValidateAllowedExtension(t *testing.T) { func TestValidateAllowedExtension(t *testing.T) {
var failTest = []string{"/foo", "/test.toml", "regular.json"} var failTest = []string{"/foo", "/test.toml"}
for _, test := range failTest { for _, test := range failTest {
err := validateAllowedExtension(test) err := validateAllowedExtension(test)
if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") { if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") {
t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test) t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test)
} }
} }
var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt", "_envoy.json"} var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt"}
for _, test := range successTest { for _, test := range successTest {
err := validateAllowedExtension(test) err := validateAllowedExtension(test)
if err != nil { if err != nil {
@ -401,6 +401,7 @@ func TestEmptyWithCommentsManifests(t *testing.T) {
t.Fatalf("Expected 0 lint errors, got %d", l) t.Fatalf("Expected 0 lint errors, got %d", l)
} }
} }
func TestValidateListAnnotations(t *testing.T) { func TestValidateListAnnotations(t *testing.T) {
md := &k8sYamlStruct{ md := &k8sYamlStruct{
APIVersion: "v1", APIVersion: "v1",
@ -439,3 +440,35 @@ items:
t.Fatalf("List objects keep annotations should pass. got: %s", err) t.Fatalf("List objects keep annotations should pass. got: %s", err)
} }
} }
func TestIsHelperFile(t *testing.T) {
tests := []struct {
fileName string
expected bool
}{
// Should return true (helper files, non-yaml/yml)
{"_helpers.go", true},
{"_helpers.json", true},
{"subdir/_helpers.json", true},
{"subdir/_partial.tpl", true},
{"_config.txt", true},
// Should return false (yaml/yml files, even with _)
{"_helpers.yaml", false},
{"_config.yml", false},
{"subdir/_partial.yaml", false},
// Should return false (regular files without _)
{"helpers.go", false},
{"config.json", false},
{"deployment.yaml", false},
}
for _, tt := range tests {
t.Run(tt.fileName, func(t *testing.T) {
result := isHelperFile(tt.fileName)
if result != tt.expected {
t.Errorf("isHelperFile(%q) = %v, expected %v", tt.fileName, result, tt.expected)
}
})
}
}

@ -123,6 +123,11 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string
fileName := template.Name fileName := template.Name
fpath = fileName fpath = fileName
// skip the linting rules if the file starts with _ and is not a yaml/yml file
if isHelperFile(fileName) {
continue
}
linter.RunLinterRule(support.ErrorSev, fpath, validateAllowedExtension(fileName)) linter.RunLinterRule(support.ErrorSev, fpath, validateAllowedExtension(fileName))
// We only apply the following lint rules to yaml files // We only apply the following lint rules to yaml files
@ -219,13 +224,19 @@ func validateTemplatesDir(templatesPath string) error {
return nil return nil
} }
func validateAllowedExtension(fileName string) error { // checks if the file starts with '_'.
func isHelperFile(fileName string) bool {
baseName := filepath.Base(fileName) baseName := filepath.Base(fileName)
ext := filepath.Ext(fileName)
if strings.HasPrefix(baseName, "_") { if strings.HasPrefix(baseName, "_") && ext != ".yaml" && ext != ".yml" {
return nil return true
} }
return false
}
func validateAllowedExtension(fileName string) error {
ext := filepath.Ext(fileName) ext := filepath.Ext(fileName)
validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"} validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"}

@ -32,14 +32,14 @@ import (
const templateTestBasedir = "./testdata/albatross" const templateTestBasedir = "./testdata/albatross"
func TestValidateAllowedExtension(t *testing.T) { func TestValidateAllowedExtension(t *testing.T) {
var failTest = []string{"/foo", "/test.toml", "regular.json"} var failTest = []string{"/foo", "/test.toml"}
for _, test := range failTest { for _, test := range failTest {
err := validateAllowedExtension(test) err := validateAllowedExtension(test)
if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") { if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") {
t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test) t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test)
} }
} }
var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt", "_regular.json"} var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt"}
for _, test := range successTest { for _, test := range successTest {
err := validateAllowedExtension(test) err := validateAllowedExtension(test)
if err != nil { if err != nil {
@ -439,3 +439,35 @@ items:
t.Fatalf("List objects keep annotations should pass. got: %s", err) t.Fatalf("List objects keep annotations should pass. got: %s", err)
} }
} }
func TestIsHelperFile(t *testing.T) {
tests := []struct {
fileName string
expected bool
}{
// Should return true (helper files, non-yaml/yml)
{"_helpers.go", true},
{"_helpers.json", true},
{"subdir/_helpers.json", true},
{"subdir/_partial.tpl", true},
{"_config.txt", true},
// Should return false (yaml/yml files, even with _)
{"_helpers.yaml", false},
{"_config.yml", false},
{"subdir/_partial.yaml", false},
// Should return false (regular files without _)
{"helpers.go", false},
{"config.json", false},
{"deployment.yaml", false},
}
for _, tt := range tests {
t.Run(tt.fileName, func(t *testing.T) {
result := isHelperFile(tt.fileName)
if result != tt.expected {
t.Errorf("isHelperFile(%q) = %v, expected %v", tt.fileName, result, tt.expected)
}
})
}
}

Loading…
Cancel
Save