Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var inherits = require('util').inherits;
17
var path = require('path');
18
19
var docblock = require('../parse/docblock');
20
var extract = require('../parse/extract');
21
var ResourceLoader = require('./ResourceLoader');
22
var JSTest = require('../resource/JSTest');
23
24
function escapeStrForRegex(str) {
25
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
26
}
27
28
/**
29
* @class Loads and parses __tests__ / *.js files
30
*
31
* @extends {ResourceLoader}
32
*/
33
function JSTestLoader(options) {
34
ResourceLoader.call(this, options);
35
36
this.pathRe = new RegExp(
37
'(?:/|^)__tests__/' +
38
(this.options.matchSubDirs ? '(.+)' : '([^/]+)') +
39
'(' + this.getExtensions().map(escapeStrForRegex).join('|') + ')$'
40
);
41
}
42
inherits(JSTestLoader, ResourceLoader);
43
JSTestLoader.prototype.path = __filename;
44
45
var requireRe = /(?:\brequire|\.map)\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g;
46
47
JSTestLoader.prototype.getResourceTypes = function() {
48
return [JSTest];
49
};
50
51
JSTestLoader.prototype.getExtensions = function() {
52
return this.options.extensions || ['.js'];
53
};
54
55
56
/**
57
* Initialize a resource with the source code and configuration
58
* Loader can parse, gzip, minify the source code to build the resulting
59
* Resource value object
60
*
61
* @protected
62
* @param {String} filePath resource being built
63
* @param {ProjectConfiguration} configuration configuration for the path
64
* @param {String} sourceCode
65
* @param {Function} callback
66
*/
67
JSTestLoader.prototype.loadFromSource =
68
function(filePath, configuration, sourceCode, messages, callback) {
69
70
var test = new JSTest(filePath);
71
var match = filePath.match(this.pathRe);
72
test.id = match ? match[1] : path.basename(filePath, '.js');
73
74
docblock.parse(docblock.extract(sourceCode)).forEach(function(pair) {
75
var name = pair[0];
76
var value = pair[1];
77
78
switch (name) {
79
case 'emails':
80
test.contacts = value.split(/\s/);
81
break;
82
default:
83
// do nothing
84
}
85
});
86
87
test.requiredModules = extract.strings(sourceCode, requireRe, 1);
88
callback(messages, test);
89
};
90
91
/**
92
* Only match __tests__ / *.js files
93
* @static
94
* @param {String} filePath
95
* @return {Boolean}
96
*/
97
JSTestLoader.prototype.matchPath = function(filePath) {
98
return this.pathRe.test(filePath);
99
};
100
101
102
module.exports = JSTestLoader;
103
104