Skip to content

Commit bf7bf99

Browse files
committed
fix(pr create & stubs): handle exitcode in stubs
1 parent 188e138 commit bf7bf99

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

git/command.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,21 @@ func (gc *Command) Output() ([]byte, error) {
4343
out, err := run.PrepareCmd(gc.Cmd).Output()
4444
if err != nil {
4545
ge := GitError{err: err}
46+
47+
// In real implementation, this should be an exec.ExitError, as below,
48+
// but the tests use a different type because exec.ExitError are difficult
49+
// to create. We want to get the exit code and stderr, but stderr
50+
// is not a method and so tests can't access it.
51+
// THIS MEANS THAT TESTS WILL NOT CORRECTLY HAVE STDERR SET,
52+
// but at least tests can get the exit code.
53+
var exitErrorWithExitCode errWithExitCode
54+
if errors.As(err, &exitErrorWithExitCode) {
55+
ge.ExitCode = exitErrorWithExitCode.ExitCode()
56+
}
57+
4658
var exitError *exec.ExitError
4759
if errors.As(err, &exitError) {
4860
ge.Stderr = string(exitError.Stderr)
49-
ge.ExitCode = exitError.ExitCode()
5061
}
5162
err = &ge
5263
}

pkg/cmd/pr/create/create_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,9 +1304,9 @@ func Test_createRun(t *testing.T) {
13041304
},
13051305
customPushDestination: true,
13061306
cmdStubs: func(cs *run.CommandStubber) {
1307-
cs.Register("git rev-parse --abbrev-ref feature@{push}", 0, "")
1308-
cs.Register("git config remote.pushDefault", 0, "")
1309-
cs.Register("git config push.default", 0, "")
1307+
cs.Register("git rev-parse --abbrev-ref feature@{push}", 1, "fatal: not a git repository (or any of the parent directories): .git")
1308+
cs.Register("git config remote.pushDefault", 1, "")
1309+
cs.Register("git config push.default", 1, "")
13101310
},
13111311
expectedOut: "https://github.com/OWNER/REPO/pull/12\n",
13121312
},

0 commit comments

Comments
 (0)