Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2022-04-26 10:02:53 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2022-04-26 11:14:16 +0300
commit8f00e0c9ee07e9f699b5835253f70ee172e23c14 (patch)
treec4e90be0d39e601f7b3d1160d04ff20cb0ea63d9
parentfcc3c5e76691eeed51aa9aeba37065cf31b30e01 (diff)
gittest: Fix erroneousversion interception on factory self-calls
We're about to introduce a version check whenever the Git command factory creates a new command, which means that the factory will start to call `GitVersion()` on itself. This breaks the intercepting command factory though, which will erroneously try to parse the version by using the output of the intercepting script. Fix this issue by writing a second wrapper script that detects the version check in case the version check is not to be intercepted. If so, then the script will call the real Git binary to determine the version.
-rw-r--r--internal/git/gittest/intercepting_command_factory.go26
-rw-r--r--internal/git/gittest/intercepting_command_factory_test.go4
2 files changed, 26 insertions, 4 deletions
diff --git a/internal/git/gittest/intercepting_command_factory.go b/internal/git/gittest/intercepting_command_factory.go
index ebc162487..f68a3bca4 100644
--- a/internal/git/gittest/intercepting_command_factory.go
+++ b/internal/git/gittest/intercepting_command_factory.go
@@ -2,6 +2,7 @@ package gittest
import (
"context"
+ "fmt"
"path/filepath"
"testing"
@@ -70,9 +71,32 @@ func NewInterceptingCommandFactory(
// 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, interceptingCommandFactoryCfg.opts...)
+ execEnv := gitCmdFactory.GetExecutionEnvironment(ctx)
scriptPath := filepath.Join(testhelper.TempDir(tb), "git")
- testhelper.WriteExecutable(tb, scriptPath, []byte(generateScript(gitCmdFactory.GetExecutionEnvironment(ctx))))
+ testhelper.WriteExecutable(tb, scriptPath, []byte(generateScript(execEnv)))
+
+ // In case the user requested us to not intercept the Git version we need to write another
+ // wrapper script. This wrapper script detects whether git-version(1) is executed and, if
+ // so, instead executes the real Git binary. Otherwise, it executes the Git script as
+ // provided by the user.
+ //
+ // This is required in addition to the interception of `GitVersion()` itself so that calls
+ // to `interceptingCommandFactory.GitVersion()` also return the correct results whenever the
+ // factory calls its own `GitVersion()` function.
+ if !interceptingCommandFactoryCfg.interceptVersion {
+ wrapperScriptPath := filepath.Join(testhelper.TempDir(tb), "git")
+ testhelper.WriteExecutable(tb, wrapperScriptPath, []byte(fmt.Sprintf(
+ `#!/bin/bash
+ if test "$1" = "version"
+ then
+ exec %q "$@"
+ fi
+ exec %q "$@"
+ `, execEnv.BinaryPath, scriptPath)))
+
+ scriptPath = wrapperScriptPath
+ }
return &InterceptingCommandFactory{
realCommandFactory: gitCmdFactory,
diff --git a/internal/git/gittest/intercepting_command_factory_test.go b/internal/git/gittest/intercepting_command_factory_test.go
index 6b0cc136f..d326393ea 100644
--- a/internal/git/gittest/intercepting_command_factory_test.go
+++ b/internal/git/gittest/intercepting_command_factory_test.go
@@ -116,11 +116,9 @@ func TestInterceptingCommandFactory_GitVersion(t *testing.T) {
// different versions depending on whether the version is intercepted or
// not. This is required such that it correctly handles version checks in
// case it calls `GitVersion()` on itself.
- //
- // Right now, this doesn't work correctly though.
version, err = factory.interceptingCommandFactory.GitVersion(ctx)
require.NoError(t, err)
- require.Equal(t, fakeVersion, version)
+ require.Equal(t, tc.expectedVersion, version)
})
}
}