github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/cmds/test/main.go (about) 1 package test 2 3 import ( 4 "context" 5 "fmt" 6 "os/exec" 7 "strings" 8 9 "github.com/gobuffalo/plugins" 10 "github.com/gobuffalo/plugins/plugio" 11 "github.com/gobuffalo/plugins/plugprint" 12 ) 13 14 func (tc *Cmd) Main(ctx context.Context, root string, args []string) error { 15 plugs := tc.ScopedPlugins() 16 if t := FindTesterFromArgs(args, plugs); t != nil { 17 return t.Test(ctx, root, args[1:]) 18 } 19 20 for _, a := range args { 21 if a == "-h" { 22 return plugprint.Print(plugio.Stdout(plugs...), tc) 23 } 24 } 25 26 err := tc.run(ctx, root, args) // go test ... 27 return tc.afterTest(ctx, root, args, err) 28 } 29 30 func (tc *Cmd) run(ctx context.Context, root string, args []string) error { 31 if err := tc.beforeTest(ctx, root, args); err != nil { 32 return plugins.Wrap(tc, err) 33 } 34 35 cmd, err := tc.Cmd(ctx, root, args) 36 if err != nil { 37 return plugins.Wrap(tc, err) 38 } 39 40 for _, p := range tc.ScopedPlugins() { 41 if br, ok := p.(Runner); ok { 42 return br.RunTests(ctx, root, cmd) 43 } 44 } 45 46 return cmd.Run() 47 } 48 49 func (tc *Cmd) beforeTest(ctx context.Context, root string, args []string) error { 50 testers := tc.ScopedPlugins() 51 for _, p := range testers { 52 if bb, ok := p.(BeforeTester); ok { 53 if err := bb.BeforeTest(ctx, root, args); err != nil { 54 return plugins.Wrap(bb, err) 55 } 56 } 57 } 58 return nil 59 } 60 61 func (tc *Cmd) afterTest(ctx context.Context, root string, args []string, err error) error { 62 testers := tc.ScopedPlugins() 63 for _, p := range testers { 64 if bb, ok := p.(AfterTester); ok { 65 if err := bb.AfterTest(ctx, root, args, err); err != nil { 66 return plugins.Wrap(bb, err) 67 } 68 } 69 } 70 return err 71 } 72 73 func (tc *Cmd) Cmd(ctx context.Context, root string, args []string) (*exec.Cmd, error) { 74 if len(args) == 0 { 75 args = append(args, "./...") 76 } 77 78 args, err := tc.buildArgs(ctx, root, args) 79 if err != nil { 80 return nil, plugins.Wrap(tc, err) 81 } 82 83 cargs := []string{ 84 "test", 85 } 86 cargs = append(cargs, args...) 87 88 c := exec.CommandContext(ctx, "go", cargs...) 89 fmt.Println(c.Args) 90 91 plugs := tc.ScopedPlugins() 92 c.Stdin = plugio.Stdin(plugs...) 93 c.Stdout = plugio.Stdout(plugs...) 94 c.Stderr = plugio.Stderr(plugs...) 95 return c, nil 96 } 97 98 func (tc *Cmd) buildArgs(ctx context.Context, root string, args []string) ([]string, error) { 99 args, err := tc.pluginArgs(ctx, root, args) 100 if err != nil { 101 return nil, plugins.Wrap(tc, err) 102 } 103 104 args = tc.reducePairedArg("-tags", args) 105 106 p := args[len(args)-1] 107 108 if strings.HasPrefix(p, ".") { 109 return args, nil 110 } 111 112 args = append(args, "./...") 113 114 return args, nil 115 } 116 117 func (tc *Cmd) reducePairedArg(key string, args []string) []string { 118 nargs := make([]string, 0, len(args)) 119 120 ind := -1 121 for i := 0; i < len(args); i++ { 122 a := args[i] 123 if a != key && len(strings.TrimSpace(a)) > 0 { 124 nargs = append(nargs, a) 125 continue 126 } 127 128 if ind == -1 { 129 ind = i 130 nargs = append(nargs, key, "") 131 } 132 133 if len(args) <= i { 134 break 135 } 136 137 n := args[i+1] 138 n = strings.TrimSpace(fmt.Sprintf("%s %s", nargs[ind+1], n)) 139 nargs[ind+1] = n 140 i++ 141 } 142 return nargs 143 } 144 145 func (tc *Cmd) pluginArgs(ctx context.Context, root string, args []string) ([]string, error) { 146 plugs := tc.ScopedPlugins() 147 for _, p := range plugs { 148 bt, ok := p.(Argumenter) 149 if !ok { 150 continue 151 } 152 tgs, err := bt.TestArgs(ctx, root) 153 if err != nil { 154 return nil, plugins.Wrap(bt, err) 155 } 156 // prepend external build args 157 args = append(tgs, args...) 158 } 159 return args, nil 160 }