Skip to content

Commit 09e839c

Browse files
Merge pull request #6673 from kou/plugin-immediately
Load plugin immediately
2 parents 1ecf75a + 4917d96 commit 09e839c

File tree

6 files changed

+78
-8
lines changed

6 files changed

+78
-8
lines changed

lib/rubygems/installer.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ def install
343343

344344
Gem::Specification.add_spec(spec)
345345

346+
load_plugin
347+
346348
run_post_install_hooks
347349

348350
spec
@@ -1006,4 +1008,17 @@ def bash_prolog_script
10061008
""
10071009
end
10081010
end
1011+
1012+
def load_plugin
1013+
specs = Gem::Specification.find_all_by_name(spec.name)
1014+
# If old version already exists, this plugin isn't loaded
1015+
# immediately. It's for avoiding a case that multiple versions
1016+
# are loaded at the same time.
1017+
return unless specs.size == 1
1018+
1019+
plugin_files = spec.plugins.map do |plugin|
1020+
File.join(@plugins_dir, "#{spec.name}_plugin#{File.extname(plugin)}")
1021+
end
1022+
Gem.load_plugin_files(plugin_files)
1023+
end
10091024
end

test/rubygems/test_gem.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,8 @@ def test_self_vendor_dir_missing
14491449
def test_load_plugins
14501450
plugin_path = File.join "lib", "rubygems_plugin.rb"
14511451

1452+
foo1_plugin_path = nil
1453+
foo2_plugin_path = nil
14521454
Dir.chdir @tempdir do
14531455
FileUtils.mkdir_p "lib"
14541456
File.open plugin_path, "w" do |fp|
@@ -1458,17 +1460,22 @@ def test_load_plugins
14581460
foo1 = util_spec "foo", "1" do |s|
14591461
s.files << plugin_path
14601462
end
1463+
foo1_plugin_path = File.join(foo1.gem_dir, plugin_path)
14611464

14621465
install_gem foo1
14631466

14641467
foo2 = util_spec "foo", "2" do |s|
14651468
s.files << plugin_path
14661469
end
1470+
foo2_plugin_path = File.join(foo2.gem_dir, plugin_path)
14671471

14681472
install_gem foo2
14691473
end
14701474

14711475
Gem::Specification.reset
1476+
PLUGINS_LOADED.clear
1477+
$LOADED_FEATURES.delete(foo1_plugin_path)
1478+
$LOADED_FEATURES.delete(foo2_plugin_path)
14721479

14731480
gem "foo"
14741481

test/rubygems/test_gem_commands_pristine_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_execute_only_plugins
546546
fp.puts "puts __FILE__"
547547
end
548548
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |fp|
549-
fp.puts "puts __FILE__"
549+
fp.puts "# do nothing"
550550
end
551551
write_file File.join(@tempdir, "bin", "foo") do |fp|
552552
fp.puts "#!/usr/bin/ruby"

test/rubygems/test_gem_commands_setup_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def gem_install_with_plugin(name)
431431
s.files = %W[lib/rubygems_plugin.rb]
432432
end
433433
write_file File.join @tempdir, "lib", "rubygems_plugin.rb" do |f|
434-
f.puts "require '#{gem.plugins.first}'"
434+
f.puts "# do nothing"
435435
end
436436
install_gem gem
437437

test/rubygems/test_gem_installer.rb

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def test_generate_bin_with_dangling_symlink
769769
def test_generate_plugins
770770
installer = util_setup_installer do |spec|
771771
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
772-
io.write "puts __FILE__"
772+
io.write "# do nothing"
773773
end
774774

775775
spec.files += %w[lib/rubygems_plugin.rb]
@@ -856,11 +856,59 @@ def test_generate_plugins_with_build_root
856856
refute_includes File.read(build_root_path), build_root
857857
end
858858

859+
class << self
860+
attr_accessor :plugin_loaded
861+
attr_accessor :post_install_is_called
862+
end
863+
864+
def test_use_plugin_immediately
865+
self.class.plugin_loaded = false
866+
self.class.post_install_is_called = false
867+
spec_version = nil
868+
plugin_path = nil
869+
installer = util_setup_installer do |spec|
870+
spec_version = spec.version
871+
plugin_path = File.join("lib", "rubygems_plugin.rb")
872+
write_file File.join(@tempdir, plugin_path) do |io|
873+
io.write <<-PLUGIN
874+
#{self.class}.plugin_loaded = true
875+
Gem.post_install do
876+
#{self.class}.post_install_is_called = true
877+
end
878+
PLUGIN
879+
end
880+
spec.files += [plugin_path]
881+
plugin_path = File.join(spec.gem_dir, plugin_path)
882+
end
883+
build_rake_in do
884+
installer.install
885+
end
886+
assert self.class.plugin_loaded, "plugin is not loaded"
887+
assert self.class.post_install_is_called,
888+
"post install hook registered by plugin is not called"
889+
890+
self.class.plugin_loaded = false
891+
$LOADED_FEATURES.delete(plugin_path)
892+
installer_new = util_setup_installer do |spec_new|
893+
spec_new.version = spec_version.version.succ
894+
plugin_path = File.join("lib", "rubygems_plugin.rb")
895+
write_file File.join(@tempdir, plugin_path) do |io|
896+
io.write "#{self.class}.plugin_loaded = true"
897+
end
898+
spec_new.files += [plugin_path]
899+
end
900+
build_rake_in do
901+
installer_new.install
902+
end
903+
assert !self.class.plugin_loaded,
904+
"plugin is loaded even when old version is already loaded"
905+
end
906+
859907
def test_keeps_plugins_up_to_date
860908
# NOTE: version a-2 is already installed by setup hooks
861909

862910
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
863-
io.write "puts __FILE__"
911+
io.write "# do nothing"
864912
end
865913

866914
build_rake_in do

test/rubygems/test_gem_uninstaller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_remove_symlinked_gem_home
173173

174174
def test_remove_plugins
175175
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
176-
io.write "puts __FILE__"
176+
io.write "# do nothing"
177177
end
178178

179179
@spec.files += %w[lib/rubygems_plugin.rb]
@@ -190,7 +190,7 @@ def test_remove_plugins
190190

191191
def test_remove_plugins_with_install_dir
192192
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
193-
io.write "puts __FILE__"
193+
io.write "# do nothing"
194194
end
195195

196196
@spec.files += %w[lib/rubygems_plugin.rb]
@@ -208,7 +208,7 @@ def test_remove_plugins_with_install_dir
208208

209209
def test_regenerate_plugins_for
210210
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
211-
io.write "puts __FILE__"
211+
io.write "# do nothing"
212212
end
213213

214214
@spec.files += %w[lib/rubygems_plugin.rb]
@@ -635,7 +635,7 @@ def test_uninstall_no_permission
635635

636636
def test_uninstall_keeps_plugins_up_to_date
637637
write_file File.join(@tempdir, "lib", "rubygems_plugin.rb") do |io|
638-
io.write "puts __FILE__"
638+
io.write "# do nothing"
639639
end
640640

641641
plugin_path = File.join Gem.plugindir, "a_plugin.rb"

0 commit comments

Comments
 (0)