(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func 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 | |
| 68 | func createMockExecutable(t *testing.T, stdout string, stderr string, exitCode int) string { |
| 69 | tmpDir := t.TempDir() |
nothing calls this directly
no test coverage detected