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
@@ -0,0 +1,49 @@
|
||||
# Codeowners Manifest Scripts
|
||||
|
||||
Scripts for generating and caching CODEOWNERS manifest data.
|
||||
|
||||
Each of these scripts can be run individually if needed, but `index.js` is most useful because it combines them all.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Combined script
|
||||
node index.js # Generate complete manifest with caching
|
||||
|
||||
# Individual scripts
|
||||
node metadata.js # Generate metadata with hashes
|
||||
node raw.js # Generate raw audit data
|
||||
node generate.js # Process raw data into manifest files
|
||||
```
|
||||
|
||||
## Control flow of `index.js`
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[index.js] --> B[metadata.js: Generate new metadata]
|
||||
B --> C{Existing metadata exists?}
|
||||
C -->|No| D[Generate all files]
|
||||
C -->|Yes| E{Hashes match?}
|
||||
E -->|No| D
|
||||
E -->|Yes| F[Skip generation]
|
||||
|
||||
D --> G[raw.js: Generate audit data]
|
||||
G --> H[generate.js: Process into JSON files]
|
||||
H --> I[Save new metadata]
|
||||
I --> J[Complete]
|
||||
|
||||
F --> J
|
||||
|
||||
style F fill:#e1f5fe
|
||||
style J fill:#e8f5e8
|
||||
```
|
||||
|
||||
## Default output
|
||||
|
||||
By default these scripts will write the following files to the `/codeowners-manifest/*` directory.
|
||||
|
||||
- `audit-raw.jsonl` - Raw CODEOWNERS audit data in JSONL format _(for fast stream processing)_
|
||||
- `teams.json` - List of all codeowners _(for validating codeowner names)_
|
||||
- `teams-by-filename.json` - Files mapped to their respective codeowners
|
||||
- `filenames-by-team.json` - Codeowners mapped to their respective files
|
||||
- `metadata.json` - Hashes for cache validation
|
||||
@@ -0,0 +1,11 @@
|
||||
const CODEOWNERS_MANIFEST_DIR = 'codeowners-manifest';
|
||||
|
||||
module.exports = {
|
||||
CODEOWNERS_FILE_PATH: '.github/CODEOWNERS',
|
||||
CODEOWNERS_MANIFEST_DIR,
|
||||
RAW_AUDIT_JSONL_PATH: `${CODEOWNERS_MANIFEST_DIR}/audit-raw.jsonl`,
|
||||
CODEOWNERS_JSON_PATH: `${CODEOWNERS_MANIFEST_DIR}/teams.json`,
|
||||
CODEOWNERS_BY_FILENAME_JSON_PATH: `${CODEOWNERS_MANIFEST_DIR}/teams-by-filename.json`,
|
||||
FILENAMES_BY_CODEOWNER_JSON_PATH: `${CODEOWNERS_MANIFEST_DIR}/filenames-by-team.json`,
|
||||
METADATA_JSON_PATH: `${CODEOWNERS_MANIFEST_DIR}/metadata.json`,
|
||||
};
|
||||
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('node:fs');
|
||||
const { stat, writeFile } = require('node:fs/promises');
|
||||
const readline = require('node:readline');
|
||||
|
||||
const {
|
||||
RAW_AUDIT_JSONL_PATH,
|
||||
CODEOWNERS_BY_FILENAME_JSON_PATH,
|
||||
FILENAMES_BY_CODEOWNER_JSON_PATH,
|
||||
CODEOWNERS_JSON_PATH,
|
||||
} = require('./constants.js');
|
||||
|
||||
/**
|
||||
* Generate codeowners manifest files from raw audit data
|
||||
* @param {string} rawAuditPath - Path to the raw audit JSONL file
|
||||
* @param {string} codeownersJsonPath - Path to write teams.json
|
||||
* @param {string} codeownersByFilenamePath - Path to write teams-by-filename.json
|
||||
* @param {string} filenamesByCodeownerPath - Path to write filenames-by-team.json
|
||||
*/
|
||||
async function generateCodeownersManifest(
|
||||
rawAuditPath,
|
||||
codeownersJsonPath,
|
||||
codeownersByFilenamePath,
|
||||
filenamesByCodeownerPath
|
||||
) {
|
||||
const hasRawAuditJsonl = await stat(rawAuditPath);
|
||||
if (!hasRawAuditJsonl) {
|
||||
throw new Error(
|
||||
`No raw CODEOWNERS audit JSONL file found at: ${rawAuditPath} ... run "yarn codeowners-manifest:raw"`
|
||||
);
|
||||
}
|
||||
|
||||
const auditFileInput = fs.createReadStream(rawAuditPath);
|
||||
|
||||
const lineReader = readline.createInterface({
|
||||
input: auditFileInput,
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
let codeowners = new Set();
|
||||
let codeownersByFilename = new Map();
|
||||
let filenamesByCodeowner = new Map();
|
||||
|
||||
lineReader.on('error', (error) => {
|
||||
console.error('Error reading file:', error);
|
||||
throw error;
|
||||
});
|
||||
|
||||
lineReader.on('line', (line) => {
|
||||
try {
|
||||
const { path, owners: fileOwners } = JSON.parse(line.toString().trim());
|
||||
|
||||
for (let owner of fileOwners) {
|
||||
codeowners.add(owner);
|
||||
}
|
||||
|
||||
codeownersByFilename.set(path, fileOwners);
|
||||
|
||||
for (let owner of fileOwners) {
|
||||
const filenames = filenamesByCodeowner.get(owner) || [];
|
||||
filenamesByCodeowner.set(owner, filenames.concat(path));
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error(`Error parsing line: ${line}`, parseError);
|
||||
throw parseError;
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((resolve) => lineReader.once('close', resolve));
|
||||
|
||||
await Promise.all([
|
||||
writeFile(codeownersJsonPath, JSON.stringify(Array.from(codeowners).sort(), null, 2)),
|
||||
writeFile(codeownersByFilenamePath, JSON.stringify(Object.fromEntries(codeownersByFilename), null, 2)),
|
||||
writeFile(filenamesByCodeownerPath, JSON.stringify(Object.fromEntries(filenamesByCodeowner), null, 2)),
|
||||
]);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
(async () => {
|
||||
try {
|
||||
console.log(`📋 Generating files ↔ teams manifests from ${RAW_AUDIT_JSONL_PATH} ...`);
|
||||
await generateCodeownersManifest(
|
||||
RAW_AUDIT_JSONL_PATH,
|
||||
CODEOWNERS_JSON_PATH,
|
||||
CODEOWNERS_BY_FILENAME_JSON_PATH,
|
||||
FILENAMES_BY_CODEOWNER_JSON_PATH
|
||||
);
|
||||
console.log('✅ Manifest files generated:');
|
||||
console.log(` • ${CODEOWNERS_JSON_PATH}`);
|
||||
console.log(` • ${CODEOWNERS_BY_FILENAME_JSON_PATH}`);
|
||||
console.log(` • ${FILENAMES_BY_CODEOWNER_JSON_PATH}`);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
module.exports = { generateCodeownersManifest };
|
||||
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 };
|
||||
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { execSync } = require('node:child_process');
|
||||
const { writeFile, mkdir, access } = require('node:fs/promises');
|
||||
|
||||
const { CODEOWNERS_FILE_PATH, CODEOWNERS_MANIFEST_DIR, METADATA_JSON_PATH } = require('./constants.js');
|
||||
|
||||
/**
|
||||
* @typedef {Object} CodeownersMetadata
|
||||
* @property {string} generatedAt - ISO timestamp when metadata was generated
|
||||
* @property {string} filesHash - SHA-256 hash of all repository files
|
||||
* @property {string} codeownersHash - SHA-256 hash of CODEOWNERS file
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generate codeowners metadata for caching
|
||||
* @param {string} codeownersFilePath - Path to CODEOWNERS file
|
||||
* @param {string} manifestDir - Directory for manifest files
|
||||
* @param {string} metadataFilename - Filename for metadata file
|
||||
* @returns {CodeownersMetadata} Metadata object with hashes
|
||||
*/
|
||||
function generateCodeownersMetadata(codeownersFilePath, manifestDir, metadataFilename) {
|
||||
const [filesHash] = execSync('git ls-files --cached --others --exclude-standard | sort | sha256sum', {
|
||||
encoding: 'utf8',
|
||||
})
|
||||
.trim()
|
||||
.split(' ');
|
||||
|
||||
const [codeownersHash] = execSync(`sha256sum "${codeownersFilePath}"`, { encoding: 'utf8' }).trim().split(' ');
|
||||
|
||||
return {
|
||||
generatedAt: new Date().toISOString(),
|
||||
filesHash,
|
||||
codeownersHash,
|
||||
};
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
(async () => {
|
||||
try {
|
||||
console.log('⚙️ Generating codeowners-manifest metadata ...');
|
||||
|
||||
try {
|
||||
await access(CODEOWNERS_MANIFEST_DIR);
|
||||
} catch (error) {
|
||||
await mkdir(CODEOWNERS_MANIFEST_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
const metadata = generateCodeownersMetadata(CODEOWNERS_FILE_PATH, CODEOWNERS_MANIFEST_DIR, METADATA_JSON_PATH);
|
||||
|
||||
await writeFile(METADATA_JSON_PATH, JSON.stringify(metadata, null, 2), 'utf8');
|
||||
console.log('✅ Metadata generated:');
|
||||
console.log(` • ${METADATA_JSON_PATH}`);
|
||||
} catch (error) {
|
||||
console.error('❌ Error generating codeowners metadata:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
module.exports = { generateCodeownersMetadata };
|
||||
Executable
+84
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { spawn } = require('node:child_process');
|
||||
const fs = require('node:fs');
|
||||
const { access } = require('node:fs/promises');
|
||||
|
||||
const { CODEOWNERS_FILE_PATH, CODEOWNERS_MANIFEST_DIR, RAW_AUDIT_JSONL_PATH } = require('./constants.js');
|
||||
|
||||
/**
|
||||
* Generate raw CODEOWNERS audit data using github-codeowners CLI
|
||||
* @param {string} codeownersPath - Path to CODEOWNERS file
|
||||
* @param {string} outputPath - Path to write audit JSONL file
|
||||
*/
|
||||
async function generateCodeownersRawAudit(codeownersPath, outputPath) {
|
||||
try {
|
||||
await access(codeownersPath);
|
||||
} catch (error) {
|
||||
throw new Error(`CODEOWNERS file not found at: ${codeownersPath}`);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const outputStream = fs.createWriteStream(outputPath);
|
||||
|
||||
const child = spawn('yarn', ['github-codeowners', 'audit', '--output', 'jsonl'], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
cwd: process.cwd(),
|
||||
shell: true,
|
||||
});
|
||||
|
||||
let stderrData = '';
|
||||
child.stderr.on('data', (data) => {
|
||||
stderrData += data.toString();
|
||||
});
|
||||
|
||||
outputStream.on('error', (error) => {
|
||||
child.kill();
|
||||
reject(new Error(`Failed to write to output file: ${error.message}`));
|
||||
});
|
||||
|
||||
child.stdout.pipe(outputStream);
|
||||
|
||||
child.on('close', (code) => {
|
||||
outputStream.end();
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
} else {
|
||||
const error = new Error(`github-codeowners process exited with code ${code}`);
|
||||
if (stderrData) {
|
||||
error.message += `\nStderr: ${stderrData.trim()}`;
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
outputStream.end();
|
||||
if (err.code === 'ENOENT') {
|
||||
reject(new Error('yarn command not found. Please ensure yarn and github-codeowners are available'));
|
||||
} else {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
(async () => {
|
||||
try {
|
||||
if (!fs.existsSync(CODEOWNERS_MANIFEST_DIR)) {
|
||||
fs.mkdirSync(CODEOWNERS_MANIFEST_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
console.log(`🍣 Getting raw CODEOWNERS data for manifest ...`);
|
||||
await generateCodeownersRawAudit(CODEOWNERS_FILE_PATH, RAW_AUDIT_JSONL_PATH);
|
||||
console.log('✅ Raw audit generated:');
|
||||
console.log(` • ${RAW_AUDIT_JSONL_PATH}`);
|
||||
} catch (e) {
|
||||
console.error('❌ Error generating raw audit:', e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
module.exports = { generateCodeownersRawAudit };
|
||||
@@ -0,0 +1,67 @@
|
||||
#!/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 };
|
||||
Reference in New Issue
Block a user