feat(chore): Add tests and use "any" instead of "inteface"
This commit is contained in:
40
generator/extrafiles/notes_test.go
Normal file
40
generator/extrafiles/notes_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -21,7 +21,7 @@ type chart struct {
|
|||||||
Values []string
|
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 {
|
for key, value := range values {
|
||||||
path := key
|
path := key
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
@@ -29,11 +29,11 @@ func parseValues(prefix string, values map[string]interface{}, result map[string
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case []interface{}:
|
case []any:
|
||||||
for i, u := range v {
|
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)
|
parseValues(path, v, result)
|
||||||
default:
|
default:
|
||||||
strValue := fmt.Sprintf("`%v`", value)
|
strValue := fmt.Sprintf("`%v`", value)
|
||||||
|
33
generator/extrafiles/readme_test.go
Normal file
33
generator/extrafiles/readme_test.go
Normal 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])
|
||||||
|
}
|
||||||
|
}
|
@@ -87,7 +87,7 @@ func UnWrapTPL(in []byte) []byte {
|
|||||||
return regexpLineWrap.ReplaceAll(in, []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 {
|
if o, err := yaml.Marshal(obj); err != nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else {
|
} else {
|
||||||
|
@@ -22,7 +22,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Warn prints a warning message
|
// Warn prints a warning message
|
||||||
func Warn(msg ...interface{}) {
|
func Warn(msg ...any) {
|
||||||
orange := "\033[38;5;214m"
|
orange := "\033[38;5;214m"
|
||||||
reset := "\033[0m"
|
reset := "\033[0m"
|
||||||
fmt.Print(IconWarning, orange, " ")
|
fmt.Print(IconWarning, orange, " ")
|
||||||
|
@@ -136,12 +136,12 @@ func GetValuesFromLabel(service types.ServiceConfig, LabelValues string) map[str
|
|||||||
switch val := value.(type) {
|
switch val := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
descriptions[val] = nil
|
descriptions[val] = nil
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
for k, v := range value.(map[string]interface{}) {
|
for k, v := range value.(map[string]any) {
|
||||||
descriptions[k] = &EnvConfig{Service: service, Description: v.(string)}
|
descriptions[k] = &EnvConfig{Service: service, Description: v.(string)}
|
||||||
}
|
}
|
||||||
case map[interface{}]interface{}:
|
case map[any]any:
|
||||||
for k, v := range value.(map[interface{}]interface{}) {
|
for k, v := range value.(map[any]any) {
|
||||||
descriptions[k.(string)] = &EnvConfig{Service: service, Description: v.(string)}
|
descriptions[k.(string)] = &EnvConfig{Service: service, Description: v.(string)}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
Reference in New Issue
Block a user