-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(e2e): Initial e2e test for CLI (#1576)
Setups a new e2e test suite for testing the CLI. Works by creating an http server around the RPC server returned by the e2e `setup` util, and running the CLI in-proc with a custom logger that collects all output. And in an unrelated change, loads CLI version from package.json. Related to #1450
- Loading branch information
1 parent
58dc9bf
commit c2c30da
Showing
15 changed files
with
191 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env -S node --no-warnings | ||
import { createDebugLogger } from '@aztec/aztec.js'; | ||
import { createConsoleLogger } from '@aztec/foundation/log'; | ||
|
||
import { getProgram } from '../index.js'; | ||
|
||
const debugLogger = createDebugLogger('aztec:cli'); | ||
const log = createConsoleLogger(); | ||
|
||
/** CLI main entrypoint */ | ||
async function main() { | ||
const program = getProgram(log, debugLogger); | ||
await program.parseAsync(process.argv); | ||
} | ||
|
||
main().catch(err => { | ||
log(`Error in command execution`); | ||
log(err); | ||
process.exit(1); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { getHttpRpcServer } from '@aztec/aztec-rpc'; | ||
import { DeployL1Contracts } from '@aztec/ethereum'; | ||
import { AztecRPC } from '@aztec/types'; | ||
|
||
import http from 'http'; | ||
|
||
import { createApiRouter } from './routes.js'; | ||
|
||
/** | ||
* Creates an http server that forwards calls to the rpc server and starts it on the given port. | ||
* @param aztecRpcServer - RPC server that answers queries to the created HTTP server. | ||
* @param deployedL1Contracts - Info on L1 deployed contracts. | ||
* @param port - Port to listen in. | ||
* @returns A running http server. | ||
*/ | ||
export function startHttpRpcServer( | ||
aztecRpcServer: AztecRPC, | ||
deployedL1Contracts: DeployL1Contracts, | ||
port: string | number, | ||
): http.Server { | ||
const rpcServer = getHttpRpcServer(aztecRpcServer); | ||
|
||
const app = rpcServer.getApp(); | ||
const apiRouter = createApiRouter(deployedL1Contracts); | ||
app.use(apiRouter.routes()); | ||
app.use(apiRouter.allowedMethods()); | ||
|
||
const httpServer = http.createServer(app.callback()); | ||
httpServer.listen(port); | ||
return httpServer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { AztecNodeService } from '@aztec/aztec-node'; | ||
import { AztecAddress, AztecRPCServer } from '@aztec/aztec-rpc'; | ||
import { startHttpRpcServer } from '@aztec/aztec-sandbox/http'; | ||
import { createDebugLogger } from '@aztec/aztec.js'; | ||
import { getProgram } from '@aztec/cli'; | ||
import { DebugLogger } from '@aztec/foundation/log'; | ||
import { AztecRPC } from '@aztec/types'; | ||
|
||
import stringArgv from 'string-argv'; | ||
import { format } from 'util'; | ||
|
||
import { setup } from './fixtures/utils.js'; | ||
|
||
const HTTP_PORT = 9009; | ||
|
||
// Spins up a new http server wrapping the set up rpc server, and tests cli commands against it | ||
describe('cli', () => { | ||
let cli: ReturnType<typeof getProgram>; | ||
let http: ReturnType<typeof startHttpRpcServer>; | ||
let debug: DebugLogger; | ||
let aztecNode: AztecNodeService | undefined; | ||
let aztecRpcServer: AztecRPC; | ||
|
||
// All logs emitted by the cli will be collected here, and reset between tests | ||
const logs: string[] = []; | ||
|
||
beforeAll(async () => { | ||
debug = createDebugLogger('aztec:e2e_cli'); | ||
const context = await setup(2); | ||
debug(`Environment set up`); | ||
const { deployL1ContractsValues } = context; | ||
({ aztecNode, aztecRpcServer } = context); | ||
http = startHttpRpcServer(aztecRpcServer, deployL1ContractsValues, HTTP_PORT); | ||
debug(`HTTP RPC server started in port ${HTTP_PORT}`); | ||
const log = (...args: any[]) => { | ||
logs.push(format(...args)); | ||
debug(...args); | ||
}; | ||
cli = getProgram(log, debug); | ||
}); | ||
|
||
afterAll(async () => { | ||
http.close(); | ||
await aztecNode?.stop(); | ||
await (aztecRpcServer as AztecRPCServer).stop(); | ||
}); | ||
|
||
beforeEach(() => { | ||
logs.splice(0); | ||
}); | ||
|
||
// Run a command on the CLI | ||
const run = (cmd: string) => | ||
cli.parseAsync(stringArgv(cmd, 'node', 'dest/bin/index.js').concat(['--rpc-url', `http://localhost:${HTTP_PORT}`])); | ||
|
||
// Returns first match across all logs collected so far | ||
const findInLogs = (regex: RegExp) => { | ||
for (const log of logs) { | ||
const match = regex.exec(log); | ||
if (match) return match; | ||
} | ||
}; | ||
|
||
it('creates an account', async () => { | ||
const accountsBefore = await aztecRpcServer.getAccounts(); | ||
await run(`create-account`); | ||
const newAddress = findInLogs(/Address:\s+(?<address>0x[a-fA-F0-9]+)/)?.groups?.address; | ||
expect(newAddress).toBeDefined(); | ||
|
||
const accountsAfter = await aztecRpcServer.getAccounts(); | ||
const expectedAccounts = [...accountsBefore.map(a => a.address), AztecAddress.fromString(newAddress!)]; | ||
expect(accountsAfter.map(a => a.address)).toEqual(expectedAccounts); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.