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

Fixes #34079 - Allow explicit strings in key=value options #356

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/hammer_cli/options/normalizers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ def parse_key_value(val)

def strip_value(value)
if value.is_a? Array
value.map do |item|
strip_chars(item.strip, '"\'')
end
value.map(&:strip)
elsif value.is_a? Hash
value.map do |key, val|
[strip_chars(key.strip, '"\''), strip_chars(val.strip, '"\'')]
[strip_chars(key.strip, '"\''), val.strip]
end.to_h
else
strip_chars(value.strip, '"\'')
value.strip
end
end

Expand Down
36 changes: 26 additions & 10 deletions test/unit/options/normalizers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,20 @@
formatter.format("a= 1 , b = 2 ,c =3").must_equal({'a' => '1', 'b' => '2', 'c' => '3'})
end

it "should parse a comma separated string with spaces using single quotes" do
formatter.format("a= ' 1 ' , b =' 2',c ='3'").must_equal({'a' => ' 1 ', 'b' => ' 2', 'c' => '3'})
it 'should parse a comma separated string with spaces using single quotes' do
formatter.format("a= ' 1 ' , b =' 2',c ='3'").must_equal({ 'a' => "' 1 '", 'b' => "' 2'", 'c' => "'3'" })
end

it "should parse a comma separated string with spaces using double quotes" do
formatter.format("a= \" 1 \" , b =\" 2\",c =\"3\"").must_equal({'a' => ' 1 ', 'b' => ' 2', 'c' => '3'})
it 'should parse a comma separated string with spaces using double quotes' do
formatter.format('a= " 1 " , b =" 2",c ="3"').must_equal({ 'a' => '" 1 "', 'b' => '" 2"', 'c' => '"3"' })
end

it "should deal with equal sign in value" do
formatter.format("a=1,b='2=2',c=3").must_equal({'a' => '1', 'b' => '2=2', 'c' => '3'})
it 'should deal with equal sign in string value' do
formatter.format("a=1,b='2=2',c=3").must_equal({ 'a' => '1', 'b' => "'2=2'", 'c' => '3' })
end

it 'should deal with equal sign in value' do
formatter.format('a=1,b=2=2,c=3').must_equal({ 'a' => '1', 'b' => '2=2', 'c' => '3' })
end

it "should parse arrays" do
Expand All @@ -197,12 +201,20 @@
formatter.format("a=1,b=[1, 2, 3],c=3").must_equal({'a' => '1', 'b' => ['1', '2', '3'], 'c' => '3'})
end

it "should parse arrays with spaces using by single quotes" do
formatter.format("a=1,b=['1 1', ' 2 ', ' 3 3'],c=3").must_equal({'a' => '1', 'b' => ['1 1', ' 2 ', ' 3 3'], 'c' => '3'})
it 'should parse arrays with spaces using by single quotes' do
formatter.format("a=1,b=['1 1', ' 2 ', ' 3 3'],c=3").must_equal(
{ 'a' => '1', 'b' => ["'1 1'", "' 2 '", "' 3 3'"], 'c' => '3' }
)
end

it 'should parse arrays with spaces using by double quotes' do
formatter.format('a=1,b=["1 1", " 2 ", " 3 3"],c=3').must_equal(
{ 'a' => '1', 'b' => ['"1 1"', '" 2 "', '" 3 3"'], 'c' => '3' }
)
end

it "should parse arrays with spaces using by double quotes" do
formatter.format("a=1,b=[\"1 1\", \" 2 \", \" 3 3\"],c=3").must_equal({'a' => '1', 'b' => ['1 1', ' 2 ', ' 3 3'], 'c' => '3'})
it 'should parse arrays with spaces' do
formatter.format('a=1,b=[1 1, 2 , 3 3],c=3').must_equal({ 'a' => '1', 'b' => ['1 1', '2', '3 3'], 'c' => '3' })
end

it "should parse array with one item" do
Expand All @@ -226,6 +238,10 @@
it "should parse a comma separated string 2" do
proc { formatter.format("a=1,b,c=3") }.must_raise ArgumentError
end

it 'should parse explicit strings' do
formatter.format('name="*"').must_equal({ 'name' => '"*"' })
end
end

describe 'json format' do
Expand Down