diff options
author | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2023-08-31 06:56:43 +0300 |
---|---|---|
committer | Quang-Minh Nguyen <qmnguyen@gitlab.com> | 2023-08-31 06:56:43 +0300 |
commit | 1d2d2c54dbc8b6bdc6cf45a5f9ccf8d731c44c0c (patch) | |
tree | 73f2ba058215ac82fb40c0ebb2214f7f810bd0c5 | |
parent | 18832a255c81ec4a6c98c693e74d9a21ac89ef55 (diff) | |
parent | 3adbbfb5a6f8ff81590693d19f8c9828af1b7d45 (diff) |
Merge branch 'wc/testhelper-lint' into 'master'
lint: Add linter for testhelper.Run
Closes #5522
See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/6284
Merged-by: Quang-Minh Nguyen <qmnguyen@gitlab.com>
Approved-by: Quang-Minh Nguyen <qmnguyen@gitlab.com>
Reviewed-by: Patrick Steinhardt <psteinhardt@gitlab.com>
Reviewed-by: Will Chandler <wchandler@gitlab.com>
Reviewed-by: Quang-Minh Nguyen <qmnguyen@gitlab.com>
Co-authored-by: Will Chandler <wchandler@gitlab.com>
99 files changed, 1008 insertions, 196 deletions
diff --git a/.golangci.yml b/.golangci.yml index 77b2715f7..c1346546c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -147,6 +147,9 @@ linters-settings: unavailable_code: included-functions: - gitlab.com/gitlab-org/gitaly/v16/internal/structerr.NewUnavailable + testhelper_run: + included-functions: + - gitlab.com/gitlab-org/gitaly/v16/internal/testhelper.Run issues: exclude-use-default: false diff --git a/auth/testhelper_test.go b/auth/testhelper_test.go new file mode 100644 index 000000000..cb7efe000 --- /dev/null +++ b/auth/testhelper_test.go @@ -0,0 +1,11 @@ +package gitalyauth + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/client/client_test.go b/client/testhelper_test.go index 137104d4d..137104d4d 100644 --- a/client/client_test.go +++ b/client/testhelper_test.go diff --git a/cmd/gitaly-gpg/testhelper_test.go b/cmd/gitaly-gpg/testhelper_test.go new file mode 100644 index 000000000..432115dc0 --- /dev/null +++ b/cmd/gitaly-gpg/testhelper_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go index 902317711..934f1a3ce 100644 --- a/cmd/gitaly-hooks/hooks_test.go +++ b/cmd/gitaly-hooks/hooks_test.go @@ -120,10 +120,6 @@ func envForHooks(tb testing.TB, ctx context.Context, cfg config.Cfg, repo *gital return env } -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestHooksPrePostWithSymlinkedStoragePath(t *testing.T) { tempDir := testhelper.TempDir(t) diff --git a/cmd/gitaly-hooks/testhelper_test.go b/cmd/gitaly-hooks/testhelper_test.go new file mode 100644 index 000000000..432115dc0 --- /dev/null +++ b/cmd/gitaly-hooks/testhelper_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/cmd/gitaly/check_test.go b/cmd/gitaly/check_test.go index ffaf7c6ec..596367051 100644 --- a/cmd/gitaly/check_test.go +++ b/cmd/gitaly/check_test.go @@ -12,10 +12,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestCheckOK(t *testing.T) { user, password := "user123", "password321" diff --git a/cmd/gitaly/testhelper_test.go b/cmd/gitaly/testhelper_test.go new file mode 100644 index 000000000..432115dc0 --- /dev/null +++ b/cmd/gitaly/testhelper_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/backoff/testhelper_test.go b/internal/backoff/testhelper_test.go new file mode 100644 index 000000000..cc3edbe5d --- /dev/null +++ b/internal/backoff/testhelper_test.go @@ -0,0 +1,11 @@ +package backoff + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/blackbox/testhelper_test.go b/internal/blackbox/testhelper_test.go new file mode 100644 index 000000000..e88e9446d --- /dev/null +++ b/internal/blackbox/testhelper_test.go @@ -0,0 +1,11 @@ +package blackbox + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/bootstrap/starter/testhelper_test.go b/internal/bootstrap/starter/testhelper_test.go new file mode 100644 index 000000000..ab294fdf6 --- /dev/null +++ b/internal/bootstrap/starter/testhelper_test.go @@ -0,0 +1,11 @@ +package starter + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/cgroups/cgroups_linux_test.go b/internal/cgroups/cgroups_linux_test.go index 028d4bcd7..9abcf8cb4 100644 --- a/internal/cgroups/cgroups_linux_test.go +++ b/internal/cgroups/cgroups_linux_test.go @@ -10,10 +10,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestNewManager(t *testing.T) { require.IsType(t, &NoopManager{}, NewManager(cgroups.Config{}, testhelper.NewDiscardingLogEntry(t), 1)) } diff --git a/internal/cgroups/testhelper_test.go b/internal/cgroups/testhelper_test.go new file mode 100644 index 000000000..5f594195a --- /dev/null +++ b/internal/cgroups/testhelper_test.go @@ -0,0 +1,13 @@ +//go:build linux + +package cgroups + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/cli/praefect/main_test.go b/internal/cli/praefect/main_test.go index 4137c5277..42e764496 100644 --- a/internal/cli/praefect/main_test.go +++ b/internal/cli/praefect/main_test.go @@ -9,29 +9,12 @@ import ( "github.com/pelletier/go-toml/v2" "github.com/stretchr/testify/require" - "github.com/urfave/cli/v2" "gitlab.com/gitlab-org/gitaly/v16/internal/bootstrap/starter" "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" "gitlab.com/gitlab-org/gitaly/v16/internal/praefect/config" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" ) -func TestMain(m *testing.M) { - defer func(old func(code int)) { cli.OsExiter = old }(cli.OsExiter) - cli.OsExiter = func(code int) {} - - defer func(old cli.Flag) { cli.BashCompletionFlag = old }(cli.BashCompletionFlag) - cli.BashCompletionFlag = stubFlag{} - - defer func(old cli.Flag) { cli.VersionFlag = old }(cli.VersionFlag) - cli.VersionFlag = stubFlag{} - - defer func(old cli.Flag) { cli.HelpFlag = old }(cli.HelpFlag) - cli.HelpFlag = stubFlag{} - - testhelper.Run(m) -} - type stubFlag struct{} func (stubFlag) String() string { return "it is a stub" } diff --git a/internal/cli/praefect/testhelper_test.go b/internal/cli/praefect/testhelper_test.go new file mode 100644 index 000000000..9817ac4d5 --- /dev/null +++ b/internal/cli/praefect/testhelper_test.go @@ -0,0 +1,24 @@ +package praefect + +import ( + "testing" + + "github.com/urfave/cli/v2" + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + defer func(old func(code int)) { cli.OsExiter = old }(cli.OsExiter) + cli.OsExiter = func(code int) {} + + defer func(old cli.Flag) { cli.BashCompletionFlag = old }(cli.BashCompletionFlag) + cli.BashCompletionFlag = stubFlag{} + + defer func(old cli.Flag) { cli.VersionFlag = old }(cli.VersionFlag) + cli.VersionFlag = stubFlag{} + + defer func(old cli.Flag) { cli.HelpFlag = old }(cli.HelpFlag) + cli.HelpFlag = stubFlag{} + + testhelper.Run(m) +} diff --git a/internal/featureflag/context.go b/internal/featureflag/context.go index caa0f0e48..58ca68782 100644 --- a/internal/featureflag/context.go +++ b/internal/featureflag/context.go @@ -1,4 +1,4 @@ -package featureflag +package featureflag //nolint:gitaly-linters // Importing testhelper creates a cyclic dependency. import ( "context" diff --git a/internal/git/conflict/testhelper_test.go b/internal/git/conflict/testhelper_test.go new file mode 100644 index 000000000..2de131e4b --- /dev/null +++ b/internal/git/conflict/testhelper_test.go @@ -0,0 +1,11 @@ +package conflict + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/gitio/testhelper_test.go b/internal/git/gitio/testhelper_test.go new file mode 100644 index 000000000..39575eb47 --- /dev/null +++ b/internal/git/gitio/testhelper_test.go @@ -0,0 +1,11 @@ +package gitio + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/pktline/testhelper_test.go b/internal/git/pktline/testhelper_test.go new file mode 100644 index 000000000..528293c8c --- /dev/null +++ b/internal/git/pktline/testhelper_test.go @@ -0,0 +1,11 @@ +package pktline + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/rawdiff/testhelper_test.go b/internal/git/rawdiff/testhelper_test.go new file mode 100644 index 000000000..f52f567c2 --- /dev/null +++ b/internal/git/rawdiff/testhelper_test.go @@ -0,0 +1,11 @@ +package rawdiff + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/smudge/testhelper_test.go b/internal/git/smudge/testhelper_test.go new file mode 100644 index 000000000..b19dd75b5 --- /dev/null +++ b/internal/git/smudge/testhelper_test.go @@ -0,0 +1,11 @@ +package smudge + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/trace2/testhelper_test.go b/internal/git/trace2/testhelper_test.go new file mode 100644 index 000000000..4500d7404 --- /dev/null +++ b/internal/git/trace2/testhelper_test.go @@ -0,0 +1,11 @@ +package trace2 + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/trace2hooks/packobjects_metrics_ext_test.go b/internal/git/trace2hooks/packobjects_metrics_ext_test.go index bdf29eb36..ee6ac89ad 100644 --- a/internal/git/trace2hooks/packobjects_metrics_ext_test.go +++ b/internal/git/trace2hooks/packobjects_metrics_ext_test.go @@ -19,10 +19,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestPackObjectsMetrics(t *testing.T) { t.Parallel() diff --git a/internal/git/trace2hooks/testhelper_test.go b/internal/git/trace2hooks/testhelper_test.go new file mode 100644 index 000000000..772af3543 --- /dev/null +++ b/internal/git/trace2hooks/testhelper_test.go @@ -0,0 +1,11 @@ +package trace2hooks + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/trailerparser/testhelper_test.go b/internal/git/trailerparser/testhelper_test.go new file mode 100644 index 000000000..b26271c1a --- /dev/null +++ b/internal/git/trailerparser/testhelper_test.go @@ -0,0 +1,11 @@ +package trailerparser + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/updateref/testhelper_test.go b/internal/git/updateref/testhelper_test.go new file mode 100644 index 000000000..bd3c3117a --- /dev/null +++ b/internal/git/updateref/testhelper_test.go @@ -0,0 +1,11 @@ +package updateref + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/git/updateref/updateref_test.go b/internal/git/updateref/updateref_test.go index 869e7f04b..d542c992c 100644 --- a/internal/git/updateref/updateref_test.go +++ b/internal/git/updateref/updateref_test.go @@ -21,10 +21,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func setupUpdater(tb testing.TB, ctx context.Context) (config.Cfg, *localrepo.Repo, string, *Updater) { tb.Helper() diff --git a/internal/gitaly/config/cgroups/testhelper_test.go b/internal/gitaly/config/cgroups/testhelper_test.go new file mode 100644 index 000000000..2eabe6510 --- /dev/null +++ b/internal/gitaly/config/cgroups/testhelper_test.go @@ -0,0 +1,11 @@ +package cgroups + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/gitaly/config/prometheus/testhelper_test.go b/internal/gitaly/config/prometheus/testhelper_test.go new file mode 100644 index 000000000..9653a830e --- /dev/null +++ b/internal/gitaly/config/prometheus/testhelper_test.go @@ -0,0 +1,11 @@ +package prometheus + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/gitaly/diff/testhelper_test.go b/internal/gitaly/diff/testhelper_test.go new file mode 100644 index 000000000..053cf6df0 --- /dev/null +++ b/internal/gitaly/diff/testhelper_test.go @@ -0,0 +1,11 @@ +package diff + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/gitaly/linguist/linguist_test.go b/internal/gitaly/linguist/linguist_test.go index c70b2a790..222173496 100644 --- a/internal/gitaly/linguist/linguist_test.go +++ b/internal/gitaly/linguist/linguist_test.go @@ -20,10 +20,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestInstance_Stats(t *testing.T) { t.Parallel() diff --git a/internal/gitaly/linguist/testhelper_test.go b/internal/gitaly/linguist/testhelper_test.go new file mode 100644 index 000000000..199d589a3 --- /dev/null +++ b/internal/gitaly/linguist/testhelper_test.go @@ -0,0 +1,11 @@ +package linguist + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/gitaly/maintenance/main_test.go b/internal/gitaly/maintenance/testhelper_test.go index c6832ae05..c6832ae05 100644 --- a/internal/gitaly/maintenance/main_test.go +++ b/internal/gitaly/maintenance/testhelper_test.go diff --git a/internal/gitaly/server/auth_test.go b/internal/gitaly/server/auth_test.go index 385a494d2..a30599de9 100644 --- a/internal/gitaly/server/auth_test.go +++ b/internal/gitaly/server/auth_test.go @@ -39,10 +39,6 @@ import ( healthpb "google.golang.org/grpc/health/grpc_health_v1" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestSanity(t *testing.T) { serverSocketPath := runServer(t, testcfg.Build(t)) diff --git a/internal/gitaly/service/server/server_test.go b/internal/gitaly/server/testhelper_test.go index 7846a01f9..7846a01f9 100644 --- a/internal/gitaly/service/server/server_test.go +++ b/internal/gitaly/server/testhelper_test.go diff --git a/internal/gitaly/service/namespace/namespace_test.go b/internal/gitaly/service/namespace/namespace_test.go index a77eecfa7..d49b78fcf 100644 --- a/internal/gitaly/service/namespace/namespace_test.go +++ b/internal/gitaly/service/namespace/namespace_test.go @@ -15,10 +15,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func TestNamespaceExists(t *testing.T) { cfg, client := setupNamespaceService(t, testserver.WithDisablePraefect()) existingStorage := cfg.Storages[0] diff --git a/internal/gitaly/service/namespace/testhelper_test.go b/internal/gitaly/service/namespace/testhelper_test.go index 7d5db0e07..2d8bd9bb4 100644 --- a/internal/gitaly/service/namespace/testhelper_test.go +++ b/internal/gitaly/service/namespace/testhelper_test.go @@ -14,6 +14,10 @@ import ( "google.golang.org/grpc/credentials/insecure" ) +func TestMain(m *testing.M) { + testhelper.Run(m) +} + func setupNamespaceService(tb testing.TB, opts ...testserver.GitalyServerOpt) (config.Cfg, gitalypb.NamespaceServiceClient) { cfgBuilder := testcfg.NewGitalyCfgBuilder(testcfg.WithStorages("default", "other")) cfg := cfgBuilder.Build(tb) diff --git a/internal/gitaly/service/server/testhelper_test.go b/internal/gitaly/service/server/testhelper_test.go new file mode 100644 index 000000000..7846a01f9 --- /dev/null +++ b/internal/gitaly/service/server/testhelper_test.go @@ -0,0 +1,11 @@ +package server + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/gitlab/client/testhelper_test.go b/internal/gitlab/client/testhelper_test.go new file mode 100644 index 000000000..137104d4d --- /dev/null +++ b/internal/gitlab/client/testhelper_test.go @@ -0,0 +1,11 @@ +package client + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/backchannel/testhelper_test.go b/internal/grpc/backchannel/testhelper_test.go new file mode 100644 index 000000000..c2ba27cbb --- /dev/null +++ b/internal/grpc/backchannel/testhelper_test.go @@ -0,0 +1,11 @@ +package backchannel + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/client/testhelper_test.go b/internal/grpc/client/testhelper_test.go new file mode 100644 index 000000000..137104d4d --- /dev/null +++ b/internal/grpc/client/testhelper_test.go @@ -0,0 +1,11 @@ +package client + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/grpcstats/testhelper_test.go b/internal/grpc/grpcstats/testhelper_test.go index f57175b92..d0b298d5f 100644 --- a/internal/grpc/grpcstats/testhelper_test.go +++ b/internal/grpc/grpcstats/testhelper_test.go @@ -1,4 +1,4 @@ -package grpcstats_test +package grpcstats import ( "testing" diff --git a/internal/grpc/listenmux/testhelper_test.go b/internal/grpc/listenmux/testhelper_test.go new file mode 100644 index 000000000..3cd68209c --- /dev/null +++ b/internal/grpc/listenmux/testhelper_test.go @@ -0,0 +1,11 @@ +package listenmux + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/metadata/testhelper_test.go b/internal/grpc/metadata/testhelper_test.go new file mode 100644 index 000000000..bfb393903 --- /dev/null +++ b/internal/grpc/metadata/testhelper_test.go @@ -0,0 +1,11 @@ +package metadata + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/cache/cache_test.go b/internal/grpc/middleware/cache/cache_test.go index e9b924d09..02b2fa603 100644 --- a/internal/grpc/middleware/cache/cache_test.go +++ b/internal/grpc/middleware/cache/cache_test.go @@ -137,6 +137,7 @@ func TestInvalidators(t *testing.T) { grpc.WithTransportCredentials(insecure.NewCredentials()), ) require.NoError(t, err) + defer testhelper.MustClose(t, conn) tc.invokeRPC(t, conn) diff --git a/internal/grpc/middleware/cache/testhelper_test.go b/internal/grpc/middleware/cache/testhelper_test.go new file mode 100644 index 000000000..6c55da37a --- /dev/null +++ b/internal/grpc/middleware/cache/testhelper_test.go @@ -0,0 +1,11 @@ +package cache + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/customfieldshandler/customfields_handler_test.go b/internal/grpc/middleware/customfieldshandler/customfields_handler_test.go index 67568378b..60adabe47 100644 --- a/internal/grpc/middleware/customfieldshandler/customfields_handler_test.go +++ b/internal/grpc/middleware/customfieldshandler/customfields_handler_test.go @@ -25,10 +25,6 @@ import ( "google.golang.org/grpc/test/bufconn" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - func createNewServer(t *testing.T, cfg config.Cfg, logger *logrus.Logger) *grpc.Server { t.Helper() diff --git a/internal/grpc/middleware/customfieldshandler/testhelper_test.go b/internal/grpc/middleware/customfieldshandler/testhelper_test.go new file mode 100644 index 000000000..16e038e4e --- /dev/null +++ b/internal/grpc/middleware/customfieldshandler/testhelper_test.go @@ -0,0 +1,11 @@ +package customfieldshandler + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/featureflag/testhelper_test.go b/internal/grpc/middleware/featureflag/testhelper_test.go new file mode 100644 index 000000000..3b7d11638 --- /dev/null +++ b/internal/grpc/middleware/featureflag/testhelper_test.go @@ -0,0 +1,11 @@ +package featureflag + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/metadatahandler/testhelper_test.go b/internal/grpc/middleware/metadatahandler/testhelper_test.go new file mode 100644 index 000000000..ee8340678 --- /dev/null +++ b/internal/grpc/middleware/metadatahandler/testhelper_test.go @@ -0,0 +1,11 @@ +package metadatahandler + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/sentryhandler/testhelper_test.go b/internal/grpc/middleware/sentryhandler/testhelper_test.go new file mode 100644 index 000000000..d9287d14e --- /dev/null +++ b/internal/grpc/middleware/sentryhandler/testhelper_test.go @@ -0,0 +1,11 @@ +package sentryhandler + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/middleware/statushandler/testhelper_test.go b/internal/grpc/middleware/statushandler/testhelper_test.go new file mode 100644 index 000000000..3437d6df2 --- /dev/null +++ b/internal/grpc/middleware/statushandler/testhelper_test.go @@ -0,0 +1,11 @@ +package statushandler + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/protoregistry/testhelper_test.go b/internal/grpc/protoregistry/testhelper_test.go new file mode 100644 index 000000000..07f146a30 --- /dev/null +++ b/internal/grpc/protoregistry/testhelper_test.go @@ -0,0 +1,11 @@ +package protoregistry + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/grpc/proxy/proxy_test_testhelper_test.go b/internal/grpc/proxy/proxy_test_testhelper_test.go new file mode 100644 index 000000000..2c3493743 --- /dev/null +++ b/internal/grpc/proxy/proxy_test_testhelper_test.go @@ -0,0 +1,121 @@ +package proxy_test + +import ( + "context" + "net" + "testing" + + grpcmwtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/sentryhandler" + "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/proxy" + "gitlab.com/gitlab-org/gitaly/v16/internal/helper/fieldextractors" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/interop/grpc_testing" +) + +func newListener(tb testing.TB) net.Listener { + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(tb, err, "must be able to allocate a port for listener") + + return listener +} + +func newBackendPinger(tb testing.TB, ctx context.Context) (*grpc.ClientConn, *interceptPinger) { + ip := &interceptPinger{} + + srvr := grpc.NewServer() + listener := newListener(tb) + + grpc_testing.RegisterTestServiceServer(srvr, ip) + + done := make(chan struct{}) + go func() { + defer close(done) + require.NoError(tb, srvr.Serve(listener)) + }() + + cc, err := grpc.DialContext( + ctx, + listener.Addr().String(), + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithBlock(), + grpc.WithDefaultCallOptions( + grpc.ForceCodec(proxy.NewCodec()), + ), + ) + require.NoError(tb, err) + + tb.Cleanup(func() { + srvr.GracefulStop() + require.NoError(tb, cc.Close()) + <-done + }) + + return cc, ip +} + +func newProxy(tb testing.TB, ctx context.Context, director proxy.StreamDirector, svc, method string) *grpc.ClientConn { + proxySrvr := grpc.NewServer( + grpc.ForceServerCodec(proxy.NewCodec()), + grpc.ChainStreamInterceptor( + // context tags usage is required by sentryhandler.StreamLogHandler + grpcmwtags.StreamServerInterceptor(grpcmwtags.WithFieldExtractorForInitialReq(fieldextractors.FieldExtractor)), + // sentry middleware to capture errors + sentryhandler.StreamLogHandler, + ), + grpc.UnknownServiceHandler(proxy.TransparentHandler(director)), + ) + proxy.RegisterService(proxySrvr, director, svc, method) + + done := make(chan struct{}) + listener := newListener(tb) + go func() { + defer close(done) + require.NoError(tb, proxySrvr.Serve(listener)) + }() + + proxyCC, err := grpc.DialContext( + ctx, + listener.Addr().String(), + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithBlock(), + ) + require.NoError(tb, err) + + tb.Cleanup(func() { + proxySrvr.GracefulStop() + require.NoError(tb, proxyCC.Close()) + <-done + }) + + return proxyCC +} + +// interceptPinger allows an RPC to be intercepted with a custom +// function defined in each unit test +type interceptPinger struct { + grpc_testing.UnimplementedTestServiceServer + + fullDuplexCall func(grpc_testing.TestService_FullDuplexCallServer) error + emptyCall func(context.Context, *grpc_testing.Empty) (*grpc_testing.Empty, error) + unaryCall func(context.Context, *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) + streamingOutputCall func(*grpc_testing.StreamingOutputCallRequest, grpc_testing.TestService_StreamingOutputCallServer) error +} + +func (ip *interceptPinger) FullDuplexCall(stream grpc_testing.TestService_FullDuplexCallServer) error { + return ip.fullDuplexCall(stream) +} + +func (ip *interceptPinger) EmptyCall(ctx context.Context, req *grpc_testing.Empty) (*grpc_testing.Empty, error) { + return ip.emptyCall(ctx, req) +} + +func (ip *interceptPinger) UnaryCall(ctx context.Context, req *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) { + return ip.unaryCall(ctx, req) +} + +func (ip *interceptPinger) StreamingOutputCall(req *grpc_testing.StreamingOutputCallRequest, stream grpc_testing.TestService_StreamingOutputCallServer) error { + return ip.streamingOutputCall(req, stream) +} diff --git a/internal/grpc/proxy/testhelper_test.go b/internal/grpc/proxy/testhelper_test.go index 5efcfa265..458219da2 100644 --- a/internal/grpc/proxy/testhelper_test.go +++ b/internal/grpc/proxy/testhelper_test.go @@ -1,126 +1,11 @@ -package proxy_test +package proxy import ( - "context" - "net" "testing" - grpcmwtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" - "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/middleware/sentryhandler" - "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/proxy" - "gitlab.com/gitlab-org/gitaly/v16/internal/helper/fieldextractors" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/interop/grpc_testing" ) func TestMain(m *testing.M) { testhelper.Run(m) } - -func newListener(tb testing.TB) net.Listener { - listener, err := net.Listen("tcp", "127.0.0.1:0") - require.NoError(tb, err, "must be able to allocate a port for listener") - - return listener -} - -func newBackendPinger(tb testing.TB, ctx context.Context) (*grpc.ClientConn, *interceptPinger) { - ip := &interceptPinger{} - - srvr := grpc.NewServer() - listener := newListener(tb) - - grpc_testing.RegisterTestServiceServer(srvr, ip) - - done := make(chan struct{}) - go func() { - defer close(done) - require.NoError(tb, srvr.Serve(listener)) - }() - - cc, err := grpc.DialContext( - ctx, - listener.Addr().String(), - grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithBlock(), - grpc.WithDefaultCallOptions( - grpc.ForceCodec(proxy.NewCodec()), - ), - ) - require.NoError(tb, err) - - tb.Cleanup(func() { - srvr.GracefulStop() - require.NoError(tb, cc.Close()) - <-done - }) - - return cc, ip -} - -func newProxy(tb testing.TB, ctx context.Context, director proxy.StreamDirector, svc, method string) *grpc.ClientConn { - proxySrvr := grpc.NewServer( - grpc.ForceServerCodec(proxy.NewCodec()), - grpc.ChainStreamInterceptor( - // context tags usage is required by sentryhandler.StreamLogHandler - grpcmwtags.StreamServerInterceptor(grpcmwtags.WithFieldExtractorForInitialReq(fieldextractors.FieldExtractor)), - // sentry middleware to capture errors - sentryhandler.StreamLogHandler, - ), - grpc.UnknownServiceHandler(proxy.TransparentHandler(director)), - ) - proxy.RegisterService(proxySrvr, director, svc, method) - - done := make(chan struct{}) - listener := newListener(tb) - go func() { - defer close(done) - require.NoError(tb, proxySrvr.Serve(listener)) - }() - - proxyCC, err := grpc.DialContext( - ctx, - listener.Addr().String(), - grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithBlock(), - ) - require.NoError(tb, err) - - tb.Cleanup(func() { - proxySrvr.GracefulStop() - require.NoError(tb, proxyCC.Close()) - <-done - }) - - return proxyCC -} - -// interceptPinger allows an RPC to be intercepted with a custom -// function defined in each unit test -type interceptPinger struct { - grpc_testing.UnimplementedTestServiceServer - - fullDuplexCall func(grpc_testing.TestService_FullDuplexCallServer) error - emptyCall func(context.Context, *grpc_testing.Empty) (*grpc_testing.Empty, error) - unaryCall func(context.Context, *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) - streamingOutputCall func(*grpc_testing.StreamingOutputCallRequest, grpc_testing.TestService_StreamingOutputCallServer) error -} - -func (ip *interceptPinger) FullDuplexCall(stream grpc_testing.TestService_FullDuplexCallServer) error { - return ip.fullDuplexCall(stream) -} - -func (ip *interceptPinger) EmptyCall(ctx context.Context, req *grpc_testing.Empty) (*grpc_testing.Empty, error) { - return ip.emptyCall(ctx, req) -} - -func (ip *interceptPinger) UnaryCall(ctx context.Context, req *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) { - return ip.unaryCall(ctx, req) -} - -func (ip *interceptPinger) StreamingOutputCall(req *grpc_testing.StreamingOutputCallRequest, stream grpc_testing.TestService_StreamingOutputCallServer) error { - return ip.streamingOutputCall(req, stream) -} diff --git a/internal/grpc/sidechannel/testhelper_test.go b/internal/grpc/sidechannel/testhelper_test.go new file mode 100644 index 000000000..746f17eba --- /dev/null +++ b/internal/grpc/sidechannel/testhelper_test.go @@ -0,0 +1,11 @@ +package sidechannel + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/chunk/chunker_test.go b/internal/helper/chunk/chunker_test.go index 68a444bab..1eb1a1e36 100644 --- a/internal/helper/chunk/chunker_test.go +++ b/internal/helper/chunk/chunker_test.go @@ -14,10 +14,6 @@ import ( "google.golang.org/protobuf/proto" ) -func TestMain(m *testing.M) { - testhelper.Run(m) -} - type testSender struct { stream grpc_testing.TestService_StreamingOutputCallServer body []byte diff --git a/internal/helper/chunk/testhelper_test.go b/internal/helper/chunk/testhelper_test.go new file mode 100644 index 000000000..e81d92db6 --- /dev/null +++ b/internal/helper/chunk/testhelper_test.go @@ -0,0 +1,11 @@ +package chunk + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/duration/testhelper_test.go b/internal/helper/duration/testhelper_test.go new file mode 100644 index 000000000..a0d9e8f7f --- /dev/null +++ b/internal/helper/duration/testhelper_test.go @@ -0,0 +1,11 @@ +package duration + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/env/testhelper_test.go b/internal/helper/env/testhelper_test.go new file mode 100644 index 000000000..5fb46d8dc --- /dev/null +++ b/internal/helper/env/testhelper_test.go @@ -0,0 +1,11 @@ +package env_test + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/fstype/testhelper_test.go b/internal/helper/fstype/testhelper_test.go new file mode 100644 index 000000000..10ba5c1a8 --- /dev/null +++ b/internal/helper/fstype/testhelper_test.go @@ -0,0 +1,11 @@ +package fstype + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/lines/testhelper_test.go b/internal/helper/lines/testhelper_test.go new file mode 100644 index 000000000..c1230a761 --- /dev/null +++ b/internal/helper/lines/testhelper_test.go @@ -0,0 +1,11 @@ +package lines + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/perm/perm_test.go b/internal/helper/perm/perm_test.go index 39ee78625..283d768ff 100644 --- a/internal/helper/perm/perm_test.go +++ b/internal/helper/perm/perm_test.go @@ -1,4 +1,4 @@ -package perm +package perm_test import ( "io/fs" @@ -6,13 +6,14 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" ) func TestUmaskMask(t *testing.T) { for _, tc := range []struct { desc string mode fs.FileMode - umask Umask + umask perm.Umask expectedMode fs.FileMode }{ { diff --git a/internal/helper/perm/testhelper_test.go b/internal/helper/perm/testhelper_test.go new file mode 100644 index 000000000..011814ee8 --- /dev/null +++ b/internal/helper/perm/testhelper_test.go @@ -0,0 +1,11 @@ +package perm_test + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/testhelper_test.go b/internal/helper/testhelper_test.go new file mode 100644 index 000000000..c57af0dad --- /dev/null +++ b/internal/helper/testhelper_test.go @@ -0,0 +1,11 @@ +package helper + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/helper/text/chomp_test.go b/internal/helper/text/chomp_test.go index fbdb1ecbc..5d707bbeb 100644 --- a/internal/helper/text/chomp_test.go +++ b/internal/helper/text/chomp_test.go @@ -1,9 +1,10 @@ -package text +package text_test import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v16/internal/helper/text" ) func TestChompBytes(t *testing.T) { @@ -23,7 +24,7 @@ func TestChompBytes(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - require.Equal(t, tc.out, ChompBytes(tc.in)) + require.Equal(t, tc.out, text.ChompBytes(tc.in)) }) } } diff --git a/internal/helper/text/testhelper_test.go b/internal/helper/text/testhelper_test.go new file mode 100644 index 000000000..6483830f2 --- /dev/null +++ b/internal/helper/text/testhelper_test.go @@ -0,0 +1,11 @@ +package text_test + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/limiter/testhelper_test.go b/internal/limiter/testhelper_test.go new file mode 100644 index 000000000..419ba2158 --- /dev/null +++ b/internal/limiter/testhelper_test.go @@ -0,0 +1,11 @@ +package limiter + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/limiter/watchers/testhelper_test.go b/internal/limiter/watchers/testhelper_test.go index 39cb89546..fa9539a20 100644 --- a/internal/limiter/watchers/testhelper_test.go +++ b/internal/limiter/watchers/testhelper_test.go @@ -2,9 +2,11 @@ package watchers import ( "os/exec" + "testing" "github.com/prometheus/client_golang/prometheus" "gitlab.com/gitlab-org/gitaly/v16/internal/cgroups" + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" ) type testCgroupManager struct { @@ -14,6 +16,10 @@ type testCgroupManager struct { statsIndex int } +func TestMain(m *testing.M) { + testhelper.Run(m) +} + func (m *testCgroupManager) Ready() bool { return m.ready } func (m *testCgroupManager) Stats() (cgroups.Stats, error) { m.statsIndex++ diff --git a/internal/log/customfields.go b/internal/log/customfields.go index 38874e15c..64bb59566 100644 --- a/internal/log/customfields.go +++ b/internal/log/customfields.go @@ -1,4 +1,4 @@ -package log +package log //nolint:gitaly-linters // Importing testhelper creates a cyclic dependency. import ( "context" diff --git a/internal/praefect/metrics/testhelper_test.go b/internal/praefect/metrics/testhelper_test.go new file mode 100644 index 000000000..843d467d4 --- /dev/null +++ b/internal/praefect/metrics/testhelper_test.go @@ -0,0 +1,11 @@ +package metrics + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/praefect/middleware/helper_test.go b/internal/praefect/middleware/testhelper_test.go index 06fab764a..06fab764a 100644 --- a/internal/praefect/middleware/helper_test.go +++ b/internal/praefect/middleware/testhelper_test.go diff --git a/internal/praefect/nodes/init_test.go b/internal/praefect/nodes/testhelper_test.go index 74decdf85..74decdf85 100644 --- a/internal/praefect/nodes/init_test.go +++ b/internal/praefect/nodes/testhelper_test.go diff --git a/internal/praefect/nodes/tracker/testhelper_test.go b/internal/praefect/nodes/tracker/testhelper_test.go new file mode 100644 index 000000000..aabde6553 --- /dev/null +++ b/internal/praefect/nodes/tracker/testhelper_test.go @@ -0,0 +1,11 @@ +package tracker + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/praefect/reconciler/testhelper_test.go b/internal/praefect/reconciler/testhelper_test.go new file mode 100644 index 000000000..d489e2e02 --- /dev/null +++ b/internal/praefect/reconciler/testhelper_test.go @@ -0,0 +1,11 @@ +package reconciler + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/praefect/repocleaner/init_test.go b/internal/praefect/repocleaner/testhelper_test.go index ce0132551..ce0132551 100644 --- a/internal/praefect/repocleaner/init_test.go +++ b/internal/praefect/repocleaner/testhelper_test.go diff --git a/internal/praefect/service/helper_test.go b/internal/praefect/service/testhelper_test.go index 084c50d6c..084c50d6c 100644 --- a/internal/praefect/service/helper_test.go +++ b/internal/praefect/service/testhelper_test.go diff --git a/internal/praefect/transactions/testhelper_test.go b/internal/praefect/transactions/testhelper_test.go new file mode 100644 index 000000000..cdc7934b9 --- /dev/null +++ b/internal/praefect/transactions/testhelper_test.go @@ -0,0 +1,11 @@ +package transactions + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/ps/testhelper_test.go b/internal/ps/testhelper_test.go new file mode 100644 index 000000000..86cb86e6d --- /dev/null +++ b/internal/ps/testhelper_test.go @@ -0,0 +1,11 @@ +package ps + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/safe/main_test.go b/internal/safe/main_test.go deleted file mode 100644 index 64ba6b23e..000000000 --- a/internal/safe/main_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package safe_test - -import ( - "testing" - - "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" -) - -func TestMain(m *testing.M) { - testhelper.Run(m) -} diff --git a/internal/safe/testhelper_test.go b/internal/safe/testhelper_test.go new file mode 100644 index 000000000..56f4b4002 --- /dev/null +++ b/internal/safe/testhelper_test.go @@ -0,0 +1,11 @@ +package safe + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/signature/testhelper_test.go b/internal/signature/testhelper_test.go new file mode 100644 index 000000000..52b02e6da --- /dev/null +++ b/internal/signature/testhelper_test.go @@ -0,0 +1,11 @@ +package signature + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/structerr/error.go b/internal/structerr/error.go index 048258b6b..4b88f2c16 100644 --- a/internal/structerr/error.go +++ b/internal/structerr/error.go @@ -1,4 +1,4 @@ -package structerr +package structerr //nolint:gitaly-linters // Importing testhelper creates a cyclic dependency. import ( "errors" diff --git a/internal/testhelper/testdb/testhelper_test.go b/internal/testhelper/testdb/testhelper_test.go new file mode 100644 index 000000000..124932072 --- /dev/null +++ b/internal/testhelper/testdb/testhelper_test.go @@ -0,0 +1,11 @@ +package testdb + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/testhelper/testhelper_test.go b/internal/testhelper/testhelper_test.go new file mode 100644 index 000000000..a91779dd4 --- /dev/null +++ b/internal/testhelper/testhelper_test.go @@ -0,0 +1,9 @@ +package testhelper + +import ( + "testing" +) + +func TestMain(m *testing.M) { + Run(m) +} diff --git a/internal/testhelper/testserver/testhelper_test.go b/internal/testhelper/testserver/testhelper_test.go new file mode 100644 index 000000000..d09ca3b54 --- /dev/null +++ b/internal/testhelper/testserver/testhelper_test.go @@ -0,0 +1,11 @@ +package testserver + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/internal/tracing/passthrough_test.go b/internal/tracing/passthrough_test.go index 5d52fb770..31bd3a50f 100644 --- a/internal/tracing/passthrough_test.go +++ b/internal/tracing/passthrough_test.go @@ -279,6 +279,7 @@ func startFakeGitalyServer(t *testing.T, svc *testSvc, spanContext opentracing.S grpc.WithStreamInterceptor(StreamPassthroughInterceptor(spanContext)), ) require.NoError(t, err) + t.Cleanup(func() { testhelper.MustClose(t, conn) }) return grpc_testing.NewTestServiceClient(conn) } diff --git a/internal/tracing/testhelper_test.go b/internal/tracing/testhelper_test.go new file mode 100644 index 000000000..f940df2d1 --- /dev/null +++ b/internal/tracing/testhelper_test.go @@ -0,0 +1,11 @@ +package tracing + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/streamio/testhelper_test.go b/streamio/testhelper_test.go new file mode 100644 index 000000000..2edd01de6 --- /dev/null +++ b/streamio/testhelper_test.go @@ -0,0 +1,11 @@ +package streamio + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/testhelper_test.go b/testhelper_test.go new file mode 100644 index 000000000..0aa8282d5 --- /dev/null +++ b/testhelper_test.go @@ -0,0 +1,11 @@ +package gitaly + +import ( + "testing" + + "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" +) + +func TestMain(m *testing.M) { + testhelper.Run(m) +} diff --git a/tools/golangci-lint/gitaly/lint.go b/tools/golangci-lint/gitaly/lint.go index 0d49db4ed..93e4da5ac 100644 --- a/tools/golangci-lint/gitaly/lint.go +++ b/tools/golangci-lint/gitaly/lint.go @@ -26,6 +26,10 @@ func New(conf any) ([]*analysis.Analyzer, error) { unavailableCodeAnalyzerName, "included-functions", )}), + newTesthelperRunAnalyzer(&testhelperRunAnalyzerSettings{IncludedFunctions: configStringSlicesAt( + testhelperRunAnalyzerName, + "included-functions", + )}), newTestParamsOrder(), }, nil } diff --git a/tools/golangci-lint/gitaly/matcher.go b/tools/golangci-lint/gitaly/matcher.go index 9b180a726..ec7620792 100644 --- a/tools/golangci-lint/gitaly/matcher.go +++ b/tools/golangci-lint/gitaly/matcher.go @@ -33,6 +33,8 @@ var funcNamePattern = regexp.MustCompile(`^\(?([^\\)].*)\)?\.(.*)$`) // "gitlab.com/gitlab-org/gitaly/v15/internal/structerr.NewInternal", // - A function of a struct inside a package: // "(*gitlab.com/gitlab-org/gitaly/v15/internal/structerr.Error).Unwrap", +// - A local function call: +// "New(1)", // // This Matcher doesn't support interface match (yet). func (m *Matcher) MatchFunction(call *ast.CallExpr, rules []string) bool { @@ -76,11 +78,18 @@ func (m *Matcher) functionName(call *ast.CallExpr) string { } func (m *Matcher) getFunction(call *ast.CallExpr) (*types.Func, bool) { - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { + var ident *ast.Ident + + switch ty := call.Fun.(type) { + case *ast.SelectorExpr: + ident = ty.Sel + case *ast.Ident: + ident = ty + default: return nil, false } - fn, ok := m.typesInfo.ObjectOf(sel.Sel).(*types.Func) + + fn, ok := m.typesInfo.ObjectOf(ident).(*types.Func) if !ok { return nil, false } diff --git a/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_exec_testmain/testhelper_test.go b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_exec_testmain/testhelper_test.go new file mode 100644 index 000000000..94eb334f0 --- /dev/null +++ b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_exec_testmain/testhelper_test.go @@ -0,0 +1,8 @@ +package testhelper_run_no_exec_testmain + +import ( + "testing" +) + +func TestMain(m *testing.M) { // want "testhelper.Run not called in TestMain" +} diff --git a/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_testmain/no_testmain.go b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_testmain/no_testmain.go new file mode 100644 index 000000000..364b9f101 --- /dev/null +++ b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_testmain/no_testmain.go @@ -0,0 +1,6 @@ +package testhelper_run_no_testmain // want "no TestMain in package testhelper_run_no_testmain" + +import "testing" + +func TestMe(t *testing.T) { +} diff --git a/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_tests/no_tests.go b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_tests/no_tests.go new file mode 100644 index 000000000..a5ad77c85 --- /dev/null +++ b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_no_tests/no_tests.go @@ -0,0 +1,4 @@ +package testhelper_run_no_tests + +func hello() { // OK, no tests in package. +} diff --git a/tools/golangci-lint/gitaly/testdata/src/testhelper_run_not_testhelper/not_testhelper.go b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_not_testhelper/not_testhelper.go new file mode 100644 index 000000000..28afec330 --- /dev/null +++ b/tools/golangci-lint/gitaly/testdata/src/testhelper_run_not_testhelper/not_testhelper.go @@ -0,0 +1,9 @@ +package testhelper_run_not_testhelper + +import "testing" + +func Run(m *testing.M) {} + +func TestMain(m *testing.M) { // want "TestMain should be placed in file 'testhelper_test.go'" + Run(m) +} diff --git a/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go b/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go index 5493e3f7a..b107aa4ae 100644 --- a/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go +++ b/tools/golangci-lint/gitaly/testdata/src/unavailable_code/unavailable_code_test.go @@ -17,5 +17,5 @@ func errorWrapOkay() { } func errorWrapNotOkay() { - _ = NewUnavailable("hello world") // please avoid using the Unavailable status code: https://gitlab.com/gitlab-org/gitaly/-/blob/master/STYLE.md?plain=0#unavailable-code + _ = NewUnavailable("hello world") // want "please avoid using the Unavailable status code.*" } diff --git a/tools/golangci-lint/gitaly/testhelper_run.go b/tools/golangci-lint/gitaly/testhelper_run.go new file mode 100644 index 000000000..aa9226c02 --- /dev/null +++ b/tools/golangci-lint/gitaly/testhelper_run.go @@ -0,0 +1,136 @@ +package main + +import ( + "fmt" + "go/ast" + "go/types" + "path/filepath" + "regexp" + "strings" + + "golang.org/x/tools/go/analysis" +) + +const testhelperRunAnalyzerName = "testhelper_run" + +type testhelperRunAnalyzerSettings struct { + IncludedFunctions []string `mapstructure:"included-functions"` +} + +var toolPrefixPattern = regexp.MustCompile(`^gitlab.com/gitlab-org/gitaly(/v\d{2})?/tools`) + +// newTesthelperRunAnalyzer returns an analyzer to detect if a package that has tests does +// not contain a `TestMain()` function that executes `testhelper.Run()`. +// For more information: +// https://gitlab.com/gitlab-org/gitaly/-/blob/master/STYLE.md?ref_type=heads#common-setup +func newTesthelperRunAnalyzer(settings *testhelperRunAnalyzerSettings) *analysis.Analyzer { + return &analysis.Analyzer{ + Name: testhelperRunAnalyzerName, + Doc: `TestMain must be present and call testhelper.Run()`, + Run: runTesthelperRunAnalyzer(settings.IncludedFunctions), + } +} + +func runTesthelperRunAnalyzer(rules []string) func(*analysis.Pass) (interface{}, error) { + return func(pass *analysis.Pass) (interface{}, error) { + var hasTestMain, hasTests bool + + // Blackbox test packages ending with `_test` are considered to be + // part of the primary package for compilation, but are scanned in a + // separate pass by the analyzer. The primary and test packages cannot + // both define `TestMain`. Only require `TestMain` in the primary package. + if strings.HasSuffix(pass.Pkg.Name(), "_test") { + return nil, nil + } + + // Don't lint tools, they can't import `testhelper`. + if toolPrefixPattern.MatchString(pass.Pkg.Path()) { + return nil, nil + } + + for _, file := range pass.Files { + if hasTestMain { + break + } + + ast.Inspect(file, func(node ast.Node) bool { + if decl, ok := node.(*ast.FuncDecl); ok { + declName := decl.Name.Name + + if declName == "TestMain" { + hasTestMain = true + + analyzeTestMain(pass, decl, rules) + analyzeFilename(pass, file, decl) + } + + // Actual tests must start with `Test`, helpers could take a `testing.TB`. + if strings.HasPrefix(declName, "Test") { + params := decl.Type.Params + for _, field := range params.List { + fieldType := pass.TypesInfo.TypeOf(field.Type) + + // Do we have any tests in this package? + if types.Implements(fieldType, testingTB) { + hasTests = true + } + } + } + } + return true + }) + } + + // If we have tests but there's no `TestMain`, report. + if hasTests && !hasTestMain { + // We don't have a specific location for this failure, so use the location of the package name + // in its first file and provide the name in the error text. This list is sorted lexically by + // filename, so the location of `nolint` directives may not be stable when new files are added. + pass.Report(analysis.Diagnostic{ + Pos: pass.Files[0].Name.Pos(), + End: pass.Files[0].Name.End(), + Message: fmt.Sprintf("no TestMain in package %v", pass.Pkg.Path()), + SuggestedFixes: nil, + }) + } + + return nil, nil + } +} + +func analyzeFilename(pass *analysis.Pass, file *ast.File, decl *ast.FuncDecl) { + fullpath := pass.Fset.File(file.Pos()).Name() + filename := filepath.Base(fullpath) + + if filename != "testhelper_test.go" { + pass.Report(analysis.Diagnostic{ + Pos: decl.Pos(), + End: decl.End(), + Message: "TestMain should be placed in file 'testhelper_test.go'", + SuggestedFixes: nil, + }) + } +} + +func analyzeTestMain(pass *analysis.Pass, decl *ast.FuncDecl, rules []string) { + matcher := NewMatcher(pass) + var hasRun bool + + ast.Inspect(decl, func(node ast.Node) bool { + if call, ok := node.(*ast.CallExpr); ok { + if matcher.MatchFunction(call, rules) { + hasRun = true + } + } + return true + }) + + if !hasRun { + pass.Report(analysis.Diagnostic{ + Pos: decl.Pos(), + End: decl.End(), + Message: "testhelper.Run not called in TestMain", + SuggestedFixes: nil, + }) + } +} diff --git a/tools/golangci-lint/gitaly/testhelper_run_test.go b/tools/golangci-lint/gitaly/testhelper_run_test.go new file mode 100644 index 000000000..e557d5287 --- /dev/null +++ b/tools/golangci-lint/gitaly/testhelper_run_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "golang.org/x/tools/go/analysis/analysistest" +) + +func TestTesthelperRun(t *testing.T) { + wd, err := os.Getwd() + if err != nil { + t.Fatalf("Failed to get wd: %s", err) + } + + testdata := filepath.Join(wd, "testdata") + analyzer := newTesthelperRunAnalyzer(&testhelperRunAnalyzerSettings{IncludedFunctions: []string{ + "testhelper_run_not_testhelper.Run", + }}) + analysistest.Run( + t, + testdata, + analyzer, + "testhelper_run_no_tests", + "testhelper_run_no_testmain", + "testhelper_run_no_exec_testmain", + "testhelper_run_not_testhelper", + ) +} |