From e183d2321e1d69acca553d5f0c02b663ea3fff8b Mon Sep 17 00:00:00 2001 From: Gustavo Ribeiro Date: Wed, 28 Dec 2022 11:07:25 -0300 Subject: [PATCH] change stub line separator to : --- lib/rubygems/specification.rb | 5 ++- lib/rubygems/stub_specification.rb | 8 +++- .../foo-0.0.1-x86-mswin32.gemspec | Bin 269 -> 269 bytes test/rubygems/test_gem_specification.rb | 21 ++++++++- test/rubygems/test_gem_stub_specification.rb | 42 ++++++++++++++++-- 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index 31b8ef95460f..56fbb5a13031 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -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|" diff --git a/lib/rubygems/stub_specification.rb b/lib/rubygems/stub_specification.rb index d87abdd9933f..be5109841a17 100644 --- a/lib/rubygems/stub_specification.rb +++ b/lib/rubygems/stub_specification.rb @@ -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 @@ -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 @@ -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 diff --git a/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec b/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec index 7fbc56429fba475c21ea03d9bfc6badd0a9bd862..82d64e4afca3fd6722ef9933e2c53694ee323f3a 100644 GIT binary patch delta 11 ScmeBW>SdZ>&S*8!;wk_XQUkvL delta 11 ScmeBW>SdZ>&d4y);wk_WY6FD; diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index b20e8b321ae8..259a8110171b 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -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 @@ -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) diff --git a/test/rubygems/test_gem_stub_specification.rb b/test/rubygems/test_gem_stub_specification.rb index 22a0b6bb6f5e..52332fb11f14 100644 --- a/test/rubygems/test_gem_stub_specification.rb +++ b/test/rubygems/test_gem_stub_specification.rb @@ -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 @@ -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|