|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { createVitest } from 'vitest/node' |
| 4 | + |
| 5 | +import { readdir } from 'node:fs/promises' |
| 6 | +import { join, relative, resolve } from 'node:path' |
| 7 | + |
| 8 | +/** |
| 9 | + * Recursively find all test files in a directory |
| 10 | + */ |
| 11 | +async function findTestFiles(dir: string): Promise<string[]> { |
| 12 | + const entries = await readdir(dir, { withFileTypes: true }) |
| 13 | + const files = await Promise.all( |
| 14 | + entries.map(async (entry) => { |
| 15 | + const fullPath = join(dir, entry.name) |
| 16 | + if (entry.isDirectory() && entry.name !== '__snapshots__' && entry.name !== 'node_modules') { |
| 17 | + return findTestFiles(fullPath) |
| 18 | + } |
| 19 | + if (entry.isFile() && entry.name.endsWith('.test.ts')) { |
| 20 | + return [fullPath] |
| 21 | + } |
| 22 | + return [] |
| 23 | + }), |
| 24 | + ) |
| 25 | + return files.flat() |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Recursively visit and print test collection |
| 30 | + */ |
| 31 | +function visit(collection: any, indent: string = '', isLast: boolean = true): { tests: number; suites: number } { |
| 32 | + let tests = 0 |
| 33 | + let suites = 0 |
| 34 | + |
| 35 | + const tasks = Array.from(collection) |
| 36 | + |
| 37 | + for (let i = 0; i < tasks.length; i++) { |
| 38 | + const task = tasks[i] |
| 39 | + const isLastTask = i === tasks.length - 1 |
| 40 | + const connector = isLastTask ? '└─ ' : '├─ ' |
| 41 | + const nextIndent = indent + (isLastTask ? ' ' : '│ ') |
| 42 | + |
| 43 | + if (task.type === 'suite') { |
| 44 | + console.log(`${indent}${connector}📦 ${task.name}`) |
| 45 | + suites++ |
| 46 | + |
| 47 | + const childCounts = visit(task.children, nextIndent, isLastTask) |
| 48 | + tests += childCounts.tests |
| 49 | + suites += childCounts.suites |
| 50 | + } else if (task.type === 'test') { |
| 51 | + console.log(`${indent}${connector}✓ ${task.name}`) |
| 52 | + tests++ |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return { tests, suites } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Main function |
| 61 | + */ |
| 62 | +async function main() { |
| 63 | + const network = process.argv[2] |
| 64 | + const chain = process.argv[3] |
| 65 | + |
| 66 | + if (!network) { |
| 67 | + console.error('Usage: tsx scripts/print-test-tree.ts <network> [chain]') |
| 68 | + console.error('') |
| 69 | + console.error('Examples:') |
| 70 | + console.error(' tsx scripts/print-test-tree.ts kusama # All Kusama tests') |
| 71 | + console.error(' tsx scripts/print-test-tree.ts kusama assetHub # AssetHub Kusama tests only') |
| 72 | + console.error(' tsx scripts/print-test-tree.ts kusama kusama # Kusama relay chain only') |
| 73 | + console.error(' tsx scripts/print-test-tree.ts polkadot people # People Polkadot tests only') |
| 74 | + process.exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + const filterInfo = chain ? ` (chain: ${chain})` : '' |
| 79 | + console.log(`\n🔍 Collecting tests for network: ${network}${filterInfo}...\n`) |
| 80 | + |
| 81 | + const packagesDir = resolve(process.cwd(), 'packages', network, 'src') |
| 82 | + |
| 83 | + // Find all test files |
| 84 | + let testFiles = await findTestFiles(packagesDir) |
| 85 | + |
| 86 | + // Filter by chain if specified |
| 87 | + if (chain) { |
| 88 | + const chainPattern = chain.charAt(0).toLowerCase() + chain.slice(1) |
| 89 | + testFiles = testFiles.filter((file) => { |
| 90 | + const fileName = file.split('/').pop() || '' |
| 91 | + return fileName.toLowerCase().startsWith(chainPattern.toLowerCase()) |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + if (testFiles.length === 0) { |
| 96 | + console.log('⚠️ No test files found!') |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // Print header |
| 101 | + console.log('='.repeat(80)) |
| 102 | + const title = chain |
| 103 | + ? `TEST TREE STRUCTURE FOR: ${network.toUpperCase()} - ${chain.toUpperCase()}` |
| 104 | + : `TEST TREE STRUCTURE FOR: ${network.toUpperCase()}` |
| 105 | + console.log(title) |
| 106 | + console.log('='.repeat(80)) |
| 107 | + |
| 108 | + let totalTests = 0 |
| 109 | + let totalSuites = 0 |
| 110 | + let totalFiles = 0 |
| 111 | + |
| 112 | + // Collect and print each file |
| 113 | + for (const file of testFiles.sort()) { |
| 114 | + const relPath = relative(packagesDir, file) |
| 115 | + |
| 116 | + // Create fresh vitest instance for each file to avoid state accumulation |
| 117 | + const vitest = await createVitest('test', { |
| 118 | + watch: false, |
| 119 | + run: false, |
| 120 | + }) |
| 121 | + |
| 122 | + // Collect tests from this file |
| 123 | + const result = await vitest.collect([file]) |
| 124 | + |
| 125 | + // Close immediately after collection |
| 126 | + await vitest.close() |
| 127 | + |
| 128 | + if (!result || !result.testModules || result.testModules.length === 0) { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + console.log(`\n📄 ${relPath}`) |
| 133 | + console.log('─'.repeat(80)) |
| 134 | + |
| 135 | + // Visit all test modules |
| 136 | + const fileCounts = { tests: 0, suites: 0 } |
| 137 | + for (const module of result.testModules) { |
| 138 | + const counts = visit(module.children, '', true) |
| 139 | + fileCounts.tests += counts.tests |
| 140 | + fileCounts.suites += counts.suites |
| 141 | + } |
| 142 | + |
| 143 | + totalTests += fileCounts.tests |
| 144 | + totalSuites += fileCounts.suites |
| 145 | + totalFiles++ |
| 146 | + |
| 147 | + console.log(`\n Tests: ${fileCounts.tests}, Suites: ${fileCounts.suites}`) |
| 148 | + } |
| 149 | + |
| 150 | + console.log(`\n${'='.repeat(80)}`) |
| 151 | + console.log('SUMMARY') |
| 152 | + console.log('='.repeat(80)) |
| 153 | + console.log(`Total files: ${totalFiles}`) |
| 154 | + console.log(`Total tests: ${totalTests}`) |
| 155 | + console.log(`Total suites: ${totalSuites}`) |
| 156 | + console.log('='.repeat(80) + '\n') |
| 157 | + } catch (error) { |
| 158 | + console.error('❌ Error:', error instanceof Error ? error.message : error) |
| 159 | + if (error instanceof Error && error.stack) { |
| 160 | + console.error(error.stack) |
| 161 | + } |
| 162 | + process.exit(1) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +main() |
0 commit comments