diff options
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 223 | ||||
-rw-r--r-- | go.mod | 11 | ||||
-rw-r--r-- | go.sum | 21 | ||||
-rw-r--r-- | tools.go | 1 |
5 files changed, 155 insertions, 108 deletions
@@ -39,12 +39,15 @@ secure: secure-practices secure-vulnerabilities .PHONY: secure-practices secure-practices: - go install github.com/praetorian-inc/gokart +# TODO: change pinned version of GoKart to "latest" once PR #84 is merged +# https://github.com/praetorian-inc/gokart/pull/84 +# go install github.com/praetorian-inc/gokart@latest + go install github.com/selesy/gokart-pre gokart scan .PHONY: secure-vulnerabilities secure-vulnerabilities: - go install golang.org/x/vuln/cmd/govulncheck@latest + go install golang.org/x/vuln/cmd/govulncheck govulncheck ./... .PHONY: test diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index ac5a7c75..39814733 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -18,106 +18,125 @@ import ( func TestCache(t *testing.T) { repo := repository.CreateGoGitTestRepo(t, false) - cache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) - - // Create, set and get user identity - iden1, err := cache.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) - err = cache.SetUserIdentity(iden1) - require.NoError(t, err) - userIden, err := cache.GetUserIdentity() - require.NoError(t, err) - require.Equal(t, iden1.Id(), userIden.Id()) - - // it's possible to create two identical identities - iden2, err := cache.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) + indexCount := func(t *testing.T, name string) uint64 { + t.Helper() - // Two identical identities yield a different id - require.NotEqual(t, iden1.Id(), iden2.Id()) - - indexCount := func(name string) uint64 { idx, err := repo.GetIndex(name) require.NoError(t, err) count, err := idx.DocCount() require.NoError(t, err) + return count } - // There is now two identities in the cache - require.Len(t, cache.Identities().AllIds(), 2) - require.Len(t, cache.identities.excerpts, 2) - require.Len(t, cache.identities.cached, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(0), indexCount(bug.Namespace)) + var ( + cache *RepoCache + bug1 *BugCache + iden1 *IdentityCache + ) - // Create a bug - bug1, _, err := cache.Bugs().New("title", "message") - require.NoError(t, err) + t.Run("entity operations on cache", func(t *testing.T) { + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) - // It's possible to create two identical bugs - bug2, _, err := cache.Bugs().New("title", "marker") - require.NoError(t, err) + // Create, set and get user identity + iden1, err = cache.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + err = cache.SetUserIdentity(iden1) + require.NoError(t, err) + userIden, err := cache.GetUserIdentity() + require.NoError(t, err) + require.Equal(t, iden1.Id(), userIden.Id()) - // two identical bugs yield a different id - require.NotEqual(t, bug1.Id(), bug2.Id()) + // it's possible to create two identical identities + iden2, err := cache.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) - // There is now two bugs in the cache - require.Len(t, cache.Bugs().AllIds(), 2) - require.Len(t, cache.bugs.excerpts, 2) - require.Len(t, cache.bugs.cached, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(2), indexCount(bug.Namespace)) + // Two identical identities yield a different id + require.NotEqual(t, iden1.Id(), iden2.Id()) - // Resolving - _, err = cache.Identities().Resolve(iden1.Id()) - require.NoError(t, err) - _, err = cache.Identities().ResolveExcerpt(iden1.Id()) - require.NoError(t, err) - _, err = cache.Identities().ResolvePrefix(iden1.Id().String()[:10]) - require.NoError(t, err) + // There is now two identities in the cache + require.Len(t, cache.Identities().AllIds(), 2) + require.Len(t, cache.identities.excerpts, 2) + require.Len(t, cache.identities.cached, 2) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(0), indexCount(t, bug.Namespace)) - _, err = cache.Bugs().Resolve(bug1.Id()) - require.NoError(t, err) - _, err = cache.Bugs().ResolveExcerpt(bug1.Id()) - require.NoError(t, err) - _, err = cache.Bugs().ResolvePrefix(bug1.Id().String()[:10]) - require.NoError(t, err) + // Create a bug + bug1, _, err = cache.Bugs().New("title", "message") + require.NoError(t, err) - // Querying - q, err := query.Parse("status:open author:descartes sort:edit-asc") - require.NoError(t, err) - res, err := cache.Bugs().Query(q) - require.NoError(t, err) - require.Len(t, res, 2) + // It's possible to create two identical bugs + bug2, _, err := cache.Bugs().New("title", "marker") + require.NoError(t, err) - q, err = query.Parse("status:open marker") // full-text search - require.NoError(t, err) - res, err = cache.Bugs().Query(q) - require.NoError(t, err) - require.Len(t, res, 1) + // two identical bugs yield a different id + require.NotEqual(t, bug1.Id(), bug2.Id()) - // Close - require.NoError(t, cache.Close()) - require.Empty(t, cache.bugs.cached) - require.Empty(t, cache.bugs.excerpts) - require.Empty(t, cache.identities.cached) - require.Empty(t, cache.identities.excerpts) + // There is now two bugs in the cache + require.Len(t, cache.Bugs().AllIds(), 2) + require.Len(t, cache.bugs.excerpts, 2) + require.Len(t, cache.bugs.cached, 2) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(2), indexCount(t, bug.Namespace)) + + // Resolving + _, err = cache.Identities().Resolve(iden1.Id()) + require.NoError(t, err) + _, err = cache.Identities().ResolveExcerpt(iden1.Id()) + require.NoError(t, err) + _, err = cache.Identities().ResolvePrefix(iden1.Id().String()[:10]) + require.NoError(t, err) + + _, err = cache.Bugs().Resolve(bug1.Id()) + require.NoError(t, err) + _, err = cache.Bugs().ResolveExcerpt(bug1.Id()) + require.NoError(t, err) + _, err = cache.Bugs().ResolvePrefix(bug1.Id().String()[:10]) + require.NoError(t, err) + + // Querying + q, err := query.Parse("status:open author:descartes sort:edit-asc") + require.NoError(t, err) + res, err := cache.Bugs().Query(q) + require.NoError(t, err) + require.Len(t, res, 2) + + q, err = query.Parse("status:open marker") // full-text search + require.NoError(t, err) + res, err = cache.Bugs().Query(q) + require.NoError(t, err) + require.Len(t, res, 1) + + // Close + require.NoError(t, cache.Close()) + require.Empty(t, cache.bugs.cached) + require.Empty(t, cache.bugs.excerpts) + require.Empty(t, cache.identities.cached) + require.Empty(t, cache.identities.excerpts) + }) + + t.Run("closed caches are empty", func(t *testing.T) { + t.Skip() + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) + + require.Empty(t, cache.identities.excerpts) + }) // Reload, only excerpt are loaded, but as we need to load the identities used in the bugs // to check the signatures, we also load the identity used above - cache, err = NewRepoCacheNoEvents(repo) - require.NoError(t, err) + cache = createTestRepoCacheNoEvents(t, repo) + require.Len(t, cache.bugs.cached, 0) require.Len(t, cache.bugs.excerpts, 2) require.Len(t, cache.identities.cached, 0) require.Len(t, cache.identities.excerpts, 2) - require.Equal(t, uint64(2), indexCount(identity.Namespace)) - require.Equal(t, uint64(2), indexCount(bug.Namespace)) + require.Equal(t, uint64(2), indexCount(t, identity.Namespace)) + require.Equal(t, uint64(2), indexCount(t, bug.Namespace)) // Resolving load from the disk - _, err = cache.Identities().Resolve(iden1.Id()) + _, err := cache.Identities().Resolve(iden1.Id()) require.NoError(t, err) _, err = cache.Identities().ResolveExcerpt(iden1.Id()) require.NoError(t, err) @@ -142,11 +161,8 @@ func TestCache(t *testing.T) { func TestCachePushPull(t *testing.T) { repoA, repoB, _ := repository.SetupGoGitReposAndRemote(t) - cacheA, err := NewRepoCacheNoEvents(repoA) - require.NoError(t, err) - - cacheB, err := NewRepoCacheNoEvents(repoB) - require.NoError(t, err) + cacheA := createTestRepoCacheNoEvents(t, repoA) + cacheB := createTestRepoCacheNoEvents(t, repoB) // Create, set and get user identity reneA, err := cacheA.Identities().New("René Descartes", "rene@descartes.fr") @@ -208,8 +224,7 @@ func TestRemove(t *testing.T) { err = repo.AddRemote("remoteB", remoteB.GetLocalRemote()) require.NoError(t, err) - repoCache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + repoCache := createTestRepoCacheNoEvents(t, repo) rene, err := repoCache.Identities().New("René Descartes", "rene@descartes.fr") require.NoError(t, err) @@ -247,8 +262,7 @@ func TestRemove(t *testing.T) { func TestCacheEviction(t *testing.T) { repo := repository.CreateGoGitTestRepo(t, false) - repoCache, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + repoCache := createTestRepoCacheNoEvents(t, repo) repoCache.setCacheSize(2) require.Equal(t, 2, repoCache.bugs.maxLoaded) @@ -298,7 +312,25 @@ func TestCacheEviction(t *testing.T) { require.Equal(t, 2, repoCache.bugs.lru.Len()) } +func TestLongDescription(t *testing.T) { + // See https://github.com/MichaelMure/git-bug/issues/606 + + text := strings.Repeat("x", 65536) + + repo := repository.CreateGoGitTestRepo(t, false) + + backend := createTestRepoCacheNoEvents(t, repo) + + i, err := backend.Identities().New("René Descartes", "rene@descartes.fr") + require.NoError(t, err) + + _, _, err = backend.Bugs().NewRaw(i, time.Now().Unix(), text, text, nil, nil) + require.NoError(t, err) +} + func checkBugPresence(t *testing.T, cache *RepoCache, bug *BugCache, presence bool) { + t.Helper() + id := bug.Id() require.Equal(t, presence, cache.bugs.lru.Contains(id)) b, ok := cache.bugs.cached[id] @@ -308,19 +340,26 @@ func checkBugPresence(t *testing.T, cache *RepoCache, bug *BugCache, presence bo } } -func TestLongDescription(t *testing.T) { - // See https://github.com/MichaelMure/git-bug/issues/606 +func createTestRepoCacheNoEvents(t *testing.T, repo repository.TestedRepo) *RepoCache { + t.Helper() - text := strings.Repeat("x", 65536) + cache, err := NewRepoCacheNoEvents(repo) + require.NoError(t, err) - repo := repository.CreateGoGitTestRepo(t, false) + t.Cleanup(func() { + require.NoError(t, cache.Close()) + }) - backend, err := NewRepoCacheNoEvents(repo) - require.NoError(t, err) + return cache +} - i, err := backend.Identities().New("René Descartes", "rene@descartes.fr") - require.NoError(t, err) +func indexCount(t *testing.T, repo repository.TestedRepo, name string) uint64 { + t.Helper() - _, _, err = backend.Bugs().NewRaw(i, time.Now().Unix(), text, text, nil, nil) + idx, err := repo.GetIndex(name) require.NoError(t, err) + count, err := idx.DocCount() + require.NoError(t, err) + + return count } @@ -21,17 +21,18 @@ require ( github.com/mattn/go-isatty v0.0.17 github.com/phayes/freeport v0.0.0-20171002181615-b8543db493a5 github.com/pkg/errors v0.9.1 + github.com/selesy/gokart-pre v0.5.2-rc1 github.com/shurcooL/githubv4 v0.0.0-20190601194912-068505affed7 github.com/skratchdot/open-golang v0.0.0-20190402232053-79abb63cd66e github.com/spf13/cobra v1.6.1 github.com/stretchr/testify v1.8.1 github.com/vektah/gqlparser/v2 v2.5.1 github.com/xanzy/go-gitlab v0.77.0 - golang.org/x/crypto v0.4.0 + golang.org/x/crypto v0.5.0 golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c golang.org/x/sync v0.1.0 - golang.org/x/sys v0.3.0 - golang.org/x/text v0.5.0 + golang.org/x/sys v0.4.0 + golang.org/x/text v0.6.0 ) require ( @@ -106,8 +107,8 @@ require ( github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.etcd.io/bbolt v1.3.5 // indirect golang.org/x/mod v0.7.0 // indirect - golang.org/x/net v0.4.0 // indirect - golang.org/x/term v0.3.0 // indirect + golang.org/x/net v0.5.0 // indirect + golang.org/x/term v0.4.0 // indirect golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect golang.org/x/tools v0.4.0 // indirect golang.org/x/vuln v0.0.0-20220908155419-5537ad2271a7 @@ -250,6 +250,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM= github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY= +github.com/selesy/gokart-pre v0.5.2-rc1 h1:TGgWu0hhD7kW1nKqx7qfOsBPm1jp1OlVtTMRPKetW5U= +github.com/selesy/gokart-pre v0.5.2-rc1/go.mod h1:a2Ej7TE206x7KjQr2kud7nckfVFQaIv7ljzT1KxFdVg= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= @@ -322,8 +324,8 @@ golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8= -golang.org/x/crypto v0.4.0/go.mod h1:3quD/ATkf6oY+rnes5c3ExXTbLc8mueNue5/DoinL80= +golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= +golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= @@ -340,8 +342,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= -golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c h1:q3gFqPqH7NVofKo3c3yETAP//pPI+G5mvB7qqj1Y5kY= golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -376,15 +378,16 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI= -golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/term v0.4.0 h1:O7UWfv5+A2qiuulQk30kVinPoMtoIPeVaKLEgLpVkvg= +golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -392,8 +395,8 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= -golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ= golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -6,5 +6,6 @@ import ( _ "github.com/99designs/gqlgen" _ "github.com/cheekybits/genny" _ "github.com/praetorian-inc/gokart" + _ "github.com/selesy/gokart-pre" _ "golang.org/x/vuln/cmd/govulncheck" ) |