[rubygems/rubygems] Diagnose when OpenSSL can't be loaded.

https://github.com/rubygems/rubygems/commit/e6aa8aabcd
This commit is contained in:
Edouard CHIN 2025-04-09 23:42:59 +02:00 committed by Hiroshi SHIBATA
parent cda29294a9
commit ae308ae523
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2
3 changed files with 65 additions and 12 deletions

View File

@ -9,6 +9,9 @@ module Bundler
end
def run
return unless openssl_installed?
output_ssl_environment
end
private
@ -28,5 +31,29 @@ module Bundler
@verify_mode ||= mode.then {|mod| OpenSSL::SSL.const_get("verify_#{mod}".upcase) }
end
def openssl_installed?
require "openssl"
true
rescue LoadError
Bundler.ui.warn(<<~MSG)
Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}.
You'll need to recompile or reinstall Ruby with OpenSSL support and try again.
MSG
false
end
def output_ssl_environment
Bundler.ui.info(<<~MESSAGE)
Here's your OpenSSL environment:
OpenSSL: #{OpenSSL::VERSION}
Compiled with: #{OpenSSL::OPENSSL_VERSION}
Loaded with: #{OpenSSL::OPENSSL_LIBRARY_VERSION}
MESSAGE
end
end
end

View File

@ -4,14 +4,6 @@
require 'uri'
require 'net/http'
begin
require 'openssl'
rescue LoadError
puts "Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to #{host}.",
"You'll need to recompile or reinstall Ruby with OpenSSL support and try again."
exit 1
end
begin
# Some versions of Ruby need this require to do HTTPS
require 'net/https'
@ -39,10 +31,6 @@ puts
puts "Ruby: %s" % ruby_version
puts "RubyGems: %s" % Gem::VERSION if defined?(Gem::VERSION)
puts "Bundler: %s" % Bundler::VERSION if defined?(Bundler::VERSION)
puts "OpenSSL: %s" % OpenSSL::VERSION if defined?(OpenSSL::VERSION)
puts "Compiled with: %s" % OpenSSL::OPENSSL_VERSION
puts "Loaded with: %s" % OpenSSL::OPENSSL_LIBRARY_VERSION if defined?(OpenSSL::OPENSSL_LIBRARY_VERSION)
puts
def show_ssl_certs
puts "", "Below affect only Ruby net/http connections:"

View File

@ -5,4 +5,42 @@ require "bundler/cli/doctor"
require "bundler/cli/doctor/ssl"
RSpec.describe "bundle doctor ssl" do
before(:each) do
@previous_level = Bundler.ui.level
Bundler.ui.instance_variable_get(:@warning_history).clear
Bundler.ui.level = "info"
end
after(:each) do
Bundler.ui.level = @previous_level
end
context "when a diagnostic fails" do
it "prints the diagnostic when openssl can't be loaded" do
subject = Bundler::CLI::Doctor::SSL.new({})
allow(subject).to receive(:require).with("openssl").and_raise(LoadError)
expected_err = <<~MSG
Oh no! Your Ruby doesn't have OpenSSL, so it can't connect to rubygems.org.
You'll need to recompile or reinstall Ruby with OpenSSL support and try again.
MSG
expect { subject.run }.to output("").to_stdout.and output(expected_err).to_stderr
end
end
context "when no diagnostic fails" do
it "prints the SSL environment" do
expected_out = <<~MSG
Here's your OpenSSL environment:
OpenSSL: #{OpenSSL::VERSION}
Compiled with: #{OpenSSL::OPENSSL_VERSION}
Loaded with: #{OpenSSL::OPENSSL_LIBRARY_VERSION}
MSG
subject = Bundler::CLI::Doctor::SSL.new({})
expect { subject.run }.to output(expected_out).to_stdout.and output("").to_stderr
end
end
end