2019-10-12 21:52:04 +05:30
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts the number of frontend test files and compares them against the number of application files.
|
|
|
|
*
|
|
|
|
* Example output:
|
|
|
|
*
|
|
|
|
* Source files: 1551
|
|
|
|
* Test files: 716
|
|
|
|
* Coverage: 46.16%
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const sourceDirectories = ['app/assets/javascripts'];
|
|
|
|
const testDirectories = ['spec/javascripts', 'spec/frontend'];
|
|
|
|
|
|
|
|
if (fs.existsSync('ee')) {
|
2021-03-08 18:12:59 +05:30
|
|
|
sourceDirectories.forEach((dir) => {
|
2019-10-12 21:52:04 +05:30
|
|
|
sourceDirectories.push(`ee/${dir}`);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
testDirectories.forEach((dir) => {
|
2019-10-12 21:52:04 +05:30
|
|
|
testDirectories.push(`ee/${dir}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let numSourceFiles = 0;
|
|
|
|
let numTestFiles = 0;
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const isVerbose = process.argv.some((arg) => arg === '-v');
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const countSourceFiles = (path) =>
|
|
|
|
forEachFileIn(path, (fileName) => {
|
2019-10-12 21:52:04 +05:30
|
|
|
if (fileName.endsWith('.vue') || fileName.endsWith('.js')) {
|
|
|
|
if (isVerbose) {
|
|
|
|
console.log(`source file: ${fileName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
numSourceFiles += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const countTestFiles = (path) =>
|
|
|
|
forEachFileIn(path, (fileName) => {
|
2019-10-12 21:52:04 +05:30
|
|
|
if (fileName.endsWith('_spec.js')) {
|
|
|
|
if (isVerbose) {
|
|
|
|
console.log(`test file: ${fileName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
numTestFiles += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function forEachFileIn(dirPath, callback) {
|
|
|
|
fs.readdir(dirPath, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
files.forEach((fileName) => {
|
2019-10-12 21:52:04 +05:30
|
|
|
const absolutePath = path.join(dirPath, fileName);
|
|
|
|
const stats = fs.statSync(absolutePath);
|
|
|
|
if (stats.isFile()) {
|
|
|
|
callback(absolutePath);
|
|
|
|
} else if (stats.isDirectory()) {
|
|
|
|
forEachFileIn(absolutePath, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Source directories: ${sourceDirectories.join(', ')}`);
|
|
|
|
console.log(`Test directories: ${testDirectories.join(', ')}`);
|
|
|
|
|
|
|
|
sourceDirectories.forEach(countSourceFiles);
|
|
|
|
testDirectories.forEach(countTestFiles);
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
console.log(`Source files: ${numSourceFiles}`);
|
|
|
|
console.log(`Test files: ${numTestFiles}`);
|
|
|
|
console.log(`Coverage: ${((100 * numTestFiles) / numSourceFiles).toFixed(2)}%`);
|
|
|
|
});
|