-
Notifications
You must be signed in to change notification settings - Fork 26
/
unconvert_test.go
193 lines (162 loc) · 3.78 KB
/
unconvert_test.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
189
190
191
192
193
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
)
func TestBinary(t *testing.T) {
exePath := build(t)
tests := []struct {
name string
dir string
args []string
}{
{"relative", ".", []string{"./testdata"}},
{"dot", "./testdata", []string{"."}},
{"no-args", "./testdata", []string{}},
{"pattern", "./testdata", []string{"./..."}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd := exec.Command(exePath, test.args...)
cmd.Dir = test.dir
output, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected to quit with an error code")
}
t.Log(string(output))
got, err := ParseOutput(t, "testdata", string(output))
if err != nil {
t.Fatal(err)
}
expected, err := ParseDir("testdata")
if err != nil {
t.Fatal(err)
}
SortAnnotations(got)
SortAnnotations(expected)
need := map[Annotation]struct{}{}
for _, annotation := range expected {
need[annotation] = struct{}{}
}
for _, annotation := range got {
_, ok := need[annotation]
if ok {
delete(need, annotation)
} else {
t.Errorf("unexpected: %v", annotation)
}
}
for _, annotation := range expected {
_, ok := need[annotation]
if ok {
t.Errorf("missing: %v", annotation)
}
}
})
}
}
type Annotation struct {
File string
Line int
Message string
}
func SortAnnotations(annotations []Annotation) {
sort.Slice(annotations, func(i, j int) (x bool) {
ai, aj := &annotations[i], &annotations[j]
if ai.File != aj.File {
return ai.File < aj.File
}
if ai.Line != aj.Line {
return ai.Line < aj.Line
}
return ai.Message < aj.Message
})
}
func (ann Annotation) String() string {
return fmt.Sprintf("%s:%d: %s", ann.File, ann.Line, ann.Message)
}
func ParseOutput(t *testing.T, dir, output string) ([]Annotation, error) {
var all []Annotation
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
folderStart := strings.Index(line, dir)
if folderStart < 0 {
t.Errorf("unexpected: %s", line)
continue
}
line = line[folderStart+len(dir)+1:]
tokens := strings.SplitN(line, ":", 4)
if len(tokens) != 4 {
t.Errorf("unexpected: %s", line)
continue
}
line, err := strconv.Atoi(tokens[1])
if err != nil {
return nil, err
}
all = append(all, Annotation{
File: tokens[0],
Line: line,
Message: strings.TrimSpace(tokens[3]),
})
}
return all, nil
}
func ParseDir(dir string) ([]Annotation, error) {
var all []Annotation
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, file := range files {
if filepath.Ext(file.Name()) != ".go" {
continue
}
xs, err := ParseFile(filepath.Join(dir, file.Name()))
if err != nil {
return all, err
}
all = append(all, xs...)
}
return all, nil
}
func ParseFile(file string) ([]Annotation, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
filename := filepath.Base(file)
var all []Annotation
for lineNumber, line := range strings.Split(string(data), "\n") {
p := strings.Index(line, "//@")
if p < 0 {
continue
}
all = append(all, Annotation{
File: filename,
Line: lineNumber + 1,
Message: strings.TrimSpace(line[p+3:]),
})
}
return all, nil
}
func build(t *testing.T) (exePath string) {
exePath = filepath.Join(t.TempDir(), "test_unconvert.exe")
output, err := exec.Command("go", "build", "-o", exePath, ".").CombinedOutput()
if err != nil {
t.Fatalf("failed to build service program: %v\n%v", err, string(output))
}
return exePath
}