-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand_test.go
104 lines (92 loc) · 2.24 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package git
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOutput(t *testing.T) {
tests := []struct {
name string
exitCode int
stdout string
stderr string
wantErr *GitError
}{
{
name: "successful command",
stdout: "hello world",
stderr: "",
exitCode: 0,
wantErr: nil,
},
{
name: "not a repo failure",
stdout: "",
stderr: "fatal: not a git repository (or any of the parent directories): .git",
exitCode: 128,
wantErr: &GitError{
ExitCode: 128,
Stderr: "fatal: not a git repository (or any of the parent directories): .git",
err: &exec.ExitError{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := Command{
&exec.Cmd{
Path: createMockExecutable(t, tt.stdout, tt.stderr, tt.exitCode),
},
}
out, err := cmd.Output()
if tt.wantErr != nil {
require.Error(t, err)
var gitError *GitError
require.ErrorAs(t, err, &gitError)
assert.Equal(t, tt.wantErr.ExitCode, gitError.ExitCode)
assert.Equal(t, tt.wantErr.Stderr, gitError.Stderr)
assert.Equal(t, tt.wantErr.Error(), gitError.Error())
} else {
require.NoError(t, err)
}
assert.Equal(t, tt.stdout, string(out))
})
}
}
func createMockExecutable(t *testing.T, stdout string, stderr string, exitCode int) string {
tmpDir := t.TempDir()
sourcePath := filepath.Join(tmpDir, "main.go")
binaryPath := filepath.Join(tmpDir, "mockexec")
if runtime.GOOS == "windows" {
binaryPath += ".exe"
}
// Create Go source
source := buildCommandSourceCode(exitCode, stdout, stderr)
// Write source file
if err := os.WriteFile(sourcePath, []byte(source), 0600); err != nil {
t.Fatal(err)
}
// Compile
cmd := exec.Command("go", "build", "-o", binaryPath, sourcePath)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to compile: %v\n%s", err, out)
}
return binaryPath
}
func buildCommandSourceCode(exitCode int, stdout, stderr string) string {
return fmt.Sprintf(`package main
import (
"fmt"
"os"
)
func main() {
fmt.Printf(%q)
fmt.Fprintf(os.Stderr, %q)
os.Exit(%d)
}`, stdout, stderr, exitCode)
}