2023-06-20 04:35:04 -07:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import util from 'util';
|
|
|
|
import { exec } from 'child_process';
|
2023-09-01 04:29:31 -07:00
|
|
|
import { glob } from 'glob';
|
2023-06-20 04:35:04 -07:00
|
|
|
import ts from 'typescript';
|
|
|
|
|
|
|
|
const readFileAsync = util.promisify(fs.readFile);
|
|
|
|
const execAsync = util.promisify(exec);
|
|
|
|
|
|
|
|
const filterAsync = async (asyncPredicate, arr) => {
|
2023-09-01 04:29:31 -07:00
|
|
|
const filterResults = await Promise.all(
|
|
|
|
arr.map(async (item) => ({
|
|
|
|
item,
|
|
|
|
shouldKeep: await asyncPredicate(item),
|
|
|
|
})),
|
|
|
|
);
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
return filterResults.filter(({ shouldKeep }) => shouldKeep).map(({ item }) => item);
|
|
|
|
};
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-07-04 00:42:58 -07:00
|
|
|
const isAbstractClass = (node) => {
|
|
|
|
if (ts.isClassDeclaration(node)) {
|
2023-09-01 04:29:31 -07:00
|
|
|
return (
|
|
|
|
node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword) || false
|
|
|
|
);
|
2023-07-04 00:42:58 -07:00
|
|
|
}
|
|
|
|
return false;
|
2023-09-01 04:29:31 -07:00
|
|
|
};
|
2023-07-04 00:42:58 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
const isAbstractMethod = (node) => {
|
|
|
|
return (
|
|
|
|
ts.isMethodDeclaration(node) &&
|
|
|
|
Boolean(node.modifiers?.find((modifier) => modifier.kind === ts.SyntaxKind.AbstractKeyword))
|
|
|
|
);
|
|
|
|
};
|
2023-07-04 00:42:58 -07:00
|
|
|
|
2023-06-20 04:35:04 -07:00
|
|
|
// Function to check if a file has a function declaration, function expression, object method or class
|
2023-09-01 04:29:31 -07:00
|
|
|
const hasFunctionOrClass = async (filePath) => {
|
2023-06-20 04:35:04 -07:00
|
|
|
const fileContent = await readFileAsync(filePath, 'utf-8');
|
|
|
|
const sourceFile = ts.createSourceFile(filePath, fileContent, ts.ScriptTarget.Latest, true);
|
|
|
|
|
|
|
|
let hasFunctionOrClass = false;
|
2023-09-01 04:29:31 -07:00
|
|
|
const visit = (node) => {
|
2023-07-04 00:42:58 -07:00
|
|
|
if (
|
2023-09-01 04:29:31 -07:00
|
|
|
ts.isFunctionDeclaration(node) ||
|
|
|
|
ts.isFunctionExpression(node) ||
|
|
|
|
ts.isArrowFunction(node) ||
|
|
|
|
(ts.isMethodDeclaration(node) && !isAbstractMethod(node)) ||
|
|
|
|
(ts.isClassDeclaration(node) && !isAbstractClass(node))
|
2023-07-04 00:42:58 -07:00
|
|
|
) {
|
2023-06-20 04:35:04 -07:00
|
|
|
hasFunctionOrClass = true;
|
|
|
|
}
|
|
|
|
node.forEachChild(visit);
|
2023-09-01 04:29:31 -07:00
|
|
|
};
|
2023-06-20 04:35:04 -07:00
|
|
|
|
|
|
|
visit(sourceFile);
|
|
|
|
|
|
|
|
return hasFunctionOrClass;
|
2023-09-01 04:29:31 -07:00
|
|
|
};
|
2023-06-20 04:35:04 -07:00
|
|
|
|
|
|
|
const main = async () => {
|
2023-09-01 04:29:31 -07:00
|
|
|
// Run a git command to get a list of all changed files in the branch (branch has to be up to date with master)
|
|
|
|
const changedFiles = await execAsync(
|
|
|
|
'git diff --name-only --diff-filter=d origin/master..HEAD',
|
|
|
|
).then(({ stdout }) => stdout.trim().split('\n').filter(Boolean));
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
// Get all .spec.ts and .test.ts files from the packages
|
2023-07-18 03:54:05 -07:00
|
|
|
const specAndTestTsFiles = await glob('packages/*/**/{test,__tests__}/**/*.{spec,test}.ts');
|
2023-09-01 04:29:31 -07:00
|
|
|
const specAndTestTsFilesNames = specAndTestTsFiles.map((file) =>
|
|
|
|
path.parse(file).name.replace(/\.(test|spec)/, ''),
|
|
|
|
);
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
// Filter out the .ts and .vue files from the changed files
|
|
|
|
const changedVueFiles = changedFiles.filter((file) => file.endsWith('.vue'));
|
2023-06-20 04:35:04 -07:00
|
|
|
// .ts files with any kind of function declaration or class and not in any of the test folders
|
|
|
|
const changedTsFilesWithFunction = await filterAsync(
|
2023-09-01 04:29:31 -07:00
|
|
|
async (filePath) =>
|
2023-06-20 04:35:04 -07:00
|
|
|
filePath.endsWith('.ts') &&
|
|
|
|
!(await glob('packages/*/**/{test,__tests__}/*.ts')).includes(filePath) &&
|
2023-09-01 04:29:31 -07:00
|
|
|
(await hasFunctionOrClass(filePath)),
|
|
|
|
changedFiles,
|
2023-06-20 04:35:04 -07:00
|
|
|
);
|
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
// For each .ts or .vue file, check if there's a corresponding .test.ts or .spec.ts file in the repository
|
|
|
|
const missingTests = changedVueFiles
|
|
|
|
.concat(changedTsFilesWithFunction)
|
|
|
|
.reduce((filesList, nextFile) => {
|
|
|
|
const fileName = path.parse(nextFile).name;
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
if (!specAndTestTsFilesNames.includes(fileName)) {
|
|
|
|
filesList.push(nextFile);
|
|
|
|
}
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
return filesList;
|
|
|
|
}, []);
|
2023-06-20 04:35:04 -07:00
|
|
|
|
2023-09-01 04:29:31 -07:00
|
|
|
if (missingTests.length) {
|
2023-06-20 04:35:04 -07:00
|
|
|
console.error(`Missing tests for:\n${missingTests.join('\n')}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
main();
|