Rename TeeWithPrefix to TeePrefix to match Go idioms
parent
9b707fe234
commit
7f7587d96c
|
@ -80,13 +80,13 @@ func teeSSH(s *ssh.Session, cmd string, outB io.Writer, errB io.Writer) error {
|
|||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
if err := util.TeeWithPrefix(util.ErrPrefix, errPipe, errB, glog.Infof); err != nil {
|
||||
if err := util.TeePrefix(util.ErrPrefix, errPipe, errB, glog.Infof); err != nil {
|
||||
glog.Errorf("tee stderr: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
go func() {
|
||||
if err := util.TeeWithPrefix(util.OutPrefix, outPipe, outB, glog.Infof); err != nil {
|
||||
if err := util.TeePrefix(util.OutPrefix, outPipe, outB, glog.Infof); err != nil {
|
||||
glog.Errorf("tee stdout: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
|
|
|
@ -205,10 +205,9 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// TeeWithPrefix logs new lines from a reader. Designed to be run in the background.
|
||||
func TeeWithPrefix(prefix string, r io.Reader, w io.Writer, logger func(format string, args ...interface{})) error {
|
||||
// TeePrefix copies bytes from a reader to writer, logging each new line.
|
||||
func TeePrefix(prefix string, r io.Reader, w io.Writer, logger func(format string, args ...interface{})) error {
|
||||
scanner := bufio.NewScanner(r)
|
||||
// Collect individual bytes so that we don't accidentally strip newlines required by callers.
|
||||
scanner.Split(bufio.ScanBytes)
|
||||
var line bytes.Buffer
|
||||
|
||||
|
|
|
@ -163,39 +163,38 @@ func TestGetBinaryDownloadURL(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestTeeWithPrefix(t *testing.T) {
|
||||
func TestTeePrefix(t *testing.T) {
|
||||
var in bytes.Buffer
|
||||
var out bytes.Buffer
|
||||
var logged strings.Builder
|
||||
|
||||
logSink := func(format string, args ...interface{}) {
|
||||
logged.WriteString("(")
|
||||
logged.WriteString(fmt.Sprintf(format, args...))
|
||||
logged.WriteString(")")
|
||||
logged.WriteString("(" + fmt.Sprintf(format, args...) + ")")
|
||||
}
|
||||
|
||||
// Simulate the primary use case: tee in the background. This also helps avoid I/O races.
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
TeeWithPrefix(": ", &in, &out, logSink)
|
||||
TeePrefix(":", &in, &out, logSink)
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
in.Write([]byte("goo"))
|
||||
in.Write([]byte("\n"))
|
||||
in.Write([]byte("gle"))
|
||||
in.Write([]byte("g\r\n\r\n"))
|
||||
in.Write([]byte("le"))
|
||||
wg.Wait()
|
||||
|
||||
gotBytes := out.Bytes()
|
||||
wantBytes := []byte("goo\ngle")
|
||||
wantBytes := []byte("goo\ng\r\n\r\nle")
|
||||
if !bytes.Equal(gotBytes, wantBytes) {
|
||||
t.Errorf("got bytes: %v, want: %v", gotBytes, wantBytes)
|
||||
t.Errorf("output=%q, want: %q", gotBytes, wantBytes)
|
||||
}
|
||||
|
||||
gotLog := logged.String()
|
||||
wantLog := "(: goo)(: gle)"
|
||||
wantLog := "(:goo)(:g)(:le)"
|
||||
if gotLog != wantLog {
|
||||
t.Errorf("got log %q, want log %q", gotLog, wantLog)
|
||||
t.Errorf("log=%q, want: %q", gotLog, wantLog)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,13 +95,13 @@ func (m *MinikubeRunner) teeRun(cmd *exec.Cmd) (string, string, error) {
|
|||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
if err := commonutil.TeeWithPrefix(commonutil.ErrPrefix, errPipe, &errB, Logf); err != nil {
|
||||
if err := commonutil.TeePrefix(commonutil.ErrPrefix, errPipe, &errB, Logf); err != nil {
|
||||
m.T.Logf("tee: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
go func() {
|
||||
if err := commonutil.TeeWithPrefix(commonutil.OutPrefix, outPipe, &outB, Logf); err != nil {
|
||||
if err := commonutil.TeePrefix(commonutil.OutPrefix, outPipe, &outB, Logf); err != nil {
|
||||
m.T.Logf("tee: %v", err)
|
||||
}
|
||||
wg.Done()
|
||||
|
|
Loading…
Reference in New Issue