-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
149 lines (120 loc) · 3.44 KB
/
test.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const test = require('tape')
const remark = require('remark')
const lint = require('remark-lint')
const vfile = require('vfile')
const appropriateHeading = require('./')
const path = require('path')
/**
* Creates file based on given options, runs remark-lint and returns resulting messages.
*
* @function
* @param {VFileOptions} file
* @param {string} [options]
* @returns {string[]}
*/
const processFile = (file, options) =>
remark().use(lint).use(appropriateHeading, options)
.processSync(vfile(file))
.messages.map(String)
const ok = `# Heading
`
const invalidHeading = `# Invalid
`
const missingHeading = `Paragraph
`
const tooLowHeading = `
# Heading
`
const empty = ``
const multiWordHeading = `# Multi-Word Heading
`
const disabledHeading = `<!--lint disable appropriate-heading-->
Paragraph
`
const allowHtmlComment = `<!-- some comment-->
# Heading
`
const allowHtmlCommentAndOneNewLine = `<!-- some comment-->
# Heading
`
const tooManyNewLinesAfterHtmlComment = `<!-- some comment-->
# Heading
`
test('remark-lint-appropriate-heading', (t) => {
var fp = path.join('~', 'heading', 'readme.md')
var mwfp = path.join('~', 'multi-word-heading', 'readme.md')
t.deepEqual(
processFile({ path: fp, contents: ok }),
[],
'should work on valid fixtures'
)
t.deepEqual(
processFile({ path: mwfp, contents: multiWordHeading }, 'slug'),
[],
'should work on valid fixtures'
)
t.deepEqual(
processFile({ path: fp, contents: invalidHeading }),
[`${fp}:1:1-1:10: Heading 'invalid' is not the directory name`],
'should warn for invalid headings (mismatch in directory and heading)'
)
t.deepEqual(
processFile({ path: mwfp, contents: multiWordHeading }),
[`${mwfp}:1:1-1:21: Heading 'multi-word heading' is not the directory name`],
'should warn for invalid headings (mismatch in directory and heading)'
)
t.deepEqual(
processFile({ path: fp, contents: missingHeading }),
[`${fp}:1:1-1:10: Document must start with a heading`],
'should warn if the first thing is not a heading'
)
t.deepEqual(
processFile({ path: fp, contents: tooLowHeading }),
[`${fp}:2:1-2:10: Heading does not start at beginning of document`],
'should warn if the first heading is not on the first line'
)
t.deepEqual(
processFile({ path: fp, contents: empty }),
[`${fp}:1:1-1:1: Document must start with a heading`],
'should warn without heading'
)
t.deepEqual(
processFile({ path: fp, contents: disabledHeading }),
[],
'should work when disabling rule'
)
t.deepEqual(
processFile({ path: fp, contents: allowHtmlComment }),
[],
'should work with html comment'
)
t.deepEqual(
processFile({ path: fp, contents: allowHtmlCommentAndOneNewLine }),
[],
'should work with html comment'
)
t.deepEqual(
processFile({ path: fp, contents: tooManyNewLinesAfterHtmlComment }),
[`${fp}:4:1-4:10: Heading does not start at beginning of document`],
'should warn if too much newlines after html comment'
)
t.deepEqual(
processFile({
path: path.join('~', '@scope', 'package', 'README.md'),
contents: `# package
`
}),
[],
'should work for scoped npm packages, non-breaking, so scope is optional in header'
)
t.deepEqual(
processFile({
path: path.join('~', '@scope', 'package', 'README.md'),
contents: `# @scope/package
`
}),
[],
'should work for scoped npm packages'
)
t.end()
})