Skip to content
This repository
Browse code

- Escape strings in ruby-format specs using #dump instead of #to_s an…

…d %q. Issue #165
  • Loading branch information...
commit f6db093abe0840118808acfe843c7931fdcbb27f 1 parent a2498af
Eric Hodel authored August 25, 2011
8  lib/rubygems/specification.rb
@@ -1690,11 +1690,11 @@ def ri_dir
1690 1690
 
1691 1691
   def ruby_code(obj)
1692 1692
     case obj
1693  
-    when String            then '%q{' + obj + '}'
  1693
+    when String            then obj.dump
1694 1694
     when Array             then '[' + obj.map { |x| ruby_code x }.join(", ") + ']'
1695  
-    when Gem::Version      then obj.to_s.inspect
1696  
-    when Date              then '%q{' + obj.strftime('%Y-%m-%d') + '}'
1697  
-    when Time              then '%q{' + obj.strftime('%Y-%m-%d') + '}'
  1695
+    when Gem::Version      then obj.to_s.dump
  1696
+    when Date              then obj.strftime('%Y-%m-%d').dump
  1697
+    when Time              then obj.strftime('%Y-%m-%d').dump
1698 1698
     when Numeric           then obj.inspect
1699 1699
     when true, false, nil  then obj.inspect
1700 1700
     when Gem::Platform     then "Gem::Platform.new(#{obj.to_a.inspect})"
2  test/rubygems/test_gem_commands_specification_command.rb
@@ -135,7 +135,7 @@ def test_execute_ruby
135 135
     end
136 136
 
137 137
     assert_match %r|Gem::Specification.new|, @ui.output
138  
-    assert_match %r|s.name = %q\{foo\}|, @ui.output
  138
+    assert_match %r|s.name = "foo"|, @ui.output
139 139
     assert_equal '', @ui.error
140 140
   end
141 141
 
121  test/rubygems/test_gem_specification.rb
@@ -172,6 +172,51 @@ def test_self_load
172 172
     assert_equal @a2, spec
173 173
   end
174 174
 
  175
+  def test_self_load_escape_curly
  176
+    @a2.name = 'a};raise "improper escaping";%q{'
  177
+
  178
+    full_path = @a2.spec_file
  179
+    write_file full_path do |io|
  180
+      io.write @a2.to_ruby_for_cache
  181
+    end
  182
+
  183
+    spec = Gem::Specification.load full_path
  184
+
  185
+    @a2.files.clear
  186
+
  187
+    assert_equal @a2, spec
  188
+  end
  189
+
  190
+  def test_self_load_escape_interpolation
  191
+    @a2.name = 'a#{raise %<improper escaping>}'
  192
+
  193
+    full_path = @a2.spec_file
  194
+    write_file full_path do |io|
  195
+      io.write @a2.to_ruby_for_cache
  196
+    end
  197
+
  198
+    spec = Gem::Specification.load full_path
  199
+
  200
+    @a2.files.clear
  201
+
  202
+    assert_equal @a2, spec
  203
+  end
  204
+
  205
+  def test_self_load_escape_quote
  206
+    @a2.name = 'a";raise "improper escaping";"'
  207
+
  208
+    full_path = @a2.spec_file
  209
+    write_file full_path do |io|
  210
+      io.write @a2.to_ruby_for_cache
  211
+    end
  212
+
  213
+    spec = Gem::Specification.load full_path
  214
+
  215
+    @a2.files.clear
  216
+
  217
+    assert_equal @a2, spec
  218
+  end
  219
+
175 220
   def test_self_load_legacy_ruby
176 221
     spec = Deprecate.skip_during do
177 222
       eval LEGACY_RUBY_SPEC
@@ -785,19 +830,19 @@ def test_to_ruby
785 830
 # -*- encoding: utf-8 -*-
786 831
 
787 832
 Gem::Specification.new do |s|
788  
-  s.name = %q{a}
789  
-  s.version = \"2\"
  833
+  s.name = "a"
  834
+  s.version = "2"
790 835
 
