node-red/test/_spec.js

85 lines
3.1 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 13:15:47 +00:00
var fs = require("fs-extra");
var should = require("should");
2014-09-10 08:30:16 +00:00
var path = require('path');
2014-09-10 08:30:16 +00:00
// Directories to check with .js files and _spec.js files respectively
2018-01-16 13:15:47 +00:00
var rootdir = path.resolve(__dirname, "..");
2014-09-10 08:30:16 +00:00
var jsdir = path.resolve(__dirname, "../red");
var testdir = path.resolve(__dirname, "red");
2018-01-16 13:15:47 +00: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);
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 13:15:47 +00:00
})
}
2018-01-16 13:15:47 +00:00
});
}
}));
});
return Promise.all(promises).then(function() {
return errors;
})
});
2018-01-16 13:15:47 +00: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 13:15:47 +00: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();
}
});
});
});