Skip to content

Commit

Permalink
fix ut for lookpath
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 committed Jan 9, 2025
1 parent f028e9a commit 7dd690a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tool/tsh/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions tool/tsh/common/git_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
9 changes: 5 additions & 4 deletions tool/tsh/common/git_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7dd690a

Please sign in to comment.