Skip to content

Commit

Permalink
Merge #2782
Browse files Browse the repository at this point in the history
2782: Make Gem::Specification#ruby_code handle OpenSSL::PKey::RSA objects r=bronzdoc a=bronzdoc

# Description:
closes #2776

@zenspider what you think?
______________
I will abide by the [code of conduct](https://github.com/rubygems/rubygems/blob/master/CODE_OF_CONDUCT.md).


Co-authored-by: bronzdoc <lsagastume1990@gmail.com>
  • Loading branch information
bundlerbot and bronzdoc committed Sep 17, 2019
2 parents 5c1636d + ba021fb commit 7daa6a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/rubygems/specification.rb
Expand Up @@ -2293,18 +2293,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}"
Expand Down Expand Up @@ -2461,9 +2461,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

Expand Down
29 changes: 29 additions & 0 deletions test/rubygems/test_gem_specification.rb
Expand Up @@ -2445,6 +2445,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
Expand Down

0 comments on commit 7daa6a6

Please sign in to comment.