feat(chore): Add tests and use "any" instead of "inteface"

This commit is contained in:
2025-06-04 15:17:26 +02:00
parent a8341a9b44
commit b143f743ef
6 changed files with 83 additions and 10 deletions

View File

@@ -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)
}
}
}

View File

@@ -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)

View File

@@ -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])
}
}

View File

@@ -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 {

View File

@@ -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, " ")

View File

@@ -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: