Merge RubyGems-3.3.23 and Bundler-2.3.23

This commit is contained in:
Hiroshi SHIBATA 2022-10-06 15:43:48 +09:00 committed by nagachika
parent d2f4cbf042
commit d77e6e653d
35 changed files with 736 additions and 414 deletions

View file

@ -32,7 +32,11 @@ module Bundler
file << spec.to_gemfile file << spec.to_gemfile
end end
else else
FileUtils.cp(File.expand_path("../templates/#{gemfile}", __dir__), gemfile) File.open(File.expand_path("../templates/#{gemfile}", __dir__), "r") do |template|
File.open(gemfile, "wb") do |destination|
IO.copy_stream(template, destination)
end
end
end end
puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}" puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}"

View file

@ -106,6 +106,7 @@ module Bundler
@locked_gems = nil @locked_gems = nil
@locked_deps = {} @locked_deps = {}
@locked_specs = SpecSet.new([]) @locked_specs = SpecSet.new([])
@originally_locked_specs = @locked_specs
@locked_sources = [] @locked_sources = []
@locked_platforms = [] @locked_platforms = []
end end
@ -149,18 +150,7 @@ module Bundler
end end
def gem_version_promoter def gem_version_promoter
@gem_version_promoter ||= begin @gem_version_promoter ||= GemVersionPromoter.new(@originally_locked_specs, @unlock[:gems])
locked_specs =
if unlocking? && @locked_specs.empty? && !@lockfile_contents.empty?
# Definition uses an empty set of locked_specs to indicate all gems
# are unlocked, but GemVersionPromoter needs the locked_specs
# for conservative comparison.
Bundler::SpecSet.new(@locked_gems.specs)
else
@locked_specs
end
GemVersionPromoter.new(locked_specs, @unlock[:gems])
end
end end
def resolve_only_locally! def resolve_only_locally!

View file

@ -67,7 +67,6 @@ module Bundler
gemspecs = Gem::Util.glob_files_in_dir("{,*}.gemspec", expanded_path).map {|g| Bundler.load_gemspec(g) }.compact gemspecs = Gem::Util.glob_files_in_dir("{,*}.gemspec", expanded_path).map {|g| Bundler.load_gemspec(g) }.compact
gemspecs.reject! {|s| s.name != name } if name gemspecs.reject! {|s| s.name != name } if name
Index.sort_specs(gemspecs)
specs_by_name_and_version = gemspecs.group_by {|s| [s.name, s.version] } specs_by_name_and_version = gemspecs.group_by {|s| [s.name, s.version] }
case specs_by_name_and_version.size case specs_by_name_and_version.size

View file

@ -116,15 +116,14 @@ module Bundler
end end
def sort_dep_specs(spec_groups, locked_spec) def sort_dep_specs(spec_groups, locked_spec)
return spec_groups unless locked_spec @locked_version = locked_spec&.version
@gem_name = locked_spec.name @gem_name = locked_spec&.name
@locked_version = locked_spec.version
result = spec_groups.sort do |a, b| result = spec_groups.sort do |a, b|
@a_ver = a.version @a_ver = a.version
@b_ver = b.version @b_ver = b.version
unless @prerelease_specified[@gem_name] unless @gem_name && @prerelease_specified[@gem_name]
a_pre = @a_ver.prerelease? a_pre = @a_ver.prerelease?
b_pre = @b_ver.prerelease? b_pre = @b_ver.prerelease?
@ -148,7 +147,7 @@ module Bundler
end end
def either_version_older_than_locked def either_version_older_than_locked
@a_ver < @locked_version || @b_ver < @locked_version @locked_version && (@a_ver < @locked_version || @b_ver < @locked_version)
end end
def segments_do_not_match(level) def segments_do_not_match(level)
@ -157,7 +156,7 @@ module Bundler
end end
def unlocking_gem? def unlocking_gem?
unlock_gems.empty? || unlock_gems.include?(@gem_name) unlock_gems.empty? || (@gem_name && unlock_gems.include?(@gem_name))
end end
# Specific version moves can't always reliably be done during sorting # Specific version moves can't always reliably be done during sorting
@ -165,7 +164,7 @@ module Bundler
def post_sort(result) def post_sort(result)
# default :major behavior in Bundler does not do this # default :major behavior in Bundler does not do this
return result if major? return result if major?
if unlocking_gem? if unlocking_gem? || @locked_version.nil?
result result
else else
move_version_to_end(result, @locked_version) move_version_to_end(result, @locked_version)

View file

@ -57,36 +57,13 @@ module Bundler
# Search this index's specs, and any source indexes that this index knows # Search this index's specs, and any source indexes that this index knows
# about, returning all of the results. # about, returning all of the results.
def search(query) def search(query)
sort_specs(unsorted_search(query))
end
def unsorted_search(query)
results = local_search(query) results = local_search(query)
return results unless @sources.any?
seen = results.map(&:full_name).uniq unless @sources.empty?
@sources.each do |source| @sources.each do |source|
source.unsorted_search(query).each do |spec| results.concat(source.search(query))
next if seen.include?(spec.full_name)
seen << spec.full_name
results << spec
end end
end results.uniq(&:full_name)
results
end
protected :unsorted_search
def self.sort_specs(specs)
specs.sort_by do |s|
platform_string = s.platform.to_s
[s.version, platform_string == RUBY ? NULL : platform_string]
end
end
def sort_specs(specs)
self.class.sort_specs(specs)
end end
def local_search(query) def local_search(query)

View file

@ -42,8 +42,7 @@ module Bundler
remove_from_candidates(spec) remove_from_candidates(spec)
end end
@gem_version_promoter.prerelease_specified = @prerelease_specified = {} requirements.each {|dep| prerelease_specified[dep.name] ||= dep.prerelease? }
requirements.each {|dep| @prerelease_specified[dep.name] ||= dep.prerelease? }
verify_gemfile_dependencies_are_found!(requirements) verify_gemfile_dependencies_are_found!(requirements)
result = @resolver.resolve(requirements). result = @resolver.resolve(requirements).
@ -127,13 +126,6 @@ module Bundler
results = results_for(dependency) + locked_results results = results_for(dependency) + locked_results
results = results.select {|spec| requirement_satisfied_by?(locked_requirement, nil, spec) } if locked_requirement results = results.select {|spec| requirement_satisfied_by?(locked_requirement, nil, spec) } if locked_requirement
if !@prerelease_specified[name] && locked_results.empty?
# Move prereleases to the beginning of the list, so they're considered
# last during resolution.
pre, results = results.partition {|spec| spec.version.prerelease? }
results = pre + results
end
if results.any? if results.any?
results = @gem_version_promoter.sort_versions(dependency, results) results = @gem_version_promoter.sort_versions(dependency, results)
@ -221,6 +213,10 @@ module Bundler
@base.base_requirements @base.base_requirements
end end
def prerelease_specified
@gem_version_promoter.prerelease_specified
end
def remove_from_candidates(spec) def remove_from_candidates(spec)
@base.delete(spec) @base.delete(spec)
@ -255,7 +251,7 @@ module Bundler
all - 1_000_000 all - 1_000_000
else else
search = search_for(dependency) search = search_for(dependency)
search = @prerelease_specified[dependency.name] ? search.count : search.count {|s| !s.version.prerelease? } search = prerelease_specified[dependency.name] ? search.count : search.count {|s| !s.version.prerelease? }
search - all search - all
end end
end end
@ -284,7 +280,7 @@ module Bundler
end end
def gem_not_found_message(name, requirement, source, extra_message = "") def gem_not_found_message(name, requirement, source, extra_message = "")
specs = source.specs.search(name) specs = source.specs.search(name).sort_by {|s| [s.version, s.platform.to_s] }
matching_part = name matching_part = name
requirement_label = SharedHelpers.pretty_dependency(requirement) requirement_label = SharedHelpers.pretty_dependency(requirement)
cache_message = begin cache_message = begin

View file

@ -261,10 +261,21 @@ module Gem
# version # version
( (
(@os != "linux" && (@version.nil? || other.version.nil?)) || (@os != "linux" && (@version.nil? || other.version.nil?)) ||
(@os == "linux" && (other.version == "gnu#{@version}" || other.version == "musl#{@version}" || @version == "gnu#{other.version}")) || (@os == "linux" && (normalized_linux_version_ext == other.normalized_linux_version_ext || ["musl#{@version}", "musleabi#{@version}", "musleabihf#{@version}"].include?(other.version))) ||
@version == other.version @version == other.version
) )
end end
# This is a copy of RubyGems 3.3.23 or higher `normalized_linux_method`.
# Once only 3.3.23 is supported, we can use the method in RubyGems.
def normalized_linux_version_ext
return nil unless @version
without_gnu_nor_abi_modifiers = @version.sub(/\Agnu/, "").sub(/eabi(hf)?\Z/, "")
return nil if without_gnu_nor_abi_modifiers.empty?
without_gnu_nor_abi_modifiers
end
end end
end end

View file

@ -176,7 +176,7 @@ module Bundler
def lookup def lookup
@lookup ||= begin @lookup ||= begin
lookup = Hash.new {|h, k| h[k] = [] } lookup = Hash.new {|h, k| h[k] = [] }
Index.sort_specs(@specs).reverse_each do |s| @specs.each do |s|
lookup[s.name] << s lookup[s.name] << s
end end
lookup lookup

View file

@ -1,3 +1,4 @@
default:
image: ruby:<%= RUBY_VERSION %> image: ruby:<%= RUBY_VERSION %>
before_script: before_script:

View file

@ -1,7 +1,7 @@
# frozen_string_literal: false # frozen_string_literal: false
module Bundler module Bundler
VERSION = "2.3.22".freeze VERSION = "2.3.23".freeze
def self.bundler_major_version def self.bundler_major_version
@bundler_major_version ||= VERSION.split(".").first.to_i @bundler_major_version ||= VERSION.split(".").first.to_i

View file

@ -8,7 +8,7 @@
require "rbconfig" require "rbconfig"
module Gem module Gem
VERSION = "3.3.22".freeze VERSION = "3.3.23".freeze
end end
# Must be first since it unloads the prelude from 1.9.2 # Must be first since it unloads the prelude from 1.9.2

View file

@ -201,7 +201,8 @@ module Gem::GemcutterUtilities
# block was given or shows the response body to the user. # block was given or shows the response body to the user.
# #
# If the response was not successful, shows an error to the user including # If the response was not successful, shows an error to the user including
# the +error_prefix+ and the response body. # the +error_prefix+ and the response body. If the response was a permanent redirect,
# shows an error to the user including the redirect location.
def with_response(response, error_prefix = nil) def with_response(response, error_prefix = nil)
case response case response
@ -211,6 +212,12 @@ module Gem::GemcutterUtilities
else else
say clean_text(response.body) say clean_text(response.body)
end end
when Net::HTTPPermanentRedirect, Net::HTTPRedirection then
message = "The request has redirected permanently to #{response['location']}. Please check your defined push host URL."
message = "#{error_prefix}: #{message}" if error_prefix
say clean_text(message)
terminate_interaction(ERROR_CODE)
else else
message = response.body message = response.body
message = "#{error_prefix}: #{message}" if error_prefix message = "#{error_prefix}: #{message}" if error_prefix

View file

@ -444,10 +444,10 @@ EOM
directories << mkdir directories << mkdir
end end
File.open destination, "wb" do |out| if entry.file?
out.write entry.read File.open(destination, "wb") {|out| out.write entry.read }
FileUtils.chmod file_mode(entry.header.mode), destination FileUtils.chmod file_mode(entry.header.mode), destination
end if entry.file? end
verbose destination verbose destination
end end
@ -467,7 +467,12 @@ EOM
end end
def file_mode(mode) # :nodoc: def file_mode(mode) # :nodoc:
((mode & 0111).zero? ? data_mode : prog_mode) || mode ((mode & 0111).zero? ? data_mode : prog_mode) ||
# If we're not using one of the default modes, then we're going to fall
# back to the mode from the tarball. In this case we need to mask it down
# to fit into 2^16 bits (the maximum value for a mode in CRuby since it
# gets put into an unsigned short).
(mode & ((1 << 16) - 1))
end end
## ##

View file

@ -22,6 +22,7 @@ class Gem::Platform
end end
def self.match_platforms?(platform, platforms) def self.match_platforms?(platform, platforms)
platform = Gem::Platform.new(platform) unless platform.is_a?(Gem::Platform)
platforms.any? do |local_platform| platforms.any? do |local_platform|
platform.nil? || platform.nil? ||
local_platform == platform || local_platform == platform ||
@ -162,6 +163,9 @@ class Gem::Platform
# runtime platform "no version" stands for 'gnu'. To be able to disinguish # runtime platform "no version" stands for 'gnu'. To be able to disinguish
# these, the method receiver is the gem platform, while the argument is # these, the method receiver is the gem platform, while the argument is
# the runtime platform. # the runtime platform.
#
#--
# NOTE: Until it can be removed, changes to this method must also be reflected in `bundler/lib/bundler/rubygems_ext.rb`
def ===(other) def ===(other)
return nil unless Gem::Platform === other return nil unless Gem::Platform === other
@ -180,11 +184,23 @@ class Gem::Platform
# version # version
( (
(@os != "linux" && (@version.nil? || other.version.nil?)) || (@os != "linux" && (@version.nil? || other.version.nil?)) ||
(@os == "linux" && (other.version == "gnu#{@version}" || other.version == "musl#{@version}" || @version == "gnu#{other.version}")) || (@os == "linux" && (normalized_linux_version == other.normalized_linux_version || ["musl#{@version}", "musleabi#{@version}", "musleabihf#{@version}"].include?(other.version))) ||
@version == other.version @version == other.version
) )
end end
#--
# NOTE: Until it can be removed, changes to this method must also be reflected in `bundler/lib/bundler/rubygems_ext.rb`
def normalized_linux_version
return nil unless @version
without_gnu_nor_abi_modifiers = @version.sub(/\Agnu/, "").sub(/eabi(hf)?\Z/, "")
return nil if without_gnu_nor_abi_modifiers.empty?
without_gnu_nor_abi_modifiers
end
## ##
# Does +other+ match this platform? If +other+ is a String it will be # Does +other+ match this platform? If +other+ is a String it will be
# converted to a Gem::Platform first. See #=== for matching rules. # converted to a Gem::Platform first. See #=== for matching rules.

View file

@ -246,7 +246,7 @@ class Gem::Resolver
sources.each do |source| sources.each do |source|
groups[source]. groups[source].
sort_by {|spec| [spec.version, Gem::Platform.local =~ spec.platform ? 1 : 0] }. sort_by {|spec| [spec.version, spec.platform =~ Gem::Platform.local ? 1 : 0] }.
map {|spec| ActivationRequest.new spec, dependency }. map {|spec| ActivationRequest.new spec, dependency }.
each {|activation_request| activation_requests << activation_request } each {|activation_request| activation_requests << activation_request }
end end

View file

@ -7,6 +7,29 @@ RSpec.describe "bundle init" do
expect(bundled_app_gemfile).to be_file expect(bundled_app_gemfile).to be_file
end end
context "with a template with permission flags not matching current process umask" do
let(:template_file) do
gemfile = Bundler.preferred_gemfile_name
templates_dir.join(gemfile)
end
let(:target_dir) { bundled_app("init_permissions_test") }
around do |example|
old_chmod = File.stat(template_file).mode
FileUtils.chmod(old_chmod | 0o111, template_file) # chmod +x
example.run
FileUtils.chmod(old_chmod, template_file)
end
it "honours the current process umask when generating from a template" do
FileUtils.mkdir(target_dir)
bundle :init, :dir => target_dir
generated_mode = File.stat(File.join(target_dir, "Gemfile")).mode & 0o111
expect(generated_mode).to be_zero
end
end
context "when a Gemfile already exists" do context "when a Gemfile already exists" do
before do before do
create_file "Gemfile", <<-G create_file "Gemfile", <<-G

View file

@ -60,6 +60,8 @@ RSpec.configure do |config|
config.expect_with :rspec do |c| config.expect_with :rspec do |c|
c.syntax = :expect c.syntax = :expect
c.max_formatted_output_length = 1000
end end
config.mock_with :rspec do |mocks| config.mock_with :rspec do |mocks|

View file

@ -312,6 +312,10 @@ module Spec
source_root.join("tool/bundler") source_root.join("tool/bundler")
end end
def templates_dir
lib_dir.join("bundler", "templates")
end
extend self extend self
end end
end end

View file

