/
format.spec.js
228 lines (203 loc) · 6.26 KB
/
format.spec.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
/* eslint-env mocha */
'use strict';
const { createVinylFile, noop } = require('./test-util');
const { strict: assert } = require('assert');
const { ESLint } = require('eslint');
const eslint = require('gulp-eslint-new');
const { PassThrough } = require('stream');
const File = require('vinyl');
function getFiles() {
return [
new File({
path: process.cwd(),
contents: null,
isDirectory: true
}),
createVinylFile(
'use-strict.js',
'(function () {\n\n\tvoid 0;\n\n}());\n\n'
),
createVinylFile(
'undeclared.js',
'(function () {\n\t"use strict";\n\n\tx = 0;\n\n}());\n'
),
createVinylFile(
'passing.js',
'(function () {\n\n\t"use strict";\n\n}());\n'
)
];
}
describe('gulp-eslint-new format function', () => {
let formatCount;
let writeCount;
/**
* Custom ESLint formatted result writer for counting write attempts rather than writing to the console.
*
* @param {string} message - A message to count as written.
*/
function outputWriter(message) {
assert.match(message, /^1 message|\d+ messages$/);
writeCount++;
}
describe('format', () => {
/**
* Custom ESLint result formatter for counting format passes and
* returning an expected formatted result message.
*
* @param {Array} results - ESLint results
* @param {Object} config - format config
* @returns {string} formatted results
*/
function formatResults(results, config) {
assert(config);
assert(Array.isArray(results));
assert.equal(results.length, 3);
formatCount++;
const messageCount = results.reduce((sum, result) => {
return sum + result.messages.length;
}, 0);
return `${messageCount} ${messageCount === 1 ? 'message' : 'messages'}`;
}
beforeEach(() => {
formatCount = 0;
writeCount = 0;
});
it('should format all ESLint results at once', done => {
const files = getFiles();
const formatStream = eslint
.format(formatResults, outputWriter)
.on('error', done)
.on('finish', () => {
assert.equal(formatCount, 1);
assert.equal(writeCount, 1);
done();
});
const lintStream
= eslint({ useEslintrc: false, rules: { 'strict': 2 } }).on('error', done);
lintStream
.pipe(eslint.format(noop, noop)) // Test that files are passed through.
.pipe(formatStream);
files.forEach(file => lintStream.write(file));
lintStream.end();
});
it('should not attempt to format when no linting results are found', done => {
const files = getFiles();
const formatStream = eslint
.format(formatResults, outputWriter)
.on('error', done)
.on('finish', () => {
assert.equal(formatCount, 0);
assert.equal(writeCount, 0);
done();
});
const passthruStream = new PassThrough({ objectMode: true }).on('error', done);
passthruStream.pipe(formatStream);
files.forEach(file => passthruStream.write(file));
passthruStream.end();
});
it('should emit an error if a linted file has no ESLint instance', done => {
const file = createVinylFile('file.js', '');
file.eslint = { };
eslint
.format()
.on('error', err => {
assert.equal(err.fileName, file.path);
assert.equal(err.message, 'ESLint information not available');
assert.equal(err.plugin, 'gulp-eslint-new');
done();
})
.end(file);
});
it('should emit an error if the linted files have different ESLint instances', done => {
const formatStream = eslint
.format()
.on('error', err => {
assert.equal(
err.message,
'The files in the stream were not processes by the same instance of '
+ 'ESLint'
);
assert.equal(err.plugin, 'gulp-eslint-new');
done();
});
function addFile(path) {
const file = createVinylFile(path, '');
file.eslint = { };
file._eslintInfo = { cwd: process.cwd(), eslint: new ESLint() };
formatStream.write(file);
}
addFile('file1.js');
addFile('file2.js');
formatStream.end();
});
});
describe('formatEach', () => {
function formatResult(results, config) {
assert(config);
assert(Array.isArray(results));
assert.equal(results.length, 1);
formatCount++;
const messageCount = results.reduce((sum, result) => sum + result.messages.length, 0);
return `${messageCount} ${messageCount === 1 ? 'message' : 'messages'}`;
}
it('should format individual ESLint results', done => {
formatCount = 0;
writeCount = 0;
const files = getFiles();
const lintStream
= eslint({ useEslintrc: false, rules: { 'strict': 2 } }).on('error', done);
lintStream
.pipe(eslint.formatEach(noop, noop)) // Test that files are passed through.
.pipe(
eslint.formatEach(formatResult, outputWriter)
.on('error', done)
.on('finish', function () {
// The stream should not have emitted an error.
assert.equal(this._writableState.errorEmitted, false);
const fileCount = files.length - 1; // Remove directory.
assert.equal(formatCount, fileCount);
assert.equal(writeCount, fileCount);
done();
})
);
files.forEach(file => lintStream.write(file));
lintStream.end();
});
it('should catch and wrap format writer errors in a PluginError', done => {
const files = getFiles();
const lintStream
= eslint({ useEslintrc: false, rules: { 'strict': 2 } }).on('error', done);
const testMessage = 'Writer Test Error';
const testErrorName = 'TestError';
lintStream.pipe(
eslint.formatEach(formatResult, message => {
assert.equal(message, '1 message');
const error = Error(testMessage);
error.name = testErrorName;
throw error;
})
.on('error', err => {
assert.equal(err.message, testMessage);
assert.equal(err.name, testErrorName);
assert.equal(err.plugin, 'gulp-eslint-new');
done();
})
);
files.forEach(file => lintStream.write(file));
lintStream.end();
});
it('should emit an error if a linted file has no ESLint instance', done => {
const file = createVinylFile('file.js', '');
file.eslint = { };
eslint
.formatEach()
.on('error', err => {
assert.equal(err.fileName, file.path);
assert.equal(err.message, 'ESLint information not available');
assert.equal(err.plugin, 'gulp-eslint-new');
done();
})
.end(file);
});
});
});