diff --git a/lib/rubygems/commands/unpack_command.rb b/lib/rubygems/commands/unpack_command.rb index 938db701e5..317fd44251 100644 --- a/lib/rubygems/commands/unpack_command.rb +++ b/lib/rubygems/commands/unpack_command.rb @@ -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 diff --git a/lib/rubygems/package.rb b/lib/rubygems/package.rb index de811bf4e4..b667196bfc 100644 --- a/lib/rubygems/package.rb +++ b/lib/rubygems/package.rb @@ -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' diff --git a/test/rubygems/test_gem_package.rb b/test/rubygems/test_gem_package.rb index cedd2f5e55..c7b3cb3e20 100644 --- a/test/rubygems/test_gem_package.rb +++ b/test/rubygems/test_gem_package.rb @@ -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