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>
This commit is contained in:
co-authored by
Paul Marbach
parent
9d60d03d11
commit
70dc9a0027
Executable
+101
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { writeFile, readFile, mkdir, access } = require('node:fs/promises');
|
||||
|
||||
const {
|
||||
CODEOWNERS_FILE_PATH,
|
||||
CODEOWNERS_MANIFEST_DIR,
|
||||
RAW_AUDIT_JSONL_PATH,
|
||||
CODEOWNERS_BY_FILENAME_JSON_PATH,
|
||||
FILENAMES_BY_CODEOWNER_JSON_PATH,
|
||||
CODEOWNERS_JSON_PATH,
|
||||
METADATA_JSON_PATH,
|
||||
} = require('./constants.js');
|
||||
const { generateCodeownersManifest } = require('./generate.js');
|
||||
const { generateCodeownersMetadata } = require('./metadata.js');
|
||||
const { generateCodeownersRawAudit } = require('./raw.js');
|
||||
|
||||
/**
|
||||
* Generate complete codeowners manifest including raw audit, metadata, and processed files
|
||||
* @param {string} codeownersFilePath - Path to CODEOWNERS file
|
||||
* @param {string} manifestDir - Directory for manifest files
|
||||
* @param {string} rawAuditPath - Path for raw audit JSONL file
|
||||
* @param {string} codeownersJsonPath - Path for teams.json
|
||||
* @param {string} codeownersByFilenamePath - Path for teams-by-filename.json
|
||||
* @param {string} filenamesByCodeownerPath - Path for filenames-by-team.json
|
||||
* @param {string} metadataPath - Path for metadata.json
|
||||
*/
|
||||
async function generateCodeownersManifestComplete(
|
||||
codeownersFilePath,
|
||||
manifestDir,
|
||||
rawAuditPath,
|
||||
codeownersJsonPath,
|
||||
codeownersByFilenamePath,
|
||||
filenamesByCodeownerPath,
|
||||
metadataPath
|
||||
) {
|
||||
try {
|
||||
await access(manifestDir);
|
||||
} catch (error) {
|
||||
await mkdir(manifestDir, { recursive: true });
|
||||
}
|
||||
|
||||
const newMetadata = generateCodeownersMetadata(codeownersFilePath, manifestDir, 'metadata.json');
|
||||
|
||||
let isCacheUpToDate = false;
|
||||
try {
|
||||
const existingMetadata = JSON.parse(await readFile(metadataPath, 'utf8'));
|
||||
if (
|
||||
existingMetadata.filesHash === newMetadata.filesHash &&
|
||||
existingMetadata.codeownersHash === newMetadata.codeownersHash
|
||||
) {
|
||||
isCacheUpToDate = true;
|
||||
}
|
||||
} catch (error) {
|
||||
isCacheUpToDate = false;
|
||||
}
|
||||
|
||||
if (!isCacheUpToDate) {
|
||||
await generateCodeownersRawAudit(codeownersFilePath, rawAuditPath);
|
||||
await generateCodeownersManifest(
|
||||
rawAuditPath,
|
||||
codeownersJsonPath,
|
||||
codeownersByFilenamePath,
|
||||
filenamesByCodeownerPath
|
||||
);
|
||||
await writeFile(metadataPath, JSON.stringify(newMetadata, null, 2), 'utf8');
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
(async () => {
|
||||
try {
|
||||
console.log('📋 Generating complete codeowners manifest...');
|
||||
|
||||
const wasGenerated = await generateCodeownersManifestComplete(
|
||||
CODEOWNERS_FILE_PATH,
|
||||
CODEOWNERS_MANIFEST_DIR,
|
||||
RAW_AUDIT_JSONL_PATH,
|
||||
CODEOWNERS_JSON_PATH,
|
||||
CODEOWNERS_BY_FILENAME_JSON_PATH,
|
||||
FILENAMES_BY_CODEOWNER_JSON_PATH,
|
||||
METADATA_JSON_PATH
|
||||
);
|
||||
|
||||
if (wasGenerated) {
|
||||
console.log('✅ Complete manifest generated:');
|
||||
console.log(` • ${CODEOWNERS_MANIFEST_DIR}/`);
|
||||
} else {
|
||||
console.log('✅ Manifest up-to-date, skipped generation');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('❌ Error generating codeowners manifest:', e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
module.exports = { generateCodeownersManifestComplete };
|
||||
Reference in New Issue
Block a user