|
1 | 1 | import test from 'ava' |
2 | 2 |
|
3 | | -import { extractLinks } from '../index' |
| 3 | +import { extractLinks, extractLinksFromFile } from '../index' |
4 | 4 |
|
5 | 5 | const dedent = (s: string) => s.replace('\n ', '') |
6 | 6 |
|
@@ -108,3 +108,49 @@ test('extractLinks: appropriate jsx error message', (t) => { |
108 | 108 | t.is(error.name, 'Error') |
109 | 109 | t.is(error.message, '1:13: Expected a closing tag for `<Admonition>` (1:1) (markdown-rs:end-tag-mismatch)') |
110 | 110 | }) |
| 111 | + |
| 112 | +test('extractLinksFromFile: mdx file', async (t) => { |
| 113 | + const links = await extractLinksFromFile('__test__/fixtures/markdown.mdx') |
| 114 | + t.deepEqual(links, ['/path']) |
| 115 | +}) |
| 116 | + |
| 117 | +test('extractLinksFromFile: notebook', async (t) => { |
| 118 | + const links = (await extractLinksFromFile('__test__/fixtures/markdown.ipynb')).sort() |
| 119 | + t.deepEqual(links, ['/path', '/path2'].sort()) |
| 120 | +}) |
| 121 | + |
| 122 | +test('extractLinksFromFile: markdown file not found', async (t) => { |
| 123 | + const error = await t.throwsAsync( |
| 124 | + async () => await extractLinksFromFile('__test__/fixtures/file_that_does_not_exist.md'), |
| 125 | + ) |
| 126 | + t.is(error.name, 'Error') |
| 127 | + |
| 128 | + // The error message changes depending on OS, but both are acceptable |
| 129 | + const acceptableMessages = [ |
| 130 | + 'Could not read "__test__/fixtures/file_that_does_not_exist.md": No such file or directory (os error 2)', |
| 131 | + 'Could not read "__test__/fixtures/file_that_does_not_exist.md": The system cannot find the file specified. (os error 2)', |
| 132 | + ] |
| 133 | + t.assert(acceptableMessages.includes(error.message)) |
| 134 | +}) |
| 135 | + |
| 136 | +test('extractLinksFromFile: invalid notebook (not JSON)', async (t) => { |
| 137 | + const error = await t.throwsAsync( |
| 138 | + async () => await extractLinksFromFile('__test__/fixtures/invalid-notebook-json.ipynb'), |
| 139 | + ) |
| 140 | + t.is(error.name, 'Error') |
| 141 | + t.is( |
| 142 | + error.message, |
| 143 | + 'Could not read "__test__/fixtures/invalid-notebook-json.ipynb": trailing comma at line 7 column 7', |
| 144 | + ) |
| 145 | +}) |
| 146 | + |
| 147 | +test('extractLinksFromFile: invalid notebook (bad schema)', async (t) => { |
| 148 | + const error = await t.throwsAsync( |
| 149 | + async () => await extractLinksFromFile('__test__/fixtures/invalid-notebook-schema.ipynb'), |
| 150 | + ) |
| 151 | + t.is(error.name, 'Error') |
| 152 | + t.is( |
| 153 | + error.message, |
| 154 | + 'Could not read "__test__/fixtures/invalid-notebook-schema.ipynb": missing field `source` at line 10 column 5', |
| 155 | + ) |
| 156 | +}) |
0 commit comments