diff --git a/pkg/test/test.go b/pkg/test/test.go index 9167c49d8..02f2992cd 100644 --- a/pkg/test/test.go +++ b/pkg/test/test.go @@ -34,9 +34,27 @@ func BrokenTest(t *testing.T) { } } +// TB is a copy of Go 1.2's testing.TB. +type TB interface { + Error(args ...interface{}) + Errorf(format string, args ...interface{}) + Fail() + FailNow() + Failed() bool + Fatal(args ...interface{}) + Fatalf(format string, args ...interface{}) + Log(args ...interface{}) + Logf(format string, args ...interface{}) + Skip(args ...interface{}) + SkipNow() + Skipf(format string, args ...interface{}) + Skipped() bool +} + // TLog changes the log package's output to log to t and returns a function // to reset it back to stderr. -func TLog(t *testing.T) func() { +func TLog(t TB) func() { + // TODO(bradfitz): once we rely on Go 1.2, change this to take a testing.TB. log.SetOutput(&twriter{t: t}) return func() { log.SetOutput(os.Stderr) @@ -44,7 +62,7 @@ func TLog(t *testing.T) func() { } type twriter struct { - t *testing.T + t TB buf bytes.Buffer } diff --git a/pkg/test/test_test.go b/pkg/test/test_test.go index 5760ab05e..5bd91df6d 100644 --- a/pkg/test/test_test.go +++ b/pkg/test/test_test.go @@ -17,8 +17,41 @@ limitations under the License. package test_test import ( + "log" + "reflect" + "testing" + "camlistore.org/pkg/index" . "camlistore.org/pkg/test" ) var _ index.Interface = (*FakeIndex)(nil) + +type tbLogger struct { + TB + log []string +} + +func (l *tbLogger) Log(args ...interface{}) { + l.log = append(l.log, args[0].(string)) +} + +func TestTLog(t *testing.T) { + tb := new(tbLogger) + defer TLog(tb)() + defer log.SetFlags(log.Flags()) + log.SetFlags(0) + + log.Printf("hello") + log.Printf("hello\n") + log.Printf("some text\nand more text\n") + want := []string{ + "hello", + "hello", + "some text", + "and more text", + } + if !reflect.DeepEqual(tb.log, want) { + t.Errorf("Got %q; want %q", tb.log, want) + } +}