diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index d691e7c87bcd..7972a75b6bb7 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -2348,18 +2348,18 @@ def ri_dir def ruby_code(obj) case obj - when String then obj.dump + ".freeze" - when Array then '[' + obj.map { |x| ruby_code x }.join(", ") + ']' - when Hash then + when String then obj.dump + ".freeze" + when Array then '[' + obj.map { |x| ruby_code x }.join(", ") + ']' + when Hash then seg = obj.keys.sort.map { |k| "#{k.to_s.dump} => #{obj[k].to_s.dump}" } "{ #{seg.join(', ')} }" - when Gem::Version then obj.to_s.dump - when DateLike then obj.strftime('%Y-%m-%d').dump - when Time then obj.strftime('%Y-%m-%d').dump - when Numeric then obj.inspect - when true, false, nil then obj.inspect - when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" - when Gem::Requirement then + when Gem::Version then obj.to_s.dump + when DateLike then obj.strftime('%Y-%m-%d').dump + when Time then obj.strftime('%Y-%m-%d').dump + when Numeric then obj.inspect + when true, false, nil then obj.inspect + when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" + when Gem::Requirement then list = obj.as_list "Gem::Requirement.new(#{ruby_code(list.size == 1 ? obj.to_s : list)})" else raise Gem::Exception, "ruby_code case not handled: #{obj.class}" @@ -2516,9 +2516,8 @@ def to_ruby @@attributes.each do |attr_name| next if handled.include? attr_name current_value = self.send(attr_name) - if current_value != default_value(attr_name) or - self.class.required_attribute? attr_name - result << " s.#{attr_name} = #{ruby_code current_value}" + if current_value != default_value(attr_name) || self.class.required_attribute?(attr_name) + result << " s.#{attr_name} = #{ruby_code current_value}" unless current_value.is_a?(OpenSSL::PKey::RSA) end end diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index d2ecbf443499..175688a3df1e 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -2419,6 +2419,35 @@ def test_to_ruby assert_equal @a2, same_spec end + def test_to_ruby_with_rsa_key + rsa_key = OpenSSL::PKey::RSA.new(2048) + @a2.signing_key = rsa_key + ruby_code = @a2.to_ruby + + expected = <<-SPEC +# -*- encoding: utf-8 -*- +# stub: a 2 ruby lib + +Gem::Specification.new do |s| + s.name = "a".freeze + s.version = "2" + + s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= + s.require_paths = ["lib".freeze] + s.authors = ["A User".freeze] + s.date = "#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}" + s.description = "This is a test description".freeze + s.email = "example@example.com".freeze + s.files = ["lib/code.rb".freeze] + s.homepage = "http://example.com".freeze + s.rubygems_version = "3.1.0.pre1".freeze + s.summary = "this is a summary".freeze +end + SPEC + + assert_equal expected, ruby_code + end + def test_to_ruby_for_cache @a2.add_runtime_dependency 'b', '1' @a2.dependencies.first.instance_variable_set :@type, nil