From b143f743efe9d8b827baa8e272d5ce45215b2965 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 4 Jun 2025 15:17:26 +0200 Subject: [PATCH] feat(chore): Add tests and use "any" instead of "inteface" --- generator/extrafiles/notes_test.go | 40 +++++++++++++++++++++++++++++ generator/extrafiles/readme.go | 8 +++--- generator/extrafiles/readme_test.go | 33 ++++++++++++++++++++++++ generator/utils.go | 2 +- utils/icons.go | 2 +- utils/utils.go | 8 +++--- 6 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 generator/extrafiles/notes_test.go create mode 100644 generator/extrafiles/readme_test.go diff --git a/generator/extrafiles/notes_test.go b/generator/extrafiles/notes_test.go new file mode 100644 index 0000000..656aecd --- /dev/null +++ b/generator/extrafiles/notes_test.go @@ -0,0 +1,40 @@ +package extrafiles + +import ( + "strings" + "testing" +) + +// override the embedded template for testing +var testTemplate = ` +Some header +{{ ingress_list }} +Some footer +` + +func init() { + notesTemplate = testTemplate +} + +func TestNotesFile_NoServices(t *testing.T) { + result := NotesFile([]string{}) + if !strings.Contains(result, "Some header") || !strings.Contains(result, "Some footer") { + t.Errorf("Expected template header/footer in output, got: %s", result) + } +} + +func TestNotesFile_WithServices(t *testing.T) { + services := []string{"svc1", "svc2"} + result := NotesFile(services) + + for _, svc := range services { + cond := "{{- if and .Values." + svc + ".ingress .Values." + svc + ".ingress.enabled }}" + line := "{{- $count = add1 $count -}}{{- $listOfURL = printf \"%s\\n- http://%s\" $listOfURL (tpl .Values." + svc + ".ingress.host .) -}}" + if !strings.Contains(result, cond) { + t.Errorf("Expected condition for service %s in output", svc) + } + if !strings.Contains(result, line) { + t.Errorf("Expected line for service %s in output", svc) + } + } +} diff --git a/generator/extrafiles/readme.go b/generator/extrafiles/readme.go index d328d33..8fba0c8 100644 --- a/generator/extrafiles/readme.go +++ b/generator/extrafiles/readme.go @@ -21,7 +21,7 @@ type chart struct { Values []string } -func parseValues(prefix string, values map[string]interface{}, result map[string]string) { +func parseValues(prefix string, values map[string]any, result map[string]string) { for key, value := range values { path := key if prefix != "" { @@ -29,11 +29,11 @@ func parseValues(prefix string, values map[string]interface{}, result map[string } switch v := value.(type) { - case []interface{}: + case []any: for i, u := range v { - parseValues(fmt.Sprintf("%s[%d]", path, i), map[string]interface{}{"value": u}, result) + parseValues(fmt.Sprintf("%s[%d]", path, i), map[string]any{"value": u}, result) } - case map[string]interface{}: + case map[string]any: parseValues(path, v, result) default: strValue := fmt.Sprintf("`%v`", value) diff --git a/generator/extrafiles/readme_test.go b/generator/extrafiles/readme_test.go new file mode 100644 index 0000000..e819872 --- /dev/null +++ b/generator/extrafiles/readme_test.go @@ -0,0 +1,33 @@ +package extrafiles + +import ( + "regexp" + "testing" +) + +func TestReadMeFile_Basic(t *testing.T) { + values := map[string]any{ + "replicas": 2, + "image": map[string]any{ + "repository": "nginx", + "tag": "latest", + }, + } + + result := ReadMeFile("testchart", "A test chart", values) + t.Logf("Generated README content:\n%s", result) + paramerRegExp := regexp.MustCompile(`\|\s+` + "`" + `(.*?)` + "`" + `\s+\|\s+` + "`" + `(.*?)` + "`" + `\s+\|`) + matches := paramerRegExp.FindAllStringSubmatch(result, -1) + if len(matches) != 3 { + t.Errorf("Expected 5 lines in the table for headers and parameters, got %d", len(matches)) + } + if matches[0][1] != "image.repository" || matches[0][2] != "nginx" { + t.Errorf("Expected third line to be image.repository, got %s", matches[1]) + } + if matches[1][1] != "image.tag" || matches[1][2] != "latest" { + t.Errorf("Expected fourth line to be image.tag, got %s", matches[2]) + } + if matches[2][1] != "replicas" || matches[2][2] != "2" { + t.Errorf("Expected second line to be replicas, got %s", matches[0]) + } +} diff --git a/generator/utils.go b/generator/utils.go index 539f07d..c10cd15 100644 --- a/generator/utils.go +++ b/generator/utils.go @@ -87,7 +87,7 @@ func UnWrapTPL(in []byte) []byte { return regexpLineWrap.ReplaceAll(in, []byte(" }}")) } -func ToK8SYaml(obj interface{}) ([]byte, error) { +func ToK8SYaml(obj any) ([]byte, error) { if o, err := yaml.Marshal(obj); err != nil { return nil, nil } else { diff --git a/utils/icons.go b/utils/icons.go index 5028c24..2a2a38d 100644 --- a/utils/icons.go +++ b/utils/icons.go @@ -22,7 +22,7 @@ const ( ) // Warn prints a warning message -func Warn(msg ...interface{}) { +func Warn(msg ...any) { orange := "\033[38;5;214m" reset := "\033[0m" fmt.Print(IconWarning, orange, " ") diff --git a/utils/utils.go b/utils/utils.go index b9910c8..ab4ba59 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -136,12 +136,12 @@ func GetValuesFromLabel(service types.ServiceConfig, LabelValues string) map[str switch val := value.(type) { case string: descriptions[val] = nil - case map[string]interface{}: - for k, v := range value.(map[string]interface{}) { + case map[string]any: + for k, v := range value.(map[string]any) { descriptions[k] = &EnvConfig{Service: service, Description: v.(string)} } - case map[interface{}]interface{}: - for k, v := range value.(map[interface{}]interface{}) { + case map[any]any: + for k, v := range value.(map[any]any) { descriptions[k.(string)] = &EnvConfig{Service: service, Description: v.(string)} } default: