-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_module.go
211 lines (163 loc) · 4.22 KB
/
checker_module.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package k6lint
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"golang.org/x/mod/modfile"
)
type moduleChecker struct {
file *modfile.File
exe string
js bool
}
func newModuleChecker() *moduleChecker {
return new(moduleChecker)
}
func (mc *moduleChecker) hasGoModule(_ context.Context, dir string) *checkResult {
filename := filepath.Join(dir, "go.mod")
data, err := os.ReadFile(filepath.Clean(filename)) //nolint:forbidigo
if err != nil {
return checkError(err)
}
mc.file, err = modfile.Parse(filename, data, nil)
if err != nil {
return checkError(err)
}
return checkPassed("found `%s` as go module", mc.file.Module.Mod.String())
}
func (mc *moduleChecker) hasNoReplace(_ context.Context, _ string) *checkResult {
if mc.file == nil {
return checkFailed("missing go.mod")
}
if len(mc.file.Replace) != 0 {
return checkFailed("the `go.mod` file contains `replace` directive(s)")
}
return checkPassed("no `replace` directive in the `go.mod` file")
}
func (mc *moduleChecker) canBuild(ctx context.Context, dir string) *checkResult {
if mc.file == nil {
return checkFailed("missing go.mod")
}
exe, err := build(ctx, mc.file.Module.Mod.Path, dir)
if err != nil {
return checkError(err)
}
out, err := exec.CommandContext(ctx, exe, "version").CombinedOutput() //nolint:gosec
if err != nil {
return checkError(err)
}
mc.exe = exe
mc.js = mc.isJS(out)
return checkPassed("can be built with the latest k6 version")
}
func (mc *moduleChecker) isJS(out []byte) bool {
rex, err := regexp.Compile("(?i) " + mc.file.Module.Mod.String() + "[^,]+, [^ ]+ \\[(?P<type>[a-z]+)\\]")
if err != nil {
return false
}
subs := rex.FindAllSubmatch(out, -1)
if subs == nil {
return false
}
js := false
for _, one := range subs {
if string(one[rex.SubexpIndex("type")]) == "js" {
js = true
break
}
}
return js
}
var reSmoke = regexp.MustCompile(`(?i)^smoke(\.test)?\.(?:js|ts)`)
//nolint:forbidigo
func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {
if mc.exe == "" {
return checkFailed("can't build")
}
if !mc.js {
return checkPassed("skipped due to output extension")
}
filename, shortname, err := findFile(reSmoke,
dir,
filepath.Join(dir, "test"),
filepath.Join(dir, "tests"),
filepath.Join(dir, "examples"),
filepath.Join(dir, "scripts"),
)
if err != nil {
return checkError(err)
}
if len(shortname) == 0 {
return checkFailed("no smoke test file found")
}
cmd := exec.CommandContext(ctx, mc.exe, "run", "--no-usage-report", "--no-summary", "--quiet", filename) //nolint:gosec
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintln(os.Stderr, string(out))
return checkError(err)
}
return checkPassed("`%s` successfully run with k6", shortname)
}
var reIndexDTS = regexp.MustCompile("^index.d.ts$")
func (mc *moduleChecker) types(_ context.Context, dir string) *checkResult {
if mc.exe == "" {
return checkFailed("can't build")
}
if !mc.js {
return checkPassed("skipped due to output extension")
}
_, shortname, err := findFile(reIndexDTS,
dir,
filepath.Join(dir, "docs"),
filepath.Join(dir, "api-docs"),
)
if err != nil {
return checkError(err)
}
if len(shortname) > 0 {
return checkPassed("found `index.d.ts` file")
}
return checkFailed("no `index.d.ts` file found")
}
//nolint:forbidigo
func (mc *moduleChecker) examples(_ context.Context, dir string) *checkResult {
if mc.exe == "" {
return checkFailed("can't build")
}
if !mc.js {
return checkPassed("skipped due to output extension")
}
dir = filepath.Join(dir, "examples")
info, err := os.Stat(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return checkFailed("missing `examples` directory")
}
return checkError(err)
}
if !info.IsDir() {
return checkFailed("`examples` is not a directory")
}
hasRegular := false
err = filepath.WalkDir(dir, func(_ string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.Type().IsRegular() {
hasRegular = true
}
return nil
})
if err != nil {
return checkError(err)
}
if hasRegular {
return checkPassed("found `examples` as examples directory")
}
return checkFailed("no examples found in the `examples` directory")
}