2019-08-08 03:31:43 -04:00
|
|
|
#!/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'];
|
2021-09-03 11:10:48 -04:00
|
|
|
const testDirectories = ['spec/frontend'];
|
2019-08-08 03:31:43 -04:00
|
|
|
|
|
|
|
if (fs.existsSync('ee')) {
|
2020-12-23 16:10:24 -05:00
|
|
|
sourceDirectories.forEach((dir) => {
|
2019-08-08 03:31:43 -04:00
|
|
|
sourceDirectories.push(`ee/${dir}`);
|
|
|
|
});
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
testDirectories.forEach((dir) => {
|
2019-08-08 03:31:43 -04:00
|
|
|
testDirectories.push(`ee/${dir}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let numSourceFiles = 0;
|
|
|
|
let numTestFiles = 0;
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const isVerbose = process.argv.some((arg) => arg === '-v');
|
2019-08-08 03:31:43 -04:00
|
|
|
|
|
|
|
function forEachFileIn(dirPath, callback) {
|
|
|
|
fs.readdir(dirPath, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
files.forEach((fileName) => {
|
2019-08-08 03:31:43 -04:00
|
|
|
const absolutePath = path.join(dirPath, fileName);
|
|
|
|
const stats = fs.statSync(absolutePath);
|
|
|
|
if (stats.isFile()) {
|
|
|
|
callback(absolutePath);
|
|
|
|
} else if (stats.isDirectory()) {
|
|
|
|
forEachFileIn(absolutePath, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-10 10:09:11 -05:00
|
|
|
const countSourceFiles = (currentPath) =>
|
|
|
|
forEachFileIn(currentPath, (fileName) => {
|
|
|
|
if (fileName.endsWith('.vue') || fileName.endsWith('.js')) {
|
|
|
|
if (isVerbose) {
|
|
|
|
console.log(`source file: ${fileName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
numSourceFiles += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const countTestFiles = (currentPath) =>
|
|
|
|
forEachFileIn(currentPath, (fileName) => {
|
|
|
|
if (fileName.endsWith('_spec.js')) {
|
|
|
|
if (isVerbose) {
|
|
|
|
console.log(`test file: ${fileName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
numTestFiles += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-08 03:31:43 -04:00
|
|
|
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)}%`);
|
|
|
|
});
|