|
| 1 | +import { exec } from 'node:child_process' |
| 2 | +import { promises as fs } from 'node:fs' |
| 3 | +import path from 'node:path' |
| 4 | +import test, { snapshot, suite } from 'node:test' |
| 5 | + |
| 6 | +// All examples will be tested with no additional command-line arguments. To |
| 7 | +// also test with specific sets of arguments, add them here: |
| 8 | +const additionalCommandLineArguments: Readonly< |
| 9 | + Record<string, readonly string[]> |
| 10 | +> = { |
| 11 | + 'fibonacci.plz': [ |
| 12 | + '--input=0', |
| 13 | + '--input=1', |
| 14 | + '--input=2', |
| 15 | + '--input=10', |
| 16 | + '--input="not a number"', |
| 17 | + '--input=-1', |
| 18 | + ], |
| 19 | +} |
| 20 | + |
| 21 | +snapshot.setResolveSnapshotPath(_ => |
| 22 | + path.join(import.meta.dirname, '..', 'examples', '.snapshot'), |
| 23 | +) |
| 24 | + |
| 25 | +const exampleDirectoryPath = path.join(import.meta.dirname, '..', 'examples') |
| 26 | +const pleasePath = path.join( |
| 27 | + import.meta.dirname, |
| 28 | + 'language', |
| 29 | + 'cli', |
| 30 | + 'please.js', |
| 31 | +) |
| 32 | + |
| 33 | +suite('examples', async () => { |
| 34 | + const exampleFileNames = (await fs.readdir(exampleDirectoryPath)).filter( |
| 35 | + fileName => fileName.endsWith('.plz'), |
| 36 | + ) |
| 37 | + for (const exampleFileName of exampleFileNames) { |
| 38 | + const setsOfCommandLineArguments = [ |
| 39 | + '', // Always test with no arguments. |
| 40 | + ...(additionalCommandLineArguments[exampleFileName] ?? []), |
| 41 | + ] as const |
| 42 | + |
| 43 | + for (const commandLineArguments of setsOfCommandLineArguments) { |
| 44 | + const exampleFilePath = path.join(exampleDirectoryPath, exampleFileName) |
| 45 | + test( |
| 46 | + exampleFileName.concat( |
| 47 | + commandLineArguments === '' ? '' : ` ${commandLineArguments}`, |
| 48 | + ), |
| 49 | + _ => |
| 50 | + new Promise((resolve, reject) => { |
| 51 | + const _childProcess = exec( |
| 52 | + `cat "${exampleFilePath}" | node "${pleasePath}" --no-color --output-format=plz ${commandLineArguments}`, |
| 53 | + (error, stdout, stderr) => { |
| 54 | + // `error` is an `ExecException` when exit status is nonzero. |
| 55 | + if (error !== null) { |
| 56 | + reject(error) |
| 57 | + } else { |
| 58 | + Promise.all([ |
| 59 | + test('stdout', t => |
| 60 | + t.assert.snapshot(stdout, snapshotOptions)), |
| 61 | + test('stderr', t => |
| 62 | + t.assert.snapshot(stderr, snapshotOptions)), |
| 63 | + ]) |
| 64 | + .then(_ => resolve(undefined)) |
| 65 | + .catch(reject) |
| 66 | + } |
| 67 | + }, |
| 68 | + ) |
| 69 | + }), |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | +}) |
| 74 | + |
| 75 | +// Expect snapshots to already be strings. |
| 76 | +const snapshotOptions = { |
| 77 | + serializers: [ |
| 78 | + (value: unknown) => { |
| 79 | + if (typeof value !== 'string') { |
| 80 | + throw new Error( |
| 81 | + `snapshot was not a string (was \`${JSON.stringify(value)}\`)`, |
| 82 | + ) |
| 83 | + } else { |
| 84 | + return value |
| 85 | + } |
| 86 | + }, |
| 87 | + ], |
| 88 | +} |
0 commit comments