-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathma_integration_test.go
72 lines (65 loc) · 1.38 KB
/
ma_integration_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
//go:build integration
package ma_test
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// root finds the root of the source tree by recursively ascending until 'go.mod' is located
func root() (string, error) {
path, err := os.Getwd()
if err != nil {
panic(err)
}
path, err = filepath.Abs(path)
if err != nil {
panic(err)
}
paths := []string{string(os.PathSeparator)}
paths = append(paths, strings.Split(path, string(os.PathSeparator))...)
for len(paths) > 0 {
x := filepath.Join(paths...)
root := filepath.Join(x, "go.mod")
if _, err := os.Stat(root); os.IsNotExist(err) {
paths = paths[:len(paths)-1]
} else {
return x, nil
}
}
return "", errors.New("unable to find go.mod")
}
func command(t *testing.T, args ...string) *exec.Cmd {
dir, err := root()
if err != nil {
t.Error(err)
}
cmd := filepath.Join(dir, "dist", "ma")
if _, err = os.Stat(cmd); err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
return exec.Command(cmd, args...)
}
type harnessIntegration struct {
name string
args []string
exit int
after func(map[string]any)
}
func runIntegration(t *testing.T, tt harnessIntegration) {
a := assert.New(t)
ma := command(t, tt.args...)
out, err := ma.Output()
a.NoError(err)
res := decode(a, bytes.NewBuffer(out))
if tt.after != nil {
tt.after(res)
}
}