Skip to content

Commit 6af1a7b

Browse files
committed
fix(charts): anchor Chart.yaml metadata normalizer to top-level keys
The unanchored pattern also rewrote nested name:/version: lines (dependencies, maintainers), which would mask real dependency drift in the content comparison and inject extra %s verbs into the two-argument fmt template the init flow writes Chart.yaml with. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 5e9dfc2 commit 6af1a7b

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎charts/charts.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ const chartYamlName = "Chart.yaml"
2828
//go:embed all:cozystack all:generic all:talm
2929
var embeddedCharts embed.FS
3030

31-
// chartMetaRegex matches the `name:`/`version:` metadata lines of a
32-
// Chart.yaml. Both are rewritten to a %s placeholder so the init flow can
31+
// chartMetaRegex matches the TOP-LEVEL `name:`/`version:` metadata lines of
32+
// a Chart.yaml. Both are rewritten to a %s placeholder so the init flow can
3333
// substitute the real project name/version, and so the drift check can
34-
// compare two chart trees without a version stamp counting as content.
35-
var chartMetaRegex = regexp.MustCompile(`(name|version): \S+`)
34+
// compare two chart trees without a version stamp counting as content. The
35+
// `(?m)^` anchor keeps nested occurrences (dependencies[].name/version,
36+
// maintainers[].name) intact: rewriting those would mask real dependency
37+
// drift and inject extra verbs into the two-argument fmt template the init
38+
// flow writes Chart.yaml with.
39+
var chartMetaRegex = regexp.MustCompile(`(?m)^(name|version): \S+`)
3640

3741
// NormalizeChartMeta rewrites the name/version lines of a Chart.yaml to %s
3842
// placeholders. Files other than Chart.yaml pass through unchanged. base is

‎charts/library_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,42 @@ func TestNormalizeChartMeta_LeavesNonChartYamlUntouched(t *testing.T) {
117117
}
118118
}
119119

120+
// TestNormalizeChartMeta_PreservesNestedNameVersion pins that only the
121+
// TOP-LEVEL name/version metadata lines are folded to placeholders. Nested
122+
// occurrences — dependencies[].name/version, maintainers[].name — must
123+
// survive verbatim: rewriting them would (a) mask real dependency drift in
124+
// the content comparison and (b) inject extra %s verbs into the fmt
125+
// template the init flow renders with exactly two arguments, writing
126+
// %!s(MISSING) into the project's Chart.yaml.
127+
func TestNormalizeChartMeta_PreservesNestedNameVersion(t *testing.T) {
128+
const chart = "apiVersion: v2\n" +
129+
"name: cozystack\n" +
130+
"version: 0.1.0\n" +
131+
"dependencies:\n" +
132+
" - name: talm\n" +
133+
" version: 1.2.3\n" +
134+
"maintainers:\n" +
135+
" - name: maintainer-handle\n"
136+
137+
got := NormalizeChartMeta("Chart.yaml", chart)
138+
139+
if !strings.Contains(got, "name: %s\nversion: %s\n") {
140+
t.Errorf("top-level name/version not normalized:\n%s", got)
141+
}
142+
143+
if !strings.Contains(got, " - name: talm\n version: 1.2.3\n") {
144+
t.Errorf("nested dependency name/version were rewritten:\n%s", got)
145+
}
146+
147+
if !strings.Contains(got, " - name: maintainer-handle\n") {
148+
t.Errorf("nested maintainer name was rewritten:\n%s", got)
149+
}
150+
151+
if n := strings.Count(got, "%s"); n != 2 {
152+
t.Errorf("expected exactly 2 placeholders for the init-time fmt template, got %d:\n%s", n, got)
153+
}
154+
}
155+
120156
// TestNormalizeChartMeta_PreservesApiAndAppVersion pins that only the
121157
// `name`/`version` metadata is folded to a placeholder. The camelCase
122158
// `apiVersion`/`appVersion` keys must survive verbatim — otherwise a real

0 commit comments

Comments
 (0)