stock-bot/tools/coverage-cli/src/runner.ts
2026-10-20 14:35:45 -04:00

450 lines
14 KiB
TypeScript

import { spawn } from 'child_process';
import { existsSync, mkdirSync, mkdtempSync, readFileSync } from 'fs';
import { join, resolve as resolvePath } from 'path';
import { glob, globSync } from 'glob';
import { defined } from '@stock-bot/invariants';
import type { CoverageConfig, RawCoverage, RawFileCoverage } from './types';
export interface RunnerResult {
success: boolean;
coverage: RawCoverage;
testResults:
| { package?: string; stdout?: string; stderr?: string; exitCode?: number | null }
| unknown[];
error?: string;
}
export class CoverageRunner {
constructor(private config: CoverageConfig) {}
async run(): Promise<RunnerResult> {
const packages = await this.findPackages();
if (packages.length === 0) {
console.log('No packages found to test');
return {
success: false,
error: 'No packages found to test',
coverage: {
files: [],
lines: { found: 0, hit: 0 },
functions: { found: 0, hit: 0 },
branches: { found: 0, hit: 0 },
},
testResults: [],
};
}
console.log(`Found ${packages.length} packages to test`);
const results: RunnerResult[] = [];
// Ensure output directory exists
if (!existsSync(this.config.outputDir)) {
mkdirSync(this.config.outputDir, { recursive: true });
}
// Run tests for each package
for (const pkg of packages) {
console.log(`Running tests for ${pkg.name}...`);
const result = await this.runPackageTests(pkg);
results.push(result);
}
// Merge coverage results
const mergedCoverage = this.mergeCoverageResults(results);
return {
success: results.every(r => r.success),
coverage: mergedCoverage,
testResults: results.map(r => r.testResults),
};
}
private async findPackages(): Promise<Array<{ name: string; path: string }>> {
const root = this.config.workspaceRoot || process.cwd();
const packages: Array<{ name: string; path: string }> = [];
// If specific packages are requested
if (this.config.packages && this.config.packages.length > 0) {
for (const pkgName of this.config.packages) {
const patterns = [
`tools/${pkgName}`,
`packages/${pkgName}`,
`apps/${pkgName}`,
`libs/${pkgName}`,
`libs/*/${pkgName}`,
`libs/core/${pkgName}`,
`libs/data/${pkgName}`,
`libs/services/${pkgName}`,
pkgName,
];
for (const pattern of patterns) {
const pkgPath = join(root, pattern);
if (existsSync(join(pkgPath, 'package.json'))) {
const pkg = JSON.parse(readFileSync(join(pkgPath, 'package.json'), 'utf-8'));
packages.push({ name: pkg.name || pkgName, path: pkgPath });
break;
}
}
}
} else {
// Find all packages - include all workspace patterns
const patterns = [
'tools/*/package.json',
'packages/*/package.json',
'apps/*/package.json',
'apps/*/*/package.json',
'libs/*/package.json',
'libs/*/*/package.json',
'libs/*/*/*/package.json', // Added for libs/core/handlers and similar
];
for (const pattern of patterns) {
const files = await glob(pattern, { cwd: root });
for (const file of files) {
const pkgPath = resolvePath(root, file, '..');
const pkg = JSON.parse(readFileSync(join(root, file), 'utf-8'));
packages.push({ name: pkg.name || pkgPath, path: pkgPath });
console.log(`Found package: ${pkg.name} at ${pkgPath}`);
}
}
// Also check root package.json
if (existsSync(join(root, 'package.json'))) {
const rootPkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'));
if (!rootPkg.workspaces) {
packages.push({ name: rootPkg.name || 'root', path: root });
}
}
}
return packages;
}
private async runPackageTests(pkg: { name: string; path: string }): Promise<RunnerResult> {
return new Promise(resolve => {
const sanitizedPkgName = pkg.name.replace('/', '-');
const temporaryRoot = resolvePath(this.config.outputDir, 'tmp');
mkdirSync(temporaryRoot, { recursive: true });
const coverageDir = mkdtempSync(
join(temporaryRoot, sanitizedPkgName.replaceAll('/', '-') + '-')
);
// Run tests with coverage enabled
const args = [
'test',
'--coverage',
'--coverage-reporter=lcov',
`--coverage-dir=${coverageDir}`,
];
const proc = spawn('bun', args, {
cwd: pkg.path,
stdio: 'pipe',
env: {
...process.env,
NODE_ENV: 'test',
},
});
let stdout = '';
let stderr = '';
proc.stdout.on('data', data => {
const output = data.toString();
stdout += output;
});
proc.stderr.on('data', data => {
const output = data.toString();
stderr += output;
});
proc.on('close', code => {
const success = code === 0;
// Parse LCOV coverage (even if tests failed, Bun still generates coverage)
let coverage = null;
// Always try to find coverage, not just when tests pass
// Look for LCOV in various possible locations
// Bun may generate LCOV at different locations depending on configuration
const possiblePaths = [join(coverageDir, 'lcov.info')];
let lcovFound = false;
for (const lcovPath of possiblePaths) {
if (existsSync(lcovPath)) {
coverage = this.parseLcovFile(lcovPath, pkg);
lcovFound = true;
break;
}
}
if (!lcovFound) {
console.error(`No measured LCOV coverage was generated for ${pkg.name}`);
}
// Now echo the output after parsing
if (stdout) {
process.stdout.write(stdout);
}
if (stderr) {
process.stderr.write(stderr);
}
resolve({
success: success && lcovFound && coverage !== null,
coverage: coverage ?? { files: [] },
testResults: {
package: pkg.name,
stdout,
stderr,
exitCode: code,
},
error:
!lcovFound || coverage === null
? 'Measured coverage is missing'
: success
? undefined
: stderr || 'Tests failed',
});
});
});
}
private findTestFiles(packagePath: string): string[] {
const testPatterns = [
'test/**/*.test.ts',
'test/**/*.test.js',
'src/**/*.test.ts',
'src/**/*.test.js',
'**/*.spec.ts',
'**/*.spec.js',
];
const testFiles: string[] = [];
for (const pattern of testPatterns) {
try {
const files = globSync(pattern, {
cwd: packagePath,
ignore: ['node_modules/**', 'dist/**'],
});
testFiles.push(...files);
} catch (error) {
console.warn(`Unable to discover tests for ${packagePath}: ${String(error)}`);
}
}
return testFiles;
}
private findSourceFileForTest(testFile: string, packagePath: string): string | null {
// Convert test file to source file path
let srcFile = testFile
.replace(/\.test\.(ts|js)$/, '.$1')
.replace(/\.spec\.(ts|js)$/, '.$1')
.replace(/^test\//, 'src/')
.replace(/\/__tests__\//, '/');
// Check if it's already in src
if (!srcFile.startsWith('src/')) {
srcFile = 'src/' + srcFile;
}
const fullPath = join(packagePath, srcFile);
if (existsSync(fullPath)) {
return fullPath;
}
// Try without src prefix
srcFile = testFile
.replace(/\.test\.(ts|js)$/, '.$1')
.replace(/\.spec\.(ts|js)$/, '.$1')
.replace(/^test\//, '');
const altPath = join(packagePath, srcFile);
if (existsSync(altPath)) {
return altPath;
}
return null;
}
private parseLcovFile(lcovPath: string, pkg: { name: string; path: string }) {
try {
const lcovContent = readFileSync(lcovPath, 'utf-8');
const coverage: RawCoverage = {
files: [],
};
let currentFile: RawFileCoverage | null = null;
const lines = lcovContent.split('\n');
for (const line of lines) {
if (line.startsWith('SF:')) {
if (currentFile) {
coverage.files.push(currentFile);
}
const relativePath = line.substring(3);
// Only include files that belong to this package
const fullPath = resolvePath(pkg.path, relativePath);
const normalizedPkgPath = pkg.path.replace(/\\/g, '/');
const normalizedFullPath = fullPath.replace(/\\/g, '/');
// Skip files that are outside the package directory
if (!normalizedFullPath.startsWith(normalizedPkgPath)) {
currentFile = null;
continue;
}
currentFile = {
path: fullPath,
lines: { found: 0, hit: 0 },
functions: { found: 0, hit: 0 },
branches: { found: 0, hit: 0 },
};
} else if (currentFile) {
if (line.startsWith('DA:')) {
const [_lineNum, hitCount = '0'] = line.substring(3).split(',');
currentFile.lines.found++;
if (parseInt(hitCount) > 0) {
currentFile.lines.hit++;
}
} else if (line.startsWith('FN:')) {
currentFile.functions.found++;
} else if (line.startsWith('FNDA:')) {
const [hitCount = '0'] = line.substring(5).split(',');
if (parseInt(hitCount) > 0) {
currentFile.functions.hit++;
}
} else if (line.startsWith('FNF:')) {
// Functions Found
currentFile.functions.found = parseInt(line.substring(4));
} else if (line.startsWith('FNH:')) {
// Functions Hit
currentFile.functions.hit = parseInt(line.substring(4));
} else if (line.startsWith('BRF:')) {
// Branches Found
currentFile.branches.found = parseInt(line.substring(4));
} else if (line.startsWith('BRH:')) {
// Branches Hit
currentFile.branches.hit = parseInt(line.substring(4));
} else if (line.startsWith('LF:')) {
// Lines Found
currentFile.lines.found = parseInt(line.substring(3));
} else if (line.startsWith('LH:')) {
// Lines Hit
currentFile.lines.hit = parseInt(line.substring(3));
}
}
}
if (currentFile) {
coverage.files.push(currentFile);
}
// If no files were found for this package after filtering, return null to trigger fallback
if (coverage.files.length === 0) {
console.log(`No coverage files found for ${pkg.name} after filtering`);
return null;
}
return coverage;
} catch (error) {
console.warn('Failed to parse LCOV file:', error);
return null;
}
}
private mergeCoverageResults(results: RunnerResult[]) {
const merged: Required<RawCoverage> = {
files: [],
lines: { found: 0, hit: 0 },
functions: { found: 0, hit: 0 },
branches: { found: 0, hit: 0 },
packages: {}, // Track per-package stats
};
for (const result of results) {
if (!result.coverage) {
continue;
}
// Get package name from test results
const packageName =
(!Array.isArray(result.testResults) ? result.testResults.package : undefined) || 'unknown';
if (!merged.packages[packageName]) {
merged.packages[packageName] = {
files: [],
lines: { found: 0, hit: 0 },
functions: { found: 0, hit: 0 },
branches: { found: 0, hit: 0 },
};
}
for (const file of result.coverage.files) {
// Skip excluded files
if (this.shouldExcludeFile(file.path)) {
continue;
}
merged.files.push(file);
defined(merged.packages[packageName]).files.push(file);
// Update overall stats
merged.lines.found += file.lines.found;
merged.lines.hit += file.lines.hit;
merged.functions.found += file.functions.found;
merged.functions.hit += file.functions.hit;
merged.branches.found += file.branches.found;
merged.branches.hit += file.branches.hit;
// Update package stats
merged.packages[packageName].lines.found += file.lines.found;
merged.packages[packageName].lines.hit += file.lines.hit;
merged.packages[packageName].functions.found += file.functions.found;
merged.packages[packageName].functions.hit += file.functions.hit;
merged.packages[packageName].branches.found += file.branches.found;
merged.packages[packageName].branches.hit += file.branches.hit;
}
}
return merged;
}
private shouldExcludeFile(filePath: string): boolean {
const normalizedPath = filePath.replace(/\\/g, '/');
// Check exclusion patterns
for (const pattern of this.config.exclude) {
if (this.matchesPattern(normalizedPath, pattern)) {
return true;
}
}
// Check inclusion patterns if specified
if (this.config.include && this.config.include.length > 0) {
for (const pattern of this.config.include) {
if (this.matchesPattern(normalizedPath, pattern)) {
return false;
}
}
return true; // Exclude if not in include list
}
return false;
}
private matchesPattern(path: string, pattern: string): boolean {
// Simple glob matching - in production, use a proper glob library
const regex = pattern
.replace(/\*\*/g, '.*')
.replace(/\*/g, '[^/]*')
.replace(/\?/g, '.')
.replace(/\//g, '\\/');
return new RegExp(regex).test(path);
}
}