Skip to content

Commit

Permalink
Make tests granular
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Feb 17, 2022
1 parent a959970 commit f7b6cb7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 0 additions & 8 deletions test/fixtures/other-branches.md

This file was deleted.

28 changes: 0 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})
93 changes: 93 additions & 0 deletions test/other-branches.js
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit f7b6cb7

Please sign in to comment.