This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement console.log in Truffle (#5687)
* Enable Ganache console log - Introduce a config namespace for `solidityLog` (see below) - Make managed Ganache instances aware of truffle-config because `connectOrStart` (ganache) needs to know relevant solidityLog settings to hook up Ganache events. - Use asynchronous swawn instead of synchronous option in core(console). It is necessary to interleave ganache's console stdout events (child process), with mocha's test runner (parent process). - Modify the GanacheMixin in `chain.js` to register for and, forward Ganache's console log events using the `truffle.solidity.log` channel/topic. - Use chalk decorated `console.log` to display solidity console log which will allow users to redirect `truffle test` output to a file. The debug module uses `stderr` insted of stdout which would make redirect to file awkward and complicated. - Update truffle resolver to handle `@truffle/Console` or `@truffle/console` Truffle will include @ganache/console.log/console.sol in its asset and allow smart contract writers users to integrate console.log by importing "truffle/Console.sol". This dependency is managed through npm and is currently pinned. The user does not have to import any console.log packages. yay! - Add @truffle/[Cc]onsole.log as a resolver dependency. This dependency is pinned for the time being - Make `includeTruffleSources` the default resolver option. Settings ======== The settings are namespaced in solidityLog ``` solidityLog: { displayPrefix: string; preventConsoleLogMigration: boolean; } ``` - solidityLog.displayPrefix - string [ default: "" ]. it is the display prefix for all detected console-log messages. NOTE: there is some defensive guards that resolve, null, undefined to behave exactly like the default setting () - solidityLog.preventConsoleLogMigration - boolean [ default: false]. If set to true, `truffle migrate` will not allow deployment on Mainnet. File changes ============ packages/config/src/configDefaults.ts packages/config/test/unit.test.ts - add defaults and tests for solidityLog.{displayPrefix,preventConsoleLogMigration} packages/core/lib/commands/develop/run.js - pass configOptions to connectOrStart packages/core/lib/commands/migrate/runMigrations.js - add migration guards to log when a deployment set has consoleLog assets and additionally prevent deployment based on settings. packages/core/lib/commands/test/run.js - hook up consoleLog for test command packages/core/lib/console.js - use spawn instead of spawnSync so that child process commands can be printed in-order instead of buffering and printing when process ends. This allows consoleLog events from the child process to be interleaved correctly with test outline from parent process. - modify provision method to eliminate need for filter loop packages/core/package.json - add JSONStream dependency packages/environment/chain.js packages/environment/develop.js packages/environment/package.json packages/environment/test/connectOrStartTest.js - hook into the "ganache:vm:tx:console.log" provider event and forward it across IPC infra as SolidityConsoleLogs - hook up client side IPC to listen for SolidityConsoleLogs - add @truffle/config and chalk dependencies - update tests to handle updated interfaces packages/resolver/.eslintrc.json - add eslintrc settings for truffle packages packages/core/lib/debug/compiler.js packages/test/src/Test.ts packages/test/src/TestRunner.ts - allow resolver to includeTruffleSources by default packages/resolver/lib/sources/truffle/index.ts packages/resolver/lib/resolver.ts packages/resolver/test/truffle.js packages/resolver/package.json - resolve "truffle/[cC]onsole.sol" - add tests for console.sol resolutions - add pinned @ganache/console.log/console.sol dependency - use for-of instead of forEach to utilize short circuit breakout. for-each will visit every item, even after a match has been found because there's no way to [1] break out of a for-each loop. > >There is no way to stop or break a forEach() loop other than by >throwing an exception. If you need such behavior, the forEach() method >is the wrong tool. > >Early termination may be accomplished with looping statements like for, >for...of, and for...in. Array methods like every(), some(), find(), and >findIndex() also stops iteration immediately when further iteration is >not necessary. packages/test/src/SolidityTest.ts - include `console.sol` in set of truffle assets to deploy for testing packages/truffle/package.json - add @ganache/console.log dependency packages/truffle/test/scenarios/solidity_console/Printf.sol packages/truffle/test/scenarios/solidity_console/printf.js packages/truffle/test/sources/init/config-disable-migrate-false.js packages/truffle/test/sources/init/config-disable-migrate-true.js packages/truffle/test/sources/init/truffle-config.js - integration tests for consoleLog packages/truffle/test/scenarios/sandbox.js - return tempDirPath when creating a sandbox. Printf tests depend on that. packages/truffle/webpack.config.js - bundle @ganache/console.log/console.sol Links ===== 1: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#:~:text=There%20is%20no%20way%20to,and%20for...in%20. Co-authored-by: David Murdoch <[email protected]> Co-authored-by: Micaiah Reid <[email protected]>
- Loading branch information