Skip to content

Commit

Permalink
Example test runner: make script more readable / ensure examples do n…
Browse files Browse the repository at this point in the history
…ot `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
  • Loading branch information
jochem-brouwer authored Aug 14, 2024
1 parent 210b0fa commit 9cb2ca8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
7 changes: 1 addition & 6 deletions packages/vm/examples/run-blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 1 addition & 6 deletions packages/vm/examples/run-solidity-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 7 additions & 3 deletions packages/vm/examples/vmWithGenesisState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createBlockchain } from '@ethereumjs/blockchain'
import { Chain } from '@ethereumjs/common'
import { getGenesis } from '@ethereumjs/genesis'
import { createAddressFromString } from '@ethereumjs/util'
Expand All @@ -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,
Expand Down
24 changes: 14 additions & 10 deletions scripts/examples-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdirSync } from 'fs'
import { extname, join } from 'path'
import { readdir } from 'fs'

const pkg = process.argv[3]
if (!pkg) {
Expand All @@ -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<NodeModule> | undefined => {
if (extname(fileName) === '.cts' || extname(fileName) === '.ts') {
return import(examplesPath + fileName)
}
}

const getTsFiles = (fileName: string): Promise<NodeModule> | 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()

0 comments on commit 9cb2ca8

Please sign in to comment.