Repository: helm/helm (go)

Issue:
 Extra newline being added when using "range .Files.Lines <filename>"
Exactly as in #3617. Templates generated with `range .Files.Lines <filename>` pretend like there is an empty line at the end of the file, but there isn't. For example:

paths.list:
```
/api/v1
/api/v2
```
with template
```
{{- range .Files.Lines "paths.list" }}
- path: {{ . }}
  pathType: ImplementationSpecific
{{- end }}
```
produces
```
- path: /api/v1
  pathType: ImplementationSpecific
- path: /api/v2
  pathType: ImplementationSpecific
- path: 
  pathType: ImplementationSpecific
```

Output of `helm version`: `version.BuildInfo{Version:"v3.7.2", GitCommit:"663a896f4a815053445eec4153677ddc24a0a361", GitTreeState:"clean", GoVersion:"go1.16.10"}`


Pull request description:
strip trailing newline from Files.Lines
**What this PR does / why we need it**:
A trailing newline in a file would produce an extra empty list entry when using Files.Lines

**Special notes for your reviewer**:

**If applicable**:
- [ ] this PR contains documentation
- [x] this PR contains unit tests
- [ ] this PR has been tested for backwards compatibility

Fixes #10561 

Patch:
diff --git a/pkg/engine/files.go b/pkg/engine/files.go
index f29b0ab7c..f2cfdb3f3 100644
--- a/pkg/engine/files.go
+++ b/pkg/engine/files.go
@@ -131,7 +131,7 @@ func (f files) AsConfig() string {
 //
 //	data:
 //
-// {{ .Files.Glob("secrets/*").AsSecrets() }}
+// {{ .Files.Glob("secrets/*").AsSecrets() | indent 4 }}
 func (f files) AsSecrets() string {
 	if f == nil {
 		return ""
@@ -157,6 +157,9 @@ func (f files) Lines(path string) []string {
 	if f == nil || f[path] == nil {
 		return []string{}
 	}
-
-	return strings.Split(string(f[path]), "\n")
+	s := string(f[path])
+	if s[len(s)-1] == '\n' {
+		s = s[:len(s)-1]
+	}
+	return strings.Split(s, "\n")
 }


Test patch:
diff --git a/pkg/engine/files_test.go b/pkg/engine/files_test.go
index 4b37724f9..e53263c76 100644
--- a/pkg/engine/files_test.go
+++ b/pkg/engine/files_test.go
@@ -28,7 +28,8 @@ var cases = []struct {
 	{"ship/stowaway.txt", "Legatt"},
 	{"story/name.txt", "The Secret Sharer"},
 	{"story/author.txt", "Joseph Conrad"},
-	{"multiline/test.txt", "bar\nfoo"},
+	{"multiline/test.txt", "bar\nfoo\n"},
+	{"multiline/test_with_blank_lines.txt", "bar\nfoo\n\n\n"},
 }
 
 func getTestFiles() files {
@@ -96,3 +97,15 @@ func TestLines(t *testing.T) {
 
 	as.Equal("bar", out[0])
 }
+
+func TestBlankLines(t *testing.T) {
+	as := assert.New(t)
+
+	f := getTestFiles()
+
+	out := f.Lines("multiline/test_with_blank_lines.txt")
+	as.Len(out, 4)
+
+	as.Equal("bar", out[0])
+	as.Equal("", out[3])
+}