diff --git a/tool/tsh/common/git.go b/tool/tsh/common/git.go index c5fb895c999f6..0b47c2e6e46ed 100644 --- a/tool/tsh/common/git.go +++ b/tool/tsh/common/git.go @@ -128,7 +128,7 @@ func execGit(cf *CLIConf, args ...string) error { func execGitWithStdoutAndStderr(cf *CLIConf, stdout, stderr io.Writer, args ...string) error { const gitExecutable = "git" - gitPath, err := exec.LookPath(gitExecutable) + gitPath, err := cf.LookPath(gitExecutable) if err != nil { return trace.NotFound(`could not locate the executable %q. The following error occurred: %s diff --git a/tool/tsh/common/git_clone_test.go b/tool/tsh/common/git_clone_test.go index b2091e6de995f..4e27e3ac3286f 100644 --- a/tool/tsh/common/git_clone_test.go +++ b/tool/tsh/common/git_clone_test.go @@ -104,9 +104,10 @@ func TestGitCloneCommand(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cf := &CLIConf{ - Context: context.Background(), - executablePath: "tsh", - cmdRunner: tt.verifyCommand, + Context: context.Background(), + executablePath: "tsh", + cmdRunner: tt.verifyCommand, + lookPathOverride: "git", } tt.checkError(t, tt.cmd.run(cf)) }) diff --git a/tool/tsh/common/git_config_test.go b/tool/tsh/common/git_config_test.go index 265dd68fbfd62..b045e9342bb5f 100644 --- a/tool/tsh/common/git_config_test.go +++ b/tool/tsh/common/git_config_test.go @@ -171,10 +171,11 @@ func TestGitConfigCommand(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var buf bytes.Buffer cf := &CLIConf{ - Context: context.Background(), - OverrideStdout: &buf, - executablePath: "tsh", - cmdRunner: tt.fakeRunner.run, + Context: context.Background(), + OverrideStdout: &buf, + executablePath: "tsh", + cmdRunner: tt.fakeRunner.run, + lookPathOverride: "git", } tt.checkError(t, tt.cmd.run(cf)) require.Contains(t, buf.String(), tt.checkOutputContains) diff --git a/tool/tsh/common/tsh.go b/tool/tsh/common/tsh.go index d8701a668ebd4..60d59e06bfd57 100644 --- a/tool/tsh/common/tsh.go +++ b/tool/tsh/common/tsh.go @@ -572,6 +572,9 @@ type CLIConf struct { // profileStatusOverride overrides return of ProfileStatus(). used in tests. profileStatusOverride *client.ProfileStatus + + // lookPathOverride overrides return of LookPath(). used in tests. + lookPathOverride string } // Stdout returns the stdout writer. @@ -611,6 +614,14 @@ func (c *CLIConf) RunCommand(cmd *exec.Cmd) error { return trace.Wrap(cmd.Run()) } +// LookPath searches for an executable named file. +func (c *CLIConf) LookPath(file string) (string, error) { + if c.lookPathOverride != "" { + return c.lookPathOverride, nil + } + return exec.LookPath(file) +} + func Main() { cmdLineOrig := os.Args[1:] var cmdLine []string