|
| 1 | +import path from 'path' |
| 2 | + |
| 3 | +import { afterAll, beforeAll, describe, expect, test } from 'vitest' |
| 4 | + |
| 5 | +import { runRule } from '../../lib/init-test' |
| 6 | +import { frontmatterIntroLinks } from '../../lib/linting-rules/frontmatter-intro-links' |
| 7 | + |
| 8 | +const fmOptions = { markdownlintOptions: { frontMatter: null } } |
| 9 | + |
| 10 | +describe(frontmatterIntroLinks.names.join(' - '), () => { |
| 11 | + const envVarValueBefore = process.env.ROOT |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + process.env.ROOT = path.join('src', 'fixtures', 'fixtures') |
| 15 | + }) |
| 16 | + |
| 17 | + afterAll(() => { |
| 18 | + process.env.ROOT = envVarValueBefore |
| 19 | + }) |
| 20 | + |
| 21 | + test('valid introLinks keys pass', async () => { |
| 22 | + const markdown = [ |
| 23 | + '---', |
| 24 | + 'title: Test', |
| 25 | + 'introLinks:', |
| 26 | + ' overview: /path/to/overview', |
| 27 | + ' quickstart: /path/to/quickstart', |
| 28 | + '---', |
| 29 | + '', |
| 30 | + '# Test', |
| 31 | + ].join('\n') |
| 32 | + const result = await runRule(frontmatterIntroLinks, { |
| 33 | + strings: { 'content/test/index.md': markdown }, |
| 34 | + ...fmOptions, |
| 35 | + }) |
| 36 | + const errors = result['content/test/index.md'] |
| 37 | + expect(errors.length).toBe(0) |
| 38 | + }) |
| 39 | + |
| 40 | + test('missing introLinks is ignored', async () => { |
| 41 | + const markdown = ['---', 'title: Test', '---', '', '# Test'].join('\n') |
| 42 | + const result = await runRule(frontmatterIntroLinks, { |
| 43 | + strings: { 'content/test/index.md': markdown }, |
| 44 | + ...fmOptions, |
| 45 | + }) |
| 46 | + const errors = result['content/test/index.md'] |
| 47 | + expect(errors.length).toBe(0) |
| 48 | + }) |
| 49 | + |
| 50 | + test('invalid introLinks key fails', async () => { |
| 51 | + const markdown = [ |
| 52 | + '---', |
| 53 | + 'title: Test', |
| 54 | + 'introLinks:', |
| 55 | + ' overview: /path/to/overview', |
| 56 | + ' invalidKey: /path/to/invalid', |
| 57 | + '---', |
| 58 | + '', |
| 59 | + '# Test', |
| 60 | + ].join('\n') |
| 61 | + const result = await runRule(frontmatterIntroLinks, { |
| 62 | + strings: { 'content/test/index.md': markdown }, |
| 63 | + ...fmOptions, |
| 64 | + }) |
| 65 | + const errors = result['content/test/index.md'] |
| 66 | + expect(errors.length).toBe(1) |
| 67 | + expect(errors[0].errorDetail).toContain('Invalid introLinks key') |
| 68 | + expect(errors[0].errorDetail).toContain('invalidKey') |
| 69 | + }) |
| 70 | + |
| 71 | + test('multiple invalid introLinks keys fail', async () => { |
| 72 | + const markdown = [ |
| 73 | + '---', |
| 74 | + 'title: Test', |
| 75 | + 'introLinks:', |
| 76 | + ' invalidKey1: /path/to/invalid1', |
| 77 | + ' invalidKey2: /path/to/invalid2', |
| 78 | + '---', |
| 79 | + '', |
| 80 | + '# Test', |
| 81 | + ].join('\n') |
| 82 | + const result = await runRule(frontmatterIntroLinks, { |
| 83 | + strings: { 'content/test/index.md': markdown }, |
| 84 | + ...fmOptions, |
| 85 | + }) |
| 86 | + const errors = result['content/test/index.md'] |
| 87 | + expect(errors.length).toBe(2) |
| 88 | + }) |
| 89 | + |
| 90 | + test('all common valid introLinks keys pass', async () => { |
| 91 | + const markdown = [ |
| 92 | + '---', |
| 93 | + 'title: Test', |
| 94 | + 'introLinks:', |
| 95 | + ' overview: /path/to/overview', |
| 96 | + ' quickstart: /path/to/quickstart', |
| 97 | + ' reference: /path/to/reference', |
| 98 | + '---', |
| 99 | + '', |
| 100 | + '# Test', |
| 101 | + ].join('\n') |
| 102 | + const result = await runRule(frontmatterIntroLinks, { |
| 103 | + strings: { 'content/test/index.md': markdown }, |
| 104 | + ...fmOptions, |
| 105 | + }) |
| 106 | + const errors = result['content/test/index.md'] |
| 107 | + expect(errors.length).toBe(0) |
| 108 | + }) |
| 109 | + |
| 110 | + test('non-object introLinks is ignored', async () => { |
| 111 | + const markdown = [ |
| 112 | + '---', |
| 113 | + 'title: Test', |
| 114 | + 'introLinks: this is not an object', |
| 115 | + '---', |
| 116 | + '', |
| 117 | + '# Test', |
| 118 | + ].join('\n') |
| 119 | + const result = await runRule(frontmatterIntroLinks, { |
| 120 | + strings: { 'content/test/index.md': markdown }, |
| 121 | + ...fmOptions, |
| 122 | + }) |
| 123 | + const errors = result['content/test/index.md'] |
| 124 | + expect(errors.length).toBe(0) |
| 125 | + }) |
| 126 | +}) |
0 commit comments