diff --git a/package.json b/package.json index c7b8506..cdeea62 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "scripts": { "build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage", "format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix", - "test-api": "node --conditions development test/index.js", + "test-api": "tape test/index.js test/other-branches.js", "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api", "test": "npm run build && npm run format && npm run test-coverage" }, diff --git a/test/fixtures/other-branches.md b/test/fixtures/other-branches.md deleted file mode 100644 index 2f7432c..0000000 --- a/test/fixtures/other-branches.md +++ /dev/null @@ -1,8 +0,0 @@ -# Links to other branches - -- [Same branch, working link](https://github.com/wooorm/test/blob/main/examples/github.md#hello): Nothing to report. -- [Other branch, working link](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#hello): Nothing to ignore. -- [Same branch, no such heading](https://github.com/wooorm/test/blob/main/examples/github.md#world): Should be reported. -- [Other branch, no such heading](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#world): Should be ignored. -- [Same branch, no such file](https://github.com/wooorm/test/blob/main/examples/world.md#hello): Should be reported. -- [Other branch, no such file](https://github.com/wooorm/test/blob/foo-bar/examples/world.md#hello): Should be ignored. diff --git a/test/index.js b/test/index.js index 458a84a..ad0655f 100644 --- a/test/index.js +++ b/test/index.js @@ -1006,31 +1006,3 @@ test('remark-validate-links', async (t) => { t.end() }) - -test('links to other branches', async (t) => { - const {stderr} = await exec( - [ - bin, - '--no-config', - '--no-ignore', - '--use', - '"../../index.js=repository:\\"wooorm/test#main\\""', - '--use', - '../sort.js', - 'other-branches.md' - ].join(' ') - ) - t.deepEqual( - strip(stderr), - [ - 'other-branches.md', - ' 5:3-5:100 warning Link to unknown heading in `examples/github.md`: `world` missing-heading-in-file remark-validate-links', - ' 7:3-7:96 warning Link to unknown file: `examples/world.md` missing-file remark-validate-links', - ' 7:3-7:96 warning Link to unknown heading in `examples/world.md`: `hello` missing-heading-in-file remark-validate-links', - '', - '⚠ 3 warnings', - '' - ].join('\n'), - 'should ignore links to other branches' - ) -}) diff --git a/test/other-branches.js b/test/other-branches.js new file mode 100644 index 0000000..f173d43 --- /dev/null +++ b/test/other-branches.js @@ -0,0 +1,93 @@ +import {remark} from 'remark' +import test from 'tape' +import {engine as engineCb} from 'unified-engine' +import {promisify} from 'node:util' +import {VFile} from 'vfile' +import remarkValidateLinks from '../index.js' + +const engine = promisify(engineCb) + +// Should ignore links to other branches. +const options = {repository: 'https://github.com/wooorm/test.git#main'} + +test('same branch, working link', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/main/examples/github.md#hello)' + ), + [], + 'nothing to report' + ) +}) + +test('other branch, working link', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#hello)' + ), + [], + 'nothing to ignore' + ) +}) + +test('same branch, no such heading', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/main/examples/github.md#world)' + ), + [ + 'input.md:1:1-1:70: Link to unknown heading in `examples/github.md`: `world`' + ], + 'should be reported' + ) +}) + +test('other branch, no such heading', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#world)' + ), + [], + 'should be ignored' + ) +}) + +test('same branch, no such file', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/main/examples/world.md#hello)' + ), + [ + 'input.md:1:1-1:69: Link to unknown file: `examples/world.md`', + 'input.md:1:1-1:69: Link to unknown heading in `examples/world.md`: `hello`' + ], + 'should be reported' + ) +}) + +test('other branch, no such file', async (t) => { + t.deepEqual( + await process( + '[](https://github.com/wooorm/test/blob/foo-bar/examples/world.md#hello)' + ), + [], + 'should be ignored' + ) +}) + +/** + * Run the plugin on a markdown test case and return a list of stringified messages. + * unified-engine is needed to cross-reference links between files, by creating a FileSet. + * + * @param {string} value + */ +async function process(value) { + const file = new VFile({value, path: 'input.md'}) + await engine({ + processor: remark, + files: [file], + plugins: [[remarkValidateLinks, options]], + silent: true + }) + return file.messages.map((message) => String(message)) +}