From 9cb2ca8b63152195f0e1fb889ae401f8b758ec72 Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Wed, 14 Aug 2024 03:29:55 +0200 Subject: [PATCH] Example test runner: make script more readable / ensure examples do not `process.exit` (#3585) * devp2p/vm: ensure examples do not process.exit * vm: fix genesis state example + make linter happy * Update examples-runner: run per-file * devp2p: try to exit dpt * revert change in devp2p * Merge remote-tracking branch 'origin/master' into fix-examples-test-runner --- packages/vm/examples/run-blockchain.ts | 7 +----- packages/vm/examples/run-solidity-contract.ts | 7 +----- packages/vm/examples/vmWithGenesisState.ts | 10 +++++--- scripts/examples-runner.ts | 24 +++++++++++-------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/vm/examples/run-blockchain.ts b/packages/vm/examples/run-blockchain.ts index 2bf149bcf7..5a08dfaff8 100644 --- a/packages/vm/examples/run-blockchain.ts +++ b/packages/vm/examples/run-blockchain.ts @@ -93,9 +93,4 @@ async function putBlocks(blockchain: Blockchain, common: Common, data: typeof te } } -main() - .then(() => process.exit(0)) - .catch((err) => { - console.error(err) - process.exit(1) - }) +void main() diff --git a/packages/vm/examples/run-solidity-contract.ts b/packages/vm/examples/run-solidity-contract.ts index be27a79ccc..1f5e49680e 100644 --- a/packages/vm/examples/run-solidity-contract.ts +++ b/packages/vm/examples/run-solidity-contract.ts @@ -227,9 +227,4 @@ async function main() { console.log('Everything ran correctly!') } -main() - .then(() => process.exit(0)) - .catch((err) => { - console.error(err) - process.exit(1) - }) +void main() diff --git a/packages/vm/examples/vmWithGenesisState.ts b/packages/vm/examples/vmWithGenesisState.ts index 248e48b102..e79df71b26 100644 --- a/packages/vm/examples/vmWithGenesisState.ts +++ b/packages/vm/examples/vmWithGenesisState.ts @@ -1,4 +1,3 @@ -import { createBlockchain } from '@ethereumjs/blockchain' import { Chain } from '@ethereumjs/common' import { getGenesis } from '@ethereumjs/genesis' import { createAddressFromString } from '@ethereumjs/util' @@ -7,11 +6,16 @@ import { VM } from '@ethereumjs/vm' const main = async () => { const genesisState = getGenesis(Chain.Mainnet) - const blockchain = await createBlockchain({ genesisState }) - const vm = await VM.create({ blockchain }) + const vm = await VM.create() + await vm.stateManager.generateCanonicalGenesis!(genesisState) const account = await vm.stateManager.getAccount( createAddressFromString('0x000d836201318ec6899a67540690382780743280'), ) + + if (account === undefined) { + throw new Error('Account does not exist: failed to import genesis state') + } + console.log( `This balance for account 0x000d836201318ec6899a67540690382780743280 in this chain's genesis state is ${Number( account?.balance, diff --git a/scripts/examples-runner.ts b/scripts/examples-runner.ts index 62d60db054..84ba5760ca 100644 --- a/scripts/examples-runner.ts +++ b/scripts/examples-runner.ts @@ -1,5 +1,5 @@ +import { readdirSync } from 'fs' import { extname, join } from 'path' -import { readdir } from 'fs' const pkg = process.argv[3] if (!pkg) { @@ -9,17 +9,21 @@ if (!pkg) { const examplesPath = `../packages/${pkg}/examples/` const path = join(__dirname, examplesPath) -readdir(path, async (err, files) => { - if (err) { - throw new Error('Error loading examples directory: ' + err.message) +const getExample = (fileName: string): Promise | undefined => { + if (extname(fileName) === '.cts' || extname(fileName) === '.ts') { + return import(examplesPath + fileName) } +} - const getTsFiles = (fileName: string): Promise | undefined => { - if (extname(fileName) === '.cts' || extname(fileName) === '.ts') { - return import(examplesPath + fileName) +const main = async () => { + const files = readdirSync(path) + for (const file of files) { + const runner = getExample(file) + if (runner !== undefined) { + console.log(` ---- Run example: ${file} ----`) + await runner } } +} - const importedFiles = files.map(getTsFiles).filter((file) => file) - await Promise.all(importedFiles) -}) +main()