markdownlint-rule-relative-.../test/index.test.js

195 lines
7.3 KiB
JavaScript
Raw Normal View History

const { test } = require("node:test")
const assert = require("node:assert/strict")
const { markdownlint } = require("markdownlint").promises
const relativeLinksRule = require("../src/index.js")
/**
*
* @param {string} fixtureFile
* @returns
*/
const validateMarkdownLint = async (fixtureFile) => {
const lintResults = await markdownlint({
files: [fixtureFile],
config: {
default: false,
"relative-links": true,
},
customRules: [relativeLinksRule],
})
return lintResults[fixtureFile]
}
test("ensure the rule validates correctly", async (t) => {
await t.test("should be invalid", async (t) => {
2024-01-12 00:33:05 +01:00
const testCases = [
{
name: "should be invalid with an empty id fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md",
error: '"./awesome.md#" should have a valid fragment identifier',
},
{
name: "should be invalid with a name fragment other than for an anchor",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md",
error:
'"./awesome.md#name-should-be-ignored" should have a valid fragment identifier',
},
{
name: "should be invalid with a non-existing id fragment (data-id !== id)",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md",
error:
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
},
{
name: "should be invalid with invalid heading with #L fragment",
fixturePath:
"test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md",
error: '"./awesome.md#L7abc" should have a valid fragment identifier',
},
{
name: "should be invalid with a invalid line number fragment",
fixturePath:
"test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md",
error:
'"./awesome.md#L7" should have a valid fragment identifier, "./awesome.md#L7" should have at least 7 lines to be valid',
},
2024-01-12 00:33:05 +01:00
{
name: "should be invalid with a non-existing anchor name fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md",
error:
'"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier',
},
{
name: "should be invalid with a non-existing element id fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md",
error:
'"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier',
},
{
name: "should be invalid with a non-existing heading fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
error:
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
},
{
name: "should be invalid with a link to an image with a empty fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md",
error:
'"../image.png#" should not have a fragment identifier as it is an image',
},
{
name: "should be invalid with a link to an image with a fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/invalid/ignore-fragment-checking-for-image.md",
error:
'"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image',
},
{
name: "should be invalid with a non-existing file",
2024-01-12 00:33:05 +01:00
fixturePath: "test/fixtures/invalid/non-existing-file.md",
error: '"./index.test.js" should exist in the file system',
},
{
name: "should be invalid with a non-existing image",
2024-01-12 00:33:05 +01:00
fixturePath: "test/fixtures/invalid/non-existing-image.md",
error: '"./image.png" should exist in the file system',
},
]
for (const { name, fixturePath, error } of testCases) {
await t.test(name, async () => {
const lintResults = await validateMarkdownLint(fixturePath)
assert.equal(lintResults?.length, 1)
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
2024-01-11 14:42:34 +01:00
assert.equal(
2024-01-12 00:33:05 +01:00
lintResults?.[0]?.ruleDescription,
2024-01-11 14:42:34 +01:00
relativeLinksRule.description,
)
2024-01-12 00:33:05 +01:00
assert.equal(lintResults?.[0]?.errorDetail, error)
})
}
})
2024-01-12 00:33:05 +01:00
await t.test("should be valid", async (t) => {
const testCases = [
{
name: "should be valid with an existing anchor name fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md",
},
{
name: "should be valid with an existing element id fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
},
{
name: "should be valid with an existing heading fragment (case insensitive)",
fixturePath:
"test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md",
},
2024-01-12 00:33:05 +01:00
{
name: "should be valid with an existing heading fragment",
2024-01-12 00:33:05 +01:00
fixturePath:
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
},
{
name: 'should be valid with an existing heading fragment with accents (e.g: "é")',
fixturePath:
"test/fixtures/valid/existing-heading-with-accents/existing-heading-with-accents.md",
},
{
name: "should only parse markdown files for fragments checking",
fixturePath:
"test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md",
},
{
name: 'should be valid with valid heading "like" line number fragment',
fixturePath:
"test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md",
},
{
name: "should be valid with valid line number fragment",
fixturePath:
"test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md",
},
2024-01-12 00:33:05 +01:00
{
name: "should be valid with an existing file",
2024-01-12 00:33:05 +01:00
fixturePath: "test/fixtures/valid/existing-file.md",
},
{
name: "should be valid with an existing image",
2024-01-12 00:33:05 +01:00
fixturePath: "test/fixtures/valid/existing-image.md",
},
{
name: "should ignore absolute paths",
fixturePath: "test/fixtures/valid/ignore-absolute-paths.md",
},
{
name: "should ignore external links",
fixturePath: "test/fixtures/valid/ignore-external-links.md",
},
{
name: "should ignore checking fragment in own file",
fixturePath:
"test/fixtures/valid/ignore-fragment-checking-in-own-file.md",
},
]
2024-01-12 00:33:05 +01:00
for (const { name, fixturePath } of testCases) {
await t.test(name, async () => {
const lintResults = await validateMarkdownLint(fixturePath)
assert.equal(lintResults?.length, 0)
})
}
})
})