fix(helm): fixed output leak from template command unit tests

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
pull/4084/head
Arash Deshmeh 7 years ago
parent 1984f436af
commit 9f4a9d206c

@ -91,6 +91,7 @@ func newTemplateCmd(out io.Writer) *cobra.Command {
RunE: t.run,
}
cmd.SetOutput(out)
f := cmd.Flags()
f.BoolVar(&t.showNotes, "notes", false, "show the computed NOTES.txt file as well")
f.StringVarP(&t.releaseName, "name", "n", "release-name", "release name")
@ -241,20 +242,20 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error {
if whitespaceRegex.MatchString(data) {
continue
}
err = writeToFile(t.outputDir, m.Name, data)
err = writeToFile(t.outputDir, m.Name, data, t.out)
if err != nil {
return err
}
continue
}
fmt.Printf("---\n# Source: %s\n", m.Name)
fmt.Println(data)
fmt.Fprintf(t.out, "---\n# Source: %s\n", m.Name)
fmt.Fprintln(t.out, data)
}
return nil
}
// 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))
err := ensureDirectoryForFile(outfileName)
@ -275,7 +276,7 @@ func writeToFile(outputDir string, name string, data string) error {
return err
}
fmt.Printf("wrote %s\n", outfileName)
fmt.Fprintf(out, "wrote %s\n", outfileName)
return nil
}

@ -20,7 +20,6 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
@ -178,14 +177,9 @@ func TestTemplateCmd(t *testing.T) {
},
}
var buf bytes.Buffer
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// capture stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// execute template command
out := bytes.NewBuffer(nil)
cmd := newTemplateCmd(out)
@ -206,14 +200,8 @@ func TestTemplateCmd(t *testing.T) {
} else if err != nil {
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
scanner := bufio.NewScanner(&b)
scanner := bufio.NewScanner(out)
next := false
lastKey := ""
m := map[string]string{}
@ -239,7 +227,6 @@ func TestTemplateCmd(t *testing.T) {
} else {
t.Errorf("could not find key %s", tt.expectKey)
}
buf.Reset()
})
}
}

Loading…
Cancel
Save