Sitelet https://github.com/helm/helm/commit/14f349e357522f3ddfdf64832b07655a0377b8e6
Skip to content

Commit 14f349e

Browse files
authored
Merge pull request #32303 from mahesh-sadupalli/fix/files-lines-empty-panic-v3
fix(engine): prevent Files.Lines panic on empty file (backport to v3)
2 parents 57f1dbe + c02a9ae commit 14f349e

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎pkg/engine/files.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (f files) AsSecrets() string {
154154
// {{ range .Files.Lines "foo/bar.html" }}
155155
// {{ . }}{{ end }}
156156
func (f files) Lines(path string) []string {
157-
if f == nil || f[path] == nil {
157+
if f == nil || len(f[path]) == 0 {
158158
return []string{}
159159
}
160160
s := string(f[path])

‎pkg/engine/files_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var cases = []struct {
3030
{"story/author.txt", "Joseph Conrad"},
3131
{"multiline/test.txt", "bar\nfoo\n"},
3232
{"multiline/test_with_blank_lines.txt", "bar\nfoo\n\n\n"},
33+
{"empty/empty.txt", ""},
34+
{"empty/newline_only.txt", "\n"},
3335
}
3436

3537
func getTestFiles() files {
@@ -109,3 +111,31 @@ func TestBlankLines(t *testing.T) {
109111
as.Equal("bar", out[0])
110112
as.Equal("", out[3])
111113
}
114+
115+
func TestLinesEmptyFile(t *testing.T) {
116+
as := assert.New(t)
117+
118+
f := getTestFiles()
119+
120+
out := f.Lines("empty/empty.txt")
121+
as.Empty(out)
122+
}
123+
124+
func TestLinesNewlineOnlyFile(t *testing.T) {
125+
as := assert.New(t)
126+
127+
f := getTestFiles()
128+
129+
out := f.Lines("empty/newline_only.txt")
130+
as.Len(out, 1)
131+
as.Empty(out[0])
132+
}
133+
134+
func TestLinesMissingFile(t *testing.T) {
135+
as := assert.New(t)
136+
137+
f := getTestFiles()
138+
139+
out := f.Lines("nonexistent.txt")
140+
as.Empty(out)
141+
}

0 commit comments

Comments
 (0)