From 0479652ffe6cbfd53dcbedba15638611cbb677ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Fri, 12 Jan 2024 00:33:05 +0100 Subject: [PATCH] test: add cases for fragments checking --- test/fixtures/{valid => }/image.png | Bin .../invalid/empty-id-fragment/awesome.md | 3 + .../empty-id-fragment/empty-id-fragment.md | 3 + ...gnore-empty-fragment-checking-for-image.md | 3 + .../ignore-fragment-checking-for-image.md | 3 + .../awesome.md | 3 + .../ignore-name-fragment-if-not-an-anchor.md | 3 + .../ignore-not-an-id-fragment/awesome.md | 3 + .../ignore-not-an-id-fragment.md | 3 + .../awesome.md | 3 + .../non-existing-anchor-name-fragment.md | 3 + .../awesome.md | 3 + .../non-existing-element-id-fragment.md | 3 + .../non-existing-heading-fragment/awesome.md | 4 +- .../non-existing-heading-fragment.md | 2 - .../existing-anchor-name-fragment/awesome.md | 3 + .../existing-anchor-name-fragment.md | 3 + .../existing-element-id-fragment/awesome.md | 3 + .../existing-element-id-fragment.md | 3 + .../existing-heading-fragment/awesome.md | 2 - .../existing-heading-fragment.md | 2 - test/fixtures/valid/existing-image.md | 2 +- .../ignore-fragment-checking-in-own-file.md | 5 + test/index.test.js | 208 +++++++++++------- test/utils.test.js | 8 +- 25 files changed, 183 insertions(+), 98 deletions(-) rename test/fixtures/{valid => }/image.png (100%) create mode 100644 test/fixtures/invalid/empty-id-fragment/awesome.md create mode 100644 test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md create mode 100644 test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md create mode 100644 test/fixtures/invalid/ignore-fragment-checking-for-image.md create mode 100644 test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/awesome.md create mode 100644 test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md create mode 100644 test/fixtures/invalid/ignore-not-an-id-fragment/awesome.md create mode 100644 test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md create mode 100644 test/fixtures/invalid/non-existing-anchor-name-fragment/awesome.md create mode 100644 test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md create mode 100644 test/fixtures/invalid/non-existing-element-id-fragment/awesome.md create mode 100644 test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md create mode 100644 test/fixtures/valid/existing-anchor-name-fragment/awesome.md create mode 100644 test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md create mode 100644 test/fixtures/valid/existing-element-id-fragment/awesome.md create mode 100644 test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md create mode 100644 test/fixtures/valid/ignore-fragment-checking-in-own-file.md diff --git a/test/fixtures/valid/image.png b/test/fixtures/image.png similarity index 100% rename from test/fixtures/valid/image.png rename to test/fixtures/image.png diff --git a/test/fixtures/invalid/empty-id-fragment/awesome.md b/test/fixtures/invalid/empty-id-fragment/awesome.md new file mode 100644 index 0000000..23f938d --- /dev/null +++ b/test/fixtures/invalid/empty-id-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +
Content
diff --git a/test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md b/test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md new file mode 100644 index 0000000..9578eb5 --- /dev/null +++ b/test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#) diff --git a/test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md b/test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md new file mode 100644 index 0000000..7406f43 --- /dev/null +++ b/test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md @@ -0,0 +1,3 @@ +# Invalid + +![Image](../image.png#) diff --git a/test/fixtures/invalid/ignore-fragment-checking-for-image.md b/test/fixtures/invalid/ignore-fragment-checking-for-image.md new file mode 100644 index 0000000..f07a307 --- /dev/null +++ b/test/fixtures/invalid/ignore-fragment-checking-for-image.md @@ -0,0 +1,3 @@ +# Invalid + +![Image](../image.png#non-existing-fragment) diff --git a/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/awesome.md b/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/awesome.md new file mode 100644 index 0000000..9df5196 --- /dev/null +++ b/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/awesome.md @@ -0,0 +1,3 @@ +# Awesome + + diff --git a/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md b/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md new file mode 100644 index 0000000..b629896 --- /dev/null +++ b/test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md @@ -0,0 +1,3 @@ +# Invalid + +[Invalid](./awesome.md#name-should-be-ignored) diff --git a/test/fixtures/invalid/ignore-not-an-id-fragment/awesome.md b/test/fixtures/invalid/ignore-not-an-id-fragment/awesome.md new file mode 100644 index 0000000..c07397d --- /dev/null +++ b/test/fixtures/invalid/ignore-not-an-id-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +
Content
diff --git a/test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md b/test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md new file mode 100644 index 0000000..a991643 --- /dev/null +++ b/test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Invalid](./awesome.md#not-an-id-should-be-ignored) diff --git a/test/fixtures/invalid/non-existing-anchor-name-fragment/awesome.md b/test/fixtures/invalid/non-existing-anchor-name-fragment/awesome.md new file mode 100644 index 0000000..51200e5 --- /dev/null +++ b/test/fixtures/invalid/non-existing-anchor-name-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +Link diff --git a/test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md b/test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md new file mode 100644 index 0000000..9482958 --- /dev/null +++ b/test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#non-existing-anchor-name-fragment) diff --git a/test/fixtures/invalid/non-existing-element-id-fragment/awesome.md b/test/fixtures/invalid/non-existing-element-id-fragment/awesome.md new file mode 100644 index 0000000..1341249 --- /dev/null +++ b/test/fixtures/invalid/non-existing-element-id-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +
Content
diff --git a/test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md b/test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md new file mode 100644 index 0000000..914511e --- /dev/null +++ b/test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#non-existing-element-id-fragment) diff --git a/test/fixtures/invalid/non-existing-heading-fragment/awesome.md b/test/fixtures/invalid/non-existing-heading-fragment/awesome.md index a2eeff7..11bda72 100644 --- a/test/fixtures/invalid/non-existing-heading-fragment/awesome.md +++ b/test/fixtures/invalid/non-existing-heading-fragment/awesome.md @@ -1,5 +1,3 @@ -# Valid - - +# Awesome ## Existing Heading diff --git a/test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md b/test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md index 90c0589..cd01aef 100644 --- a/test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md +++ b/test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md @@ -1,5 +1,3 @@ # Invalid [Link fragment](./awesome.md#non-existing-heading) - -[Link fragment](./awesome.md#non-existing-heading-anchor) diff --git a/test/fixtures/valid/existing-anchor-name-fragment/awesome.md b/test/fixtures/valid/existing-anchor-name-fragment/awesome.md new file mode 100644 index 0000000..51200e5 --- /dev/null +++ b/test/fixtures/valid/existing-anchor-name-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +Link diff --git a/test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md b/test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md new file mode 100644 index 0000000..60de204 --- /dev/null +++ b/test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#existing-heading-anchor) diff --git a/test/fixtures/valid/existing-element-id-fragment/awesome.md b/test/fixtures/valid/existing-element-id-fragment/awesome.md new file mode 100644 index 0000000..1341249 --- /dev/null +++ b/test/fixtures/valid/existing-element-id-fragment/awesome.md @@ -0,0 +1,3 @@ +# Awesome + +
Content
diff --git a/test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md b/test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md new file mode 100644 index 0000000..7a4df08 --- /dev/null +++ b/test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#existing-element-id-fragment) diff --git a/test/fixtures/valid/existing-heading-fragment/awesome.md b/test/fixtures/valid/existing-heading-fragment/awesome.md index 374381f..0bc1d5c 100644 --- a/test/fixtures/valid/existing-heading-fragment/awesome.md +++ b/test/fixtures/valid/existing-heading-fragment/awesome.md @@ -1,7 +1,5 @@ # Valid - - ## Existing Heading ### Repeated Heading diff --git a/test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md b/test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md index e16e91a..15dcd71 100644 --- a/test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md +++ b/test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md @@ -2,8 +2,6 @@ [Link fragment](./awesome.md#existing-heading) -[Link fragment](./awesome.md#existing-heading-anchor) - [Link fragment Repeated 0](./awesome.md#repeated-heading) [Link fragment Repeated 1](./awesome.md#repeated-heading-1) diff --git a/test/fixtures/valid/existing-image.md b/test/fixtures/valid/existing-image.md index 7606df7..08c0c06 100644 --- a/test/fixtures/valid/existing-image.md +++ b/test/fixtures/valid/existing-image.md @@ -1,3 +1,3 @@ # Valid -![Image](./image.png) +![Image](../image.png) diff --git a/test/fixtures/valid/ignore-fragment-checking-in-own-file.md b/test/fixtures/valid/ignore-fragment-checking-in-own-file.md new file mode 100644 index 0000000..ef2e345 --- /dev/null +++ b/test/fixtures/valid/ignore-fragment-checking-in-own-file.md @@ -0,0 +1,5 @@ +# Valid + +
Content
+ +[Link fragment](#non-existing-element-id-fragment) diff --git a/test/index.test.js b/test/index.test.js index 7c15d71..451d728 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -23,96 +23,134 @@ const validateMarkdownLint = async (fixtureFile) => { } test("ensure the rule validates correctly", async (t) => { - await t.test("should be valid", async (t) => { - await t.test("with an existing heading fragment", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md", - ) - assert.equal(lintResults?.length, 0) - }) - - await t.test("with an existing file", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/valid/existing-file.md", - ) - assert.equal(lintResults?.length, 0) - }) - - await t.test("with an existing image", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/valid/existing-image.md", - ) - assert.equal(lintResults?.length, 0) - }) - - await t.test("should ignore absolute paths", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/valid/ignore-absolute-paths.md", - ) - assert.equal(lintResults?.length, 0) - }) - - await t.test("should ignore external links", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/valid/ignore-external-links.md", - ) - assert.equal(lintResults?.length, 0) - }) - }) - await t.test("should be invalid", async (t) => { - await t.test("with a non-existing heading fragment", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md", - ) - assert.equal(lintResults?.length, 2) - for (let i = 0; i < 2; i++) { - assert.deepEqual(lintResults?.[i]?.ruleNames, relativeLinksRule.names) + const testCases = [ + { + name: "with an empty id fragment", + fixturePath: + "test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md", + error: '"./awesome.md#" should have a valid fragment identifier', + }, + { + name: "with a name fragment other than for an anchor", + 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: "with a non-existing id fragment (data-id !== id)", + 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: "with a non-existing anchor name fragment", + 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: "with a non-existing element id fragment", + 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: "with a non-existing heading fragment", + 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: "with a link to an image with a empty fragment", + 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: "with a link to an image with a fragment", + 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: "with a non-existing file", + fixturePath: "test/fixtures/invalid/non-existing-file.md", + error: '"./index.test.js" should exist in the file system', + }, + { + name: "with a non-existing image", + 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) assert.equal( - lintResults?.[i]?.ruleDescription, + lintResults?.[0]?.ruleDescription, relativeLinksRule.description, ) - } - assert.equal( - lintResults?.[0]?.errorDetail, - '"./awesome.md#non-existing-heading" should have a valid fragment identifier', - ) - assert.equal( - lintResults?.[1]?.errorDetail, - '"./awesome.md#non-existing-heading-anchor" should have a valid fragment identifier', - ) - }) + assert.equal(lintResults?.[0]?.errorDetail, error) + }) + } + }) - await t.test("with a non-existing file", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/invalid/non-existing-file.md", - ) - assert.equal(lintResults?.length, 1) - assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) - assert.equal( - lintResults?.[0]?.ruleDescription, - relativeLinksRule.description, - ) - assert.equal( - lintResults?.[0]?.errorDetail, - '"./index.test.js" should exist in the file system', - ) - }) + await t.test("should be valid", async (t) => { + const testCases = [ + { + name: "with an existing anchor name fragment", + fixturePath: + "test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md", + }, + { + name: "with an existing element id fragment", + fixturePath: + "test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md", + }, + { + name: "with an existing heading fragment", + fixturePath: + "test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md", + }, + { + name: "with an existing file", + fixturePath: "test/fixtures/valid/existing-file.md", + }, + { + name: "with an existing image", + 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", + }, + ] - await t.test("with a non-existing image", async () => { - const lintResults = await validateMarkdownLint( - "test/fixtures/invalid/non-existing-image.md", - ) - assert.equal(lintResults?.length, 1) - assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) - assert.equal( - lintResults?.[0]?.ruleDescription, - relativeLinksRule.description, - ) - assert.equal( - lintResults?.[0]?.errorDetail, - '"./image.png" should exist in the file system', - ) - }) + for (const { name, fixturePath } of testCases) { + await t.test(name, async () => { + const lintResults = await validateMarkdownLint(fixturePath) + assert.equal(lintResults?.length, 0) + }) + } }) }) diff --git a/test/utils.test.js b/test/utils.test.js index 4af8fc7..7d35c4b 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -38,14 +38,16 @@ test("utils", async (t) => { await t.test("getMarkdownAnchorHTMLFragments", async () => { assert.deepStrictEqual( - getMarkdownAnchorHTMLFragments(''), + getMarkdownAnchorHTMLFragments( + 'Link', + ), ["#anchorId"], ) assert.deepStrictEqual( - getMarkdownAnchorHTMLFragments(''), + getMarkdownAnchorHTMLFragments('Link'), ["#anchorName"], ) - assert.deepStrictEqual(getMarkdownAnchorHTMLFragments(""), []) + assert.deepStrictEqual(getMarkdownAnchorHTMLFragments("Link"), []) assert.deepStrictEqual(getMarkdownAnchorHTMLFragments(""), []) }) })