-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_syntax.js
241 lines (197 loc) · 7.06 KB
/
test_syntax.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
var path = require('path');
var fs = require('fs');
var falafel = require('falafel');
var glob = require('glob');
var madge = require('madge');
var readLastLines = require('read-last-lines');
var eslint = require('eslint');
var constants = require('./util/constants');
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
var libGlob = path.join(constants.pathToLib, '**/*.js');
var testGlob = path.join(constants.pathToJasmineTests, '**/*.js');
var bundleTestGlob = path.join(constants.pathToJasmineBundleTests, '**/*.js');
var EXIT_CODE = 0;
// main
assertJasmineSuites();
assertSrcContents();
assertFileNames();
assertTrailingNewLine();
assertCircularDeps();
assertES5();
// check for for focus and exclude jasmine blocks
function assertJasmineSuites() {
var BLACK_LIST = ['fdescribe', 'fit', 'xdescribe', 'xit'];
var logs = [];
glob(combineGlobs([testGlob, bundleTestGlob]), function(err, files) {
files.forEach(function(file) {
var code = fs.readFileSync(file, 'utf-8');
falafel(code, {locations: true}, function(node) {
if(node.type === 'Identifier' && BLACK_LIST.indexOf(node.name) !== -1) {
logs.push([
path.basename(file),
'[line ' + node.loc.start.line + '] :',
'contains either a *fdescribe*, *fit*,',
'*xdescribe* or *xit* block.'
].join(' '));
}
});
});
log('no jasmine suites focus/exclude blocks', logs);
});
}
/*
* tests about the contents of source (and lib) files:
* - check for header comment
* - check that we don't have .classList
*/
function assertSrcContents() {
var licenseSrc = constants.licenseSrc;
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
var logs = [];
glob(combineGlobs([srcGlob, libGlob]), function(err, files) {
files.forEach(function(file) {
var code = fs.readFileSync(file, 'utf-8');
// parse through code string while keeping track of comments
var comments = [];
falafel(code, {onComment: comments, locations: true}, function(node) {
// look for .classList
if(node.type === 'MemberExpression') {
var parts = node.source().split('.');
if(parts[parts.length - 1] === 'classList') {
logs.push(file + ' : contains .classList (IE failure)');
}
}
});
var header = comments[0];
if(!header || header.loc.start.line > 1) {
logs.push(file + ' : has no header information.');
return;
}
if(header.value !== licenseStr) {
logs.push(file + ' : has incorrect header information.');
}
});
log('correct headers and contents in lib/ and src/', logs);
});
}
// check that all file names are in lower case
function assertFileNames() {
var pattern = combineGlobs([
path.join(constants.pathToRoot, '*.*'),
path.join(constants.pathToSrc, '**/*.*'),
path.join(constants.pathToLib, '**/*.*'),
path.join(constants.pathToDist, '**/*.*'),
path.join(constants.pathToRoot, 'test', '**/*.*'),
path.join(constants.pathToRoot, 'tasks', '**/*.*'),
path.join(constants.pathToRoot, 'devtools', '**/*.*')
]);
var logs = [];
glob(pattern, function(err, files) {
files.forEach(function(file) {
var base = path.basename(file);
if(
base === 'README.md' ||
base === 'CONTRIBUTING.md' ||
base === 'CHANGELOG.md' ||
base === 'SECURITY.md' ||
file.indexOf('mathjax') !== -1
) return;
if(base !== base.toLowerCase()) {
logs.push([
file, ':',
'has a file name containing some',
'non-lower-case characters'
].join(' '));
}
});
log('lower case only file names', logs);
});
}
// check that all files have a trailing new line character
function assertTrailingNewLine() {
var pattern = combineGlobs([
path.join(constants.pathToSrc, '**/*.glsl'),
path.join(constants.pathToRoot, 'test', 'image', 'mocks', '*')
]);
var regexNewLine = /\r?\n$/;
var regexEmptyNewLine = /^\r?\n$/;
var promises = [];
var logs = [];
glob(pattern, function(err, files) {
files.forEach(function(file) {
var promise = readLastLines.read(file, 1);
promises.push(promise);
promise.then(function(lines) {
if(!regexNewLine.test(lines)) {
logs.push([
file, ':',
'does not have a trailing new line character'
].join(' '));
} else if(regexEmptyNewLine.test(lines)) {
logs.push([
file, ':',
'has more than one trailing new line'
].join(' '));
}
});
});
Promise.all(promises).then(function() {
log('trailing new line character', logs);
});
});
}
// check circular dependencies
function assertCircularDeps() {
madge(constants.pathToSrc).then(function(res) {
var circularDeps = res.circular();
var logs = [];
// see https://github.com/plotly/plotly.js/milestone/9
var MAX_ALLOWED_CIRCULAR_DEPS = 17;
if(circularDeps.length > MAX_ALLOWED_CIRCULAR_DEPS) {
console.log(circularDeps.join('\n'));
logs.push('some new circular dependencies were added to src/');
}
log('circular dependencies: ' + circularDeps.length, logs);
});
}
// Ensure no ES6 has snuck through into the build:
function assertES5() {
var CLIEngine = eslint.CLIEngine;
var cli = new CLIEngine({
useEslintrc: false,
ignore: false,
parserOptions: {
ecmaVersion: 5
}
});
var files = constants.partialBundlePaths.map(function(f) { return f.dist; });
files.unshift(constants.pathToPlotlyDist);
var report = cli.executeOnFiles(files);
var formatter = cli.getFormatter();
var errors = [];
if(report.errorCount > 0) {
console.log(formatter(report.results));
// It doesn't work well to pass formatted logs into this,
// so instead pass the empty string in a way that causes
// the test to fail
errors.push('');
}
log('es5-only syntax', errors);
}
function combineGlobs(arr) {
return '{' + arr.join(',') + '}';
}
function log(name, logs) {
if(logs.length) {
console.error('test-syntax error [' + name + ']');
console.error(logs.join('\n'));
EXIT_CODE = 1;
} else {
console.log('ok ' + name);
}
}
process.on('exit', function() {
if(EXIT_CODE) {
throw new Error('test syntax failed.');
}
});