Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2464,10 +2464,11 @@ def test_files # :nodoc:

def to_ruby
mark_version
path_separator = ":"
result = []
result << "# -*- encoding: utf-8 -*-"
result << "#{Gem::StubSpecification::PREFIX}#{name} #{version} #{platform} #{raw_require_paths.join("\0")}"
result << "#{Gem::StubSpecification::PREFIX}#{extensions.join "\0"}" unless
result << "#{Gem::StubSpecification::PREFIX}#{name} #{version} #{platform} #{raw_require_paths.join(path_separator)}"
result << "#{Gem::StubSpecification::PREFIX}#{extensions.join path_separator}" unless
extensions.empty?
result << nil
result << "Gem::Specification.new do |s|"
Expand Down
8 changes: 6 additions & 2 deletions lib/rubygems/stub_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class Gem::StubSpecification < Gem::BasicSpecification
# :nodoc:
OPEN_MODE = "r:UTF-8:-"

# `\0` is the path separator used in old gemspecs, the new ones use `:`.
# That's why this pattern needs to match both.
PATH_SEPARATOR = /\0|:/.freeze

class StubLine # :nodoc: all
attr_reader :name, :version, :platform, :require_paths, :extensions,
:full_name
Expand Down Expand Up @@ -50,7 +54,7 @@ def initialize(data, extensions)
end

path_list = parts.last
@require_paths = REQUIRE_PATH_LIST[path_list] || path_list.split("\0").map! do |x|
@require_paths = REQUIRE_PATH_LIST[path_list] || path_list.split(PATH_SEPARATOR).map! do |x|
REQUIRE_PATHS[x] || x
end
end
Expand Down Expand Up @@ -116,7 +120,7 @@ def data
stubline = file.readline.chomp
if stubline.start_with?(PREFIX)
extensions = if /\A#{PREFIX}/ =~ file.readline.chomp
$'.split "\0"
$'.split PATH_SEPARATOR
else
StubLine::NO_EXTENSIONS
end
Expand Down
Binary file modified test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec
Binary file not shown.
21 changes: 20 additions & 1 deletion test/rubygems/test_gem_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2259,7 +2259,7 @@ def test_to_ruby

expected = <<-SPEC
# -*- encoding: utf-8 -*-
# stub: a 2 ruby lib\0other
# stub: a 2 ruby lib:other

Gem::Specification.new do |s|
s.name = "a".freeze
Expand Down Expand Up @@ -2289,6 +2289,25 @@ def test_to_ruby
assert_equal @a2, same_spec
end

def test_to_ruby_extensions
first_extension = "ext/a/extconf.rb"
@a2.extensions << first_extension

ruby_code = @a2.to_ruby.split("\n")

assert_includes ruby_code, "# stub: #{first_extension}"
assert_includes ruby_code, " s.extensions = [\"#{first_extension}\".freeze]"

second_extension = "ext/a/ext/parser/extconf.rb"
@a2.extensions << second_extension

ruby_code = @a2.to_ruby.split("\n")

assert_includes ruby_code, "# stub: #{first_extension}:#{second_extension}"
assert_includes ruby_code,
" s.extensions = [\"#{first_extension}\".freeze, \"#{second_extension}\".freeze]"
end

def test_to_ruby_with_rsa_key
require "rubygems/openssl"
pend "openssl is missing" unless defined?(OpenSSL::PKey::RSA)
Expand Down
42 changes: 38 additions & 4 deletions test/rubygems/test_gem_stub_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ def test_initialize
def test_initialize_extension
stub = stub_with_extension

assert_equal "stub_e", stub.name
assert_equal v(2), stub.version
assert_equal Gem::Platform::RUBY, stub.platform
assert_equal "stub_e", stub.name
assert_equal v(2), stub.version
assert_equal Gem::Platform::RUBY, stub.platform
assert_equal [stub.extension_dir, "lib"], stub.require_paths
assert_equal %w[ext/stub_e/extconf.rb], stub.extensions

stub = stub_with_multiple_extensions

assert_equal "stub_me", stub.name
assert_equal v(2), stub.version
assert_equal Gem::Platform::RUBY, stub.platform
assert_equal [stub.extension_dir, "lib"], stub.require_paths
assert_equal %w[ext/stub_e/extconf.rb], stub.extensions
assert_equal %w[ext/stub_me/extconf.rb ext/stub_me/ext/parser/extconf.rb], stub.extensions
end

def test_initialize_version
Expand Down Expand Up @@ -252,6 +260,32 @@ def stub_with_extension
end
end

def stub_with_multiple_extensions
spec = File.join @gemhome, "specifications", "stub_me-2.gemspec"
File.open spec, "w" do |io|
io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_me 2 ruby lib
# stub: ext/stub_me/extconf.rb:ext/stub_me/ext/parser/extconf.rb

Gem::Specification.new do |s|
s.name = 'stub_me'
s.version = Gem::Version.new '2'
s.extensions = ['ext/stub_me/extconf.rb', 'ext/stub_me/ext/parser/extconf.rb']
s.installed_by_version = '2.2'
end
STUB

io.flush

stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, "gems")

yield stub if block_given?

return stub
end
end

def stub_without_extension
spec = File.join @gemhome, "specifications", "stub-2.gemspec"
File.open spec, "w" do |io|
Expand Down