Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example test runner: make script more readable / ensure examples do not process.exit #3585

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
}

main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})
void main()

Check warning on line 96 in packages/vm/examples/run-blockchain.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/run-blockchain.ts#L96

Added line #L96 was not covered by tests
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 @@
console.log('Everything ran correctly!')
}

main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})
void main()

Check warning on line 230 in packages/vm/examples/run-solidity-contract.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/run-solidity-contract.ts#L230

Added line #L230 was not covered by tests
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 @@
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)

Check warning on line 10 in packages/vm/examples/vmWithGenesisState.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/vmWithGenesisState.ts#L9-L10

Added lines #L9 - L10 were not covered by tests
const account = await vm.stateManager.getAccount(
createAddressFromString('0x000d836201318ec6899a67540690382780743280'),
)

if (account === undefined) {
throw new Error('Account does not exist: failed to import genesis state')
}

Check warning on line 17 in packages/vm/examples/vmWithGenesisState.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/examples/vmWithGenesisState.ts#L15-L17

Added lines #L15 - L17 were not covered by tests

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()
Loading