|
1 | 1 | import test from 'ava' |
2 | 2 | import { sync } from 'glob' |
3 | | -import { readFile } from 'mz/fs' |
| 3 | +import { readFile, writeFile } from 'mz/fs' |
4 | 4 | import { basename, resolve } from 'path' |
5 | 5 | import { compile } from '../src' |
6 | 6 |
|
7 | 7 | let paths = ['e2e', 'rules', 'unit'] |
8 | 8 |
|
| 9 | +// Kind of hacky thing to figure out if -u or --update-snapshots was passed to AVA. |
| 10 | +// We manually write expected output in this test. |
| 11 | +// It's more clear, and allows us to have input and output files for each test. |
| 12 | +const argvString = process.env.npm_config_argv as string |
| 13 | +const argv = JSON.parse(argvString || '') |
| 14 | +const argvOriginal = argv.original || [] |
| 15 | +const update = |
| 16 | + argvOriginal.includes('-u') || argvOriginal.includes('--update-snapshots') |
| 17 | + |
9 | 18 | paths.forEach(path => { |
10 | 19 | // TODO: Why does glob catch tslint.json even with the trailing slash? |
11 | | - let folders = sync(resolve(__dirname, `../../test/${path}/*/`)) |
| 20 | + const folders = sync(resolve(__dirname, `../../test/${path}/*/`)) |
12 | 21 | .filter(_ => !_.endsWith('.json')) |
13 | 22 | .filter(_ => !basename(_).startsWith('_')) |
14 | 23 |
|
15 | | - folders.forEach(folder => |
16 | | - test(basename(folder), async t => { |
| 24 | + const tests = folders.map(folder => ({ folder, name: basename(folder) })) |
| 25 | + |
| 26 | + tests.forEach(({ name, folder }) => |
| 27 | + test(name, async t => { |
17 | 28 | try { |
18 | | - let filein = resolve(folder, 'input.txt') |
19 | | - let input = await readFile(filein, 'utf-8') |
20 | | - let output = await readFile(resolve(folder, 'output.txt'), 'utf-8') |
21 | | - t.is(await compile(input, filein), output) |
| 29 | + const inputPath = resolve(folder, 'input.txt') |
| 30 | + const outputPath = resolve(folder, 'output.txt') |
| 31 | + const input = await readFile(inputPath, 'utf-8') |
| 32 | + const output = await compile(input, inputPath) |
| 33 | + if (update) { |
| 34 | + await writeFile(outputPath, output) |
| 35 | + t.pass() |
| 36 | + } else { |
| 37 | + const expectedOutput = await readFile(outputPath, 'utf-8') |
| 38 | + t.is(output, expectedOutput) |
| 39 | + } |
22 | 40 | } catch (e) { |
23 | 41 | console.log('error', e) |
24 | 42 | } |
|
0 commit comments