Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌈 Need to dup default or else we have a troublesome alias #715

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/thor/parser/arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before Ruby 2.4, attempting to dup a Fixnum results in a TypeError. From what I can see, that's the only accepted type for an option that could possibly throw, so for the sake of backward's compatibility I've had to add this in

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we need to call next?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right we don't. I'll remove

end
elsif argument.required?
@non_assigned_required << argument
end
Expand Down
7 changes: 3 additions & 4 deletions spec/parser/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down