-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·363 lines (302 loc) · 14.1 KB
/
cli.js
File metadata and controls
executable file
·363 lines (302 loc) · 14.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env node
// @ts-check
import { groupBy, pluck, map, indexBy, pickBy, identity } from 'ramda'
import tempy from 'tempy'
import debounce from 'debounce'
import { program, Option } from 'commander'
import url from 'url'
import { transpile } from './typescriptTranspiler.js'
import { filesTested, skippingTest } from './outputs.js'
import chalk from 'chalk'
import dependencyTree from 'dependency-tree'
import chokidar from 'chokidar'
import path from 'path'
import { spawn } from 'node:child_process'
import readline from 'node:readline'
import os from 'os'
import { parseCode } from './basic-parser.js'
import { hash } from './hash.js'
import { readFileSync, writeFileSync } from 'fs'
import glob from 'glob'
import { deserialize } from './snapshot.js'
import minimatch from 'minimatch'
const formatOption = new Option('-f, --format <format>', 'The output format').choices(['json', 'console']).default('console')
program
.name('pineapple')
.version('0.21.2')
.option('-i, --include <files...>', 'Comma separated globs of files to include.')
.option('-e, --exclude <files...>', 'Comma separated globs of files to exclude.')
.option('-w, --watch-mode', 'Will run tests only when a file is modified.')
.option('-a, --accept-all', 'Accept all snapshots.')
.option('-u, --update-all', 'Update all snapshots.')
.option('-t, --transpile', 'Enables transpilation.')
.option('--typescript', 'Enables transpilation for TypeScript. (legacy flag)')
.option('--timeout <milliseconds>', 'The timeout for each test.', '5000')
.option('--strict', 'Enables additional checks to enforce better testing, namely validating that all snapshots are used.')
.option('--clean', 'Cleans up unused snapshots.')
.option('--omit-snapshot-inputs', 'Omits input from being captured in snapshots for fuzz testing.')
.option('--only <lines...>', 'Allows you to specify which tests you would like to run.')
.option('--fuzz-runs <amount>', 'The number of runs that fuzz tests perform.', '100')
.option('--snapshot-fuzz-runs <amount>', 'The number of runs that fuzz tests perform on a snapshot.', '10')
.option('--cjs', 'Forces CommonJS to be used instead of ESM. This is not always necessary, but can be useful in some cases, particularly when using TypeScript.')
.option('--rollup', 'Forces rollup to be used instead of ESBuild. This is not always necessary, but can be useful in some cases.')
.option('--shim-resolution', 'Tries to shim the resolution of ESBuild so that it will bundle aliased paths that start with @ while packages are kept external.')
.addOption(formatOption)
if (os.platform() !== 'win32') program.option('--bun', 'Uses Bun as the test runner.')
program.parse()
const options = program.opts()
if (!options.include || !options.include.length) throw new Error('Please select files to include.')
if (!options.only && options.strict) process.env.STRICT = 'true'
if (options.clean && options.strict) throw new Error('Strict & Clean cannot be enabled at the same time.')
if (options.typescript) options.transpile = true
if (options.omitSnapshotInputs) process.env.OMIT_SNAPSHOT_INPUTS = 'true'
if (options.fuzzRuns) process.env.FAST_CHECK_NUM_RUNS = options.fuzzRuns
if (options.snapshotFuzzRuns) process.env.SNAPSHOT_FAST_CHECK_NUM_RUNS = options.snapshotFuzzRuns
if (options.timeout) process.env.TEST_TIMEOUT = options.timeout
// Used for the "watch" mode.
let child
// Add additional code to make it easier to interface with the program when in watch mode.
if (options.watchMode) {
const cleanExit = () => {
console.clear()
if (child) child.kill()
process.exit()
}
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
const forward = new Set(['down', 'up', 'return', 'backspace'])
const forwardEntered = new Set(['y', 'n'])
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') cleanExit()
if ((!child || child.exitCode !== null) && key.name === 'q') cleanExit()
// Forward certain keystrokes to the child program
if (child && child.exitCode === null && forward.has(key.name)) child.stdin.write(key.sequence)
if (child && child.exitCode === null && forwardEntered.has(key.name)) child.stdin.write(key.sequence + '\n')
})
}
// hack for now until I make the code better
process.env.ACCEPT_ALL = options.acceptAll || ''
process.env.UPDATE_ALL = options.updateAll || ''
if (options.format === 'json') process.env.OUTPUT_FORMAT = 'JSON'
process.env.OUTPUT_FORMAT = process.env.OUTPUT_FORMAT || 'CONSOLE'
if (options.bun) delete options.typescript
if (options.bun && process.env.OUTPUT_FORMAT === 'CONSOLE') process.env.FORCE_COLOR = '1'
if (process.env.OUTPUT_FORMAT === 'JSON') {
chalk.level = 0
process.env.FORCE_COLOR = '0'
}
async function main () {
const regex = /,\s?(?![^{}]*\})/
options.exclude = (options.exclude ? options.exclude.flatMap(i => i.split(regex)) : []).concat('**/*.d.ts')
const files = options.include.flatMap(i => i.split(regex)).flatMap(i => glob.sync(i, {
ignore: options.exclude
}))
// get variable declarations that are arrow functions / functions
let functions = files.flatMap(getFileFunctions)
const fileChanged = debounce(async (fileChanged) => {
const correctPath = path.resolve(fileChanged)
if (fileChanged.endsWith('.psnap')) return
console.clear()
functions = functions.filter(i => i.fileName !== url.pathToFileURL(correctPath).href).concat(
getFileFunctions(fileChanged)
)
const execFunctions = functions.filter(i => {
// we also always need to include files with @pineapple_define and global tags.
const global = i.tags.some(i => i.type === 'pineapple_define' || i.type.includes('Global') || i.type.endsWith('All') || i.type === 'pineapple_import' || i.type === 'faker')
return i.dependencies.has(correctPath) || global
})
if (execFunctions.length) {
const set = new Set()
execFunctions.forEach(i => set.add(i.relativePath))
await execute(execFunctions, true)
filesTested(Array.from(set))
}
}, 50, false)
if (options.watchMode) {
chokidar.watch(options.include).on('change', fileChanged)
} else await execute(functions, false)
}
const getFileFunctions = file => {
const functions = getFunctions(readFileSync(file).toString(), url.pathToFileURL(file).href)
if (functions.length) {
const { virtualDependencies } = functions[0]
// grab the dependencies for a given file
const dependencies = new Set(dependencyTree.toList({
filename: file,
directory: '.',
filter: str => !str.includes('/node_modules/'),
noTypeDefinitions: false
}))
// add the virtual dependencies
virtualDependencies.forEach(toFile => {
dependencyTree.toList({
filename: path.resolve(file, '../' + toFile),
directory: '.',
filter: str => !str.includes('/node_modules/'),
noTypeDefinitions: false
}).forEach(i => dependencies.add(i))
})
// attach the dependencies to the files.
return functions.map(i => ({ ...i, dependencies }))
}
return []
}
const cwd = url.pathToFileURL(process.cwd()).href
/**
* @param {*} functions
* @param {boolean} forkProcess
*/
async function execute (functions, forkProcess = false) {
const tmp = tempy.file({ extension: 'mjs' })
const imports = Object.entries(map(i => {
return { namedImports: pluck('name', i), original: indexBy(i => i.name, i) }
}, groupBy(i => i.fileName, functions)))
const transpileFunctions = await Promise.all(functions.filter(i => i.tags.some(i => i.type === 'pineapple_transpile')).map(async ({ name, tags, fileName }) => {
const files = (await deserialize(tags.find(i => i.type === 'pineapple_transpile').text))
const transpile = (await import(fileName))[name]
return { transpile, files }
}))
const specifier = import.meta.url.split(/\/|\\/)
specifier.pop()
const runFile = [...specifier, 'run.js'].join('/')
const outputFile = [...specifier, 'outputs.js'].join('/')
const pathScheme = options.bun ? url.fileURLToPath : i => i
let testFileString = ''
testFileString += `import { check, run, addMethod, addFaker, addDefinitions, execute, hof } from '${pathScheme(runFile)}';\n`
testFileString += `import { aggregate } from '${pathScheme(outputFile)}';\n`
let counter = 0
// add imports
await Promise.all(imports.map(async ([moduleSpecifier, { namedImports, original }], index) => {
if (options.transpile) {
const extension = options.cjs ? '.cjs' : '.mjs'
const recommended = `./pineapple-runner/${hash(moduleSpecifier)}${extension}`
const transpileFunc = transpileFunctions.find(i => i.files.some(i =>
minimatch(moduleSpecifier, i, { matchBase: true })
))?.transpile ?? transpile
moduleSpecifier = url.pathToFileURL(await transpileFunc(url.fileURLToPath(moduleSpecifier), path.resolve(recommended), { rollup: options.rollup, shim: options.shimResolution })).href
}
const str = `
import * as $$${index} from '${pathScheme(moduleSpecifier)}';
const { ${namedImports.map(i => {
original[i].alias = `$${counter++}`
return `${i}: ${original[i].alias}`
}).join(', ')} } = { ...$$${index}.default, ...$$${index} };
`
testFileString += str
}))
// add test functions
testFileString += '\nasync function test() {\n'
const executeTag = tag => `await execute(${JSON.stringify(tag.text)})`
let testCount = 0
const { addedMethods, tests, beforeAll, afterAll, beforeEachGlobal, beforeGlobal, afterGlobal, afterEachGlobal } = functions.map(func => {
const addedMethods = func.tags
.filter(i => i.type === 'pineapple_import')
.map(i => `addMethod(${JSON.stringify(i.text || func.originalName || func.name)}, ${func.alias})\n`)
.join('') + '\n' +
func.tags
.filter(i => i.type === 'pineapple_define')
.map((i) => `addDefinitions(${func.alias}, ${JSON.stringify(i.text.trim() || '')})\n`)
.join('') + '\n' + func.tags
.filter(i => i.type === 'faker')
.map(i => `addFaker(${JSON.stringify(i.text || func.originalName || func.name)}, ${func.alias})\n`)
.join('')
const globalLifecycle = name => func.tags
.filter(i => i.type === name)
.map(() => `await ${func.alias}()\n`)
.join('')
const beforeAll = globalLifecycle('beforeAll')
const afterAll = globalLifecycle('afterAll')
const beforeEachGlobal = globalLifecycle('beforeEachGlobal')
const beforeGlobal = globalLifecycle('beforeGlobal')
const afterGlobal = globalLifecycle('afterGlobal')
const afterEachGlobal = globalLifecycle('afterEachGlobal')
// before / beforeEach / after / afterEach will get integrated in directly with the tests.
const testLifecycle = name => func.tags
.filter(i => i.type === name)
.map(executeTag)
.join('\n')
const before = testLifecycle('before')
const beforeEach = testLifecycle('beforeEach')
const after = testLifecycle('after')
const afterEach = testLifecycle('afterEach')
const wrapHof = (alias, tag) => func.isClass ? `hof(${alias}, ${tag.type === 'test_static'})` : alias
// this is sloppy, I should make this part of the reducer
testCount += func.tags.filter(i => i.type === 'test' || i.type === 'test_static').length
const tests = `%beforeGlobal%\n${before}\n${func.tags.filter(i => i.type === 'test' || i.type === 'test_static').map((tag, index) => `
%beforeEachGlobal%
${beforeEach}
sum += await run(${JSON.stringify(tag.text)}, '${func.originalName || func.name}', ${wrapHof(func.alias, tag)}, "${func.fileName}:${tag.lineNo}")
%afterEachGlobal%
${afterEach}
`).join('')}\n${after}\n%afterGlobal%`
return { addedMethods, tests, beforeAll, afterAll, beforeEachGlobal, afterEachGlobal, afterGlobal, beforeGlobal }
}).reduce((acc, i) => {
// eslint-disable-next-line no-return-assign
Object.keys(i).forEach(k => acc[k] = [...(acc[k] || []), ...i[k]])
return acc
}, {})
const testString = tests.join('')
.replace(/%beforeEachGlobal%/g, beforeEachGlobal.join(''))
.replace(/%beforeGlobal%/g, beforeGlobal.join(''))
.replace(/%afterEachGlobal%/g, afterEachGlobal.join(''))
.replace(/%afterGlobal%/g, afterGlobal.join(''))
testFileString += `const count = ${testCount};\nlet sum = 0;\n${addedMethods.join('')}\n${beforeAll.join('')}\n${testString}\n${afterAll.join('')};
return { sum, count }`
testFileString += '\n}\n'
// add text to end of file
testFileString += `test().then(async ({ sum, count }) => {
aggregate(sum, count);
process.exit(sum + await check(${!!options.strict || (options.clean && '"clean"')}))
});`
writeFileSync(tmp, testFileString)
if (child) child.kill()
// run the file
if (forkProcess) {
const program = options.bun ? 'bun' : 'node'
child = spawn(program, [tmp], {
stdio: ['pipe', 'inherit', 'inherit'],
env: pickBy(identity, process.env)
})
} else if (options.bun) {
child = spawn('bun', [tmp], {
stdio: ['pipe', 'inherit', 'inherit'],
env: pickBy(identity, process.env)
})
child.on('exit', (code) => {
process.exit(code || undefined)
})
} else await import(url.pathToFileURL(tmp).href)
}
/**
* Gets each of the functions from the file & figures out if they have associated pineapple
* annotations.
*/
function getFunctions (fileText, fileName) {
let onlyLines = null
if (options.only && options.only.length) {
onlyLines = new Set(options.only.map(i => {
const index = i.lastIndexOf(':')
const fileRequested = i.substring(0, index)
return fileName === fileRequested ? +i.substring(index + 1) : null
}).filter(i => i))
if (!onlyLines.size) return []
}
const functions = parseCode(fileText).map(i => ({
...i,
fileName,
isClass: i.type === 'class',
relativePath: fileName.startsWith(cwd) ? fileName.substring(cwd.length + 1) : ''
})).filter(i => {
if (onlyLines) {
i.tags = i.tags.filter(tag => {
if (tag.type !== 'test' && tag.type !== 'test_static') return true
return onlyLines.has(tag.lineNo)
})
}
if (!i.tags.length) return false
if (!i.exported) skippingTest(i.name, i.fileName, i.tags)
return i.exported
})
return functions
}
main()