forked from Songmu/gotesplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
107 lines (95 loc) · 1.93 KB
/
parser.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
package splittestgen
import (
"sort"
"strings"
)
type Packages []Package
type Package struct {
Name string
Tests []string
}
type Tests []Test
type Test struct {
Pkg string
Name string
}
func (p Packages) Tests() Tests {
var tests []Test
for _, pkg := range p {
for _, testName := range pkg.Tests {
tests = append(tests, Test{Pkg: pkg.Name, Name: testName})
}
}
return tests
}
func (t Tests) DevideEquallyBy(parallel int) []Tests {
div := len(t) / parallel
mod := len(t) % parallel
var divided []Tests
for i := 0; i < parallel; i++ {
start := i * div
end := (i + 1) * div
if i < mod {
start += i
end += i + 1
} else {
start += mod
end += mod
}
divided = append(divided, t[start:end])
}
return divided
}
type Command struct {
Pkg string
Tests []string
}
func (t Tests) Commands() []Command {
var commands []Command
var l int // length of commands
for _, test := range t {
if l == 0 || commands[l-1].Pkg != test.Pkg {
commands = append(commands, Command{Pkg: test.Pkg, Tests: nil})
l++
}
commands[l-1].Tests = append(commands[l-1].Tests, test.Name)
}
return commands
}
func (c Command) Args() []string {
return []string{
c.Pkg,
"-run",
"^(?:" + strings.Join(c.Tests, "|") + ")$",
}
}
func GetPackages(out string) Packages {
var packages Packages
var list []string
for _, v := range strings.Split(out, "\n") {
if strings.HasPrefix(v, "Test") || strings.HasPrefix(v, "Example") {
list = append(list, v)
continue
}
if strings.HasPrefix(v, "ok ") {
stuff := strings.Fields(v)
if len(stuff) != 3 {
continue
}
sort.Strings(list)
packages = append(packages, Package{
Name: stuff[1],
Tests: list,
})
list = nil
}
}
sort.Slice(packages, func(i, j int) bool {
cmp := len(packages[i].Tests) - len(packages[j].Tests)
if cmp != 0 {
return cmp > 0
}
return strings.Compare(packages[i].Name, packages[j].Name) < 0
})
return packages
}