Skip to content

Commit f35ae2c

Browse files
committed
Fix up tests
1 parent aa26184 commit f35ae2c

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

example_gh_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func ExampleRESTClient_pagination() {
103103
}
104104
return "", false
105105
}
106-
client, err := gh.RESTClient(nil)
106+
client, err := api.DefaultRESTClient()
107107
if err != nil {
108108
log.Fatal(err)
109109
}
@@ -195,8 +195,8 @@ func ExampleGQLClient() {
195195
}
196196

197197
// Add a star to the cli/go-gh repository using the GQL API.
198-
func ExampleGQLClient_mutate_simple() {
199-
client, err := gh.GQLClient(nil)
198+
func ExampleGQLClient_mutate() {
199+
client, err := api.DefaultGQLClient()
200200
if err != nil {
201201
log.Fatal(err)
202202
}
@@ -231,7 +231,7 @@ func ExampleGQLClient_mutate_simple() {
231231

232232
// Query releases from cli/cli repository using GQL API with paginated results.
233233
func ExampleGQLClient_pagination() {
234-
client, err := gh.GQLClient(nil)
234+
client, err := api.DefaultGQLClient()
235235
if err != nil {
236236
log.Fatal(err)
237237
}

gh.go

+40-15
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,62 @@ package gh
77

88
import (
99
"bytes"
10+
"context"
1011
"fmt"
12+
"io"
13+
"os"
1114
"os/exec"
1215

1316
"github.com/cli/safeexec"
1417
)
1518

16-
// Exec gh command with provided arguments.
17-
func Exec(args ...string) (stdOut, stdErr bytes.Buffer, err error) {
18-
path, err := path()
19+
// Exec invokes a gh command in a subprocess and captures the output and error streams.
20+
func Exec(args ...string) (stdout, stderr bytes.Buffer, err error) {
21+
ghExe, err := ghLookPath()
1922
if err != nil {
20-
err = fmt.Errorf("could not find gh executable in PATH. error: %w", err)
2123
return
2224
}
23-
return run(path, nil, args...)
25+
err = run(context.Background(), ghExe, nil, nil, &stdout, &stderr, args)
26+
return
27+
}
28+
29+
// ExecContext invokes a gh command in a subprocess and captures the output and error streams.
30+
func ExecContext(ctx context.Context, args ...string) (stdout, stderr bytes.Buffer, err error) {
31+
ghExe, err := ghLookPath()
32+
if err != nil {
33+
return
34+
}
35+
err = run(ctx, ghExe, nil, nil, &stdout, &stderr, args)
36+
return
2437
}
2538

26-
func path() (string, error) {
39+
// Exec invokes a gh command in a subprocess with its stdin, stdout, and stderr streams connected to
40+
// those of the parent process. This is suitable for running gh commands with interactive prompts.
41+
func ExecInteractive(ctx context.Context, args ...string) error {
42+
ghExe, err := ghLookPath()
43+
if err != nil {
44+
return err
45+
}
46+
return run(ctx, ghExe, nil, os.Stdin, os.Stdout, os.Stderr, args)
47+
}
48+
49+
func ghLookPath() (string, error) {
50+
if ghExe := os.Getenv("GH_PATH"); ghExe != "" {
51+
return ghExe, nil
52+
}
2753
return safeexec.LookPath("gh")
2854
}
2955

30-
func run(path string, env []string, args ...string) (stdOut, stdErr bytes.Buffer, err error) {
31-
cmd := exec.Command(path, args...)
32-
cmd.Stdout = &stdOut
33-
cmd.Stderr = &stdErr
56+
func run(ctx context.Context, ghExe string, env []string, stdin io.Reader, stdout, stderr io.Writer, args []string) error {
57+
cmd := exec.CommandContext(ctx, ghExe, args...)
58+
cmd.Stdin = stdin
59+
cmd.Stdout = stdout
60+
cmd.Stderr = stderr
3461
if env != nil {
3562
cmd.Env = env
3663
}
37-
err = cmd.Run()
38-
if err != nil {
39-
err = fmt.Errorf("failed to run gh: %s. error: %w", stdErr.String(), err)
40-
return
64+
if err := cmd.Run(); err != nil {
65+
return fmt.Errorf("gh execution failed: %w", err)
4166
}
42-
return
67+
return nil
4368
}

pkg/api/cache_test.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ func TestCacheResponse(t *testing.T) {
3131

3232
cacheDir := filepath.Join(t.TempDir(), "gh-cli-cache")
3333

34-
httpClient, _ := NewHTTPClient(
34+
httpClient, err := NewHTTPClient(
3535
ClientOptions{
36+
Host: "github.com",
37+
AuthToken: "token",
3638
Transport: fakeHTTP,
3739
EnableCache: true,
3840
CacheDir: cacheDir,
3941
LogIgnoreEnv: true,
40-
})
42+
},
43+
)
44+
assert.NoError(t, err)
4145

4246
do := func(method, url string, body io.Reader) (string, error) {
4347
req, err := http.NewRequest(method, url, body)
@@ -57,7 +61,6 @@ func TestCacheResponse(t *testing.T) {
5761
}
5862

5963
var res string
60-
var err error
6164

6265
res, err = do("GET", "http://example.com/path", nil)
6366
assert.NoError(t, err)
@@ -112,13 +115,17 @@ func TestCacheResponseRequestCacheOptions(t *testing.T) {
112115

113116
cacheDir := filepath.Join(t.TempDir(), "gh-cli-cache")
114117

115-
httpClient, _ := NewHTTPClient(
118+
httpClient, err := NewHTTPClient(
116119
ClientOptions{
120+
Host: "github.com",
121+
AuthToken: "token",
117122
Transport: fakeHTTP,
118123
EnableCache: false,
119124
CacheDir: cacheDir,
120125
LogIgnoreEnv: true,
121-
})
126+
},
127+
)
128+
assert.NoError(t, err)
122129

123130
do := func(method, url string, body io.Reader) (string, error) {
124131
req, err := http.NewRequest(method, url, body)
@@ -140,7 +147,6 @@ func TestCacheResponseRequestCacheOptions(t *testing.T) {
140147
}
141148

142149
var res string
143-
var err error
144150

145151
res, err = do("GET", "http://example.com/path", nil)
146152
assert.NoError(t, err)

0 commit comments

Comments
 (0)