-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (152 loc) · 3.41 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"log"
"os"
"strings"
)
type (
Type string
Test struct {
Name string `json:"name"`
Type Type `json:"type"`
Path []string `json:"path"`
LBrace int `json:"lbrace"`
RBrace int `json:"rbrace"`
}
fileOpen func(path string) (io.ReadCloser, error)
)
const (
TestType Type = "test"
SubtestType Type = "subtest"
DynamicSubtestType Type = "dynamicsubtest"
)
func main() {
fatal(rootCommand(os.Args, os.Stdout, func(path string) (io.ReadCloser, error) {
return os.Open(path)
}))
}
func rootCommand(args []string, w io.Writer, open fileOpen) error {
if len(args) == 2 {
return outlineCommand(args[1], w, open)
} else {
return helpCommand(w)
}
}
func outlineCommand(filepath string, w io.Writer, open fileOpen) error {
file, err := open(filepath)
if err != nil {
return err
}
tests, err := outline(file)
if err != nil {
return err
}
return json.NewEncoder(w).Encode(tests)
}
func helpCommand(w io.Writer) error {
_, err := fmt.Fprint(w, `gotestoutline is tool that outputs tests and subtests outline of a go test file in JSON format
Usage:
gotestoutline <go-test-file-path>
`)
return err
}
func outline(src any) ([]*Test, error) {
tests := make([]*Test, 0)
f, err := parser.ParseFile(token.NewFileSet(), "", src, parser.AllErrors)
if err != nil {
return nil, err
}
testingAlias := "testing"
// Tranverse the AST and find the tests and sub-tests that exist
ast.Inspect(f, func(n ast.Node) bool {
switch t := n.(type) {
// Figure out what the alias for testing library is
case *ast.ImportSpec:
if t.Path.Value == `"testing"` && t.Name != nil {
testingAlias = t.Name.Name
}
// Find all the root test functions
case *ast.FuncDecl:
if strings.HasPrefix(t.Name.Name, "Test") {
tests = append(tests, &Test{
Name: t.Name.Name,
Type: TestType,
Path: []string{},
LBrace: int(t.Pos()),
RBrace: int(t.End()),
})
}
// Find all the sub-tests
case *ast.CallExpr:
sel, ok := t.Fun.(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Run" {
return true
}
id, ok := sel.X.(*ast.Ident)
if !ok {
return true
}
field, ok := id.Obj.Decl.(*ast.Field)
if !ok {
return true
}
star, ok := field.Type.(*ast.StarExpr)
if !ok {
return true
}
sel, ok = star.X.(*ast.SelectorExpr)
if !ok {
return true
}
if sel.Sel.Name != "T" {
return true
}
id, ok = sel.X.(*ast.Ident)
if !ok || id.Name != testingAlias {
return true
}
test := &Test{
Path: []string{},
Type: SubtestType,
LBrace: int(t.Lparen),
RBrace: int(t.Rparen),
}
// Report cases where sub test name is dynamic and not a fixed
// string, as IDEs might still want to do something with it.
l, ok := t.Args[0].(*ast.BasicLit)
if ok {
test.Name = strings.Trim(l.Value, `"'`)
} else {
test.Type = DynamicSubtestType
}
tests = append(tests, test)
}
return true
})
// Assemble the path to each subtest
lastTestIndex := 0
for i, test := range tests {
if test.Type == TestType {
lastTestIndex = i
continue
}
for j := lastTestIndex; j < i; j++ {
parent := tests[j]
if parent.RBrace > test.LBrace {
test.Path = append(test.Path, parent.Name)
}
}
}
return tests, nil
}
func fatal(err error) {
if err != nil {
log.Fatal(err)
}
}