Unverified Commit 17b53fb3 authored by Matthew Fisher's avatar Matthew Fisher Committed by GitHub
Browse files

Merge pull request #4084 from adshmh/4082-fix-helm-output-leak-of-template-cmd-unit-tests

fixed output leak from template command unit tests
Showing with 7 additions and 19 deletions
+7 -19
...@@ -91,6 +91,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command { ...@@ -91,6 +91,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command {
RunE: t.run, RunE: t.run,
} }
cmd.SetOutput(out)
f := cmd.Flags() f := cmd.Flags()
f.BoolVar(&t.showNotes, "notes", false, "Show the computed NOTES.txt file as well") f.BoolVar(&t.showNotes, "notes", false, "Show the computed NOTES.txt file as well")
f.StringVarP(&t.releaseName, "name", "n", "release-name", "Release name") f.StringVarP(&t.releaseName, "name", "n", "release-name", "Release name")
...@@ -241,20 +242,20 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { ...@@ -241,20 +242,20 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if whitespaceRegex.MatchString(data) { if whitespaceRegex.MatchString(data) {
continue continue
} }
err = writeToFile(t.outputDir, m.Name, data) err = writeToFile(t.outputDir, m.Name, data, t.out)
if err != nil { if err != nil {
return err return err
} }
continue continue
} }
fmt.Printf("---\n# Source: %s\n", m.Name) fmt.Fprintf(t.out, "---\n# Source: %s\n", m.Name)
fmt.Println(data) fmt.Fprintln(t.out, data)
} }
return nil return nil
} }
// write the <data> to <output-dir>/<name> // write the <data> to <output-dir>/<name>
func writeToFile(outputDir string, name string, data string) error { func writeToFile(outputDir string, name string, data string, out io.Writer) error {
outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator)) outfileName := strings.Join([]string{outputDir, name}, string(filepath.Separator))
err := ensureDirectoryForFile(outfileName) err := ensureDirectoryForFile(outfileName)
...@@ -275,7 +276,7 @@ func writeToFile(outputDir string, name string, data string) error { ...@@ -275,7 +276,7 @@ func writeToFile(outputDir string, name string, data string) error {
return err return err
} }
fmt.Printf("wrote %s\n", outfileName) fmt.Fprintf(out, "wrote %s\n", outfileName)
return nil return nil
} }
......
...@@ -20,7 +20,6 @@ import ( ...@@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -178,14 +177,9 @@ func TestTemplateCmd(t *testing.T) { ...@@ -178,14 +177,9 @@ func TestTemplateCmd(t *testing.T) {
}, },
} }
var buf bytes.Buffer
for _, tt := range tests { for _, tt := range tests {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// capture stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// execute template command // execute template command
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
cmd := newTemplateCmd(out) cmd := newTemplateCmd(out)
...@@ -206,14 +200,8 @@ func TestTemplateCmd(t *testing.T) { ...@@ -206,14 +200,8 @@ func TestTemplateCmd(t *testing.T) {
} else if err != nil { } else if err != nil {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
} }
// restore stdout
w.Close()
os.Stdout = old
var b bytes.Buffer
io.Copy(&b, r)
r.Close()
// scan yaml into map[<path>]yaml // scan yaml into map[<path>]yaml
scanner := bufio.NewScanner(&b) scanner := bufio.NewScanner(out)
next := false next := false
lastKey := "" lastKey := ""
m := map[string]string{} m := map[string]string{}
...@@ -239,7 +227,6 @@ func TestTemplateCmd(t *testing.T) { ...@@ -239,7 +227,6 @@ func TestTemplateCmd(t *testing.T) {
} else { } else {
t.Errorf("could not find key %s", tt.expectKey) t.Errorf("could not find key %s", tt.expectKey)
} }
buf.Reset()
}) })
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment