[rubygems/rubygems] TermError should inherit from SystemExit

The `gem owner` command rescues standard errors, but does not rescue
SystemExit errors. If TermError is a standard error, not a system exit,
tests don't behave like realworld for this command.

cf7d500f4d
This commit is contained in:
David Rodríguez 2024-10-31 19:47:01 +01:00 committed by git
parent 7fba517d50
commit 1b190b342b
2 changed files with 38 additions and 19 deletions

View file

@ -16,7 +16,7 @@ class Gem::MockGemUi < Gem::StreamUI
end
end
class TermError < RuntimeError
class TermError < SystemExit
attr_reader :exit_code
def initialize(exit_code)

View file

@ -176,9 +176,11 @@ EOF
response = "You don't have permission to push to this gem"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
assert_match response, @stub_ui.output
end
@ -196,9 +198,11 @@ EOF
headers: { "location" => redirected_uri }
)
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
@ -255,9 +259,11 @@ EOF
response = "You don't have permission to push to this gem"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
end
end
assert_match response, @stub_ui.output
end
@ -274,9 +280,11 @@ EOF
headers: { "location" => redirected_uri }
)
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
@ -291,9 +299,11 @@ EOF
headers: { "location" => redirected_uri }
)
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
@ -317,9 +327,11 @@ EOF
response = "Owner could not be found."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 404, msg: "Not Found")
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.remove_owners("freewill", ["missing@example"])
end
end
assert_equal "Removing missing@example: #{response}\n", @stub_ui.output
end
@ -346,9 +358,12 @@ EOF
HTTPResponseFactory.create(body: "You don't have any security devices", code: 422, msg: "Unprocessable Entity")
@otp_ui = Gem::MockGemUi.new "111111\n"
assert_raise Gem::MockGemUi::TermError do
use_ui @otp_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
assert_match response, @otp_ui.output
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @otp_ui.output
@ -389,11 +404,13 @@ EOF
TCPServer.stub(:new, server) do
Gem::GemcutterUtilities::WebauthnListener.stub(:listener_thread, Thread.new { Thread.current[:error] = error }) do
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
end
end
assert_match @stub_fetcher.last_request["Authorization"], Gem.configuration.rubygems_api_key
assert_match "You have enabled multi-factor authentication. Please visit #{@stub_fetcher.webauthn_url_with_port(server.port)} " \
@ -438,10 +455,12 @@ EOF
@stub_fetcher.respond_with_webauthn_polling_failure
TCPServer.stub(:new, server) do
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
end
end
assert_match @stub_fetcher.last_request["Authorization"], Gem.configuration.rubygems_api_key
assert_match "You have enabled multi-factor authentication. Please visit #{@stub_fetcher.webauthn_url_with_port(server.port)} " \