1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[rubygems/rubygems] Move metadata method to Gem::Package

https://github.com/rubygems/rubygems/commit/2c9cfcb666
This commit is contained in:
bronzdoc 2019-06-23 20:03:50 -06:00 committed by Hiroshi SHIBATA
parent 8103d46a4d
commit 688ccc9602
No known key found for this signature in database
GPG key ID: F9CF13417264FAC2
3 changed files with 54 additions and 30 deletions

View file

@ -85,7 +85,7 @@ command help for an example.
end
if @options[:spec]
spec, metadata = get_metadata path, security_policy
spec, metadata = Gem::Package.metadata(path, security_policy)
if metadata.nil?
alert_error "--spec is unsupported on '#{name}' (old format gem)"
@ -173,32 +173,4 @@ command help for an example.
path
end
##
# Extracts the Gem::Specification and raw metadata from the .gem file at
# +path+.
#--
# TODO move to Gem::Package as #raw_spec or something
def get_metadata(path, security_policy = nil)
format = Gem::Package.new path, security_policy
spec = format.spec
metadata = nil
File.open path, Gem.binary_mode do |io|
tar = Gem::Package::TarReader.new io
tar.each_entry do |entry|
case entry.full_name
when 'metadata' then
metadata = entry.read
when 'metadata.gz' then
metadata = Gem::Util.gunzip entry.read
end
end
end
return spec, metadata
end
end

View file

@ -156,6 +156,32 @@ class Gem::Package
Gem::Package::Old.new gem
end
##
# Extracts the Gem::Specification and raw metadata from the .gem file at
# +path+.
#--
def self.metadata(path, security_policy = nil)
format = new(path, security_policy)
spec = format.spec
metadata = nil
File.open path, Gem.binary_mode do |io|
tar = Gem::Package::TarReader.new io
tar.each_entry do |entry|
case entry.full_name
when 'metadata' then
metadata = entry.read
when 'metadata.gz' then
metadata = Gem::Util.gunzip entry.read
end
end
end
return spec, metadata
end
##
# Creates a new package that will read or write to the file +gem+.
@ -690,7 +716,6 @@ EOM
rescue Zlib::GzipFile::Error => e
raise Gem::Package::FormatError.new(e.message, entry.full_name)
end
end
require 'rubygems/package/digest_io'

View file

@ -420,6 +420,33 @@ class TestGemPackage < Gem::Package::TarTestCase
assert_equal %w[lib/code.rb], reader.contents
end
def test_metadata
data_tgz = util_tar_gz { }
gem = util_tar do |tar|
tar.add_file 'data.tar.gz', 0644 do |io|
io.write data_tgz.string
end
tar.add_file 'metadata.gz', 0644 do |io|
Zlib::GzipWriter.wrap io do |gzio|
gzio.write @spec.to_yaml
end
end
end
gem_path = "#{@destination}/test.gem"
File.open gem_path, "wb" do |io|
io.write gem.string
end
spec, metadata = Gem::Package.metadata(gem_path)
assert_equal @spec, spec
assert_match @spec.to_yaml, metadata.force_encoding("UTF-8")
end
def test_contents
package = Gem::Package.new @gem