-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.go
71 lines (57 loc) · 1.31 KB
/
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
package cmd
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/spf13/cobra"
)
var testCmd = &cobra.Command{
Use: "test",
Short: "Runs the tests for your abcweb app",
Long: `Runs all Go tests for your abcweb app and skips your vendor directory.`,
Example: "abcweb test -v",
Run: testCmdRun,
}
func init() {
testCmd.Flags().BoolP("verbose", "v", false, "Enable verbose output")
RootCmd.AddCommand(testCmd)
testCmd.PreRun = func(*cobra.Command, []string) {
cnf.ModeViper.BindPFlags(testCmd.Flags())
}
}
func testCmdRun(cmd *cobra.Command, args []string) {
checkDep("go")
var exc *exec.Cmd
exc = exec.Command("go", "list", "./...")
exc.Dir = cnf.AppPath
out, err := exc.CombinedOutput()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if cnf.ModeViper.GetBool("verbose") {
exc = exec.Command("go", "test", "-v", "-p", "1")
} else {
exc = exec.Command("go", "test", "-p", "1")
}
rgx, err := regexp.Compile(cnf.AppName + "/vendor")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pkgs := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, pkg := range pkgs {
if !rgx.MatchString(pkg) {
exc.Args = append(exc.Args, pkg)
}
}
exc.Dir = cnf.AppPath
out, err = exc.CombinedOutput()
fmt.Print(string(out))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}