This repository was archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
248 lines (231 loc) · 8.41 KB
/
test.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const axios = require('axios')
const camelcase = require('camelcase')
const {readJsonSync} = require('fs-extra/lib/json')
const {get, pickBy} = require('lodash/object')
const {join} = require('path')
const {hasStrict} = require('tcompare')
const {cached, TODAY} = require('./cache')
const {execJson} = require('./execJSON')
const {createValidator, FN_FEATURES_JSON, walkFeatureSchema} = require('./features')
const {forEach} = require('./iterate')
const CHECKMARK = '✓'
const FeatureCollector = {
onPath: (/*pkg, scope, path*/) => {
return []
},
onObject: (value, scope, path, traverse) => {
return Object.entries(scope.properties).reduce(
(acc, [prop, next]) => ([...acc, ...traverse(next, [...path, prop])]),
value
)
},
onFeature: (value, scope, path/*, traverse*/) => {
value.push([path, scope.description || 'no description'])
return value
},
}
function logFeature (path, status, {error = [], info = [], warn = []} = {}) {
if (isWatchMode() && error.length === 0 && warn.length === 0) return
console.error(' ', path.join(' > '), status ? `(${status})` : '')
error.forEach(it => console.error(' X', it))
warn.forEach(it => console.error(' ?', it))
info.forEach(it => console.error(' ', it))
}
const hasCommentsOrRefs = ({comments, references}) =>
(comments && comments.length) || (references && references.length)
const verifyBooleanSpec = (spec, key, lines) => {
if (key in spec) {
if (hasCommentsOrRefs(spec)) {
lines.info(`${key}`)
} else if (spec[key] === false) {
lines.warn(`${key} (without references)`)
} else {
lines.error(`${key} feature missing 'comments' or 'references'`)
}
}
}
const referenceReachable = cached([__filename, TODAY], async (url) =>
(await axios.head(url, {validateStatus: s => s < 400, headers: {accept: '*/*'}})).status)
const verifyRefs = async (refs = [], lines) => {
let result = true
for (const ref of refs) {
try {
await referenceReachable(ref)
} catch (err) {
result = false
lines.error(`reference not reachable: "${ref}" (${err.message})`)
}
}
if (refs.length && result) {
lines.info(`${refs.length} references reachable`)
}
}
function assertExpectedString (actual, expected) {
const msg = (e = expected) => `expected "${e}" but was "${String(actual).replace(/\n/g, '\\n')}"`
if (expected === '') throw new Error(`${msg()} (empty is not a valid assertion)`)
if (!actual) throw new Error(`${msg()} (empty - assertion not possible)`)
const [, regex, flags] = /^\/(.*)\/([a-z]*)$/.exec(expected) || []
const matches = regex ? RegExp(regex, flags).test(actual) : actual.includes(expected)
if (!matches) throw new Error(
msg(regex ? `RegExp(${RegExp(regex, flags)})` : expected)
)
}
const verifyExecutableSpec = (pkg, spec, key, reporter) => {
if (spec[key]) {
const entries = Object.entries(spec[key])
if (entries.length === 0) {
reporter.warn(`${key} (without spec)`)
return
}
for (const [cmd, expected] of entries) {
const prefix = `${key}::"${cmd}"`
const safePkg = camelcase(pkg)
const executable = cmd.startsWith(`${safePkg}.`) || cmd.startsWith(`${safePkg}(`)
? `node -e 'const ${safePkg} = require("${pkg}"); console.log(JSON.stringify(${cmd.replace(/'/g, '"')}))'`
: cmd
const result = execJson(executable, {cwd: join('.', pkg)})
if (typeof expected === 'string') {
try {
assertExpectedString(result.stdout, expected)
reporter.info(prefix)
} catch (err) {
reporter.error(`${prefix}: ${err.message}`, JSON.stringify(result))
}
} else {
if (Object.keys(expected).length === 0) {
reporter.warn(cmd,` executed: ${executable}`, ` returned: ${JSON.stringify(result)}`)
continue
}
const checkable = result
if (typeof expected.stdout === 'string') {
try {
assertExpectedString(result.stdout, expected.stdout)
reporter.info(`${prefix}: (stdout)`)
delete checkable.stdout
delete expected.stdout
} catch (err) {
reporter.error(`${prefix}: (stdout) ${err.message}`)
}
}
if (typeof expected.stderr === 'string') {
try {
assertExpectedString(result.stderr, expected.stderr)
reporter.info(`${prefix}: (stderr)`)
delete checkable.stderr
delete expected.stderr
} catch (err) {
reporter.error(`${prefix}: (stderr) ${err.message}`)
}
}
const keysLeft = Object.keys(expected)
if (keysLeft.length) {
const {match, diff} = hasStrict(checkable, expected)
if (match) {
reporter.info(`${prefix}: (${keysLeft.join(',')})`)
} else {
reporter.error(
`${prefix}: diff`, diff,` execJson: ${JSON.stringify(checkable)}`
)
}
}
}
}
}
}
function isWatchMode() {
return process.env.npm_lifecycle_event === 'dev'
}
const Reporter = () => {
const failed = [], specified = [], verified = []
return {
error: failed.push.bind(failed),
warn: specified.push.bind(specified),
info: verified.push.bind(verified),
failed: () => failed.length,
specified: () => specified.length,
verified: () => verified.length,
log: (path, collector) => {
const status = failed.length ? 'failed' :
specified.length ? 'specified' :
verified.length ? 'verified' :
'unspecified'
collector[status]++
collector.checks += verified.length
if (isWatchMode() && failed.length === 0 && specified.length === 0) return
console.error(' ', path.join(' > '), `(${status})`)
failed.forEach(it => console.error(' ', 'X', it))
specified.forEach(it => console.error(' ', '?', it))
verified.forEach(it => console.error(' ', CHECKMARK, it))
}
}
}
const createVerify = (validateJson) => {
const availableFeatures = walkFeatureSchema(FeatureCollector)('')
return async function verifyFeatures (pkg) {
const report = {
features: availableFeatures.length,
failed: 0, unspecified: 0, specified: 0, verified: 0,
checks: 0
}
const fnPkgFeatures = join('.', pkg, FN_FEATURES_JSON)
const pkgFeatures = readJsonSync(fnPkgFeatures)
if (!validateJson(pkgFeatures)) {
console.log(`invalid features file`, fnPkgFeatures)
validateJson.errors.forEach((it, i) => console.error(i, it))
report.errors += validateJson.errors.length
console.log(`(tests are run anyway, but report will contain errors)`)
}
for (const [path, desciption] of availableFeatures) {
const {$comment, missing, ...spec} = get(pkgFeatures, path, {
$comment: desciption,
missing: true
})
if (missing || Object.keys(spec).length === 0) {
report.unspecified++
logFeature(path, missing ? 'missing' : 'unspecified')
continue
}
const reporter = Reporter()
verifyBooleanSpec(spec, 'mandatory', reporter)
verifyBooleanSpec(spec, 'unsupported', reporter)
await verifyRefs(spec.references, reporter)
Object.keys(spec)
.filter(key =>
key !== 'mandatory' && key !== 'unsupported' && key !== 'references' && key !== 'comments'
)
.forEach(key => verifyExecutableSpec(pkg, spec, key, reporter))
reporter.log(path, report)
}
return report
}
}
async function run (...args) {
const verifyFeatures = createVerify(createValidator)
let unspecifiedSum = 0, specifiedSum = 0, verifiedSum = 0
await forEach(
// first failing package will stop checks for more packages
// since it's easy to run them individually or all at once
async function testFeatures (pkg) {
const report = await verifyFeatures(pkg)
console.error(JSON.stringify(pickBy(report, it => it)))
const {features, failed, unspecified, specified, verified} = report
if (failed > 0) {
throw `There have been ${failed} errors when verifying features of ${pkg}`
}
const tested = verified + unspecified + specified
if (tested < features) {
throw `only ${tested} of ${features} features have been tested`
}
verifiedSum += verified
specifiedSum += specified
unspecifiedSum += unspecified
},
args
)
if (args.length !== 1) {
return {unspecifiedSum, specifiedSum, verifiedSum}
}
}
module.exports = {
run
}