diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-04-26 09:26:05 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-04-26 10:46:32 +0300 |
commit | 8339301f386ec041fac36d9a1098d89141672225 (patch) | |
tree | 0ec694ae3b4c99935eb28564b054a9be51ff967f | |
parent | 313f278f1a0a7ee22ef6fd73d0fab54d39957894 (diff) |
gittest: Refactor intercepting command factory options to be extensible
It's currently only possible to pass options intended for the real Git
command factory when constructing an intercepting factory. We're about
to add another option though that changes behaviour of the intercepting
factory itself.
Introduce a new `InterceptingCommandFactoryOption` type to make this
more flexible and allow for the new usecase.
-rw-r--r-- | internal/git/command_factory_test.go | 6 | ||||
-rw-r--r-- | internal/git/gittest/intercepting_command_factory.go | 25 |
2 files changed, 27 insertions, 4 deletions
diff --git a/internal/git/command_factory_test.go b/internal/git/command_factory_test.go index 438e23adc..b8ae02ad9 100644 --- a/internal/git/command_factory_test.go +++ b/internal/git/command_factory_test.go @@ -359,7 +359,8 @@ func TestExecCommandFactory_GitVersion(t *testing.T) { } { t.Run(tc.desc, func(t *testing.T) { gitCmdFactory := gittest.NewInterceptingCommandFactory( - ctx, t, testcfg.Build(t), generateVersionScript(tc.versionString), git.WithSkipHooks(), + ctx, t, testcfg.Build(t), generateVersionScript(tc.versionString), + gittest.WithRealCommandFactoryOptions(git.WithSkipHooks()), ) actualVersion, err := gitCmdFactory.GitVersion(ctx) @@ -374,7 +375,8 @@ func TestExecCommandFactory_GitVersion(t *testing.T) { t.Run("caching", func(t *testing.T) { gitCmdFactory := gittest.NewInterceptingCommandFactory( - ctx, t, testcfg.Build(t), generateVersionScript("git version 1.2.3"), git.WithSkipHooks(), + ctx, t, testcfg.Build(t), generateVersionScript("git version 1.2.3"), + gittest.WithRealCommandFactoryOptions(git.WithSkipHooks()), ) gitPath := gitCmdFactory.GetExecutionEnvironment(ctx).BinaryPath diff --git a/internal/git/gittest/intercepting_command_factory.go b/internal/git/gittest/intercepting_command_factory.go index 46b06f721..393ff38e5 100644 --- a/internal/git/gittest/intercepting_command_factory.go +++ b/internal/git/gittest/intercepting_command_factory.go @@ -21,6 +21,22 @@ type InterceptingCommandFactory struct { interceptingCommandFactory git.CommandFactory } +type interceptingCommandFactoryConfig struct { + opts []git.ExecCommandFactoryOption +} + +// InterceptingCommandFactoryOption is an option that can be passed to +// NewInterceptingCommandFactory. +type InterceptingCommandFactoryOption func(*interceptingCommandFactoryConfig) + +// WithRealCommandFactoryOptions is an option that allows the caller to pass options to the real +// git.ExecCommandFactory. +func WithRealCommandFactoryOptions(opts ...git.ExecCommandFactoryOption) InterceptingCommandFactoryOption { + return func(cfg *interceptingCommandFactoryConfig) { + cfg.opts = opts + } +} + // NewInterceptingCommandFactory creates a new command factory which intercepts Git commands. The // given configuration must point to the real Git executable. The function will be executed to // generate the script and receives as input the Git execution environment pointing to the real Git @@ -30,14 +46,19 @@ func NewInterceptingCommandFactory( tb testing.TB, cfg config.Cfg, generateScript func(git.ExecutionEnvironment) string, - opts ...git.ExecCommandFactoryOption, + opts ...InterceptingCommandFactoryOption, ) *InterceptingCommandFactory { + var interceptingCommandFactoryCfg interceptingCommandFactoryConfig + for _, opt := range opts { + opt(&interceptingCommandFactoryCfg) + } + // We create two different command factories. The first one will set up the execution // environment such that we can deterministically resolve the Git binary path as well as // required environment variables for both bundled and non-bundled Git. The second one will // then use a separate config which overrides the Git binary path to point to a custom // script supplied by the user. - gitCmdFactory := NewCommandFactory(tb, cfg, opts...) + gitCmdFactory := NewCommandFactory(tb, cfg, interceptingCommandFactoryCfg.opts...) scriptPath := filepath.Join(testhelper.TempDir(tb), "git") testhelper.WriteExecutable(tb, scriptPath, []byte(generateScript(gitCmdFactory.GetExecutionEnvironment(ctx)))) |