mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2024-11-03 18:11:31 +01:00
test: separate cases, so it's easier to know what fails
This commit is contained in:
parent
1ddcdc7b18
commit
7465ffd8bc
@ -6,6 +6,6 @@
|
||||
"MD033": false
|
||||
},
|
||||
"globs": ["**/*.{md,mdx}"],
|
||||
"ignores": ["**/node_modules", "**/test/fixtures"],
|
||||
"ignores": ["**/node_modules", "**/test/fixtures/**"],
|
||||
"customRules": ["./src/index.js"]
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -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',
|
||||
)
|
||||
})
|
19
test/fixtures/Invalid.md
vendored
19
test/fixtures/Invalid.md
vendored
@ -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
|
21
test/fixtures/Valid.md
vendored
21
test/fixtures/Valid.md
vendored
@ -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)
|
3
test/fixtures/invalid/non-existing-file.md
vendored
Normal file
3
test/fixtures/invalid/non-existing-file.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Invalid
|
||||
|
||||
[File](./index.test.js)
|
3
test/fixtures/invalid/non-existing-heading-fragment/awesome.md
vendored
Normal file
3
test/fixtures/invalid/non-existing-heading-fragment/awesome.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
## Existing Heading
|
3
test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md
vendored
Normal file
3
test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Invalid
|
||||
|
||||
[Link fragment](./awesome.md#non-existing-heading)
|
3
test/fixtures/invalid/non-existing-image.md
vendored
Normal file
3
test/fixtures/invalid/non-existing-image.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Invalid
|
||||
|
||||
![Image](./image.png)
|
3
test/fixtures/valid/existing-file.md
vendored
Normal file
3
test/fixtures/valid/existing-file.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[File](../../index.test.js)
|
13
test/fixtures/valid/existing-heading-fragment/awesome.md
vendored
Normal file
13
test/fixtures/valid/existing-heading-fragment/awesome.md
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
# Valid
|
||||
|
||||
## Existing Heading
|
||||
|
||||
### Repeated Heading
|
||||
|
||||
Text
|
||||
|
||||
### Repeated Heading
|
||||
|
||||
Text
|
||||
|
||||
### Repeated Heading
|
9
test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md
vendored
Normal file
9
test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md
vendored
Normal file
@ -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)
|
3
test/fixtures/valid/existing-image.md
vendored
Normal file
3
test/fixtures/valid/existing-image.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
![Image](./image.png)
|
3
test/fixtures/valid/ignore-absolute-paths.md
vendored
Normal file
3
test/fixtures/valid/ignore-absolute-paths.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
![Absolute Path](/absolute/path.png)
|
7
test/fixtures/valid/ignore-external-links.md
vendored
Normal file
7
test/fixtures/valid/ignore-external-links.md
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
# Valid
|
||||
|
||||
[External https link](https://example.com/)
|
||||
|
||||
[External https link 2](https:./external.https)
|
||||
|
||||
[External ftp link](ftp:./external.ftp)
|
Before Width: | Height: | Size: 95 B After Width: | Height: | Size: 95 B |
112
test/index.test.js
Normal file
112
test/index.test.js
Normal file
@ -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',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user