Skip to content

Commit bfa0c49

Browse files
Merge pull request #8074 from rubygems/deivid-rodriguez/incomplete-errors
Fix `Gem::MissingSpecVersionError#to_s` not showing exception message (cherry picked from commit 78c88f6)
1 parent 0939b86 commit bfa0c49

File tree

8 files changed

+27
-46
lines changed

8 files changed

+27
-46
lines changed

lib/rubygems/command_manager.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,11 @@ def find_command_possibilities(cmd_name)
230230
def load_and_instantiate(command_name)
231231
command_name = command_name.to_s
232232
const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase } << "Command"
233-
load_error = nil
234233

235234
begin
236-
begin
237-
require "rubygems/commands/#{command_name}_command"
238-
rescue LoadError => e
239-
load_error = e
240-
end
235+
require "rubygems/commands/#{command_name}_command"
241236
Gem::Commands.const_get(const_name).new
242-
rescue StandardError => e
243-
e = load_error if load_error
244-
237+
rescue StandardError, LoadError => e
245238
alert_error clean_text("Loading command: #{command_name} (#{e.class})\n\t#{e}")
246239
ui.backtrace e
247240
end

lib/rubygems/errors.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def initialize(name, requirement, extra_message=nil)
3030
@name = name
3131
@requirement = requirement
3232
@extra_message = extra_message
33+
super(message)
3334
end
3435

3536
def message # :nodoc:
@@ -53,8 +54,8 @@ class MissingSpecVersionError < MissingSpecError
5354
attr_reader :specs
5455

5556
def initialize(name, requirement, specs)
56-
super(name, requirement)
5757
@specs = specs
58+
super(name, requirement)
5859
end
5960

6061
private
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Gem::Commands::InsCommand < Gem::Command
4+
def initialize
5+
super("ins", "Does something different from install", {})
6+
end
7+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class Gem::Commands::InterruptCommand < Gem::Command
4+
def initialize
5+
super("interrupt", "Raises an Interrupt Exception", {})
6+
end
7+
8+
def execute
9+
raise Interrupt, "Interrupt exception"
10+
end
11+
end

test/rubygems/rubygems_plugin.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,4 @@
22

33
require "rubygems/command_manager"
44

5-
##
6-
# This is an example of exactly what NOT to do.
7-
#
8-
# DO NOT include code like this in your rubygems_plugin.rb
9-
10-
module Gem::Commands
11-
remove_const(:InterruptCommand) if defined?(InterruptCommand)
12-
end
13-
14-
class Gem::Commands::InterruptCommand < Gem::Command
15-
def initialize
16-
super("interrupt", "Raises an Interrupt Exception", {})
17-
end
18-
19-
def execute
20-
raise Interrupt, "Interrupt exception"
21-
end
22-
end
23-
245
Gem::CommandManager.instance.register_command :interrupt

test/rubygems/test_gem.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def setup
2121
common_installer_setup
2222

2323
@additional = %w[a b].map {|d| File.join @tempdir, d }
24-
25-
util_remove_interrupt_command
2624
end
2725

2826
def test_self_finish_resolve
@@ -1524,8 +1522,6 @@ def test_load_env_plugins
15241522
nil
15251523
end
15261524

1527-
util_remove_interrupt_command
1528-
15291525
# Should attempt to cause a StandardError
15301526
with_plugin("standarderror") { Gem.load_env_plugins }
15311527
begin
@@ -1534,8 +1530,6 @@ def test_load_env_plugins
15341530
nil
15351531
end
15361532

1537-
util_remove_interrupt_command
1538-
15391533
# Should attempt to cause an Exception
15401534
with_plugin("scripterror") { Gem.load_env_plugins }
15411535
begin
@@ -1791,11 +1785,6 @@ def util_exec_gem
17911785
spec
17921786
end
17931787

1794-
def util_remove_interrupt_command
1795-
Gem::Commands.send :remove_const, :InterruptCommand if
1796-
Gem::Commands.const_defined? :InterruptCommand
1797-
end
1798-
17991788
def util_cache_dir
18001789
File.join Gem.dir, "cache"
18011790
end

test/rubygems/test_gem_command_manager.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ def test_find_logout_alias_comamnd
5050
end
5151

5252
def test_find_command_ambiguous_exact
53-
ins_command = Class.new
54-
Gem::Commands.send :const_set, :InsCommand, ins_command
53+
old_load_path = $:.dup
54+
$: << File.expand_path("test/rubygems", PROJECT_DIR)
5555

5656
@command_manager.register_command :ins
5757

5858
command = @command_manager.find_command "ins"
5959

60-
assert_kind_of ins_command, command
60+
assert_kind_of Gem::Commands::InsCommand, command
6161
ensure
62-
Gem::Commands.send :remove_const, :InsCommand
62+
$:.replace old_load_path
63+
@command_manager.unregister_command :ins
6364
end
6465

6566
def test_find_command_unknown

test/rubygems/test_gem_commands_help_command.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ def setup
1111
super
1212

1313
@cmd = Gem::Commands::HelpCommand.new
14-
15-
load File.expand_path("rubygems_plugin.rb", __dir__) unless Gem::Commands.const_defined? :InterruptCommand
1614
end
1715

1816
def test_gem_help_bad

0 commit comments

Comments
 (0)