/
test.js
322 lines (289 loc) · 10.5 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
import fs from 'fs';
import path from 'path';
import test from 'ava';
import execa from 'execa';
const errorPattern = /(\.md|\.markdown|stdin): \d+: MD\d{3}/gm;
test('--version option', async t => {
const result = await execa('../markdownlint.js', ['--version']);
t.true(/^\d+\.\d+\.\d+$/.test(result.stdout));
t.true(result.stderr === '');
});
test('--help option', async t => {
const result = await execa('../markdownlint.js', ['--help']);
t.true(result.stdout.indexOf('markdownlint') >= 0);
t.true(result.stdout.indexOf('--version') >= 0);
t.true(result.stdout.indexOf('--help') >= 0);
t.true(result.stderr === '');
});
test('no files shows help', async t => {
const result = await execa('../markdownlint.js', []);
t.true(result.stdout.indexOf('--help') >= 0);
t.true(result.stderr === '');
});
test('files and --stdin shows help', async t => {
const result = await execa('../markdownlint.js', ['--stdin', 'correct.md']);
t.true(result.stdout.indexOf('--help') >= 0);
t.true(result.stderr === '');
});
test('linting of correct Markdown file yields no output', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', 'correct.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('linting of correct Markdown file yields no output with absolute path', async t => {
const result = await execa('../markdownlint.js',
['--config', path.resolve('test-config.json'), 'correct.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('linting of incorrect Markdown file fails', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', 'incorrect.md']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 8);
}
});
test('linting of incorrect Markdown file fails with absolute path', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', path.resolve('incorrect.md')]);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 8);
}
});
test('linting of incorrect Markdown via npm run file fails with eol', async t => {
try {
await execa('npm', ['run', 'invalid']);
t.fail();
} catch (err) {
t.true(/\nnpm ERR! code ELIFECYCLE/.test(err.stderr));
}
});
test('glob linting works with passing files', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', '**/correct.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('glob linting works with failing files', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 16);
}
});
test('dir linting works with passing .markdown files', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-correct']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('dir linting works with failing .markdown files', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-incorrect']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 10);
}
});
test('dir linting works with failing .markdown files and absolute path', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', path.resolve('subdir-incorrect')]);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 10);
}
});
test('glob linting with failing files passes when failures ignored by glob', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', '**/incorrect.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('glob linting with failing files passes when everything ignored by glob', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', '**/*']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('glob linting with failing files has fewer errors when ignored by dir', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', 'subdir-incorrect']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 8);
}
});
test('glob linting with failing files has fewer errors when ignored by dir and absolute path', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*.md', '--ignore', path.resolve('subdir-incorrect')]);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 8);
}
});
test('dir linting with failing files has fewer errors when ignored by file', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-incorrect', '--ignore', 'subdir-incorrect/incorrect.md']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 2);
}
});
test('dir linting with failing files has fewer errors when ignored by file and absolute path', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', path.resolve('subdir-incorrect'), '--ignore', 'subdir-incorrect/incorrect.md']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 2);
}
});
test('glob linting with failing files passes when ignored by multiple globs', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', 'subdir-incorrect', '--ignore', '**/*.md', '--ignore', '**/*.markdown']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('linting results are sorted by file/line/names/description', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', 'incorrect.md']);
t.fail();
} catch (err) {
const expected = [
'incorrect.md: 1: MD002/first-heading-h1/first-header-h1 First heading should be a top level heading [Expected: h1; Actual: h2]',
'incorrect.md: 1: MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Context: "## header 2"]',
'incorrect.md: 1: MD041/first-line-h1 First line in file should be a top level heading [Context: "## header 2"]',
'incorrect.md: 2: MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Context: "# header"]',
'incorrect.md: 4: MD014/commands-show-output Dollar signs used before commands without showing output [Context: "$ code"]',
'incorrect.md: 10: MD014/commands-show-output Dollar signs used before commands without showing output [Context: "$ code"]',
'incorrect.md: 16: MD014/commands-show-output Dollar signs used before commands without showing output [Context: "$ code"]',
'incorrect.md: 23: MD014/commands-show-output Dollar signs used before commands without showing output [Context: "$ code"]',
''
].join('\n');
t.true(err.stdout === '');
t.true(err.stderr === expected);
}
});
test('glob linting does not try to lint directories as files', async t => {
try {
await execa('../markdownlint.js',
['--config', 'test-config.json', '**/*']);
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length > 0);
}
});
test('--stdin with empty input has no output', async t => {
var input = '';
const result = await execa('../markdownlint.js', ['--stdin'], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('--stdin with valid input has no output', async t => {
var input = [
'# Heading',
'',
'Text'
].join('\n');
const result = await execa('../markdownlint.js', ['--stdin'], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('--stdin with invalid input reports violations', async t => {
var input = [
'Heading',
'',
'Text '
].join('\n');
try {
await execa('../markdownlint.js', ['--stdin'], {input: input});
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 2);
}
});
test('stdin support does not interfere with file linting', async t => {
const result = await execa('../markdownlint.js',
['--config', 'md043-config.json', 'md043-config.md']);
t.true(result.stdout === '');
t.true(result.stderr === '');
});
test('--output with empty input has empty output', async t => {
var input = '';
var output = '../outputA.txt';
const result = await execa('../markdownlint.js',
['--stdin', '--output', output], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
t.true(fs.readFileSync(output, 'utf8') === '');
fs.unlinkSync(output);
});
test('--output with valid input has empty output', async t => {
var input = [
'# Heading',
'',
'Text'
].join('\n');
var output = '../outputB.txt';
const result = await execa('../markdownlint.js',
['--stdin', '--output', output], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
t.true(fs.readFileSync(output, 'utf8') === '');
fs.unlinkSync(output);
});
test('--output with invalid input outputs violations', async t => {
var input = [
'Heading',
'',
'Text '
].join('\n');
var output = '../outputC.txt';
try {
await execa('../markdownlint.js', ['--stdin', '--output', output], {input: input});
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr === '');
t.true(fs.readFileSync(output, 'utf8').match(errorPattern).length === 2);
fs.unlinkSync(output);
}
});
test('--output with invalid path fails', async t => {
var input = '';
var output = 'invalid/outputD.txt';
try {
await execa('../markdownlint.js',
['--stdin', '--output', output], {input: input});
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.replace(/: ENOENT[^]*$/, '') === 'Cannot write to output file ' + output);
t.throws(() => fs.accessSync(output, 'utf8'), Error);
}
});