|
2 | 2 | * @typedef {import('../index.js').Options} Options
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import fs from 'node:fs' |
6 |
| -import path from 'node:path' |
7 |
| -import test from 'tape' |
| 5 | +import assert from 'node:assert/strict' |
| 6 | +import fs from 'node:fs/promises' |
| 7 | +import process from 'node:process' |
| 8 | +import test from 'node:test' |
8 | 9 | import {remark} from 'remark'
|
9 | 10 | import {isHidden} from 'is-hidden'
|
10 | 11 | import remarkToc from '../index.js'
|
11 | 12 |
|
12 |
| -test('Fixtures', (t) => { |
13 |
| - const root = path.join('test', 'fixtures') |
14 |
| - const fixtures = fs.readdirSync(root) |
| 13 | +test('remarkToc', async function (t) { |
| 14 | + const base = new URL('fixtures/', import.meta.url) |
| 15 | + const folders = await fs.readdir(base) |
| 16 | + |
15 | 17 | let index = -1
|
16 | 18 |
|
17 |
| - while (++index < fixtures.length) { |
18 |
| - const fixture = fixtures[index] |
| 19 | + while (++index < folders.length) { |
| 20 | + const folder = folders[index] |
| 21 | + |
| 22 | + if (isHidden(folder)) continue |
19 | 23 |
|
20 |
| - if (isHidden(fixture)) { |
21 |
| - continue |
22 |
| - } |
| 24 | + await t.test(folder, async function () { |
| 25 | + const folderUrl = new URL(folder + '/', base) |
| 26 | + const inputUrl = new URL('input.md', folderUrl) |
| 27 | + const outputUrl = new URL('output.md', folderUrl) |
| 28 | + const configUrl = new URL('config.json', folderUrl) |
23 | 29 |
|
24 |
| - /** @type {Options} */ |
25 |
| - let config = {} |
| 30 | + const input = String(await fs.readFile(inputUrl)) |
26 | 31 |
|
27 |
| - try { |
28 |
| - config = JSON.parse( |
29 |
| - String(fs.readFileSync(path.join(root, fixture, 'config.json'))) |
30 |
| - ) |
31 |
| - } catch {} |
| 32 | + /** @type {Options | undefined} */ |
| 33 | + let config |
| 34 | + /** @type {string} */ |
| 35 | + let output |
32 | 36 |
|
33 |
| - t.equal( |
34 |
| - remark() |
| 37 | + try { |
| 38 | + config = JSON.parse(String(await fs.readFile(configUrl))) |
| 39 | + } catch {} |
| 40 | + |
| 41 | + const proc = remark() |
| 42 | + // To do: use defaults. |
35 | 43 | .use({settings: {bullet: '-'}})
|
| 44 | + // @ts-expect-error: to do: fix type. |
36 | 45 | .use(remarkToc, config)
|
37 |
| - .processSync(fs.readFileSync(path.join(root, fixture, 'input.md'))) |
38 |
| - .toString(), |
39 |
| - String(fs.readFileSync(path.join(root, fixture, 'output.md'))), |
40 |
| - 'should work on `' + fixture + '`' |
41 |
| - ) |
42 |
| - } |
43 | 46 |
|
44 |
| - t.end() |
| 47 | + const actual = String(await proc.process(input)) |
| 48 | + |
| 49 | + try { |
| 50 | + if ('UPDATE' in process.env) { |
| 51 | + throw new Error('Updating…') |
| 52 | + } |
| 53 | + |
| 54 | + output = String(await fs.readFile(outputUrl)) |
| 55 | + } catch { |
| 56 | + output = actual |
| 57 | + await fs.writeFile(outputUrl, actual) |
| 58 | + } |
| 59 | + |
| 60 | + assert.equal(actual, String(output)) |
| 61 | + }) |
| 62 | + } |
45 | 63 | })
|
0 commit comments