From 7031277b109daeb0f1a4f69d35d22d2de5955b97 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Mon, 28 Nov 2022 21:47:02 -0700 Subject: [PATCH 01/11] Add --url option to truffle migrate command --- packages/core/lib/commands/migrate/meta.js | 18 ++++++++++++++++-- packages/core/lib/commands/migrate/run.js | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/core/lib/commands/migrate/meta.js b/packages/core/lib/commands/migrate/meta.js index 1bc941990ec..ab6fbea4a32 100644 --- a/packages/core/lib/commands/migrate/meta.js +++ b/packages/core/lib/commands/migrate/meta.js @@ -54,6 +54,10 @@ module.exports = { type: "boolean", default: true, hidden: true + }, + "url": { + describe: "Use specified URL for provider", + type: "string" } }, help: { @@ -62,7 +66,7 @@ module.exports = { " " + // spacing to align with previous line "[--compile-all] [--compile-none] [--verbose-rpc] [--interactive]\n" + " " + // spacing to align with previous line - "[--skip-dry-run] [--describe-json] [--dry-run]", + "[--skip-dry-run] [--describe-json] [--dry-run] [--network |--url ]", options: [ { option: "--reset", @@ -118,8 +122,18 @@ module.exports = { option: "--save", description: "Specify whether the migration will save on chain", hidden: true + }, + { + option: "--url", + description: + "Creates a provider using the given url and connects to the network." + }, + { + option: "--network", + description: + "The network to connect to, as specified in the Truffle config." } ], - allowedGlobalOptions: ["network", "config", "quiet"] + allowedGlobalOptions: ["config", "quiet"] } }; diff --git a/packages/core/lib/commands/migrate/run.js b/packages/core/lib/commands/migrate/run.js index fb383ffa639..2e6c1c8ac82 100644 --- a/packages/core/lib/commands/migrate/run.js +++ b/packages/core/lib/commands/migrate/run.js @@ -1,15 +1,29 @@ module.exports = async function (options) { const WorkflowCompile = require("@truffle/workflow-compile").default; const { Environment } = require("@truffle/environment"); - const Config = require("@truffle/config"); const determineDryRunSettings = require("./determineDryRunSettings"); const prepareConfigForRealMigrations = require("./prepareConfigForRealMigrations"); const runMigrations = require("./runMigrations"); const setUpDryRunEnvironmentThenRunMigrations = require("./setUpDryRunEnvironmentThenRunMigrations"); + const loadConfig = require("../../loadConfig"); + const OS = require("os"); + const TruffleError = require("@truffle/error"); const tmp = require("tmp"); tmp.setGracefulCleanup(); - const config = Config.detect(options); + if (options.url && options.network) { + const message = + "" + + "Mutually exclusive options, --url and --network detected!" + + OS.EOL + + "Please use either --url or --network and try again." + + OS.EOL + + "See: https://trufflesuite.com/docs/truffle/reference/truffle-commands/#migrate" + + OS.EOL; + throw new TruffleError(message); + } + + let config = loadConfig(options); if (config.compileNone || config["compile-none"]) { config.compiler = "none"; } From 89ae0bcedf0ae223ec6afcc12f1aa3108fb63dcb Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Mon, 28 Nov 2022 21:47:32 -0700 Subject: [PATCH 02/11] Add integration test for the --url option --- packages/truffle/test/scenarios/commands/migrate.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/truffle/test/scenarios/commands/migrate.js b/packages/truffle/test/scenarios/commands/migrate.js index 86d36876f9b..2a0a2927851 100644 --- a/packages/truffle/test/scenarios/commands/migrate.js +++ b/packages/truffle/test/scenarios/commands/migrate.js @@ -34,5 +34,15 @@ describe("truffle migrate", () => { assert.fail(); } }).timeout(20000); + + it("doesn't throw when --url option is passed", async () => { + try { + await CommandRunner.run("migrate --url http://127.0.0.1:8545", config); + } catch (error) { + console.log("the logger contents -- %o", config.logger.loggedStuff); + console.log("the following error occurred -- %o", error.message); + assert.fail(); + } + }).timeout(20000); }); }); From a386352f66e0f106c48e3553ba673f1b957face0 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Tue, 29 Nov 2022 12:05:43 -0700 Subject: [PATCH 03/11] Add spacing in the usage section --- packages/core/lib/commands/migrate/meta.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/commands/migrate/meta.js b/packages/core/lib/commands/migrate/meta.js index ab6fbea4a32..46b54a4d9da 100644 --- a/packages/core/lib/commands/migrate/meta.js +++ b/packages/core/lib/commands/migrate/meta.js @@ -56,7 +56,8 @@ module.exports = { hidden: true }, "url": { - describe: "Use specified URL for provider", + describe: + "Creates a provider using the given url and connects to the network", type: "string" } }, @@ -66,7 +67,9 @@ module.exports = { " " + // spacing to align with previous line "[--compile-all] [--compile-none] [--verbose-rpc] [--interactive]\n" + " " + // spacing to align with previous line - "[--skip-dry-run] [--describe-json] [--dry-run] [--network |--url ]", + "[--skip-dry-run] [--describe-json] [--dry-run]\n" + + " " + // spacing to align with previous line + "[--network |--url ]", options: [ { option: "--reset", From afbeb9c84af48c7b3341825f5093e9ad3a8ef14f Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Mon, 5 Dec 2022 20:05:36 -0700 Subject: [PATCH 04/11] Use const instead of let --- packages/core/lib/commands/migrate/run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/lib/commands/migrate/run.js b/packages/core/lib/commands/migrate/run.js index 2e6c1c8ac82..a1b4b0b7a42 100644 --- a/packages/core/lib/commands/migrate/run.js +++ b/packages/core/lib/commands/migrate/run.js @@ -23,7 +23,7 @@ module.exports = async function (options) { throw new TruffleError(message); } - let config = loadConfig(options); + const config = loadConfig(options); if (config.compileNone || config["compile-none"]) { config.compiler = "none"; } From 8811d74d6fab5bdc315c40da42ecc5c0ef4c3ff0 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Mon, 5 Dec 2022 20:14:16 -0700 Subject: [PATCH 05/11] Filter url and network options from the REPL input --- packages/core/lib/console.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index c00b14144a6..d43760004ef 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -46,6 +46,12 @@ const processInput = (input, allowedCommands) => { return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`); } + if (words.includes("--url") || words.includes("--network")) { + const message = + "url and network options are not supported within Truffle REPL"; + throw new TruffleError(message); + } + // an expression return input.trim(); }; From d00ba47ed3515986520927ccedb58f9c8ca0a04e Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Tue, 6 Dec 2022 11:29:40 -0700 Subject: [PATCH 06/11] Use makeIIFE function instead of TruffleError --- packages/core/lib/console.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index d43760004ef..098e10de3f7 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -46,10 +46,14 @@ const processInput = (input, allowedCommands) => { return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`); } - if (words.includes("--url") || words.includes("--network")) { - const message = - "url and network options are not supported within Truffle REPL"; - throw new TruffleError(message); + if (words.includes("--url")) { + const message = "url option is not supported within Truffle REPL"; + return makeIIFE(`ℹ️ : ${message}`); + } + + if (words.includes("--network")) { + const message = "network option is not supported within Truffle REPL"; + return makeIIFE(`ℹ️ : ${message}`); } // an expression From d9293fe80b17449a1096886cd08e29a19ef8b55c Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Tue, 6 Dec 2022 11:30:21 -0700 Subject: [PATCH 07/11] Remove extra single quote --- packages/core/lib/console.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index 098e10de3f7..f02a7af8b59 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -33,7 +33,7 @@ const processInput = (input, allowedCommands) => { if (cmd === undefined) { return makeIIFE( - `ℹ️ : 'Missing truffle command. Please include a valid truffle command.` + `ℹ️ : Missing truffle command. Please include a valid truffle command.` ); } From 9aee86cecfcbc9d4df993bb7c4c90ad261365fa4 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Tue, 6 Dec 2022 11:52:04 -0700 Subject: [PATCH 08/11] Remove extra single quote from develop test case --- packages/truffle/test/scenarios/commands/develop.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/truffle/test/scenarios/commands/develop.js b/packages/truffle/test/scenarios/commands/develop.js index c7d611f8fb7..d05bd238080 100644 --- a/packages/truffle/test/scenarios/commands/develop.js +++ b/packages/truffle/test/scenarios/commands/develop.js @@ -94,7 +94,7 @@ describe("truffle develop", function () { }, { cmd: "", - expectedError: `ℹ️ : 'Missing truffle command. Please include a valid truffle command.` + expectedError: `ℹ️ : Missing truffle command. Please include a valid truffle command.` } ].forEach(({ cmd, expectedError }) => { it(`alerts on 'truffle ${cmd}'`, async function () { From b53f4e7e2418861610c0642d5a0c8ba6b798b279 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Wed, 7 Dec 2022 20:35:50 -0700 Subject: [PATCH 09/11] Add tests to filter url and network options within REPL --- .../test/scenarios/commands/develop.js | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/truffle/test/scenarios/commands/develop.js b/packages/truffle/test/scenarios/commands/develop.js index d05bd238080..445222633ec 100644 --- a/packages/truffle/test/scenarios/commands/develop.js +++ b/packages/truffle/test/scenarios/commands/develop.js @@ -116,6 +116,38 @@ describe("truffle develop", function () { }); }); + describe("Improper command options", async function () { + this.timeout(7000); + + [ + { + option: "--url", + expectedError: `ℹ️ : url option is not supported within Truffle REPL` + }, + { + option: "--network", + expectedError: `ℹ️ : network option is not supported within Truffle REPL` + } + ].forEach(({ option, expectedError }) => { + it(`alerts on 'migrate ${option}'`, async function () { + await CommandRunner.runInREPL({ + inputCommands: [`migrate ${option}`], + config, + executableCommand: "develop", + displayHost: "develop" + }); + + const output = logger.contents(); + assert( + output.includes(expectedError), + `Expected string in output: "${expectedError}"\n${formatLines( + output + )}` + ); + }); + }); + }); + it("handles awaits", async function () { this.timeout(70000); const input = "await Promise.resolve(`${6*7} is probably not a prime`)"; From 1271865a54a744299f7b4918b56fac72c2b5aa49 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Thu, 15 Dec 2022 22:08:10 -0700 Subject: [PATCH 10/11] Handle filtering of url and network options with truffle prefix without truffle prefix --- packages/core/lib/console.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index f02a7af8b59..059ce4b3eb4 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -21,6 +21,17 @@ const validTruffleCommands = require("./commands/commands"); // by the REPL const makeIIFE = str => `(() => "${str}")()`; +function filterInputOptions(words) { + if (words.includes("--url")) { + const message = "url option is not supported within Truffle REPL"; + return makeIIFE(`ℹ️ : ${message}`); + } + if (words.includes("--network")) { + const message = "network option is not supported within Truffle REPL"; + return makeIIFE(`ℹ️ : ${message}`); + } +} + const processInput = (input, allowedCommands) => { const words = input.trim().split(/\s+/); @@ -39,6 +50,9 @@ const processInput = (input, allowedCommands) => { const normalizedCommand = cmd.toLowerCase(); if (validTruffleCommands.includes(normalizedCommand)) { + const filteredOptionsMessage = filterInputOptions(words); + if (filteredOptionsMessage !== undefined) return filteredOptionsMessage; + return allowedCommands.includes(normalizedCommand) ? words.slice(1).join(" ") : makeIIFE(`ℹ️ : '${cmd}' is not allowed within Truffle REPL`); @@ -46,14 +60,9 @@ const processInput = (input, allowedCommands) => { return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`); } - if (words.includes("--url")) { - const message = "url option is not supported within Truffle REPL"; - return makeIIFE(`ℹ️ : ${message}`); - } - - if (words.includes("--network")) { - const message = "network option is not supported within Truffle REPL"; - return makeIIFE(`ℹ️ : ${message}`); + if (validTruffleCommands.includes(words[0].toLowerCase())) { + const filteredOptionsMessage = filterInputOptions(words); + if (filteredOptionsMessage !== undefined) return filteredOptionsMessage; } // an expression From 4c94755e6cc0deb29726699531b8e276a834c547 Mon Sep 17 00:00:00 2001 From: Sukanya Parashar Date: Fri, 16 Dec 2022 11:56:36 -0700 Subject: [PATCH 11/11] Refactor function name to convey its actual meaning --- packages/core/lib/console.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index 059ce4b3eb4..469e1948f01 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -21,7 +21,7 @@ const validTruffleCommands = require("./commands/commands"); // by the REPL const makeIIFE = str => `(() => "${str}")()`; -function filterInputOptions(words) { +function hasMutuallyExclusiveOptions(words) { if (words.includes("--url")) { const message = "url option is not supported within Truffle REPL"; return makeIIFE(`ℹ️ : ${message}`); @@ -50,8 +50,8 @@ const processInput = (input, allowedCommands) => { const normalizedCommand = cmd.toLowerCase(); if (validTruffleCommands.includes(normalizedCommand)) { - const filteredOptionsMessage = filterInputOptions(words); - if (filteredOptionsMessage !== undefined) return filteredOptionsMessage; + const errorMessage = hasMutuallyExclusiveOptions(words); + if (errorMessage) return errorMessage; return allowedCommands.includes(normalizedCommand) ? words.slice(1).join(" ") @@ -61,8 +61,8 @@ const processInput = (input, allowedCommands) => { } if (validTruffleCommands.includes(words[0].toLowerCase())) { - const filteredOptionsMessage = filterInputOptions(words); - if (filteredOptionsMessage !== undefined) return filteredOptionsMessage; + const errorMessage = hasMutuallyExclusiveOptions(words); + if (errorMessage) return errorMessage; } // an expression