github.com/thetechnoweenie/graven@v1.0.2/commands/tester.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/cbegin/graven/buildtool" 10 "github.com/cbegin/graven/domain" 11 "github.com/hashicorp/go-multierror" 12 "github.com/urfave/cli" 13 ) 14 15 var TestCommand = cli.Command{ 16 Name: "test", 17 Usage: "Finds and runs tests in this project", 18 Action: tester, 19 } 20 21 func tester(c *cli.Context) error { 22 if err := clean(c); err != nil { 23 return err 24 } 25 26 project, err := domain.FindProject() 27 if err != nil { 28 return err 29 } 30 31 err = os.MkdirAll(project.TargetPath("reports"), 0755) 32 if err != nil { 33 return fmt.Errorf("Could not create reports directory. %v", err) 34 } 35 36 var merr error 37 if err := filepath.Walk(project.ProjectPath(), getTestWalkerFunc(project, &merr)); err != nil { 38 merr = multierror.Append(merr, err) 39 } 40 return merr 41 } 42 43 func getTestWalkerFunc(project *domain.Project, merr *error) filepath.WalkFunc { 44 // TODO - Make this configurable 45 var buildTool builder.BuildTool = &builder.GoBuildTool{} 46 return func(path string, info os.FileInfo, err error) error { 47 if info.IsDir() { 48 subDir := path[len(project.ProjectPath()):] 49 subDirParts := strings.Split(subDir, string(filepath.Separator)) 50 matches, _ := filepath.Glob(filepath.Join(path, "*_test.go")) 51 if len(matches) > 0 && !contains(subDirParts, map[string]struct{}{ 52 "vendor": struct{}{}, 53 "target": struct{}{}, 54 ".git": struct{}{}}) { 55 fmt.Printf("Testing %v\n", subDir) 56 if err := buildTool.Test(subDir, project); err != nil { 57 *merr = multierror.Append(*merr, err) 58 } 59 } 60 } 61 return nil 62 } 63 } 64 65 func contains(strings []string, exclusions map[string]struct{}) bool { 66 for _, a := range strings { 67 if _, ok := exclusions[a]; ok { 68 return true 69 } 70 } 71 return false 72 }