791 836
   s.required_rubygems_version = Gem::Requirement.new(\"> 0\") if s.respond_to? :required_rubygems_version=
792  
-  s.authors = [%q{A User}]
793  
-  s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
794  
-  s.description = %q{This is a test description}
795  
-  s.email = %q{example@example.com}
796  
-  s.files = [%q{lib/code.rb}]
797  
-  s.homepage = %q{http://example.com}
798  
-  s.require_paths = [%q{lib}]
799  
-  s.rubygems_version = %q{#{Gem::VERSION}}
800  
-  s.summary = %q{this is a summary}
  837
+  s.authors = ["A User"]
  838
+  s.date = "#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}"
  839
+  s.description = "This is a test description"
  840
+  s.email = "example@example.com"
  841
+  s.files = ["lib/code.rb"]
  842
+  s.homepage = "http://example.com"
  843
+  s.require_paths = ["lib"]
  844
+  s.rubygems_version = "#{Gem::VERSION}"
  845
+  s.summary = "this is a summary"
801 846
 
802 847
   if s.respond_to? :specification_version then
803 848
     s.specification_version = #{Gem::Specification::CURRENT_SPECIFICATION_VERSION}
@@ -832,18 +877,18 @@ def test_to_ruby_for_cache
832 877
 # -*- encoding: utf-8 -*-
833 878
 
834 879
 Gem::Specification.new do |s|
835  
-  s.name = %q{a}
836  
-  s.version = \"2\"
  880
+  s.name = "a"
  881
+  s.version = "2"
837 882
 
838 883
   s.required_rubygems_version = Gem::Requirement.new(\"> 0\") if s.respond_to? :required_rubygems_version=
839  
-  s.authors = [%q{A User}]
840  
-  s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
841  
-  s.description = %q{This is a test description}
842  
-  s.email = %q{example@example.com}
843  
-  s.homepage = %q{http://example.com}
844  
-  s.require_paths = [%q{lib}]
845  
-  s.rubygems_version = %q{#{Gem::VERSION}}
846  
-  s.summary = %q{this is a summary}
  884
+  s.authors = ["A User"]
  885
+  s.date = "#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}"
  886
+  s.description = "This is a test description"
  887
+  s.email = "example@example.com"
  888
+  s.homepage = "http://example.com"
  889
+  s.require_paths = ["lib"]
  890
+  s.rubygems_version = "#{Gem::VERSION}"
  891
+  s.summary = "this is a summary"
847 892
 
848 893
   if s.respond_to? :specification_version then
849 894
     s.specification_version = #{Gem::Specification::CURRENT_SPECIFICATION_VERSION}
@@ -879,26 +924,26 @@ def test_to_ruby_fancy
879 924
 # -*- encoding: utf-8 -*-
880 925
 
881 926
 Gem::Specification.new do |s|
882  
-  s.name = %q{a}
883  
-  s.version = \"1\"
  927
+  s.name = "a"
  928
+  s.version = "1"
884 929
   s.platform = Gem::Platform.new(#{expected_platform})
885 930
 
886 931
   s.required_rubygems_version = Gem::Requirement.new(\">= 0\") if s.respond_to? :required_rubygems_version=
887  
-  s.authors = [%q{A User}]
888  
-  s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
889  
-  s.description = %q{This is a test description}
890  
-  s.email = %q{example@example.com}
891  
-  s.executables = [%q{exec}]
892  
-  s.extensions = [%q{ext/a/extconf.rb}]
893  
-  s.files = [%q{lib/code.rb}, %q{test/suite.rb}, %q{bin/exec}, %q{ext/a/extconf.rb}]
894  
-  s.homepage = %q{http://example.com}
895  
-  s.licenses = [%q{MIT}]
896  
-  s.require_paths = [%q{lib}]
897  
-  s.requirements = [%q{A working computer}]
898  
-  s.rubyforge_project = %q{example}
899  
-  s.rubygems_version = %q{#{Gem::VERSION}}
900  
-  s.summary = %q{this is a summary}
901  
-  s.test_files = [%q{test/suite.rb}]
  932
+  s.authors = ["A User"]
  933
+  s.date = "#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}"
  934
+  s.description = "This is a test description"
  935
+  s.email = "example@example.com"
  936
+  s.executables = ["exec"]
  937
+  s.extensions = ["ext/a/extconf.rb"]
  938
+  s.files = ["lib/code.rb", "test/suite.rb", "bin/exec", "ext/a/extconf.rb"]
  939
+  s.homepage = "http://example.com"
  940
+  s.licenses = ["MIT"]
  941
+  s.require_paths = ["lib"]
  942
+  s.requirements = ["A working computer"]
  943
+  s.rubyforge_project = "example"
  944
+  s.rubygems_version = "#{Gem::VERSION}"
  945
+  s.summary = "this is a summary"
  946
+  s.test_files = ["test/suite.rb"]
902 947
 
903 948
   if s.respond_to? :specification_version then
904 949
     s.specification_version = 3

0 notes on commit f6db093

Please sign in to comment.
Something went wrong with that request. Please try again.