From 6796a89eacdabe00e9f4ab9ed254d419fe31b7e8 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Thu, 23 Dec 2021 12:39:10 +0200 Subject: [PATCH 1/8] Smart suggestion to correct store name param --- lib/shopify_cli/command.rb | 6 ++++++ lib/shopify_cli/messages/messages.rb | 1 + 2 files changed, 7 insertions(+) diff --git a/lib/shopify_cli/command.rb b/lib/shopify_cli/command.rb index 6d79a09a9a..1f61e5bb17 100644 --- a/lib/shopify_cli/command.rb +++ b/lib/shopify_cli/command.rb @@ -36,6 +36,12 @@ def call(args, command_name, *) end rescue OptionParser::InvalidOption => error arg = error.args.first + store_name = arg.match(/\A--(?.*\.myshopify\.com)\z/)&.[](:store_name) + if store_name + # Sometimes it may look like --invalidoption=https://storename.myshopify.com + store_name = store_name.sub(%r{\A(.*=)?(https?://)?}, "") + raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name) + end raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option", arg) rescue OptionParser::MissingArgument => error arg = error.args.first diff --git a/lib/shopify_cli/messages/messages.rb b/lib/shopify_cli/messages/messages.rb index d84f94eddb..e841ebe098 100644 --- a/lib/shopify_cli/messages/messages.rb +++ b/lib/shopify_cli/messages/messages.rb @@ -20,6 +20,7 @@ module Messages missing_ruby: "Ruby is required to continue. Install Ruby here: https://www.ruby-lang.org/en/downloads.", option_parser: { invalid_option: "The option {{command:%s}} is not supported.", + invalid_option_store_equals: "The option {{command:%s}} is not supported, did you mean: {{command:--store=%s}}.", missing_argument: "The required argument {{command:%s}} is missing.", }, }, From 4d4602942f7e6461af20218ff42e670e20486dab Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Thu, 23 Dec 2021 12:52:54 +0200 Subject: [PATCH 2/8] Don't correct to --store= if --store= isn't supported --- lib/shopify_cli/command.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shopify_cli/command.rb b/lib/shopify_cli/command.rb index 1f61e5bb17..7d6f0f3e59 100644 --- a/lib/shopify_cli/command.rb +++ b/lib/shopify_cli/command.rb @@ -37,7 +37,7 @@ def call(args, command_name, *) rescue OptionParser::InvalidOption => error arg = error.args.first store_name = arg.match(/\A--(?.*\.myshopify\.com)\z/)&.[](:store_name) - if store_name + if store_name && !arg.match?(/\A--(store|shop)=/) # Sometimes it may look like --invalidoption=https://storename.myshopify.com store_name = store_name.sub(%r{\A(.*=)?(https?://)?}, "") raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name) From b8c23fe269f415bdeb040727ef34b3b4e2f527b5 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 4 Jan 2022 12:29:39 +0200 Subject: [PATCH 3/8] Test parsing of store for prompting correction --- test/shopify-cli/commands/command_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/shopify-cli/commands/command_test.rb b/test/shopify-cli/commands/command_test.rb index 3683d9c31e..f2e2125363 100644 --- a/test/shopify-cli/commands/command_test.rb +++ b/test/shopify-cli/commands/command_test.rb @@ -28,6 +28,21 @@ def test_calls_help_with_subcommand_h_flag assert_match(CLI::UI.fmt(ShopifyCLI::Commands::Populate::Customer.help), io.join) end + + ["", "http://", "https://", "invalidoption=", "invalidoption=https://"].each do |prefix| + store_name = "mystore.myshopify.com" + store_name_with_prefix = "--#{prefix}#{store_name}" + + define_method("test_calls_with#{"_prefix_#{prefix}" unless prefix.empty?}_store_as_raw_param") do + io = capture_io_and_assert_raises(ShopifyCLI::Abort) do + run_cmd("login #{store_name_with_prefix}") + end + + assert_message_output(io: io, expected_content: [ + @context.message("core.errors.option_parser.invalid_option_store_equals", store_name_with_prefix, store_name), + ]) + end + end end end end From 8366180532af300ad7bfd8b05a7a57e2a1a6c474 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 4 Jan 2022 12:34:02 +0200 Subject: [PATCH 4/8] Test that store= and shop= are excluded from the correction --- test/shopify-cli/commands/command_test.rb | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/test/shopify-cli/commands/command_test.rb b/test/shopify-cli/commands/command_test.rb index f2e2125363..c5fe661d79 100644 --- a/test/shopify-cli/commands/command_test.rb +++ b/test/shopify-cli/commands/command_test.rb @@ -29,18 +29,32 @@ def test_calls_help_with_subcommand_h_flag assert_match(CLI::UI.fmt(ShopifyCLI::Commands::Populate::Customer.help), io.join) end - ["", "http://", "https://", "invalidoption=", "invalidoption=https://"].each do |prefix| + [ + "", + "http://", + "https://", + "invalidoption=", + "invalidoption=https://", + ["store=", false], + ["shop=", false], + ].each do |prefix, correction_expected = true| store_name = "mystore.myshopify.com" store_name_with_prefix = "--#{prefix}#{store_name}" define_method("test_calls_with#{"_prefix_#{prefix}" unless prefix.empty?}_store_as_raw_param") do io = capture_io_and_assert_raises(ShopifyCLI::Abort) do - run_cmd("login #{store_name_with_prefix}") + run_cmd("help login #{store_name_with_prefix}") end - assert_message_output(io: io, expected_content: [ - @context.message("core.errors.option_parser.invalid_option_store_equals", store_name_with_prefix, store_name), - ]) + if correction_expected + assert_message_output(io: io, expected_content: [ + @context.message("core.errors.option_parser.invalid_option_store_equals", store_name_with_prefix, store_name), + ]) + else + assert_message_output(io: io, expected_content: [ + @context.message("core.errors.option_parser.invalid_option", store_name_with_prefix), + ]) + end end end end From d4d6764f4270ad76ed20941238c25408349b6a06 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 4 Jan 2022 12:42:54 +0200 Subject: [PATCH 5/8] Also test -s= is not corrected --- test/shopify-cli/commands/command_test.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/shopify-cli/commands/command_test.rb b/test/shopify-cli/commands/command_test.rb index c5fe661d79..85b8fdc7c9 100644 --- a/test/shopify-cli/commands/command_test.rb +++ b/test/shopify-cli/commands/command_test.rb @@ -37,9 +37,10 @@ def test_calls_help_with_subcommand_h_flag "invalidoption=https://", ["store=", false], ["shop=", false], - ].each do |prefix, correction_expected = true| + ["s=", false, "-s="], + ].each do |prefix, correction_expected = true, full_prefix="--#{prefix}"| store_name = "mystore.myshopify.com" - store_name_with_prefix = "--#{prefix}#{store_name}" + store_name_with_prefix = "#{full_prefix}#{store_name}" define_method("test_calls_with#{"_prefix_#{prefix}" unless prefix.empty?}_store_as_raw_param") do io = capture_io_and_assert_raises(ShopifyCLI::Abort) do From 44df78a641123a592b83b24122c594f4604e5d40 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 22 Mar 2022 13:16:27 +0200 Subject: [PATCH 6/8] Improve store param correction message --- lib/shopify_cli/messages/messages.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/shopify_cli/messages/messages.rb b/lib/shopify_cli/messages/messages.rb index e841ebe098..9eed965b26 100644 --- a/lib/shopify_cli/messages/messages.rb +++ b/lib/shopify_cli/messages/messages.rb @@ -20,7 +20,12 @@ module Messages missing_ruby: "Ruby is required to continue. Install Ruby here: https://www.ruby-lang.org/en/downloads.", option_parser: { invalid_option: "The option {{command:%s}} is not supported.", - invalid_option_store_equals: "The option {{command:%s}} is not supported, did you mean: {{command:--store=%s}}.", + invalid_option_store_equals: <<~MESSAGE, + The option {{command:%s}} isn't recognized. + + Try this: + {{command:--store=%s}}. + MESSAGE missing_argument: "The required argument {{command:%s}} is missing.", }, }, From 1eeaba071b171a35b9f5bfe7130f704b3f10c667 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 22 Mar 2022 15:16:02 +0200 Subject: [PATCH 7/8] Rubocop fixes --- lib/shopify_cli/command.rb | 3 ++- test/shopify-cli/commands/command_test.rb | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/shopify_cli/command.rb b/lib/shopify_cli/command.rb index 7d6f0f3e59..da3e352b2d 100644 --- a/lib/shopify_cli/command.rb +++ b/lib/shopify_cli/command.rb @@ -40,7 +40,8 @@ def call(args, command_name, *) if store_name && !arg.match?(/\A--(store|shop)=/) # Sometimes it may look like --invalidoption=https://storename.myshopify.com store_name = store_name.sub(%r{\A(.*=)?(https?://)?}, "") - raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name) + raise ShopifyCLI::Abort, + @ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name) end raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option", arg) rescue OptionParser::MissingArgument => error diff --git a/test/shopify-cli/commands/command_test.rb b/test/shopify-cli/commands/command_test.rb index 85b8fdc7c9..4afded50ea 100644 --- a/test/shopify-cli/commands/command_test.rb +++ b/test/shopify-cli/commands/command_test.rb @@ -38,7 +38,7 @@ def test_calls_help_with_subcommand_h_flag ["store=", false], ["shop=", false], ["s=", false, "-s="], - ].each do |prefix, correction_expected = true, full_prefix="--#{prefix}"| + ].each do |prefix, correction_expected = true, full_prefix = "--#{prefix}"| store_name = "mystore.myshopify.com" store_name_with_prefix = "#{full_prefix}#{store_name}" @@ -49,7 +49,10 @@ def test_calls_help_with_subcommand_h_flag if correction_expected assert_message_output(io: io, expected_content: [ - @context.message("core.errors.option_parser.invalid_option_store_equals", store_name_with_prefix, store_name), + @context.message( + "core.errors.option_parser.invalid_option_store_equals", + store_name_with_prefix, store_name + ), ]) else assert_message_output(io: io, expected_content: [ From 10bab7fb7b6ea30cc59f7f8179999f1a88ea24c2 Mon Sep 17 00:00:00 2001 From: Ariel Caplan Date: Tue, 22 Mar 2022 15:16:57 +0200 Subject: [PATCH 8/8] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7969f087c..561aa1a303 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ From version 2.6.0, the sections in this file adhere to the [keep a changelog](h ### Added * [#1934](https://github.com/Shopify/shopify-cli/pull/1934): Block directories in theme assets +* [#1880](https://github.com/Shopify/shopify-cli/pull/1880): Recognize attempts to pass a store name and suggest correction ### Fixed * [#1874](https://github.com/Shopify/shopify-cli/pull/1874): Make ngrok errors more robust and helpful