github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uio/uiotest/uiotest.go (about)

     1  // Copyright 2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uiotest
     6  
     7  import (
     8  	"io"
     9  	"testing"
    10  
    11  	"github.com/u-root/u-root/pkg/testutil"
    12  	"github.com/u-root/u-root/pkg/uio"
    13  )
    14  
    15  // TestLineWriter is an io.Writer that logs full lines of serial to tb.
    16  func TestLineWriter(tb testing.TB, prefix string) io.WriteCloser {
    17  	if len(prefix) > 0 {
    18  		return uio.FullLineWriter(&testLinePrefixWriter{tb: tb, prefix: prefix})
    19  	}
    20  	return uio.FullLineWriter(&testLineWriter{tb: tb})
    21  }
    22  
    23  // testLinePrefixWriter is an io.Writer that logs full lines of serial to tb.
    24  type testLinePrefixWriter struct {
    25  	tb     testing.TB
    26  	prefix string
    27  }
    28  
    29  func (tsw *testLinePrefixWriter) OneLine(p []byte) {
    30  	tsw.tb.Logf("%s %s: %s", testutil.NowLog(), tsw.prefix, p)
    31  }
    32  
    33  // testLineWriter is an io.Writer that logs full lines of serial to tb.
    34  type testLineWriter struct {
    35  	tb testing.TB
    36  }
    37  
    38  func (tsw *testLineWriter) OneLine(p []byte) {
    39  	tsw.tb.Logf("%s: %s", testutil.NowLog(), p)
    40  }