1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/unit/_spec.js

88 lines
3.3 KiB
JavaScript
Raw Normal View History

/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* This test simply checks that for every .js file there exists
* a *_spec.js file under ./test correspondingly.
*/
/**
* Currently we're only checking the core components under ./red
* TODO: Increase the scope of this check
*/
2018-01-16 14:15:47 +01:00
var fs = require("fs-extra");
var should = require("should");
2014-09-10 10:30:16 +02:00
var path = require('path');
2014-09-10 10:30:16 +02:00
// Directories to check with .js files and _spec.js files respectively
var rootdir = path.resolve(__dirname, "../..");
var jsdir = path.resolve(__dirname, "../../packages/node_modules/");
var testdir = path.resolve(__dirname);
2018-01-16 14:15:47 +01:00
var walkDirectory = function(dir) {
var p = fs.readdir(dir);
var errors = [];
return p.then(function(list) {
var promises = [];
list.forEach(function(file) {
var filePath = path.join(dir,file);
if (!/@node-red\/(editor-client|nodes)/.test(filePath)) {
promises.push(fs.stat(filePath).then(function(stat){
if (stat.isDirectory()) {
return walkDirectory(filePath).then(function(results) {
if (results) {
errors = errors.concat(results);
}
});
} else if (/\.js$/.test(filePath)) {
var testFile = filePath.replace(jsdir, testdir).replace(".js", "_spec.js");
return fs.exists(testFile).then(function(exists) {
if (!exists) {
errors.push(testFile.substring(rootdir.length+1));
} else {
return fs.stat(testFile).then(function(stat) {
if (stat.size === 0) {
errors.push("[empty] "+testFile.substring(rootdir.length+1));
}
})
}
});
}
}));
}
2018-01-16 14:15:47 +01:00
});
return Promise.all(promises).then(function() {
return errors;
})
});
2018-01-16 14:15:47 +01:00
}
describe('_spec.js', function() {
this.timeout(50000); // we might not finish within the Mocha default timeout limit, project will also grow
it('is checking if all .js files have a corresponding _spec.js test file.', function(done) {
2018-01-16 14:15:47 +01:00
walkDirectory(jsdir).then(function(errors) {
if (errors.length > 0) {
var error = new Error("Missing/empty _spec files:\n\t"+errors.join("\n\t"));
done(error);
} else {
done();
}
});
});
});