Skip to content

Commit 1e2ec17

Browse files
Greendor1234arttor
authored andcommitted
added unit test
1 parent 7183122 commit 1e2ec17

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

pkg/processor/deployment/deployment.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
130130
return true, nil, err
131131
}
132132

133-
r := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`)
134-
spec = r.ReplaceAllString(spec, "${1}")
133+
spec = replaceSingleQuotes(spec)
135134

136135
return true, &result{
137136
values: values,
@@ -155,6 +154,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
155154
}, nil
156155
}
157156

157+
func replaceSingleQuotes(s string) string {
158+
r := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`)
159+
return r.ReplaceAllString(s, "${1}")
160+
}
161+
158162
func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
159163
if deployment.Spec.Replicas == nil {
160164
return "", nil

pkg/processor/deployment/deployment_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,84 @@ func Test_deployment_Process(t *testing.T) {
134134
assert.Equal(t, false, processed)
135135
})
136136
}
137+
138+
var singleQuotesTest = []struct {
139+
input string
140+
expected string
141+
}{
142+
{
143+
"{{ .Values.x }}",
144+
"{{ .Values.x }}",
145+
},
146+
{
147+
"'{{ .Values.x }}'",
148+
"{{ .Values.x }}",
149+
},
150+
{
151+
"'{{ .Values.x }}:{{ .Values.y }}'",
152+
"{{ .Values.x }}:{{ .Values.y }}",
153+
},
154+
{
155+
"'{{ .Values.x }}:{{ .Values.y \n\t| default .Chart.AppVersion}}'",
156+
"{{ .Values.x }}:{{ .Values.y \n\t| default .Chart.AppVersion}}",
157+
},
158+
{
159+
"echo 'x'",
160+
"echo 'x'",
161+
},
162+
{
163+
"abcd: x.y['x/y']",
164+
"abcd: x.y['x/y']",
165+
},
166+
{
167+
"abcd: x.y[\"'{{}}'\"]",
168+
"abcd: x.y[\"{{}}\"]",
169+
},
170+
{
171+
"image: '{{ .Values.x }}'",
172+
"image: {{ .Values.x }}",
173+
},
174+
{
175+
"'{{ .Values.x }} y'",
176+
"{{ .Values.x }} y",
177+
},
178+
{
179+
"\t\t- mountPath: './x.y'",
180+
"\t\t- mountPath: './x.y'",
181+
},
182+
{
183+
"'{{}}'",
184+
"{{}}",
185+
},
186+
{
187+
"'{{ {nested} }}'",
188+
"{{ {nested} }}",
189+
},
190+
{
191+
"'{{ '{{nested}}' }}'",
192+
"{{ '{{nested}}' }}",
193+
},
194+
{
195+
"'{{ unbalanced }'",
196+
"'{{ unbalanced }'",
197+
},
198+
{
199+
"'{{\nincomplete content'",
200+
"'{{\nincomplete content'",
201+
},
202+
{
203+
"'{{ @#$%^&*() }}'",
204+
"{{ @#$%^&*() }}",
205+
},
206+
}
207+
208+
func Test_replaceSingleQuotes(t *testing.T) {
209+
for _, tt := range singleQuotesTest {
210+
t.Run(tt.input, func(t *testing.T) {
211+
s := replaceSingleQuotes(tt.input)
212+
if s != tt.expected {
213+
t.Errorf("got %q, want %q", s, tt.expected)
214+
}
215+
})
216+
}
217+
}

0 commit comments

Comments
 (0)