-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbro.go
151 lines (121 loc) · 2.84 KB
/
bro.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/codegangsta/cli"
)
// Just return folder where `bro` has running
func GetCurrentLocation() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return dir
}
type Bro struct {
GoExt string
Location string
Files map[string]time.Time
}
func (b *Bro) WalkLocation() {
Recurse := true
b.Files = make(map[string]time.Time)
walkFunc := func(path string, info os.FileInfo, err error) error {
path, err = filepath.Abs(path)
if err != nil {
return err
}
if info.IsDir() && path != b.Location && !Recurse {
return filepath.SkipDir
}
if filepath.Ext(path) == b.GoExt {
b.Files[path] = info.ModTime()
}
return nil
}
filepath.Walk(b.Location, walkFunc)
log.Printf("[INFO]: Eye on %d files\n", len(b.Files))
}
func (b *Bro) HelpMe() {
for {
b.LookThem()
time.Sleep(time.Duration(2 * time.Second))
}
}
func (b *Bro) LookThem() {
for file, modtime := range b.Files {
stat, err := os.Stat(file)
if err != nil {
log.Fatal("[ERROR]: ", err.Error())
}
ntime := stat.ModTime()
if ntime.Sub(modtime) > 0 {
b.Files[file] = ntime
log.Printf("[INFO]: Changed files %s\n", file)
PrepareCmd(file)
}
}
}
func IsTestFile(nameFile string) bool {
matched, _ := regexp.MatchString("._test.go", nameFile)
return matched
}
func HasTestFile(dirFile, nameFile string) bool {
testFilePattern := "%s_test.go"
fullPath := filepath.Join(dirFile, fmt.Sprintf(testFilePattern, nameFile))
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return false
}
return true
}
func PrepareCmd(path string) bool {
nameFile := filepath.Base(path)
dirFile := filepath.Dir(path)
var mainFile string
var testFile string
if IsTestFile(nameFile) == true {
testFile = path
mainFile = filepath.Join(dirFile, strings.Replace(nameFile, "_test.go", ".go", -1))
return TestCommand(mainFile, testFile)
}
if HasTestFile(dirFile, strings.Replace(nameFile, ".go", "", -1)) == true {
testFile = filepath.Join(dirFile, strings.Replace(nameFile, ".go", "_test.go", -1))
mainFile = path
return TestCommand(mainFile, testFile)
}
log.Printf("[WARNING]: %s need of test man!", nameFile)
return false
}
func TestCommand(mainFile, testFile string) bool {
cmd := exec.Command("go", "test", mainFile, testFile, "-v")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return false
}
return true
}
func main() {
app := cli.NewApp()
app.Name = "bro"
app.Usage = "I'll help you with tests"
app.Action = func(c *cli.Context) {
var where string
if c.Args().Present() == false {
where = GetCurrentLocation()
} else {
where = c.Args().First()
}
bro := Bro{GoExt: ".go", Location: where}
bro.WalkLocation()
bro.HelpMe()
}
app.Run(os.Args)
}