forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfront-matter_linter.test.js
More file actions
116 lines (97 loc) · 4.02 KB
/
Copy pathfront-matter_linter.test.js
File metadata and controls
116 lines (97 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import test from "node:test";
import assert from "node:assert/strict";
import {
getAjvValidator,
checkFrontMatter,
} from "../scripts/front-matter_utils.js";
const SAMPLES_DIRECTORY = new URL("front-matter_test_files/", import.meta.url);
const options = {};
options.config = JSON.parse(
fs.readFileSync(
fileURLToPath(new URL("config.json", SAMPLES_DIRECTORY)),
"utf-8",
),
);
options.validator = getAjvValidator(options.config.schema);
function getPath(filePath) {
return fileURLToPath(new URL(filePath, SAMPLES_DIRECTORY));
}
function getContent(filePath) {
return fs.readFileSync(getPath(filePath), "utf-8");
}
test("Front-matter linter", async (t) => {
await t.test(
"should use double quotes and remove unwanted quotes",
async () => {
const filePath = getPath("./double_quotes.md");
const validContent = getContent("./double_quotes.valid.md");
options.fix = true;
const result = await checkFrontMatter(filePath, options);
assert.deepStrictEqual(result, [null, null, validContent]);
},
);
await t.test(
"should use single quotes to enclose double quoted words",
async () => {
const filePath = getPath("./single_quotes.md");
const validContent = getContent("./single_quotes.valid.md");
options.fix = true;
const result = await checkFrontMatter(filePath, options);
assert.deepStrictEqual(result, [null, null, validContent]);
},
);
await t.test("should enforce the attribute order", async () => {
const filePath = getPath("./attribute_order.md");
const validContent = getContent("./attribute_order.valid.md");
options.fix = false;
let result = await checkFrontMatter(filePath, options);
const expected =
"tests/front-matter_test_files/attribute_order.md\n\t " +
"Front matter attributes are not in required order: " +
"title->short-title->slug->page-type->status->browser-compat->spec-urls";
assert.deepStrictEqual(result, [null, expected, null]);
options.fix = true;
result = await checkFrontMatter(filePath, options);
assert.deepStrictEqual(result, [null, null, validContent]);
});
await t.test("should flag invalid values", async () => {
const filePath = getPath("./values.md");
options.fix = false;
const result = await checkFrontMatter(filePath, options);
const expected =
"Error: tests/front-matter_test_files/values.md\n" +
"'page-type' property must be equal to one of the allowed values:\n" +
"\tlanding-page, guide, web-api-method\n" +
"Front matter must match 'then' schema\n" +
"Front matter must have required property 'slug'\n" +
"'0' property must be equal to one of the allowed values:\n" +
"\tdeprecated, experimental, non-standard\n" +
"property 'spec-urls' must match format 'uri'";
assert.deepStrictEqual(result, [expected, null, null]);
});
await t.test("should prettify the front-matter", async () => {
const filePath = getPath("./prettify.md");
const validContent = getContent("./prettify.valid.md");
options.fix = true;
const result = await checkFrontMatter(filePath, options);
const expected =
"Error: tests/front-matter_test_files/prettify.md\n" +
"property 'title' must not have more than 120 characters";
assert.deepStrictEqual(result, [expected, null, validContent]);
});
await t.test("should flag and remove unknown attribute", async () => {
const filePath = getPath("./unknown_attribute.md");
const validContent = getContent("./unknown_attribute.valid.md");
options.fix = false;
let result = await checkFrontMatter(filePath, options);
const expected =
"Error: tests/front-matter_test_files/unknown_attribute.md\n" +
"'tags' property is not expected to be here";
assert.deepStrictEqual(result, [expected, null, null]);
options.fix = true;
result = await checkFrontMatter(filePath, options);
assert.deepStrictEqual(result, [expected, null, validContent]);
});
});