github.com/cheikhshift/buffalo@v0.9.5/buffalo/cmd/test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"regexp"
    11  	"strings"
    12  
    13  	"github.com/gobuffalo/envy"
    14  	"github.com/pkg/errors"
    15  
    16  	"github.com/markbates/pop"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  const vendorPattern = "/vendor/"
    21  
    22  var vendorRegex = regexp.MustCompile(vendorPattern)
    23  
    24  func init() {
    25  	decorate("test", testCmd)
    26  	RootCmd.AddCommand(testCmd)
    27  }
    28  
    29  var testCmd = &cobra.Command{
    30  	Use:                "test",
    31  	Short:              "Runs the tests for your Buffalo app",
    32  	DisableFlagParsing: true,
    33  	RunE: func(c *cobra.Command, args []string) error {
    34  		os.Setenv("GO_ENV", "test")
    35  		if _, err := os.Stat("database.yml"); err == nil {
    36  			// there's a database
    37  			test, err := pop.Connect("test")
    38  			if err != nil {
    39  				return errors.WithStack(err)
    40  			}
    41  
    42  			// drop the test db:
    43  			test.Dialect.DropDB()
    44  
    45  			// create the test db:
    46  			err = test.Dialect.CreateDB()
    47  			if err != nil {
    48  				return errors.WithStack(err)
    49  			}
    50  
    51  			if schema := findSchema(); schema != nil {
    52  				err = test.Dialect.LoadSchema(schema)
    53  				if err != nil {
    54  					return errors.WithStack(err)
    55  				}
    56  			}
    57  		}
    58  		return testRunner(args)
    59  	},
    60  }
    61  
    62  func findSchema() io.Reader {
    63  	if f, err := os.Open(filepath.Join("migrations", "schema.sql")); err == nil {
    64  		return f
    65  	}
    66  	if dev, err := pop.Connect("development"); err == nil {
    67  		schema := &bytes.Buffer{}
    68  		if err = dev.Dialect.DumpSchema(schema); err == nil {
    69  			return schema
    70  		}
    71  	}
    72  
    73  	if test, err := pop.Connect("test"); err == nil {
    74  		if err := test.MigrateUp("./migrations"); err == nil {
    75  			if f, err := os.Open(filepath.Join("migrations", "schema.sql")); err == nil {
    76  				return f
    77  			}
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func testRunner(args []string) error {
    84  	cmd := exec.Command(envy.Get("GO_BIN", "go"), "test", "-p", "1")
    85  	if _, err := exec.LookPath("gotest"); err == nil {
    86  		cmd = exec.Command("gotest", "-p", "1")
    87  	}
    88  	cmd.Args = append(cmd.Args, args...)
    89  	cmd.Stdin = os.Stdin
    90  	cmd.Stdout = os.Stdout
    91  	cmd.Stderr = os.Stderr
    92  	runFlag := false
    93  	for _, a := range cmd.Args {
    94  		if a == "-run" {
    95  			runFlag = true
    96  		}
    97  	}
    98  	if !runFlag {
    99  		out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
   100  		if err != nil {
   101  			return err
   102  		}
   103  		pkgs := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
   104  		for _, p := range pkgs {
   105  			if !vendorRegex.Match(p) {
   106  				cmd.Args = append(cmd.Args, string(p))
   107  			}
   108  		}
   109  	}
   110  	fmt.Println(strings.Join(cmd.Args, " "))
   111  	return cmd.Run()
   112  }