From d83f32c34b1d0e4dd49dd1f0af1b665c022b2872 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Tue, 13 Sep 2022 09:57:42 +0900 Subject: [rubygems/rubygems] Load plugin immediately We can install RubyGems plugin by "gem install XXX". The installed plugin is used from the NEXT "gem ...". For example, "gem install gem-src kaminari" doesn't use gem-src plugin for kaminari. "gem install gem-src && gem install kaminari" uses gem-src plugin for kaminari. How about loading a plugin immediately when the plugin is installed? If this proposal is implemented, "gem install gem-src kaminari" works like "gem install gem-src && gem install kaminari". https://github.com/rubygems/rubygems/commit/4917d96f4c --- test/rubygems/test_gem.rb | 7 +++ .../rubygems/test_gem_commands_pristine_command.rb | 2 +- test/rubygems/test_gem_commands_setup_command.rb | 2 +- test/rubygems/test_gem_installer.rb | 52 +++++++++++++++++++++- test/rubygems/test_gem_uninstaller.rb | 8 ++-- 5 files changed, 63 insertions(+), 8 deletions(-) (limited to 'test/rubygems') diff --git a/test/rubygems/test_gem.rb b/test/rubygems/test_gem.rb index 126aad2602..6d36a8bb62 100644 --- a/test/rubygems/test_gem.rb +++ b/test/rubygems/test_gem.rb @@ -1449,6 +1449,8 @@ class TestGem < Gem::TestCase def test_load_plugins plugin_path = File.join "lib", "rubygems_plugin.rb" + foo1_plugin_path = nil + foo2_plugin_path = nil Dir.chdir @tempdir do FileUtils.mkdir_p "lib" File.open plugin_path, "w" do |fp| @@ -1458,17 +1460,22 @@ class TestGem < Gem::TestCase foo1 = util_spec "foo", "1" do |s| s.files << plugin_path end + foo1_plugin_path = File.join(foo1.gem_dir, plugin_path) install_gem foo1 foo2 = util_spec "foo", "2" do |s| s.files << plugin_path end + foo2_plugin_path = File.join(foo2.gem_dir, plugin_path) install_gem foo2 end Gem::Specification.reset + PLUGINS_LOADED.clear + $LOADED_FEATURES.delete(foo1_plugin_path) + $LOADED_FEATURES.delete(foo2_plugin_path) gem "foo" diff --git a/test/rubygems/test_gem_commands_pristine_command.rb b/test/rubygems/test_gem_commands_pristine_command.rb index 51b097dfbd..ff5e6f166d 100644 --- a/test/rubygems/test_gem_commands_pristine_command.rb +++ b/test/rubygems/test_gem_commands_pristine_command.rb @@ -546,7 +546,7 @@ class TestGemCommandsPristineCommand < Gem::TestCase fp.puts "puts __FILE__" end write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |fp| - fp.puts "puts __FILE__" + fp.puts "# do nothing" end write_file File.join(@tempdir, "bin", "foo") do |fp| fp.puts "#!/usr/bin/ruby" diff --git a/test/rubygems/test_gem_commands_setup_command.rb b/test/rubygems/test_gem_commands_setup_command.rb index 41fe4e0045..cd94f61247 100644 --- a/test/rubygems/test_gem_commands_setup_command.rb +++ b/test/rubygems/test_gem_commands_setup_command.rb @@ -431,7 +431,7 @@ class TestGemCommandsSetupCommand < Gem::TestCase s.files = %W[lib/rubygems_plugin.rb] end write_file File.join @tempdir, "lib", "rubygems_plugin.rb" do |f| - f.puts "require '#{gem.plugins.first}'" + f.puts "# do nothing" end install_gem gem diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb index 30113c629b..3a4566ce8c 100644 --- a/test/rubygems/test_gem_installer.rb +++ b/test/rubygems/test_gem_installer.rb @@ -769,7 +769,7 @@ gem 'other', version def test_generate_plugins installer = util_setup_installer do |spec| write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end spec.files += %w[lib/rubygems_plugin.rb] @@ -856,11 +856,59 @@ gem 'other', version refute_includes File.read(build_root_path), build_root end + class << self + attr_accessor :plugin_loaded + attr_accessor :post_install_is_called + end + + def test_use_plugin_immediately + self.class.plugin_loaded = false + self.class.post_install_is_called = false + spec_version = nil + plugin_path = nil + installer = util_setup_installer do |spec| + spec_version = spec.version + plugin_path = File.join("lib", "rubygems_plugin.rb") + write_file File.join(@tempdir, plugin_path) do |io| + io.write <<-PLUGIN +#{self.class}.plugin_loaded = true +Gem.post_install do + #{self.class}.post_install_is_called = true +end + PLUGIN + end + spec.files += [plugin_path] + plugin_path = File.join(spec.gem_dir, plugin_path) + end + build_rake_in do + installer.install + end + assert self.class.plugin_loaded, "plugin is not loaded" + assert self.class.post_install_is_called, + "post install hook registered by plugin is not called" + + self.class.plugin_loaded = false + $LOADED_FEATURES.delete(plugin_path) + installer_new = util_setup_installer do |spec_new| + spec_new.version = spec_version.version.succ + plugin_path = File.join("lib", "rubygems_plugin.rb") + write_file File.join(@tempdir, plugin_path) do |io| + io.write "#{self.class}.plugin_loaded = true" + end + spec_new.files += [plugin_path] + end + build_rake_in do + installer_new.install + end + assert !self.class.plugin_loaded, + "plugin is loaded even when old version is already loaded" + end + def test_keeps_plugins_up_to_date # NOTE: version a-2 is already installed by setup hooks write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end build_rake_in do diff --git a/test/rubygems/test_gem_uninstaller.rb b/test/rubygems/test_gem_uninstaller.rb index dd296b0807..dfa01768bc 100644 --- a/test/rubygems/test_gem_uninstaller.rb +++ b/test/rubygems/test_gem_uninstaller.rb @@ -173,7 +173,7 @@ class TestGemUninstaller < Gem::InstallerTestCase def test_remove_plugins write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end @spec.files += %w[lib/rubygems_plugin.rb] @@ -190,7 +190,7 @@ class TestGemUninstaller < Gem::InstallerTestCase def test_remove_plugins_with_install_dir write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end @spec.files += %w[lib/rubygems_plugin.rb] @@ -208,7 +208,7 @@ class TestGemUninstaller < Gem::InstallerTestCase def test_regenerate_plugins_for write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end @spec.files += %w[lib/rubygems_plugin.rb] @@ -635,7 +635,7 @@ create_makefile '#{@spec.name}' def test_uninstall_keeps_plugins_up_to_date write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io| - io.write "puts __FILE__" + io.write "# do nothing" end plugin_path = File.join Gem.plugindir, "a_plugin.rb" -- cgit v1.2.3