/
cmd_runner.go
51 lines (43 loc) · 969 Bytes
/
cmd_runner.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
package goclitest
import (
"fmt"
"path/filepath"
)
type MockResponse struct {
Args []string
Output string
Error error
}
type TestGoCmdRunner struct {
Responses []MockResponse
}
func (r *TestGoCmdRunner) RunGoCommand(args ...string) (string, error) {
for _, v := range r.Responses {
if len(args) != len(v.Args) {
continue
}
match := true
for i, arg := range args {
if arg != v.Args[i] {
match = false
break
}
}
if match {
return v.Output, v.Error
}
}
return "", fmt.Errorf("could not match args: %v", args)
}
func GetLatestVersionMockResponse(pathURL, version string) MockResponse {
return MockResponse{
Args: []string{"list", "-m", "-f", "{{.Version}}", fmt.Sprintf("%s@latest", pathURL)},
Output: version,
}
}
func GetModuleInfoMockResponse(gobin, binaryName, output string) MockResponse {
return MockResponse{
Args: []string{"version", "-m", filepath.Join(gobin, binaryName)},
Output: output,
}
}