@ -2,26 +2,11 @@
require "rubygems" require "rubygems"
# If bundler gemspec exists, add to stubs
bundler_gemspec = File.expand_path("../../bundler/bundler.gemspec", __dir__)
if File.exist?(bundler_gemspec)
Gem::Specification.dirs.unshift File.dirname(bundler_gemspec)
Gem::Specification.class_variable_set :@@stubs, nil
Gem::Specification.stubs
Gem::Specification.dirs.shift
end
begin begin
gem "test-unit", "~> 3.0" gem "test-unit", "~> 3.0"
rescue Gem::LoadError rescue Gem::LoadError
end end
if File.exist?(bundler_gemspec)
require_relative "../../bundler/lib/bundler"
else
require "bundler"
end
require "test/unit" require "test/unit"
ENV["JARS_SKIP"] = "true" if Gem.java_platform? # avoid unnecessary and noisy `jar-dependencies` post install hook ENV["JARS_SKIP"] = "true" if Gem.java_platform? # avoid unnecessary and noisy `jar-dependencies` post install hook
@ -409,7 +394,6 @@ class Gem::TestCase < Test::Unit::TestCase
Gem.loaded_specs.clear Gem.loaded_specs.clear
Gem.instance_variable_set(:@activated_gem_paths, 0) Gem.instance_variable_set(:@activated_gem_paths, 0)
Gem.clear_default_specs Gem.clear_default_specs
Bundler.reset!
Gem.configuration.verbose = true Gem.configuration.verbose = true
Gem.configuration.update_sources = true Gem.configuration.update_sources = true
@ -465,7 +449,7 @@ class Gem::TestCase < Test::Unit::TestCase
FileUtils.rm_rf @tempdir FileUtils.rm_rf @tempdir
ENV.replace(@orig_env) restore_env
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE, Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
@ -575,6 +559,7 @@ class Gem::TestCase < Test::Unit::TestCase
Dir.chdir directory do Dir.chdir directory do
unless File.exist? ".git" unless File.exist? ".git"
system @git, "init", "--quiet" system @git, "init", "--quiet"
system @git, "checkout", "-b", "master", "--quiet"
system @git, "config", "user.name", "RubyGems Tests" system @git, "config", "user.name", "RubyGems Tests"
system @git, "config", "user.email", "rubygems@example" system @git, "config", "user.email", "rubygems@example"
end end
@ -1301,6 +1286,10 @@ Also, a list:
$LOAD_PATH.find {|p| p == File.dirname($LOADED_FEATURES.find {|f| f.end_with?("/rubygems.rb") }) } $LOAD_PATH.find {|p| p == File.dirname($LOADED_FEATURES.find {|f| f.end_with?("/rubygems.rb") }) }
end end
def bundler_path
$LOAD_PATH.find {|p| p == File.dirname($LOADED_FEATURES.find {|f| f.end_with?("/bundler.rb") }) }
end
def with_clean_path_to_ruby def with_clean_path_to_ruby
orig_ruby = Gem.ruby orig_ruby = Gem.ruby
@ -1581,6 +1570,23 @@ Also, a list:
PUBLIC_KEY = nil PUBLIC_KEY = nil
PUBLIC_CERT = nil PUBLIC_CERT = nil
end if Gem::HAVE_OPENSSL end if Gem::HAVE_OPENSSL
private
def restore_env
unless Gem.win_platform?
ENV.replace(@orig_env)
return
end
# Fallback logic for Windows below to workaround
# https://bugs.ruby-lang.org/issues/16798. Can be dropped once all
# supported rubies include the fix for that.
ENV.clear
@orig_env.each {|k, v| ENV[k] = v }
end
end end
# https://github.com/seattlerb/minitest/blob/13c48a03d84a2a87855a4de0c959f96800100357/lib/minitest/mock.rb#L192 # https://github.com/seattlerb/minitest/blob/13c48a03d84a2a87855a4de0c959f96800100357/lib/minitest/mock.rb#L192

Binary file not shown.

View file

@ -617,6 +617,7 @@ class TestGem < Gem::TestCase
end end
def test_self_use_gemdeps def test_self_use_gemdeps
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
FileUtils.mkdir_p "detect/a/b" FileUtils.mkdir_p "detect/a/b"
FileUtils.mkdir_p "detect/a/Isolate" FileUtils.mkdir_p "detect/a/Isolate"
@ -634,6 +635,7 @@ class TestGem < Gem::TestCase
end end
end end
end end
end
def test_self_dir def test_self_dir
assert_equal @gemhome, Gem.dir assert_equal @gemhome, Gem.dir
@ -775,6 +777,7 @@ class TestGem < Gem::TestCase
end end
def test_self_find_files_with_gemfile def test_self_find_files_with_gemfile
with_local_bundler_at(Gem.dir) do
cwd = File.expand_path("test/rubygems", PROJECT_DIR) cwd = File.expand_path("test/rubygems", PROJECT_DIR)
actual_load_path = $LOAD_PATH.unshift(cwd).dup actual_load_path = $LOAD_PATH.unshift(cwd).dup
@ -806,8 +809,8 @@ class TestGem < Gem::TestCase
assert_equal expected, Gem.find_files("sff/discover").sort assert_equal expected, Gem.find_files("sff/discover").sort
assert_equal expected, Gem.find_files("sff/**.rb").sort, "[ruby-core:31730]" assert_equal expected, Gem.find_files("sff/**.rb").sort, "[ruby-core:31730]"
ensure assert_equal cwd, actual_load_path.shift
assert_equal cwd, actual_load_path.shift unless Gem.java_platform? end
end end
def test_self_find_latest_files def test_self_find_latest_files
@ -1335,13 +1338,11 @@ class TestGem < Gem::TestCase
refute Gem.try_activate "nonexistent" refute Gem.try_activate "nonexistent"
end end
unless Gem.java_platform?
expected = "Ignoring ext-1 because its extensions are not built. " + expected = "Ignoring ext-1 because its extensions are not built. " +
"Try: gem pristine ext --version 1\n" "Try: gem pristine ext --version 1\n"
assert_equal expected, err assert_equal expected, err
end end
end
def test_self_use_paths_with_nils def test_self_use_paths_with_nils
orig_home = ENV.delete "GEM_HOME" orig_home = ENV.delete "GEM_HOME"
@ -1640,6 +1641,7 @@ class TestGem < Gem::TestCase
end end
def test_auto_activation_of_specific_gemdeps_file def test_auto_activation_of_specific_gemdeps_file
with_local_bundler_at(Gem.dir) do
a = util_spec "a", "1", nil, "lib/a.rb" a = util_spec "a", "1", nil, "lib/a.rb"
b = util_spec "b", "1", nil, "lib/b.rb" b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb" c = util_spec "c", "1", nil, "lib/c.rb"
@ -1660,8 +1662,10 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1 b-1 c-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1 b-1 c-1]), loaded_spec_names
end end
end end
end
def test_auto_activation_of_used_gemdeps_file def test_auto_activation_of_used_gemdeps_file
with_local_bundler_at(Gem.dir) do
a = util_spec "a", "1", nil, "lib/a.rb" a = util_spec "a", "1", nil, "lib/a.rb"
b = util_spec "b", "1", nil, "lib/b.rb" b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb" c = util_spec "c", "1", nil, "lib/c.rb"
@ -1684,19 +1688,18 @@ class TestGem < Gem::TestCase
assert_equal expected_specs, loaded_spec_names assert_equal expected_specs, loaded_spec_names
end end
end end
end
BUNDLER_LIB_PATH = File.expand_path $LOAD_PATH.find {|lp| File.file?(File.join(lp, "bundler.rb")) }
BUNDLER_FULL_NAME = "bundler-#{Bundler::VERSION}".freeze
def add_bundler_full_name(names) def add_bundler_full_name(names)
names << BUNDLER_FULL_NAME names << "bundler-#{Bundler::VERSION}".freeze
names.sort! names.sort!
names names
end end
def test_looks_for_gemdeps_files_automatically_from_binstubs def test_looks_for_gemdeps_files_automatically_from_binstubs
pend "Requiring bundler messes things up" if Gem.java_platform? path = File.join(@tempdir, "gd-tmp")
with_local_bundler_at(path) do
a = util_spec "a", "1" do |s| a = util_spec "a", "1" do |s|
s.executables = %w[foo] s.executables = %w[foo]
s.bindir = "exe" s.bindir = "exe"
@ -1711,7 +1714,6 @@ class TestGem < Gem::TestCase
install_specs a, b, c install_specs a, b, c
path = File.join(@tempdir, "gd-tmp")
install_gem a, :install_dir => path install_gem a, :install_dir => path
install_gem b, :install_dir => path install_gem b, :install_dir => path
install_gem c, :install_dir => path install_gem c, :install_dir => path
@ -1720,7 +1722,7 @@ class TestGem < Gem::TestCase
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR) new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)
new_RUBYOPT = "-I#{rubygems_path} -I#{BUNDLER_LIB_PATH}" new_RUBYOPT = "-I#{rubygems_path} -I#{bundler_path}"
path = File.join @tempdir, "gem.deps.rb" path = File.join @tempdir, "gem.deps.rb"
@ -1742,9 +1744,13 @@ class TestGem < Gem::TestCase
assert_equal ["b-1", "c-1"], out - out0 assert_equal ["b-1", "c-1"], out - out0
end end
end end
end
def test_looks_for_gemdeps_files_automatically_from_binstubs_in_parent_dir def test_looks_for_gemdeps_files_automatically_from_binstubs_in_parent_dir
pend "Requiring bundler messes things up" if Gem.java_platform? path = File.join(@tempdir, "gd-tmp")
with_local_bundler_at(path) do
pend "IO.popen has issues on JRuby when passed :chdir" if Gem.java_platform?
a = util_spec "a", "1" do |s| a = util_spec "a", "1" do |s|
s.executables = %w[foo] s.executables = %w[foo]
@ -1760,7 +1766,6 @@ class TestGem < Gem::TestCase
install_specs a, b, c install_specs a, b, c
path = File.join(@tempdir, "gd-tmp")
install_gem a, :install_dir => path install_gem a, :install_dir => path
install_gem b, :install_dir => path install_gem b, :install_dir => path
install_gem c, :install_dir => path install_gem c, :install_dir => path
@ -1771,7 +1776,7 @@ class TestGem < Gem::TestCase
Dir.mkdir "sub1" Dir.mkdir "sub1"
new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR) new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)
new_RUBYOPT = "-I#{rubygems_path} -I#{BUNDLER_LIB_PATH}" new_RUBYOPT = "-I#{rubygems_path} -I#{bundler_path}"
path = File.join @tempdir, "gem.deps.rb" path = File.join @tempdir, "gem.deps.rb"
@ -1795,6 +1800,7 @@ class TestGem < Gem::TestCase
assert_equal ["b-1", "c-1"], out - out0 assert_equal ["b-1", "c-1"], out - out0
end end
end end
end
def test_register_default_spec def test_register_default_spec
Gem.clear_default_specs Gem.clear_default_specs
@ -1837,6 +1843,7 @@ class TestGem < Gem::TestCase
end end
def test_use_gemdeps def test_use_gemdeps
with_local_bundler_at(Gem.dir) do
gem_deps_file = "gem.deps.rb".tap(&Gem::UNTAINT) gem_deps_file = "gem.deps.rb".tap(&Gem::UNTAINT)
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1855,8 +1862,10 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
refute_nil Gem.gemdeps refute_nil Gem.gemdeps
end end
end
def test_use_gemdeps_ENV def test_use_gemdeps_ENV
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps(nil) do with_rubygems_gemdeps(nil) do
spec = util_spec "a", 1 spec = util_spec "a", 1
@ -1871,8 +1880,10 @@ class TestGem < Gem::TestCase
refute spec.activated? refute spec.activated?
end end
end end
end
def test_use_gemdeps_argument_missing def test_use_gemdeps_argument_missing
with_local_bundler_at(Gem.dir) do
e = assert_raise ArgumentError do e = assert_raise ArgumentError do
Gem.use_gemdeps "gem.deps.rb" Gem.use_gemdeps "gem.deps.rb"
end end
@ -1880,8 +1891,10 @@ class TestGem < Gem::TestCase
assert_equal "Unable to find gem dependencies file at gem.deps.rb", assert_equal "Unable to find gem dependencies file at gem.deps.rb",
e.message e.message
end end
end
def test_use_gemdeps_argument_missing_match_ENV def test_use_gemdeps_argument_missing_match_ENV
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("gem.deps.rb") do with_rubygems_gemdeps("gem.deps.rb") do
e = assert_raise ArgumentError do e = assert_raise ArgumentError do
Gem.use_gemdeps "gem.deps.rb" Gem.use_gemdeps "gem.deps.rb"
@ -1891,8 +1904,10 @@ class TestGem < Gem::TestCase
e.message e.message
end end
end end
end
def test_use_gemdeps_automatic def test_use_gemdeps_automatic
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1909,16 +1924,20 @@ class TestGem < Gem::TestCase
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
end end
end end
end
def test_use_gemdeps_automatic_missing def test_use_gemdeps_automatic_missing
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("-") do with_rubygems_gemdeps("-") do
Gem.use_gemdeps Gem.use_gemdeps
assert true # count assert true # count
end end
end end
end
def test_use_gemdeps_disabled def test_use_gemdeps_disabled
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("") do with_rubygems_gemdeps("") do
spec = util_spec "a", 1 spec = util_spec "a", 1
@ -1933,8 +1952,10 @@ class TestGem < Gem::TestCase
refute spec.activated? refute spec.activated?
end end
end end
end
def test_use_gemdeps_missing_gem def test_use_gemdeps_missing_gem
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("x") do with_rubygems_gemdeps("x") do
File.open "x", "w" do |io| File.open "x", "w" do |io|
io.write 'gem "a"' io.write 'gem "a"'
@ -1955,8 +1976,10 @@ You may need to `bundle install` to install missing gems
end end
end end
end end
end
def test_use_gemdeps_specific def test_use_gemdeps_specific
with_local_bundler_at(Gem.dir) do
with_rubygems_gemdeps("x") do with_rubygems_gemdeps("x") do
spec = util_spec "a", 1 spec = util_spec "a", 1
install_specs spec install_specs spec
@ -1973,6 +1996,7 @@ You may need to `bundle install` to install missing gems
assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names assert_equal add_bundler_full_name(%W[a-1]), loaded_spec_names
end end
end end
end
def test_operating_system_defaults def test_operating_system_defaults
operating_system_defaults = Gem.operating_system_defaults operating_system_defaults = Gem.operating_system_defaults
@ -2109,4 +2133,22 @@ You may need to `bundle install` to install missing gems
ensure ensure
ENV["RUBYGEMS_GEMDEPS"] = rubygems_gemdeps ENV["RUBYGEMS_GEMDEPS"] = rubygems_gemdeps
end end
def with_local_bundler_at(path)
require "bundler"
# If bundler gemspec exists, pretend it's installed
bundler_gemspec = File.expand_path("../../bundler/bundler.gemspec", __dir__)
if File.exist?(bundler_gemspec)
target_gemspec_location = "#{path}/specifications/bundler-#{Bundler::VERSION}.gemspec"
FileUtils.mkdir_p File.dirname(target_gemspec_location)
File.write target_gemspec_location, Gem::Specification.load(bundler_gemspec).to_ruby_for_cache
end
yield
ensure
Bundler.reset!
end
end end

