From 0222fe52ed3803fe3ee0033da5b6faac5ee6299c Mon Sep 17 00:00:00 2001 From: Timothy Smith Date: Wed, 26 Feb 2020 21:53:21 -0500 Subject: [PATCH 1/2] Ensure default option/argument is not erroneously aliased Add test add handler continue loop after rescue remove byebug history spelling --- lib/thor/parser/arguments.rb | 7 ++++++- spec/parser/options_spec.rb | 7 +++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/thor/parser/arguments.rb b/lib/thor/parser/arguments.rb index fc887ac51..698765461 100644 --- a/lib/thor/parser/arguments.rb +++ b/lib/thor/parser/arguments.rb @@ -30,7 +30,12 @@ def initialize(arguments = []) arguments.each do |argument| if !argument.default.nil? - @assigns[argument.human_name] = argument.default + begin + @assigns[argument.human_name] = argument.default.dup + rescue TypeError # Compatibility shim for un-dup-able Fixnum in Ruby < 2.4 + @assigns[argument.human_name] = argument.default + next + end elsif argument.required? @non_assigned_required << argument end diff --git a/spec/parser/options_spec.rb b/spec/parser/options_spec.rb index 843119d78..12dbda36a 100644 --- a/spec/parser/options_spec.rb +++ b/spec/parser/options_spec.rb @@ -302,11 +302,10 @@ def remaining "Expected '--fruit' to be one of #{enum.join(', ')}; got orange") end - it "allows multiple values if repeatable is specified" do - create :foo => Thor::Option.new("foo", :type => :string, :repeatable => true) - + it "does not erroneously mutate defaults" do + create :foo => Thor::Option.new("foo", :type => :string, :repeatable => true, :required => false, :default => []) expect(parse("--foo=bar", "--foo", "12")["foo"]).to eq(["bar", "12"]) - expect(parse("--foo", "13", "--foo", "14")["foo"]).to eq(["bar", "12", "13", "14"]) + expect(@opt.instance_variable_get(:@switches)["--foo"].default).to eq([]) end end From e7aad400927888e59910c99977aba53827f248d2 Mon Sep 17 00:00:00 2001 From: Timothy Smith Date: Thu, 27 Feb 2020 19:33:43 -0500 Subject: [PATCH 2/2] Don't need next: begin/end block is inside the loop already --- lib/thor/parser/arguments.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/thor/parser/arguments.rb b/lib/thor/parser/arguments.rb index 698765461..05c1a659c 100644 --- a/lib/thor/parser/arguments.rb +++ b/lib/thor/parser/arguments.rb @@ -34,7 +34,6 @@ def initialize(arguments = []) @assigns[argument.human_name] = argument.default.dup rescue TypeError # Compatibility shim for un-dup-able Fixnum in Ruby < 2.4 @assigns[argument.human_name] = argument.default - next end elsif argument.required? @non_assigned_required << argument