grafana/scripts/test-coverage-by-codeowner.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

Tests: Custom script to run unit tests filtered by code ownership (#111210) * feat(script): generate a source file x teams manifest from CODEOWNERS * feat(script): unit tests + coverage report only for files owned by team * feat(script): calculate CODEOWNERS metadata * refactor(script): export a pure codeowners manifest generation function * refactor(script): export a pure test coverage by team function * refactor(script): generate raw JSONL codeowners data from Node.js script * feat(script): put codeowners manifest all together in one script * refactor(scripts): group consistently with NPM script name * refactor(scripts): deduplicate constants for file paths etc. * refactor(scripts): make console output cute 💅✨ * refactor(tests): make coverage by "owner" directory more human readable * refactor(scripts): use consistent naming "codeowner" instead of "team" * chore(codeowners): mark DataViz as owners of scripts for now * chore(todo): leave a note where coverage metrics should be emitted later * fix(gitignore): ignore root codeowners-manifest directory not scripts/* * refactor(script): rename manifest to generate for clarity * docs(readme): add a brief README describing new scrips * chore(linter): ignore temporary files in prettier, fix whitespace format * refactor(script): simplify Jest config by using team files list directly * refactor(script): simplify script, partition sourceFiles and testFiles * refactor(script): simplify and parallelize manifest write operations * fix(script): handle errors for JSONL line reader * refactor(script): use Map instead of POJOs * fix(script): handle errors when streaming raw JSONL output * fix(script): add error handling, and use promise API for metadata check * fix(reporter): suppress duplicate Jest CLI coverage report output * refactor(script): simplify with fs promises API for consistency * fix(script): error handling for cp spawn-ed process * refactor(script): use Promise API for mkdir + exists * refactor(script): use fs Promise API * refactor(script): use fs Promise API * fix(script): same allow list for sourceFilter and all Jest config rules Co-authored-by: Paul Marbach <paul.marbach@grafana.com> * fix(script): bust cache when new files are created also --------- Co-authored-by: Paul Marbach <paul.marbach@grafana.com>
2025-10-08 05:07:55 +08:00
#!/usr/bin/env node
const cp = require('node:child_process');
const { readFile } = require('node:fs/promises');
const { CODEOWNERS_JSON_PATH: CODEOWNERS_MANIFEST_CODEOWNERS_PATH } = require('./codeowners-manifest/constants.js');
const JEST_CONFIG_PATH = 'jest.config.codeowner.js';
/**
* Run test coverage for a specific codeowner
* @param {string} codeownerName - The codeowner name to run coverage for
* @param {string} codeownersPath - Path to the teams.json file
* @param {string} jestConfigPath - Path to the Jest config file
*/
async function runTestCoverageByCodeowner(codeownerName, codeownersPath, jestConfigPath) {
const codeownersJson = await readFile(codeownersPath, 'utf8');
const codeowners = JSON.parse(codeownersJson);
if (!codeowners.includes(codeownerName)) {
throw new Error(`Codeowner ${codeownerName} was not found in ${codeownersPath}, check spelling`);
}
process.env.TEAM_NAME = codeownerName;
return new Promise((resolve, reject) => {
const child = cp.spawn('jest', [`--config=${jestConfigPath}`], { stdio: 'inherit', shell: true });
child.on('error', (error) => {
reject(new Error(`Failed to start Jest: ${error.message}`));
});
child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Jest exited with code ${code}`));
}
});
});
}
if (require.main === module) {
(async () => {
try {
const codeownerName = process.argv[2];
if (!codeownerName) {
console.error('Codeowner argument is required ...');
console.error('Usage: yarn test:coverage:by-codeowner @grafana/team-name');
process.exit(1);
}
console.log(`🧪 Running test coverage for codeowner: ${codeownerName}`);
await runTestCoverageByCodeowner(codeownerName, CODEOWNERS_MANIFEST_CODEOWNERS_PATH, JEST_CONFIG_PATH);
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`Could not read ${CODEOWNERS_MANIFEST_CODEOWNERS_PATH} ...`);
} else {
console.error(e.message);
}
process.exit(1);
}
})();
}
module.exports = { runTestCoverageByCodeowner };