View file

@ -36,7 +36,7 @@ class TestGemCommandsOwnerCommand < Gem::TestCase
- id: 4 - id: 4
EOF EOF
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.show_owners("freewill") @cmd.show_owners("freewill")
@ -66,7 +66,7 @@ EOF
- id: 4 - id: 4
EOF EOF
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
assert_raise Psych::DisallowedClass do assert_raise Psych::DisallowedClass do
use_ui @ui do use_ui @ui do
@ -80,7 +80,7 @@ EOF
host = "http://rubygems.example" host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host ENV["RUBYGEMS_HOST"] = host
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"] @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.show_owners("freewill") @cmd.show_owners("freewill")
@ -95,7 +95,7 @@ EOF
host = "http://rubygems.example" host = "http://rubygems.example"
@cmd.host = host @cmd.host = host
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"] @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.show_owners("freewill") @cmd.show_owners("freewill")
@ -107,7 +107,7 @@ EOF
def test_show_owners_denied def test_show_owners_denied
response = "You don't have permission to push to this gem" response = "You don't have permission to push to this gem"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 403, "Forbidden"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do use_ui @stub_ui do
@ -118,9 +118,32 @@ EOF
assert_match response, @stub_ui.output assert_match response, @stub_ui.output
end end
def test_show_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
path = "/api/v1/gems/freewill/owners.yaml"
redirected_uri = "https://rubygems.example#{path}"
@stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "301",
msg: "Moved Permanently",
headers: { "location" => redirected_uri }
)
assert_raise Gem::MockGemUi::TermError do
use_ui @stub_ui do
@cmd.show_owners("freewill")
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
end
def test_show_owners_key def test_show_owners_key
response = "- email: user1@example.com\n" response = "- email: user1@example.com\n"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
File.open Gem.configuration.credentials_path, "a" do |f| File.open Gem.configuration.credentials_path, "a" do |f|
f.write ":other: 701229f217cdf23b1344c7b4b54ca97" f.write ":other: 701229f217cdf23b1344c7b4b54ca97"
end end
@ -134,7 +157,7 @@ EOF
def test_add_owners def test_add_owners
response = "Owner added successfully." response = "Owner added successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"]) @cmd.add_owners("freewill", ["user-new1@example.com"])
@ -149,7 +172,7 @@ EOF
def test_add_owners_denied def test_add_owners_denied
response = "You don't have permission to push to this gem" response = "You don't have permission to push to this gem"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, "Forbidden"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"]) @cmd.add_owners("freewill", ["user-new1@example.com"])
@ -158,12 +181,33 @@ EOF
assert_match response, @stub_ui.output assert_match response, @stub_ui.output
end end
def test_add_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
path = "/api/v1/gems/freewill/owners"
redirected_uri = "https://rubygems.example#{path}"
@stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "308",
msg: "Permanent Redirect",
headers: { "location" => redirected_uri }
)
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
end
def test_add_owner_with_host_option_through_execute def test_add_owner_with_host_option_through_execute
host = "http://rubygems.example" host = "http://rubygems.example"
add_owner_response = "Owner added successfully." add_owner_response = "Owner added successfully."
show_owners_response = "- email: user1@example.com\n" show_owners_response = "- email: user1@example.com\n"
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = [add_owner_response, 200, "OK"] @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: add_owner_response, code: 200, msg: "OK")
@stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = [show_owners_response, 200, "OK"] @stub_fetcher.data["#{host}/api/v1/gems/freewill/owners.yaml"] = HTTPResponseFactory.create(body: show_owners_response, code: 200, msg: "OK")
@cmd.handle_options %W[--host #{host} --add user-new1@example.com freewill] @cmd.handle_options %W[--host #{host} --add user-new1@example.com freewill]
@ -178,7 +222,7 @@ EOF
def test_add_owners_key def test_add_owners_key
response = "Owner added successfully." response = "Owner added successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
File.open Gem.configuration.credentials_path, "a" do |f| File.open Gem.configuration.credentials_path, "a" do |f|
f.write ":other: 701229f217cdf23b1344c7b4b54ca97" f.write ":other: 701229f217cdf23b1344c7b4b54ca97"
end end
@ -192,7 +236,7 @@ EOF
def test_remove_owners def test_remove_owners
response = "Owner removed successfully." response = "Owner removed successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.remove_owners("freewill", ["user-remove1@example.com"]) @cmd.remove_owners("freewill", ["user-remove1@example.com"])
@ -207,7 +251,7 @@ EOF
def test_remove_owners_denied def test_remove_owners_denied
response = "You don't have permission to push to this gem" response = "You don't have permission to push to this gem"
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 403, "Forbidden"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.remove_owners("freewill", ["user-remove1@example.com"]) @cmd.remove_owners("freewill", ["user-remove1@example.com"])
@ -216,9 +260,46 @@ EOF
assert_match response, @stub_ui.output assert_match response, @stub_ui.output
end end
def test_remove_owners_permanent_redirect
host = "http://rubygems.example"
ENV["RUBYGEMS_HOST"] = host
path = "/api/v1/gems/freewill/owners"
redirected_uri = "https://rubygems.example#{path}"
@stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "308",
msg: "Permanent Redirect",
headers: { "location" => redirected_uri }
)
use_ui @stub_ui do
@cmd.remove_owners("freewill", ["user-remove1@example.com"])
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
path = "/api/v1/gems/freewill/owners"
redirected_uri = "https://rubygems.example#{path}"
@stub_fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "308",
msg: "Permanent Redirect",
headers: { "location" => redirected_uri }
)
use_ui @stub_ui do
@cmd.add_owners("freewill", ["user-new1@example.com"])
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @stub_ui.output
end
def test_remove_owners_key def test_remove_owners_key
response = "Owner removed successfully." response = "Owner removed successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 200, msg: "OK")
File.open Gem.configuration.credentials_path, "a" do |f| File.open Gem.configuration.credentials_path, "a" do |f|
f.write ":other: 701229f217cdf23b1344c7b4b54ca97" f.write ":other: 701229f217cdf23b1344c7b4b54ca97"
end end
@ -232,7 +313,7 @@ EOF
def test_remove_owners_missing def test_remove_owners_missing
response = "Owner could not be found." response = "Owner could not be found."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 404, "Not Found"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 404, msg: "Not Found")
use_ui @stub_ui do use_ui @stub_ui do
@cmd.remove_owners("freewill", ["missing@example"]) @cmd.remove_owners("freewill", ["missing@example"])
@ -246,8 +327,8 @@ EOF
response_success = "Owner added successfully." response_success = "Owner added successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [ @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [
[response_fail, 401, "Unauthorized"], HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@otp_ui = Gem::MockGemUi.new "111111\n" @otp_ui = Gem::MockGemUi.new "111111\n"
@ -263,7 +344,7 @@ EOF
def test_otp_verified_failure def test_otp_verified_failure
response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry." response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [response, 401, "Unauthorized"] @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized")
@otp_ui = Gem::MockGemUi.new "111111\n" @otp_ui = Gem::MockGemUi.new "111111\n"
use_ui @otp_ui do use_ui @otp_ui do
@ -281,10 +362,10 @@ EOF
response_success = "Owner removed successfully." response_success = "Owner removed successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [ @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [
[response_forbidden, 403, "Forbidden"], HTTPResponseFactory.create(body: response_forbidden, code: 403, msg: "Forbidden"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@stub_fetcher.data["#{Gem.host}/api/v1/api_key"] = ["", 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/api_key"] = HTTPResponseFactory.create(body: "", code: 200, msg: "OK")
@cmd.instance_variable_set :@scope, :remove_owner @cmd.instance_variable_set :@scope, :remove_owner
@stub_ui = Gem::MockGemUi.new "some@mail.com\npass\n" @stub_ui = Gem::MockGemUi.new "some@mail.com\npass\n"
@ -305,10 +386,10 @@ EOF
response_success = "Owner added successfully." response_success = "Owner added successfully."
@stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [ @stub_fetcher.data["#{Gem.host}/api/v1/gems/freewill/owners"] = [
[response_forbidden, 403, "Forbidden"], HTTPResponseFactory.create(body: response_forbidden, code: 403, msg: "Forbidden"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@stub_fetcher.data["#{Gem.host}/api/v1/api_key"] = ["", 200, "OK"] @stub_fetcher.data["#{Gem.host}/api/v1/api_key"] = HTTPResponseFactory.create(body: "", code: 200, msg: "OK")
@cmd.instance_variable_set :@scope, :add_owner @cmd.instance_variable_set :@scope, :add_owner
@stub_ui = Gem::MockGemUi.new "some@mail.com\npass\n" @stub_ui = Gem::MockGemUi.new "some@mail.com\npass\n"

View file

@ -68,7 +68,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
def test_execute def test_execute
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{Gem.host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
@cmd.options[:args] = [@path] @cmd.options[:args] = [@path]
@ -84,7 +84,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
host = "https://other.example" host = "https://other.example"
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
@fetcher.data["#{Gem.host}/api/v1/gems"] = @fetcher.data["#{Gem.host}/api/v1/gems"] =
["fail", 500, "Internal Server Error"] ["fail", 500, "Internal Server Error"]
@ -105,7 +105,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
end end
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{@spec.metadata['allowed_push_host']}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@spec.metadata['allowed_push_host']}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
@fetcher.data["#{Gem.host}/api/v1/gems"] = @fetcher.data["#{Gem.host}/api/v1/gems"] =
["fail", 500, "Internal Server Error"] ["fail", 500, "Internal Server Error"]
@ -136,7 +136,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
ENV["RUBYGEMS_HOST"] = @host ENV["RUBYGEMS_HOST"] = @host
Gem.configuration.disable_default_gem_server = true Gem.configuration.disable_default_gem_server = true
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
@ -163,14 +163,14 @@ class TestGemCommandsPushCommand < Gem::TestCase
FileUtils.rm Gem.configuration.credentials_path FileUtils.rm Gem.configuration.credentials_path
@response = "Successfully registered gem: freebird (1.0.1)" @response = "Successfully registered gem: freebird (1.0.1)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
def test_sending_gem def test_sending_gem
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
@ -197,7 +197,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
FileUtils.rm Gem.configuration.credentials_path FileUtils.rm Gem.configuration.credentials_path
@response = "Successfully registered gem: freebird (1.0.1)" @response = "Successfully registered gem: freebird (1.0.1)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
@ -212,7 +212,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
ENV["GEM_HOST_API_KEY"] = "PRIVKEY" ENV["GEM_HOST_API_KEY"] = "PRIVKEY"
@response = "Successfully registered gem: freebird (1.0.1)" @response = "Successfully registered gem: freebird (1.0.1)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
@ -238,7 +238,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
FileUtils.rm Gem.configuration.credentials_path FileUtils.rm Gem.configuration.credentials_path
@response = "Successfully registered gem: freebird (1.0.1)" @response = "Successfully registered gem: freebird (1.0.1)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
send_battery send_battery
end end
@ -309,7 +309,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
FileUtils.rm Gem.configuration.credentials_path FileUtils.rm Gem.configuration.credentials_path
@response = "Successfully registered gem: freebird (1.0.1)" @response = "Successfully registered gem: freebird (1.0.1)"
@fetcher.data["#{host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
# do not set @host # do not set @host
use_ui(@ui) { @cmd.send_gem(@path) } use_ui(@ui) { @cmd.send_gem(@path) }
@ -325,6 +325,27 @@ class TestGemCommandsPushCommand < Gem::TestCase
assert_match @response, @ui.output assert_match @response, @ui.output
end end
def test_sending_gem_to_host_permanent_redirect
@host = "http://rubygems.example"
redirected_uri = "https://rubygems.example/api/v1/gems"
@fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(
body: "",
code: 308,
msg: "Permanent Redirect",
headers: { "Location" => redirected_uri }
)
assert_raise Gem::MockGemUi::TermError do
use_ui @ui do
@cmd.instance_variable_set :@host, @host
@cmd.send_gem(@path)
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, @ui.output
end
def test_raises_error_with_no_arguments def test_raises_error_with_no_arguments
def @cmd.sign_in(*); end def @cmd.sign_in(*); end
assert_raise Gem::CommandLineError do assert_raise Gem::CommandLineError do
@ -334,7 +355,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
def test_sending_gem_denied def test_sending_gem_denied
response = "You don't have permission to push to this gem" response = "You don't have permission to push to this gem"
@fetcher.data["#{@host}/api/v1/gems"] = [response, 403, "Forbidden"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: response, code: 403, msg: "Forbidden")
@cmd.instance_variable_set :@host, @host @cmd.instance_variable_set :@host, @host
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
@ -348,7 +369,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
def test_sending_gem_key def test_sending_gem_key
@response = "Successfully registered gem: freewill (1.0.0)" @response = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{@host}/api/v1/gems"] = [@response, 200, "OK"] @fetcher.data["#{@host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
File.open Gem.configuration.credentials_path, "a" do |f| File.open Gem.configuration.credentials_path, "a" do |f|
f.write ":other: 701229f217cdf23b1344c7b4b54ca97" f.write ":other: 701229f217cdf23b1344c7b4b54ca97"
end end
@ -367,8 +388,8 @@ class TestGemCommandsPushCommand < Gem::TestCase
response_success = "Successfully registered gem: freewill (1.0.0)" response_success = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{Gem.host}/api/v1/gems"] = [ @fetcher.data["#{Gem.host}/api/v1/gems"] = [
[response_fail, 401, "Unauthorized"], HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@otp_ui = Gem::MockGemUi.new "111111\n" @otp_ui = Gem::MockGemUi.new "111111\n"
@ -384,7 +405,7 @@ class TestGemCommandsPushCommand < Gem::TestCase
def test_otp_verified_failure def test_otp_verified_failure
response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry." response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
@fetcher.data["#{Gem.host}/api/v1/gems"] = [response, 401, "Unauthorized"] @fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized")
@otp_ui = Gem::MockGemUi.new "111111\n" @otp_ui = Gem::MockGemUi.new "111111\n"
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
@ -405,12 +426,12 @@ class TestGemCommandsPushCommand < Gem::TestCase
response_success = "Successfully registered gem: freewill (1.0.0)" response_success = "Successfully registered gem: freewill (1.0.0)"
@fetcher.data["#{@host}/api/v1/gems"] = [ @fetcher.data["#{@host}/api/v1/gems"] = [
[response_mfa_enabled, 401, "Unauthorized"], HTTPResponseFactory.create(body: response_mfa_enabled, code: 401, msg: "Unauthorized"),
[response_forbidden, 403, "Forbidden"], HTTPResponseFactory.create(body: response_forbidden, code: 403, msg: "Forbidden"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@fetcher.data["#{@host}/api/v1/api_key"] = ["", 200, "OK"] @fetcher.data["#{@host}/api/v1/api_key"] = HTTPResponseFactory.create(body: "", code: 200, msg: "OK")
@cmd.instance_variable_set :@host, @host @cmd.instance_variable_set :@host, @host
@cmd.instance_variable_set :@scope, :push_rubygem @cmd.instance_variable_set :@scope, :push_rubygem
@ -438,16 +459,16 @@ class TestGemCommandsPushCommand < Gem::TestCase
response_profile = "mfa: disabled\n" response_profile = "mfa: disabled\n"
@fetcher.data["#{@host}/api/v1/gems"] = [ @fetcher.data["#{@host}/api/v1/gems"] = [
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@fetcher.data["#{@host}/api/v1/api_key"] = [ @fetcher.data["#{@host}/api/v1/api_key"] = [
[response_mfa_enabled, 401, "Unauthorized"], HTTPResponseFactory.create(body: response_mfa_enabled, code: 401, msg: "Unauthorized"),
["", 200, "OK"], HTTPResponseFactory.create(body: "", code: 200, msg: "OK"),
] ]
@fetcher.data["#{@host}/api/v1/profile/me.yaml"] = [ @fetcher.data["#{@host}/api/v1/profile/me.yaml"] = [
[response_profile, 200, "OK"], HTTPResponseFactory.create(body: response_profile, code: 200, msg: "OK"),
] ]
@cmd.instance_variable_set :@scope, :push_rubygem @cmd.instance_variable_set :@scope, :push_rubygem

View file

@ -71,6 +71,31 @@ class TestGemCommandsSigninCommand < Gem::TestCase
assert_equal api_key, credentials[host] assert_equal api_key, credentials[host]
end end
def test_execute_with_host_permanent_redirect
host = "http://rubygems.example/"
ENV["RUBYGEMS_HOST"] = host
path = "/api/v1/api_key"
redirected_uri = "http://rubygems.example#{path}"
fetcher = Gem::FakeFetcher.new
fetcher.data["#{host}#{path}"] = HTTPResponseFactory.create(
body: "",
code: "308",
msg: "Permanent Redirect",
headers: { "location" => redirected_uri }
)
Gem::RemoteFetcher.fetcher = fetcher
ui = Gem::MockGemUi.new("you@example.com\nsecret\n\n\n\n\n\n\n\n\n")
assert_raise Gem::MockGemUi::TermError do
use_ui ui do
@cmd.execute
end
end
response = "The request has redirected permanently to #{redirected_uri}. Please check your defined push host URL."
assert_match response, ui.output
end
def test_execute_with_valid_creds_set_for_default_host def test_execute_with_valid_creds_set_for_default_host
util_capture { @cmd.execute } util_capture { @cmd.execute }
@ -186,7 +211,7 @@ class TestGemCommandsSigninCommand < Gem::TestCase
# Set the expected response for the Web-API supplied # Set the expected response for the Web-API supplied
ENV["RUBYGEMS_HOST"] = host ENV["RUBYGEMS_HOST"] = host
data_key = "#{ENV['RUBYGEMS_HOST']}/api/v1/api_key" data_key = "#{ENV['RUBYGEMS_HOST']}/api/v1/api_key"
fetcher.data[data_key] = [api_key, 200, "OK"] fetcher.data[data_key] = HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
use_ui key_name_ui do use_ui key_name_ui do
@cmd.execute @cmd.execute
@ -209,8 +234,8 @@ class TestGemCommandsSigninCommand < Gem::TestCase
def util_capture(ui_stub = nil, host = nil, api_key = nil, fetcher = Gem::FakeFetcher.new, mfa_level = "disabled", warning = nil) def util_capture(ui_stub = nil, host = nil, api_key = nil, fetcher = Gem::FakeFetcher.new, mfa_level = "disabled", warning = nil)
api_key ||= "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key ||= "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
response = [api_key, 200, "OK"] response = HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
profile_response = [ "mfa: #{mfa_level}\nwarning: #{warning}" , 200, "OK"] profile_response = HTTPResponseFactory.create(body: "mfa: #{mfa_level}\nwarning: #{warning}", code: 200, msg: "OK")
email = "you@example.com" email = "you@example.com"
password = "secret" password = "secret"

View file

@ -43,7 +43,7 @@ class TestGemCommandsYankCommand < Gem::TestCase
def test_execute def test_execute
yank_uri = "http://example/api/v1/gems/yank" yank_uri = "http://example/api/v1/gems/yank"
@fetcher.data[yank_uri] = ["Successfully yanked", 200, "OK"] @fetcher.data[yank_uri] = HTTPResponseFactory.create(body: "Successfully yanked", code: 200, msg: "OK")
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@cmd.options[:added_platform] = true @cmd.options[:added_platform] = true
@ -69,8 +69,8 @@ class TestGemCommandsYankCommand < Gem::TestCase
response_fail = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry." response_fail = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
yank_uri = "http://example/api/v1/gems/yank" yank_uri = "http://example/api/v1/gems/yank"
@fetcher.data[yank_uri] = [ @fetcher.data[yank_uri] = [
[response_fail, 401, "Unauthorized"], HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized"),
["Successfully yanked", 200, "OK"], HTTPResponseFactory.create(body: "Successfully yanked", code: 200, msg: "OK"),
] ]
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@ -92,7 +92,7 @@ class TestGemCommandsYankCommand < Gem::TestCase
def test_execute_with_otp_failure def test_execute_with_otp_failure
response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry." response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
yank_uri = "http://example/api/v1/gems/yank" yank_uri = "http://example/api/v1/gems/yank"
@fetcher.data[yank_uri] = [response, 401, "Unauthorized"] @fetcher.data[yank_uri] = HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized")
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@cmd.options[:added_platform] = true @cmd.options[:added_platform] = true
@ -111,7 +111,7 @@ class TestGemCommandsYankCommand < Gem::TestCase
def test_execute_key def test_execute_key
yank_uri = "http://example/api/v1/gems/yank" yank_uri = "http://example/api/v1/gems/yank"
@fetcher.data[yank_uri] = ["Successfully yanked", 200, "OK"] @fetcher.data[yank_uri] = HTTPResponseFactory.create(body: "Successfully yanked", code: 200, msg: "OK")
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@cmd.options[:version] = req("= 1.0") @cmd.options[:version] = req("= 1.0")
@ -129,7 +129,7 @@ class TestGemCommandsYankCommand < Gem::TestCase
def test_execute_host def test_execute_host
host = "https://other.example" host = "https://other.example"
yank_uri = "#{host}/api/v1/gems/yank" yank_uri = "#{host}/api/v1/gems/yank"
@fetcher.data[yank_uri] = ["Successfully yanked", 200, "OK"] @fetcher.data[yank_uri] = HTTPResponseFactory.create(body: "Successfully yanked", code: 200, msg: "OK")
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@cmd.options[:version] = req("= 1.0") @cmd.options[:version] = req("= 1.0")
@ -154,11 +154,11 @@ class TestGemCommandsYankCommand < Gem::TestCase
host = "http://example" host = "http://example"
@fetcher.data["#{host}/api/v1/gems/yank"] = [ @fetcher.data["#{host}/api/v1/gems/yank"] = [
[response_forbidden, 403, "Forbidden"], HTTPResponseFactory.create(body: response_forbidden, code: 403, msg: "Forbidden"),
[response_success, 200, "OK"], HTTPResponseFactory.create(body: response_success, code: 200, msg: "OK"),
] ]
@fetcher.data["#{host}/api/v1/api_key"] = ["", 200, "OK"] @fetcher.data["#{host}/api/v1/api_key"] = HTTPResponseFactory.create(body: "", code: 200, msg: "OK")
@cmd.options[:args] = %w[a] @cmd.options[:args] = %w[a]
@cmd.options[:added_platform] = true @cmd.options[:added_platform] = true
@cmd.options[:version] = req("= 1.0") @cmd.options[:version] = req("= 1.0")

View file

@ -93,7 +93,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in def test_sign_in
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in [api_key, 200, "OK"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert @fetcher.last_request["authorization"] assert @fetcher.last_request["authorization"]
@ -106,7 +106,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_host def test_sign_in_with_host
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in [api_key, 200, "OK"], "http://example.com", ["http://example.com"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com", ["http://example.com"]
assert_match "Enter your http://example.com credentials.", assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -120,7 +120,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_host_nil def test_sign_in_with_host_nil
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in [api_key, 200, "OK"], nil, [nil] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), nil, [nil]
assert_match "Enter your RubyGems.org credentials.", assert_match "Enter your RubyGems.org credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -133,7 +133,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_host_ENV def test_sign_in_with_host_ENV
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in [api_key, 200, "OK"], "http://example.com" util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK"), "http://example.com"
assert_match "Enter your http://example.com credentials.", assert_match "Enter your http://example.com credentials.",
@sign_in_ui.output @sign_in_ui.output
@ -148,7 +148,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
Gem.configuration.rubygems_api_key = api_key Gem.configuration.rubygems_api_key = api_key
util_sign_in [api_key, 200, "OK"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_equal "", @sign_in_ui.output assert_equal "", @sign_in_ui.output
end end
@ -157,7 +157,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
Gem.configuration.api_keys[:KEY] = "other" Gem.configuration.api_keys[:KEY] = "other"
@cmd.options[:key] = :KEY @cmd.options[:key] = :KEY
util_sign_in [api_key, 200, "OK"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_equal "", @sign_in_ui.output assert_equal "", @sign_in_ui.output
end end
@ -169,7 +169,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
File.open Gem.configuration.credentials_path, "w" do |f| File.open Gem.configuration.credentials_path, "w" do |f|
f.write Hash[:other_api_key, other_api_key].to_yaml f.write Hash[:other_api_key, other_api_key].to_yaml
end end
util_sign_in [api_key, 200, "OK"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
assert_match %r{Signed in.}, @sign_in_ui.output assert_match %r{Signed in.}, @sign_in_ui.output
@ -181,7 +181,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def test_sign_in_with_bad_credentials def test_sign_in_with_bad_credentials
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
util_sign_in ["Access Denied.", 403, "Forbidden"] util_sign_in HTTPResponseFactory.create(body: "Access Denied.", code: 403, msg: "Forbidden")
end end
assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output
@ -192,7 +192,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
ENV["GEM_HOST_OTP_CODE"] = "111111" ENV["GEM_HOST_OTP_CODE"] = "111111"
api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903" api_key = "a5fdbb6ba150cbb83aad2bb2fede64cf040453903"
util_sign_in [api_key, 200, "OK"] util_sign_in HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
assert_match "Signed in with API key:", @sign_in_ui.output assert_match "Signed in with API key:", @sign_in_ui.output
assert_equal "111111", @fetcher.last_request["OTP"] assert_equal "111111", @fetcher.last_request["OTP"]
@ -204,7 +204,11 @@ class TestGemGemcutterUtilities < Gem::TestCase
util_sign_in(proc do util_sign_in(proc do
@call_count ||= 0 @call_count ||= 0
(@call_count += 1).odd? ? [response_fail, 401, "Unauthorized"] : [api_key, 200, "OK"] if (@call_count += 1).odd?
HTTPResponseFactory.create(body: response_fail, code: 401, msg: "Unauthorized")
else
HTTPResponseFactory.create(body: api_key, code: 200, msg: "OK")
end
end, nil, [], "111111\n") end, nil, [], "111111\n")
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
@ -217,7 +221,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry." response = "You have enabled multifactor authentication but your request doesn't have the correct OTP code. Please check it and retry."
assert_raise Gem::MockGemUi::TermError do assert_raise Gem::MockGemUi::TermError do
util_sign_in [response, 401, "Unauthorized"], nil, [], "111111\n" util_sign_in HTTPResponseFactory.create(body: response, code: 401, msg: "Unauthorized"), nil, [], "111111\n"
end end
assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output assert_match "You have enabled multi-factor authentication. Please enter OTP code.", @sign_in_ui.output
@ -229,7 +233,7 @@ class TestGemGemcutterUtilities < Gem::TestCase
def util_sign_in(response, host = nil, args = [], extra_input = "") def util_sign_in(response, host = nil, args = [], extra_input = "")
email = "you@example.com" email = "you@example.com"
password = "secret" password = "secret"
profile_response = [ "mfa: disabled\n" , 200, "OK"] profile_response = HTTPResponseFactory.create(body: "mfa: disabled\n", code: 200, msg: "OK")
if host if host
ENV["RUBYGEMS_HOST"] = host ENV["RUBYGEMS_HOST"] = host

View file

@ -510,6 +510,21 @@ class TestGemPackage < Gem::Package::TarTestCase
assert_path_exist @destination assert_path_exist @destination
end end
def test_extract_file_permissions
pend "chmod not supported" if win_platform?
gem_with_long_permissions = File.expand_path("packages/Bluebie-legs-0.6.2.gem", __dir__)
package = Gem::Package.new gem_with_long_permissions
package.extract_files @destination
filepath = File.join @destination, "README.rdoc"
assert_path_exist filepath
assert_equal 0104444, File.stat(filepath).mode
end
def test_extract_tar_gz_absolute def test_extract_tar_gz_absolute
package = Gem::Package.new @gem package = Gem::Package.new @gem

View file

@ -333,6 +333,34 @@ class TestGemPlatform < Gem::TestCase
refute(arm_linux_uclibceabi === arm_linux_eabi, "linux-uclibceabi =~ linux-eabi") refute(arm_linux_uclibceabi === arm_linux_eabi, "linux-uclibceabi =~ linux-eabi")
end end
def test_eabi_and_nil_version_combination_strictness
arm_linux = Gem::Platform.new "arm-linux"
arm_linux_eabi = Gem::Platform.new "arm-linux-eabi"
arm_linux_eabihf = Gem::Platform.new "arm-linux-eabihf"
arm_linux_gnueabi = Gem::Platform.new "arm-linux-gnueabi"
arm_linux_gnueabihf = Gem::Platform.new "arm-linux-gnueabihf"
arm_linux_musleabi = Gem::Platform.new "arm-linux-musleabi"
arm_linux_musleabihf = Gem::Platform.new "arm-linux-musleabihf"
arm_linux_uclibceabi = Gem::Platform.new "arm-linux-uclibceabi"
arm_linux_uclibceabihf = Gem::Platform.new "arm-linux-uclibceabihf"
# generic arm host runtime with eabi modifier accepts generic arm gems
assert(arm_linux === arm_linux_eabi, "arm-linux =~ arm-linux-eabi")
assert(arm_linux === arm_linux_eabihf, "arm-linux =~ arm-linux-eabihf")
# explicit gnu arm host runtime with eabi modifier accepts generic arm gems
assert(arm_linux === arm_linux_gnueabi, "arm-linux =~ arm-linux-gnueabi")
assert(arm_linux === arm_linux_gnueabihf, "arm-linux =~ arm-linux-gnueabihf")
# musl arm host runtime accepts libc-generic or statically linked gems...
assert(arm_linux === arm_linux_musleabi, "arm-linux =~ arm-linux-musleabi")
assert(arm_linux === arm_linux_musleabihf, "arm-linux =~ arm-linux-musleabihf")
# other libc arm hosts are not glibc compatible
refute(arm_linux === arm_linux_uclibceabi, "arm-linux =~ arm-linux-uclibceabi")
refute(arm_linux === arm_linux_uclibceabihf, "arm-linux =~ arm-linux-uclibceabihf")
end
def test_equals3_cpu_arm def test_equals3_cpu_arm
arm = Gem::Platform.new "arm-linux" arm = Gem::Platform.new "arm-linux"
armv5 = Gem::Platform.new "armv5-linux" armv5 = Gem::Platform.new "armv5-linux"
@ -452,6 +480,13 @@ class TestGemPlatform < Gem::TestCase
assert_equal 1, result.scan(/@version=/).size assert_equal 1, result.scan(/@version=/).size
end end
def test_gem_platform_match_with_string_argument
util_set_arch "x86_64-linux-musl"
assert(Gem::Platform.match(Gem::Platform.new("x86_64-linux")), "should match Gem::Platform")
assert(Gem::Platform.match("x86_64-linux"), "should match String platform")
end
def assert_local_match(name) def assert_local_match(name)
assert_match Gem::Platform.local, name assert_match Gem::Platform.local, name
end end

View file

@ -391,6 +391,39 @@ class TestGemResolver < Gem::TestCase
end end
end end
def test_pick_generic_linux_variants_on_musl_linux
util_set_arch "aarch64-linux-musl" do
is = Gem::Resolver::IndexSpecification
linux = Gem::Platform.new("aarch64-linux")
spec_fetcher do |fetcher|
fetcher.spec "libv8-node", "15.14.0.1" do |s|
s.platform = linux
end
fetcher.spec "libv8-node", "15.14.0.1"
end
v15 = v("15.14.0.1")
source = Gem::Source.new @gem_repo
s = set
v15_ruby = is.new s, "libv8-node", v15, source, Gem::Platform::RUBY
v15_linux = is.new s, "libv8-node", v15, source, linux.to_s
s.add v15_linux
s.add v15_ruby
ad = make_dep "libv8-node", "= 15.14.0.1"
res = Gem::Resolver.new([ad], s)
assert_resolves_to [v15_linux.spec], res
end
end
def test_only_returns_spec_once def test_only_returns_spec_once
a1 = util_spec "a", "1", "c" => "= 1" a1 = util_spec "a", "1", "c" => "= 1"
b1 = util_spec "b", "1", "c" => "= 1" b1 = util_spec "b", "1", "c" => "= 1"

View file

@ -252,9 +252,9 @@ class TestGemRequire < Gem::TestCase
# Activates a-1, but not b-1 and b-2 # Activates a-1, but not b-1 and b-2
assert_require "test_gem_require_a" assert_require "test_gem_require_a"
assert_equal %w[a-1], loaded_spec_names assert_equal %w[a-1], loaded_spec_names
assert $LOAD_PATH.include? a1.load_paths[0] assert $LOAD_PATH.include? a1.full_require_paths[0]
refute $LOAD_PATH.include? b1.load_paths[0] refute $LOAD_PATH.include? b1.full_require_paths[0]
refute $LOAD_PATH.include? b2.load_paths[0] refute $LOAD_PATH.include? b2.full_require_paths[0]
assert_equal unresolved_names, ["b (>= 1)"] assert_equal unresolved_names, ["b (>= 1)"]
@ -265,13 +265,13 @@ class TestGemRequire < Gem::TestCase
# and as a result #gem_original_require returns false. # and as a result #gem_original_require returns false.
refute require("benchmark"), "the benchmark stdlib should be recognized as already loaded" refute require("benchmark"), "the benchmark stdlib should be recognized as already loaded"
assert_includes $LOAD_PATH, b2.load_paths[0] assert_includes $LOAD_PATH, b2.full_require_paths[0]
assert_includes $LOAD_PATH, rubylibdir assert_includes $LOAD_PATH, rubylibdir
message = proc { message = proc {
"this test relies on the b-2 gem lib/ to be before stdlib to make sense\n" + "this test relies on the b-2 gem lib/ to be before stdlib to make sense\n" +
$LOAD_PATH.pretty_inspect $LOAD_PATH.pretty_inspect
} }
assert_operator $LOAD_PATH.index(b2.load_paths[0]), :<, $LOAD_PATH.index(rubylibdir), message assert_operator $LOAD_PATH.index(b2.full_require_paths[0]), :<, $LOAD_PATH.index(rubylibdir), message
# We detected that we should activate b-2, so we did so, but # We detected that we should activate b-2, so we did so, but
# then #gem_original_require decided "I've already got some benchmark.rb" loaded. # then #gem_original_require decided "I've already got some benchmark.rb" loaded.

View file

@ -54,13 +54,21 @@ class Gem::FakeFetcher
raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path) raise Gem::RemoteFetcher::FetchError.new("no data for #{path}", path)
end end
if @data[path].kind_of?(Array) && @data[path].first.kind_of?(Array) if @data[path].kind_of?(Array)
@data[path].shift @data[path].shift
else else
@data[path] @data[path]
end end
end end
def create_response(uri)
data = find_data(uri)
response = data.respond_to?(:call) ? data.call : data
raise TypeError, "#{response.class} is not a type of Net::HTTPResponse" unless response.kind_of?(Net::HTTPResponse)
response
end
def fetch_path(path, mtime = nil, head = false) def fetch_path(path, mtime = nil, head = false)
data = find_data(path) data = find_data(path)
@ -85,26 +93,16 @@ class Gem::FakeFetcher
# Thanks, FakeWeb! # Thanks, FakeWeb!
def open_uri_or_path(path) def open_uri_or_path(path)
data = find_data(path) find_data(path)
body, code, msg = data
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg) create_response(uri)
response.instance_variable_set(:@body, body)
response.instance_variable_set(:@read, true)
response
end end
def request(uri, request_class, last_modified = nil) def request(uri, request_class, last_modified = nil)
data = find_data(uri)
body, code, msg = (data.respond_to?(:call) ? data.call : data)
@last_request = request_class.new uri.request_uri @last_request = request_class.new uri.request_uri
yield @last_request if block_given? yield @last_request if block_given?
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg) create_response(uri)
response.instance_variable_set(:@body, body)
response.instance_variable_set(:@read, true)
response
end end
def pretty_print(q) # :nodoc: def pretty_print(q) # :nodoc:
@ -164,6 +162,30 @@ class Gem::FakeFetcher
end end
end end
##
# The HTTPResponseFactory allows easy creation of Net::HTTPResponse instances in RubyGems tests:
#
# Example:
#
# HTTPResponseFactory.create(
# body: "",
# code: 301,
# msg: "Moved Permanently",
# headers: { "location" => "http://example.com" }
# )
#
class HTTPResponseFactory
def self.create(body:, code:, msg:, headers: {})
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
response.instance_variable_set(:@body, body)
response.instance_variable_set(:@read, true)
headers.each {|name, value| response[name] = value }
response
end
end
# :stopdoc: # :stopdoc:
class Gem::RemoteFetcher class Gem::RemoteFetcher
def self.fetcher=(fetcher) def self.fetcher=(fetcher)

View file

@ -53,4 +53,4 @@ DEPENDENCIES
webrick (~> 1.6) webrick (~> 1.6)
BUNDLED WITH BUNDLED WITH
2.3.22 2.3.23

View file

@ -47,11 +47,13 @@ PLATFORMS
aarch64-linux aarch64-linux
arm64-darwin-20 arm64-darwin-20
arm64-darwin-21 arm64-darwin-21
arm64-darwin-22
universal-java-11 universal-java-11
universal-java-18 universal-java-18
x64-mingw-ucrt x64-mingw-ucrt
x86_64-darwin-19 x86_64-darwin-19
x86_64-darwin-20 x86_64-darwin-20
x86_64-darwin-21
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
@ -63,4 +65,4 @@ DEPENDENCIES
test-unit test-unit
BUNDLED WITH BUNDLED WITH
2.3.22 2.3.23

View file

@ -53,11 +53,13 @@ PLATFORMS
aarch64-linux aarch64-linux
arm64-darwin-20 arm64-darwin-20
arm64-darwin-21 arm64-darwin-21
arm64-darwin-22
universal-java-11 universal-java-11
universal-java-18 universal-java-18
x64-mingw-ucrt x64-mingw-ucrt
x86_64-darwin-19 x86_64-darwin-19
x86_64-darwin-20 x86_64-darwin-20
x86_64-darwin-21
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
@ -69,4 +71,4 @@ DEPENDENCIES
test-unit test-unit
BUNDLED WITH BUNDLED WITH
2.3.22 2.3.23

View file

@ -43,4 +43,4 @@ DEPENDENCIES
webrick (= 1.7.0) webrick (= 1.7.0)
BUNDLED WITH BUNDLED WITH
2.3.22 2.3.23