diff options
author | Toon Claes <toon@gitlab.com> | 2022-11-09 11:36:52 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2022-11-09 13:01:48 +0300 |
commit | b0931c3aaa6d4b865b004f6e433e94ac5f5075f1 (patch) | |
tree | c78ebddffb74b36f0fbe5b1aa3fe5a22ceed68cd | |
parent | 68d099cbd626038445c004b0fc698195e59d55ee (diff) |
gittest: Introduce a CountingCommandFactory
This command factory is a wrapper around the regular git command
factory, but it increments count on each call of NewXYZ().
-rw-r--r-- | internal/git/gittest/counting_command_factory.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/internal/git/gittest/counting_command_factory.go b/internal/git/gittest/counting_command_factory.go new file mode 100644 index 000000000..5e31cbafe --- /dev/null +++ b/internal/git/gittest/counting_command_factory.go @@ -0,0 +1,79 @@ +package gittest + +import ( + "context" + "sync/atomic" + "testing" + + "gitlab.com/gitlab-org/gitaly/v15/internal/command" + "gitlab.com/gitlab-org/gitaly/v15/internal/git" + "gitlab.com/gitlab-org/gitaly/v15/internal/git/repository" + "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" +) + +var _ git.CommandFactory = &CountingCommandFactory{} + +// CountingCommandFactory is a wrapper around the regular git command factory, +// but it increments count on each call of NewXYZ(). +type CountingCommandFactory struct { + realCommandFactory git.CommandFactory + count uint64 +} + +// NewCountingCommandFactory creates a CountingCommandFactory +func NewCountingCommandFactory(tb testing.TB, cfg config.Cfg, opts ...git.ExecCommandFactoryOption) *CountingCommandFactory { + return &CountingCommandFactory{ + realCommandFactory: NewCommandFactory(tb, cfg, opts...), + } +} + +// Count returns the current count +func (f *CountingCommandFactory) Count() uint64 { + return atomic.LoadUint64(&f.count) +} + +// ResetCount resets the count to zero +func (f *CountingCommandFactory) ResetCount() { + atomic.StoreUint64(&f.count, 0) +} + +// New creates a new git command and increments the command counter +func (f *CountingCommandFactory) New(ctx context.Context, repo repository.GitRepo, sc git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { + atomic.AddUint64(&f.count, 1) + + return f.realCommandFactory.New(ctx, repo, sc, opts...) +} + +// NewWithoutRepo creates a new git command and increments the command counter +func (f *CountingCommandFactory) NewWithoutRepo(ctx context.Context, sc git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { + atomic.AddUint64(&f.count, 1) + + return f.realCommandFactory.NewWithoutRepo(ctx, sc, opts...) +} + +// NewWithDir creates a new git command and increments the command counter +func (f *CountingCommandFactory) NewWithDir(ctx context.Context, dir string, sc git.Cmd, opts ...git.CmdOpt) (*command.Command, error) { + atomic.AddUint64(&f.count, 1) + + return f.realCommandFactory.NewWithDir(ctx, dir, sc, opts...) +} + +// GetExecutionEnvironment passes on call to realCommandFactory +func (f *CountingCommandFactory) GetExecutionEnvironment(ctx context.Context) git.ExecutionEnvironment { + return f.realCommandFactory.GetExecutionEnvironment(ctx) +} + +// HooksPath passes on call to realCommandFactory +func (f *CountingCommandFactory) HooksPath(ctx context.Context) string { + return f.realCommandFactory.HooksPath(ctx) +} + +// GitVersion passes on call to realCommandFactory +func (f *CountingCommandFactory) GitVersion(ctx context.Context) (git.Version, error) { + return f.realCommandFactory.GitVersion(ctx) +} + +// SidecarGitConfiguration passes on call to realCommandFactory +func (f *CountingCommandFactory) SidecarGitConfiguration(ctx context.Context) ([]git.ConfigPair, error) { + return f.realCommandFactory.SidecarGitConfiguration(ctx) +} |