From 7465ffd8bc59e9aa3502f64825c7227dd5de8f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 10 Jan 2024 00:01:13 +0100 Subject: [PATCH] test: separate cases, so it's easier to know what fails --- .markdownlint-cli2.jsonc | 2 +- package.json | 2 +- test/basic.test.js | 46 ------- test/fixtures/Invalid.md | 19 --- test/fixtures/Valid.md | 21 ---- test/fixtures/invalid/non-existing-file.md | 3 + .../non-existing-heading-fragment/awesome.md | 3 + .../non-existing-heading-fragment.md | 3 + test/fixtures/invalid/non-existing-image.md | 3 + test/fixtures/valid/existing-file.md | 3 + .../existing-heading-fragment/awesome.md | 13 ++ .../existing-heading-fragment.md | 9 ++ test/fixtures/valid/existing-image.md | 3 + test/fixtures/valid/ignore-absolute-paths.md | 3 + test/fixtures/valid/ignore-external-links.md | 7 ++ test/fixtures/{ => valid}/image.png | Bin test/index.test.js | 112 ++++++++++++++++++ 17 files changed, 164 insertions(+), 88 deletions(-) delete mode 100644 test/basic.test.js delete mode 100644 test/fixtures/Invalid.md delete mode 100644 test/fixtures/Valid.md create mode 100644 test/fixtures/invalid/non-existing-file.md create mode 100644 test/fixtures/invalid/non-existing-heading-fragment/awesome.md create mode 100644 test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md create mode 100644 test/fixtures/invalid/non-existing-image.md create mode 100644 test/fixtures/valid/existing-file.md create mode 100644 test/fixtures/valid/existing-heading-fragment/awesome.md create mode 100644 test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md create mode 100644 test/fixtures/valid/existing-image.md create mode 100644 test/fixtures/valid/ignore-absolute-paths.md create mode 100644 test/fixtures/valid/ignore-external-links.md rename test/fixtures/{ => valid}/image.png (100%) create mode 100644 test/index.test.js diff --git a/.markdownlint-cli2.jsonc b/.markdownlint-cli2.jsonc index 47ddae8..3d46c8b 100644 --- a/.markdownlint-cli2.jsonc +++ b/.markdownlint-cli2.jsonc @@ -6,6 +6,6 @@ "MD033": false }, "globs": ["**/*.{md,mdx}"], - "ignores": ["**/node_modules", "**/test/fixtures"], + "ignores": ["**/node_modules", "**/test/fixtures/**"], "customRules": ["./src/index.js"] } diff --git a/package.json b/package.json index 07ded7d..d993b4f 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "lint:prettier": "prettier . --check --ignore-path .gitignore", "lint:javascript": "tsc --project jsconfig.json --noEmit", "lint:staged": "lint-staged", - "test": "node --test ./test", + "test": "node --test --experimental-test-coverage ./test", "release": "semantic-release", "postinstall": "husky install", "prepublishOnly": "pinst --disable", diff --git a/test/basic.test.js b/test/basic.test.js deleted file mode 100644 index 80897ad..0000000 --- a/test/basic.test.js +++ /dev/null @@ -1,46 +0,0 @@ -const { test } = require("node:test") -const assert = require("node:assert/strict") - -const { markdownlint } = require("markdownlint").promises - -const relativeLinks = require("../src/index.js") - -test("ensure the rule validate correctly", async () => { - const lintResults = await markdownlint({ - files: ["test/fixtures/Valid.md", "test/fixtures/Invalid.md"], - config: { - default: false, - "relative-links": true, - }, - customRules: [relativeLinks], - }) - assert.equal(lintResults["test/fixtures/Valid.md"]?.length, 0) - assert.equal(lintResults["test/fixtures/Invalid.md"]?.length, 3) - - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[0]?.ruleDescription, - "Relative links should be valid", - ) - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[0]?.errorDetail, - '"./basic.test.js" should exist in the file system', - ) - - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[1]?.ruleDescription, - "Relative links should be valid", - ) - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[1]?.errorDetail, - '"../image.png" should exist in the file system', - ) - - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[2]?.ruleDescription, - "Relative links should be valid", - ) - assert.equal( - lintResults["test/fixtures/Invalid.md"]?.[2]?.errorDetail, - '"./Valid.md#not-existing-heading" should have a valid fragment identifier', - ) -}) diff --git a/test/fixtures/Invalid.md b/test/fixtures/Invalid.md deleted file mode 100644 index 8d02f99..0000000 --- a/test/fixtures/Invalid.md +++ /dev/null @@ -1,19 +0,0 @@ -# Invalid - -[basic.js](./basic.test.js) - -![Image](../image.png) - -[Link fragment](./Valid.md#not-existing-heading) - -## Existing Heading - -### Repeated Heading - -Text - -### Repeated Heading - -Text - -### Repeated Heading diff --git a/test/fixtures/Valid.md b/test/fixtures/Valid.md deleted file mode 100644 index 3daf9e8..0000000 --- a/test/fixtures/Valid.md +++ /dev/null @@ -1,21 +0,0 @@ -# Valid - -[basic.js](../basic.test.js) - -![Image](./image.png) - -![Absolute Path](/absolute/path.png) - -[External https link](https://example.com/) - -[External https link 2](https:./external.https) - -[External ftp link](ftp:./external.ftp) - -[Link fragment](./Invalid.md#existing-heading) - -[Link fragment Repeated 0](./Invalid.md#repeated-heading) - -[Link fragment Repeated 1](./Invalid.md#repeated-heading-1) - -[Link fragment Repeated 2](./Invalid.md#repeated-heading-2) diff --git a/test/fixtures/invalid/non-existing-file.md b/test/fixtures/invalid/non-existing-file.md new file mode 100644 index 0000000..30b1aba --- /dev/null +++ b/test/fixtures/invalid/non-existing-file.md @@ -0,0 +1,3 @@ +# Invalid + +[File](./index.test.js) diff --git a/test/fixtures/invalid/non-existing-heading-fragment/awesome.md b/test/fixtures/invalid/non-existing-heading-fragment/awesome.md new file mode 100644 index 0000000..6b3a7d5 --- /dev/null +++ b/test/fixtures/invalid/non-existing-heading-fragment/awesome.md @@ -0,0 +1,3 @@ +# Valid + +## 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 new file mode 100644 index 0000000..cd01aef --- /dev/null +++ b/test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md @@ -0,0 +1,3 @@ +# Invalid + +[Link fragment](./awesome.md#non-existing-heading) diff --git a/test/fixtures/invalid/non-existing-image.md b/test/fixtures/invalid/non-existing-image.md new file mode 100644 index 0000000..b789b9a --- /dev/null +++ b/test/fixtures/invalid/non-existing-image.md @@ -0,0 +1,3 @@ +# Invalid + +![Image](./image.png) diff --git a/test/fixtures/valid/existing-file.md b/test/fixtures/valid/existing-file.md new file mode 100644 index 0000000..c6e8641 --- /dev/null +++ b/test/fixtures/valid/existing-file.md @@ -0,0 +1,3 @@ +# Valid + +[File](../../index.test.js) diff --git a/test/fixtures/valid/existing-heading-fragment/awesome.md b/test/fixtures/valid/existing-heading-fragment/awesome.md new file mode 100644 index 0000000..0bc1d5c --- /dev/null +++ b/test/fixtures/valid/existing-heading-fragment/awesome.md @@ -0,0 +1,13 @@ +# Valid + +## Existing Heading + +### Repeated Heading + +Text + +### Repeated Heading + +Text + +### 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 new file mode 100644 index 0000000..15dcd71 --- /dev/null +++ b/test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md @@ -0,0 +1,9 @@ +# Valid + +[Link fragment](./awesome.md#existing-heading) + +[Link fragment Repeated 0](./awesome.md#repeated-heading) + +[Link fragment Repeated 1](./awesome.md#repeated-heading-1) + +[Link fragment Repeated 2](./awesome.md#repeated-heading-2) diff --git a/test/fixtures/valid/existing-image.md b/test/fixtures/valid/existing-image.md new file mode 100644 index 0000000..7606df7 --- /dev/null +++ b/test/fixtures/valid/existing-image.md @@ -0,0 +1,3 @@ +# Valid + +![Image](./image.png) diff --git a/test/fixtures/valid/ignore-absolute-paths.md b/test/fixtures/valid/ignore-absolute-paths.md new file mode 100644 index 0000000..fffcc23 --- /dev/null +++ b/test/fixtures/valid/ignore-absolute-paths.md @@ -0,0 +1,3 @@ +# Valid + +![Absolute Path](/absolute/path.png) diff --git a/test/fixtures/valid/ignore-external-links.md b/test/fixtures/valid/ignore-external-links.md new file mode 100644 index 0000000..e4c7a61 --- /dev/null +++ b/test/fixtures/valid/ignore-external-links.md @@ -0,0 +1,7 @@ +# Valid + +[External https link](https://example.com/) + +[External https link 2](https:./external.https) + +[External ftp link](ftp:./external.ftp) diff --git a/test/fixtures/image.png b/test/fixtures/valid/image.png similarity index 100% rename from test/fixtures/image.png rename to test/fixtures/valid/image.png diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..3993fec --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,112 @@ +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 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, 1) + assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) + assert.equal( + lintResults?.[0]?.ruleDescription, + relativeLinksRule.description, + ) + assert.equal( + lintResults?.[0]?.errorDetail, + '"./awesome.md#non-existing-heading" should have a valid fragment identifier', + ) + }) + + 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("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', + ) + }) + }) +})