From 5fe6c49890fd93a456f322b4a887d6a088caa8fc Mon Sep 17 00:00:00 2001 From: "Matt Joiner (aider)" Date: Wed, 2 Oct 2024 11:25:32 +1000 Subject: [PATCH] Used aider and claude to refactor storage tests to go-quicktest/qt https://github.com/anacrolix/torrent/issues/959 --- go.mod | 3 ++- go.sum | 5 +++- storage/bolt-piece-completion_test.go | 21 ++++++++--------- storage/file-misc_test.go | 33 ++++++++++++--------------- storage/file_test.go | 13 +++++------ storage/issue95_test.go | 11 ++++----- storage/issue96_test.go | 14 ++++++------ storage/mmap_test.go | 11 ++++----- storage/safe-path_test.go | 11 ++++----- 9 files changed, 59 insertions(+), 63 deletions(-) diff --git a/go.mod b/go.mod index 733c6670..4bdd42af 100644 --- a/go.mod +++ b/go.mod @@ -34,6 +34,7 @@ require ( github.com/frankban/quicktest v1.14.6 github.com/fsnotify/fsnotify v1.5.4 github.com/go-llsqlite/adapter v0.0.0-20230927005056-7f5ce7f0c916 + github.com/go-quicktest/qt v1.101.0 github.com/google/btree v1.1.2 github.com/google/go-cmp v0.6.0 github.com/gorilla/websocket v1.5.0 @@ -110,7 +111,7 @@ require ( github.com/prometheus/common v0.35.0 // indirect github.com/prometheus/procfs v0.7.3 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/rogpeppe/go-internal v1.9.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect github.com/sethvargo/go-envconfig v0.8.2 // indirect diff --git a/go.sum b/go.sum index 0a98832a..3784e7af 100644 --- a/go.sum +++ b/go.sum @@ -230,6 +230,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI= +github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -496,8 +498,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 h1:Lt9DzQALzHoDwMBGJ6v8ObDPR0dzr2a6sXTB1Fq7IHs= github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA= github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8= diff --git a/storage/bolt-piece-completion_test.go b/storage/bolt-piece-completion_test.go index 3a778a86..ca4da883 100644 --- a/storage/bolt-piece-completion_test.go +++ b/storage/bolt-piece-completion_test.go @@ -3,8 +3,7 @@ package storage import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/metainfo" ) @@ -13,24 +12,24 @@ func TestBoltPieceCompletion(t *testing.T) { td := t.TempDir() pc, err := NewBoltPieceCompletion(td) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) defer pc.Close() pk := metainfo.PieceKey{} b, err := pc.Get(pk) - require.NoError(t, err) - assert.False(t, b.Ok) + qt.Assert(t, qt.IsNil(err)) + qt.Check(t, qt.IsFalse(b.Ok)) - require.NoError(t, pc.Set(pk, false)) + qt.Check(t, qt.IsNil(pc.Set(pk, false))) b, err = pc.Get(pk) - require.NoError(t, err) - assert.Equal(t, Completion{Complete: false, Ok: true}, b) + qt.Assert(t, qt.IsNil(err)) + qt.Check(t, qt.Equals(b, Completion{Complete: false, Ok: true})) - require.NoError(t, pc.Set(pk, true)) + qt.Check(t, qt.IsNil(pc.Set(pk, true))) b, err = pc.Get(pk) - require.NoError(t, err) - assert.Equal(t, Completion{Complete: true, Ok: true}, b) + qt.Assert(t, qt.IsNil(err)) + qt.Check(t, qt.Equals(b, Completion{Complete: true, Ok: true})) } diff --git a/storage/file-misc_test.go b/storage/file-misc_test.go index 706fc210..ee373e54 100644 --- a/storage/file-misc_test.go +++ b/storage/file-misc_test.go @@ -3,8 +3,7 @@ package storage import ( "testing" - qt "github.com/frankban/quicktest" - "github.com/stretchr/testify/assert" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/common" "github.com/anacrolix/torrent/metainfo" @@ -49,9 +48,8 @@ func TestExtentCompleteRequiredLengthsV2InfoWithGaps(t *testing.T) { }, }, } - c := qt.New(t) check := func(off, n int64, expected ...requiredLength) { - c.Check(extentCompleteRequiredLengths(info, off, n), qt.DeepEquals, expected) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, off, n), expected)) } check(0, 0) check(0, 1, requiredLength{FileIndex: 0, Length: 1}) @@ -72,27 +70,26 @@ func TestExtentCompleteRequiredLengths(t *testing.T) { {Path: []string{"b"}, Length: 3}, }, } - c := qt.New(t) check := func(off, n int64, expected ...requiredLength) { - c.Check(extentCompleteRequiredLengths(info, off, n), qt.DeepEquals, expected) + qt.Assert(t, qt.DeepEquals(extentCompleteRequiredLengths(info, off, n), expected)) } - assert.Empty(t, extentCompleteRequiredLengths(info, 0, 0)) - assert.EqualValues(t, []requiredLength{ + qt.Check(t, qt.HasLen(extentCompleteRequiredLengths(info, 0, 0), 0)) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, 0, 1), []requiredLength{ {FileIndex: 0, Length: 1}, - }, extentCompleteRequiredLengths(info, 0, 1)) - assert.EqualValues(t, []requiredLength{ + })) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, 0, 2), []requiredLength{ {FileIndex: 0, Length: 2}, - }, extentCompleteRequiredLengths(info, 0, 2)) - assert.EqualValues(t, []requiredLength{ + })) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, 0, 3), []requiredLength{ {FileIndex: 0, Length: 2}, {FileIndex: 1, Length: 1}, - }, extentCompleteRequiredLengths(info, 0, 3)) - assert.EqualValues(t, []requiredLength{ + })) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, 2, 2), []requiredLength{ {FileIndex: 1, Length: 2}, - }, extentCompleteRequiredLengths(info, 2, 2)) - assert.EqualValues(t, []requiredLength{ + })) + qt.Check(t, qt.DeepEquals(extentCompleteRequiredLengths(info, 4, 1), []requiredLength{ {FileIndex: 1, Length: 3}, - }, extentCompleteRequiredLengths(info, 4, 1)) - assert.Len(t, extentCompleteRequiredLengths(info, 5, 0), 0) + })) + qt.Check(t, qt.HasLen(extentCompleteRequiredLengths(info, 5, 0), 0)) check(6, 1) } diff --git a/storage/file_test.go b/storage/file_test.go index 103e8288..cbf9f191 100644 --- a/storage/file_test.go +++ b/storage/file_test.go @@ -9,8 +9,7 @@ import ( "testing" "github.com/anacrolix/missinggo/v2" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/metainfo" ) @@ -26,19 +25,19 @@ func TestShortFile(t *testing.T) { Pieces: make([]byte, 20), } ts, err := s.OpenTorrent(context.Background(), info, metainfo.Hash{}) - assert.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) f, err := os.Create(filepath.Join(td, "a")) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) err = f.Truncate(1) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) f.Close() var buf bytes.Buffer p := info.Piece(0) n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length())) - assert.EqualValues(t, 1, n) + qt.Check(t, qt.Equals(n, int64(1))) switch err { case nil, io.EOF: default: - t.Errorf("expected nil or EOF error from truncated piece, got %v", err) + t.Fatalf("unexpected error: %v", err) } } diff --git a/storage/issue95_test.go b/storage/issue95_test.go index 2beb4161..2b012e26 100644 --- a/storage/issue95_test.go +++ b/storage/issue95_test.go @@ -5,8 +5,7 @@ import ( "testing" "github.com/anacrolix/missinggo/v2/resource" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/metainfo" ) @@ -21,14 +20,14 @@ func testIssue95(t *testing.T, ci ClientImpl) { } c := NewClient(ci) t1, err := c.OpenTorrent(context.Background(), &info, metainfo.HashBytes([]byte("a"))) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) defer t1.Close() t2, err := c.OpenTorrent(context.Background(), &info, metainfo.HashBytes([]byte("b"))) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) defer t2.Close() t2p := t2.Piece(info.Piece(0)) - assert.NoError(t, t1.Close()) - assert.NotPanics(t, func() { t2p.Completion() }) + qt.Check(t, qt.IsNil(t1.Close())) + t2p.Completion() } func TestIssue95File(t *testing.T) { diff --git a/storage/issue96_test.go b/storage/issue96_test.go index 5c43574d..7b3e42bf 100644 --- a/storage/issue96_test.go +++ b/storage/issue96_test.go @@ -5,7 +5,7 @@ import ( "testing" g "github.com/anacrolix/generics" - "github.com/stretchr/testify/require" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/metainfo" ) @@ -21,14 +21,14 @@ func testMarkedCompleteMissingOnRead(t *testing.T, csf func(string) ClientImplCl Pieces: make([]byte, 20), } ts, err := cs.OpenTorrent(context.Background(), info, metainfo.Hash{}) - require.NoError(t, err) + qt.Assert(t, qt.IsNil(err)) p := ts.PieceWithHash(info.Piece(0), g.None[[]byte]()) - require.NoError(t, p.MarkComplete()) - // require.False(t, p.GetIsComplete()) + qt.Check(t, qt.IsNil(p.MarkComplete())) + // qt.Assert(t, qt.IsFalse(p.GetIsComplete())) n, err := p.ReadAt(make([]byte, 1), 0) - require.Error(t, err) - require.EqualValues(t, 0, n) - require.False(t, p.Completion().Complete) + qt.Check(t, qt.Not(qt.IsNil(err))) + qt.Check(t, qt.Equals(n, int(0))) + qt.Check(t, qt.IsFalse(p.Completion().Complete)) } func TestMarkedCompleteMissingOnReadFile(t *testing.T) { diff --git a/storage/mmap_test.go b/storage/mmap_test.go index c72f9a9c..f3f3c526 100644 --- a/storage/mmap_test.go +++ b/storage/mmap_test.go @@ -4,23 +4,22 @@ import ( "context" "testing" - qt "github.com/frankban/quicktest" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/internal/testutil" ) func TestMmapWindows(t *testing.T) { - c := qt.New(t) dir, mi := testutil.GreetingTestTorrent() cs := NewMMap(dir) defer func() { - c.Check(cs.Close(), qt.IsNil) + qt.Check(t, qt.IsNil(cs.Close())) }() info, err := mi.UnmarshalInfo() - c.Assert(err, qt.IsNil) + qt.Assert(t, qt.IsNil(err)) ts, err := cs.OpenTorrent(context.Background(), &info, mi.HashInfoBytes()) - c.Assert(err, qt.IsNil) + qt.Assert(t, qt.IsNil(err)) defer func() { - c.Check(ts.Close(), qt.IsNil) + qt.Check(t, qt.IsNil(ts.Close())) }() } diff --git a/storage/safe-path_test.go b/storage/safe-path_test.go index 13701e4b..cbb70662 100644 --- a/storage/safe-path_test.go +++ b/storage/safe-path_test.go @@ -7,7 +7,7 @@ import ( "path/filepath" "testing" - qt "github.com/frankban/quicktest" + "github.com/go-quicktest/qt" "github.com/anacrolix/torrent/metainfo" ) @@ -49,9 +49,8 @@ func TestToSafeFilePath(t *testing.T) { // Check that safe file path handling still exists for the newer file-opt-maker variants. func TestFileOptsSafeFilePathHandling(t *testing.T) { - c := qt.New(t) for i, _case := range safeFilePathTests { - c.Run(fmt.Sprintf("Case%v", i), func(c *qt.C) { + t.Run(fmt.Sprintf("Case%v", i), func(t *testing.T) { info := metainfo.Info{ Files: []metainfo.FileInfo{ {Path: _case.input}, @@ -60,12 +59,12 @@ func TestFileOptsSafeFilePathHandling(t *testing.T) { client := NewFileOpts(NewFileClientOpts{ ClientBaseDir: t.TempDir(), }) - defer func() { c.Check(client.Close(), qt.IsNil) }() + defer func() { qt.Assert(t, qt.IsNil(client.Close())) }() torImpl, err := client.OpenTorrent(context.Background(), &info, metainfo.Hash{}) if _case.expectErr { - c.Check(err, qt.Not(qt.IsNil)) + qt.Check(t, qt.Not(qt.IsNil(err))) } else { - c.Check(torImpl.Close(), qt.IsNil) + qt.Check(t, qt.IsNil(torImpl.Close())) } }) } -- 2.48.1