Skip to content

Commit

Permalink
Used aider and claude to refactor storage tests to go-quicktest/qt
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Oct 2, 2024
1 parent 4af494e commit 5fe6c49
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 63 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
21 changes: 10 additions & 11 deletions storage/bolt-piece-completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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}))
}
33 changes: 15 additions & 18 deletions storage/file-misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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})
Expand All @@ -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)
}
13 changes: 6 additions & 7 deletions storage/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
11 changes: 5 additions & 6 deletions storage/issue95_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions storage/issue96_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions storage/mmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}()
}
11 changes: 5 additions & 6 deletions storage/safe-path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"testing"

qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"

"github.com/anacrolix/torrent/metainfo"
)
Expand Down Expand Up @@ -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},
Expand All @@ -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()))
}
})
}
Expand Down

0 comments on commit 5fe6c49

Please sign in to comment.