From d26638d7ec3ffdf86b774bec90e99a824b6af8fc Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 20 Jan 2025 17:14:15 -0800 Subject: [PATCH] iotest.Writer => ioutil.TestLogWriter Drop the partial implementation of a testing.T writer (iotest.Writer) in favor of the more complete implementation in go.abhg.dev/io/ioutil. --- config_test.go | 10 ++++----- go.mod | 5 ++++- go.sum | 2 ++ integration/integration_test.go | 6 +++--- integration/unquote_test.go | 4 ++-- internal/iotest/writer.go | 28 ------------------------- internal/iotest/writer_test.go | 37 --------------------------------- internal/log/logtest/logger.go | 4 ++-- wrap_test.go | 4 ++-- 9 files changed, 20 insertions(+), 80 deletions(-) delete mode 100644 internal/iotest/writer.go delete mode 100644 internal/iotest/writer_test.go diff --git a/config_test.go b/config_test.go index 3ec02a4..3c428b6 100644 --- a/config_test.go +++ b/config_test.go @@ -5,13 +5,13 @@ import ( "strings" "testing" - "github.com/abhinav/tmux-fastcopy/internal/iotest" "github.com/abhinav/tmux-fastcopy/internal/tmux" "github.com/abhinav/tmux-fastcopy/internal/tmux/tmuxopt" "github.com/abhinav/tmux-fastcopy/internal/tmux/tmuxtest" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.abhg.dev/io/ioutil" "pgregory.net/rapid" ) @@ -272,7 +272,7 @@ func TestConfigFlags(t *testing.T) { var cfg config fset := flag.NewFlagSet(t.Name(), flag.ContinueOnError) - fset.SetOutput(iotest.Writer(t)) + fset.SetOutput(ioutil.TestLogWriter(t, "")) cfg.RegisterFlags(fset) err := fset.Parse(tt.give) @@ -291,7 +291,7 @@ func TestConfigFlags(t *testing.T) { args := cfg.Flags() fset := flag.NewFlagSet(t.Name(), flag.ContinueOnError) - fset.SetOutput(iotest.Writer(t)) + fset.SetOutput(ioutil.TestLogWriter(t, "")) var got config got.RegisterFlags(fset) @@ -485,7 +485,7 @@ func TestConfigFlags_rapid(t *testing.T) { } flag := flag.NewFlagSet(t.Name(), flag.ContinueOnError) - flag.SetOutput(iotest.Writer(t)) + flag.SetOutput(ioutil.TestLogWriter(t, "")) var got config got.RegisterFlags(flag) @@ -507,7 +507,7 @@ func TestUsageHasAllConfigFlags(t *testing.T) { // _usage. fset := flag.NewFlagSet(t.Name(), flag.ContinueOnError) - fset.SetOutput(iotest.Writer(t)) + fset.SetOutput(ioutil.TestLogWriter(t, "")) new(config).RegisterFlags(fset) fset.VisitAll(func(f *flag.Flag) { diff --git a/go.mod b/go.mod index 93117c5..4eb18da 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/abhinav/tmux-fastcopy -go 1.23.3 +go 1.23.4 + +toolchain go1.23.5 require ( github.com/benbjohnson/clock v1.3.5 @@ -11,6 +13,7 @@ require ( github.com/rivo/uniseg v0.4.7 github.com/stretchr/testify v1.10.0 go.abhg.dev/algorithm/huffman v0.2.0 + go.abhg.dev/io/ioutil v0.1.0 go.uber.org/multierr v1.11.0 pgregory.net/rapid v1.1.0 ) diff --git a/go.sum b/go.sum index 408980a..76641b0 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.abhg.dev/algorithm/huffman v0.2.0 h1:ABToWAM+WRXa2jqwzvyN17iNn5MMqxXtFZ7b+RmRrjU= go.abhg.dev/algorithm/huffman v0.2.0/go.mod h1:FR+G7qqDYLe4hQ+8dFp6a3PjfNkg3MLCW0/oV3h6Py0= +go.abhg.dev/io/ioutil v0.1.0 h1:YGGMzh9HT52JYuVWbnr/E5GkYHbL3yRNDjcxFDaUHNk= +go.abhg.dev/io/ioutil v0.1.0/go.mod h1:79IIyZVWxNZE8hBxMtubM8zJFPs+2NCqHYfWeH3X6hM= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/integration/integration_test.go b/integration/integration_test.go index 60730a3..b89fc11 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -16,10 +16,10 @@ import ( "testing" "time" - "github.com/abhinav/tmux-fastcopy/internal/iotest" "github.com/creack/pty" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.abhg.dev/io/ioutil" "go.uber.org/multierr" ) @@ -634,10 +634,10 @@ type virtualTmuxConfig struct { } func (cfg *virtualTmuxConfig) Build(t testing.TB) *virtualTmux { - stderr := iotest.Writer(t) + stderr := ioutil.TestLogWriter(t, "") cmd := exec.Command(cfg.Tmux) cmd.Env = cfg.Env - cmd.Stderr = iotest.Writer(t) + cmd.Stderr = ioutil.TestLogWriter(t, "") cmd.Dir = cfg.Dir t.Logf("Starting tmux with size %dx%d", cfg.Width, cfg.Height) diff --git a/integration/unquote_test.go b/integration/unquote_test.go index 219d808..8a44cbd 100644 --- a/integration/unquote_test.go +++ b/integration/unquote_test.go @@ -8,10 +8,10 @@ import ( "strings" "testing" - "github.com/abhinav/tmux-fastcopy/internal/iotest" "github.com/abhinav/tmux-fastcopy/internal/tmux/tmuxopt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.abhg.dev/io/ioutil" ) func TestUnquoteTmuxOptions(t *testing.T) { @@ -39,7 +39,7 @@ func TestUnquoteTmuxOptions(t *testing.T) { "SHELL=/bin/sh", "TMUX_TMPDIR=" + tmpDir, } - cmdout := iotest.Writer(t) + cmdout := ioutil.TestLogWriter(t, "") cmd := exec.Command(tmuxExe, "start-server", ";", "new-session", "-d") cmd.Dir = root diff --git a/internal/iotest/writer.go b/internal/iotest/writer.go deleted file mode 100644 index d693c9a..0000000 --- a/internal/iotest/writer.go +++ /dev/null @@ -1,28 +0,0 @@ -// Package iotest provides IO-related testing utilities. -package iotest - -import ( - "bytes" - "io" -) - -var _newline = []byte("\n") - -// Logger is the destination for messages from the Writer. -// It is satisfied by *testing.T and *testing.B. -type Logger interface { - Logf(format string, args ...interface{}) -} - -// Writer builds an io.Writer that writes to the given testing.TB. -func Writer(t Logger) io.Writer { - return &writer{t} -} - -type writer struct{ t Logger } - -func (w *writer) Write(b []byte) (int, error) { - b = bytes.TrimSuffix(b, _newline) - w.t.Logf("%s", b) - return len(b), nil -} diff --git a/internal/iotest/writer_test.go b/internal/iotest/writer_test.go deleted file mode 100644 index 12c6fd9..0000000 --- a/internal/iotest/writer_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package iotest - -import ( - "bytes" - "fmt" - "io" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -var _ Logger = (*testing.T)(nil) - -type fakeT struct { - *testing.T - - Buffer bytes.Buffer -} - -func (t *fakeT) Logf(msg string, args ...interface{}) { - fmt.Fprintln(&t.Buffer, fmt.Sprintf(msg, args...)) - // println to make sure it ends with a newline -} - -func TestWriter(t *testing.T) { - t.Parallel() - - fakeT := fakeT{T: t} - w := Writer(&fakeT) - _, err := io.WriteString(w, "foo") - require.NoError(t, err) - assert.Equal(t, "foo\n", fakeT.Buffer.String()) - // TODO: If we wanted this to be more accurate, we would have it buffer - // the input on newlines simillar to the log-based io.Writer. It - // doesn't matter here. -} diff --git a/internal/log/logtest/logger.go b/internal/log/logtest/logger.go index 01a8421..f2e7c09 100644 --- a/internal/log/logtest/logger.go +++ b/internal/log/logtest/logger.go @@ -4,11 +4,11 @@ package logtest import ( "testing" - "github.com/abhinav/tmux-fastcopy/internal/iotest" "github.com/abhinav/tmux-fastcopy/internal/log" + "go.abhg.dev/io/ioutil" ) // NewLogger builds a logger at debug level that writes to a testing.T. func NewLogger(t testing.TB) *log.Logger { - return log.New(iotest.Writer(t)).WithLevel(log.Debug) + return log.New(ioutil.TestLogWriter(t, "")).WithLevel(log.Debug) } diff --git a/wrap_test.go b/wrap_test.go index 96e51ec..4d08deb 100644 --- a/wrap_test.go +++ b/wrap_test.go @@ -6,13 +6,13 @@ import ( "testing" "github.com/abhinav/tmux-fastcopy/internal/envtest" - "github.com/abhinav/tmux-fastcopy/internal/iotest" "github.com/abhinav/tmux-fastcopy/internal/log/logtest" "github.com/abhinav/tmux-fastcopy/internal/tmux" "github.com/abhinav/tmux-fastcopy/internal/tmux/tmuxtest" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.abhg.dev/io/ioutil" ) func TestWrapper(t *testing.T) { @@ -69,7 +69,7 @@ func TestWrapper(t *testing.T) { mockTmux.EXPECT().NewSession(gomock.Any()). Do(func(req tmux.NewSessionRequest) { fset := flag.NewFlagSet(_name, flag.ContinueOnError) - fset.SetOutput(iotest.Writer(t)) + fset.SetOutput(ioutil.TestLogWriter(t, "")) var gotConfig config gotConfig.RegisterFlags(fset)