MCPcopy Index your code
hub / github.com/cli/cli / TestOutput

Function TestOutput

git/command_test.go:15–66  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestOutput(t *testing.T) {
16 tests := []struct {
17 name string
18 exitCode int
19 stdout string
20 stderr string
21 wantErr *GitError
22 }{
23 {
24 name: "successful command",
25 stdout: "hello world",
26 stderr: "",
27 exitCode: 0,
28 wantErr: nil,
29 },
30 {
31 name: "not a repo failure",
32 stdout: "",
33 stderr: "fatal: not a git repository (or any of the parent directories): .git",
34 exitCode: 128,
35 wantErr: &GitError{
36 ExitCode: 128,
37 Stderr: "fatal: not a git repository (or any of the parent directories): .git",
38 err: &exec.ExitError{},
39 },
40 },
41 }
42
43 for _, tt := range tests {
44 t.Run(tt.name, func(t *testing.T) {
45
46 cmd := Command{
47 &exec.Cmd{
48 Path: createMockExecutable(t, tt.stdout, tt.stderr, tt.exitCode),
49 },
50 }
51
52 out, err := cmd.Output()
53 if tt.wantErr != nil {
54 require.Error(t, err)
55 var gitError *GitError
56 require.ErrorAs(t, err, &gitError)
57 assert.Equal(t, tt.wantErr.ExitCode, gitError.ExitCode)
58 assert.Equal(t, tt.wantErr.Stderr, gitError.Stderr)
59 assert.Equal(t, tt.wantErr.Error(), gitError.Error())
60 } else {
61 require.NoError(t, err)
62 }
63 assert.Equal(t, tt.stdout, string(out))
64 })
65 }
66}
67
68func createMockExecutable(t *testing.T, stdout string, stderr string, exitCode int) string {
69 tmpDir := t.TempDir()

Callers

nothing calls this directly

Calls 5

OutputMethod · 0.95
ErrorMethod · 0.95
createMockExecutableFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected