github.com/mayra-cabrera/buffalo@v0.9.4-0.20170814145312-66d2e7772f11/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  	cmd.Args = append(cmd.Args, args...)
    86  	cmd.Stdin = os.Stdin
    87  	cmd.Stdout = os.Stdout
    88  	cmd.Stderr = os.Stderr
    89  	runFlag := false
    90  	for _, a := range cmd.Args {
    91  		if a == "-run" {
    92  			runFlag = true
    93  		}
    94  	}
    95  	if !runFlag {
    96  		out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
    97  		if err != nil {
    98  			return err
    99  		}
   100  		pkgs := bytes.Split(bytes.TrimSpace(out), []byte("\n"))
   101  		for _, p := range pkgs {
   102  			if !vendorRegex.Match(p) {
   103  				cmd.Args = append(cmd.Args, string(p))
   104  			}
   105  		}
   106  	}
   107  	fmt.Println(strings.Join(cmd.Args, " "))
   108  	return cmd.Run()
   109  }