diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2019-12-13 20:19:08 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2019-12-13 20:19:33 +0900 |
commit | 82cc2843a92b286cc13afd0860a4e111d4ea2a0b (patch) | |
tree | a517dedd40d35540930cea7732f5a36e76a549e8 /test/rubygems | |
parent | 26774351dc5f494253ba031e4bc453dc4dddb2cf (diff) | |
download | ruby-82cc2843a92b286cc13afd0860a4e111d4ea2a0b.tar.gz |
Prepare to release RubyGems 3.1.0 final version.
Diffstat (limited to 'test/rubygems')
-rw-r--r-- | test/rubygems/test_gem_command.rb | 47 | ||||
-rw-r--r-- | test/rubygems/test_gem_commands_generate_index_command.rb | 38 | ||||
-rw-r--r-- | test/rubygems/test_gem_commands_help_command.rb | 7 | ||||
-rw-r--r-- | test/rubygems/test_gem_commands_sources_command.rb | 74 | ||||
-rw-r--r-- | test/rubygems/test_gem_gem_runner.rb | 3 | ||||
-rw-r--r-- | test/rubygems/test_gem_indexer.rb | 2 | ||||
-rw-r--r-- | test/rubygems/test_gem_source.rb | 14 | ||||
-rw-r--r-- | test/rubygems/test_remote_fetch_error.rb | 2 | ||||
-rw-r--r-- | test/rubygems/test_require.rb | 65 |
9 files changed, 200 insertions, 52 deletions
diff --git a/test/rubygems/test_gem_command.rb b/test/rubygems/test_gem_command.rb index d51371301e..230baa5356 100644 --- a/test/rubygems/test_gem_command.rb +++ b/test/rubygems/test_gem_command.rb @@ -197,9 +197,9 @@ class TestGemCommand < Gem::TestCase assert_equal ['-h', 'command'], args end - def test_deprecate_option_long_name + def test_deprecate_option deprecate_msg = <<-EXPECTED -WARNING: The \"--test\" option has been deprecated and will be removed in Rubygems 3.1, its use is discouraged. +WARNING: The \"--test\" option has been deprecated and will be removed in Rubygems 3.1. EXPECTED testCommand = Class.new(Gem::Command) do @@ -210,7 +210,7 @@ WARNING: The \"--test\" option has been deprecated and will be removed in Rubyg options[:test] = true end - deprecate_option(long_name: '--test', version: '3.1') + deprecate_option('--test', version: '3.1') end def execute @@ -228,7 +228,7 @@ WARNING: The \"--test\" option has been deprecated and will be removed in Rubyg def test_deprecate_option_no_version deprecate_msg = <<-EXPECTED -WARNING: The \"--test\" option has been deprecated and will be removed in future versions of Rubygems, its use is discouraged. +WARNING: The \"--test\" option has been deprecated and will be removed in future versions of Rubygems. EXPECTED testCommand = Class.new(Gem::Command) do @@ -239,7 +239,7 @@ WARNING: The \"--test\" option has been deprecated and will be removed in futur options[:test] = true end - deprecate_option(long_name: '--test') + deprecate_option('--test') end def execute @@ -255,9 +255,9 @@ WARNING: The \"--test\" option has been deprecated and will be removed in futur end end - def test_deprecate_option_short_name + def test_deprecate_option_extra_message deprecate_msg = <<-EXPECTED -WARNING: The \"-t\" option has been deprecated and will be removed in Rubygems 3.5, its use is discouraged. +WARNING: The \"--test\" option has been deprecated and will be removed in Rubygems 3.1. Whether you set `--test` mode or not, this dummy app always runs in test mode. EXPECTED testCommand = Class.new(Gem::Command) do @@ -268,7 +268,7 @@ WARNING: The \"-t\" option has been deprecated and will be removed in Rubygems options[:test] = true end - deprecate_option(short_name: '-t', version: '3.5') + deprecate_option('--test', version: '3.1', extra_msg: 'Whether you set `--test` mode or not, this dummy app always runs in test mode.') end def execute @@ -279,7 +279,36 @@ WARNING: The \"-t\" option has been deprecated and will be removed in Rubygems cmd = testCommand.new use_ui @ui do - cmd.invoke("-t") + cmd.invoke("--test") + assert_equal deprecate_msg, @ui.error + end + end + + def test_deprecate_option_extra_message_and_no_version + deprecate_msg = <<-EXPECTED +WARNING: The \"--test\" option has been deprecated and will be removed in future versions of Rubygems. Whether you set `--test` mode or not, this dummy app always runs in test mode. + EXPECTED + + testCommand = Class.new(Gem::Command) do + def initialize + super('test', 'Gem::Command instance for testing') + + add_option('-t', '--test', 'Test command') do |value, options| + options[:test] = true + end + + deprecate_option('--test', extra_msg: 'Whether you set `--test` mode or not, this dummy app always runs in test mode.') + end + + def execute + true + end + end + + cmd = testCommand.new + + use_ui @ui do + cmd.invoke("--test") assert_equal deprecate_msg, @ui.error end end diff --git a/test/rubygems/test_gem_commands_generate_index_command.rb b/test/rubygems/test_gem_commands_generate_index_command.rb index b4276702f4..d8fda32fc0 100644 --- a/test/rubygems/test_gem_commands_generate_index_command.rb +++ b/test/rubygems/test_gem_commands_generate_index_command.rb @@ -3,6 +3,10 @@ require 'rubygems/test_case' require 'rubygems/indexer' require 'rubygems/commands/generate_index_command' +unless defined?(Builder::XChar) + warn "generate_index tests are being skipped. Install builder gem." +end + class TestGemCommandsGenerateIndexCommand < Gem::TestCase def setup @@ -22,6 +26,18 @@ class TestGemCommandsGenerateIndexCommand < Gem::TestCase assert File.exist?(specs), specs end + def test_execute_no_modern + @cmd.options[:modern] = false + + use_ui @ui do + @cmd.execute + end + + specs = File.join @gemhome, "specs.4.8.gz" + + assert File.exist?(specs), specs + end + def test_handle_options_directory return if win_platform? refute_equal '/nonexistent', @cmd.options[:directory] @@ -47,4 +63,24 @@ class TestGemCommandsGenerateIndexCommand < Gem::TestCase assert @cmd.options[:update] end -end if ''.respond_to? :to_xs + def test_handle_options_modern + use_ui @ui do + @cmd.handle_options %w[--modern] + end + + assert_equal \ + "WARNING: The \"--modern\" option has been deprecated and will be removed in Rubygems 4.0. Modern indexes (specs, latest_specs, and prerelease_specs) are always generated, so this option is not needed.\n", + @ui.error + end + + def test_handle_options_no_modern + use_ui @ui do + @cmd.handle_options %w[--no-modern] + end + + assert_equal \ + "WARNING: The \"--no-modern\" option has been deprecated and will be removed in Rubygems 4.0. The `--no-modern` option is currently ignored. Modern indexes (specs, latest_specs, and prerelease_specs) are always generated.\n", + @ui.error + end + +end if defined?(Builder::XChar) diff --git a/test/rubygems/test_gem_commands_help_command.rb b/test/rubygems/test_gem_commands_help_command.rb index 8fcff6b1e7..f2a519775c 100644 --- a/test/rubygems/test_gem_commands_help_command.rb +++ b/test/rubygems/test_gem_commands_help_command.rb @@ -4,20 +4,15 @@ require "rubygems/test_case" require "rubygems/commands/help_command" require "rubygems/package" require "rubygems/command_manager" -require File.expand_path('../rubygems_plugin', __FILE__) class TestGemCommandsHelpCommand < Gem::TestCase - # previously this was calc'd in setup, but 1.8.7 had - # intermittent failures, but no issues with above require - PLUGIN = File.expand_path('../rubygems_plugin.rb', __FILE__) - def setup super @cmd = Gem::Commands::HelpCommand.new - load PLUGIN unless Gem::Commands.const_defined? :InterruptCommand + load File.expand_path('../rubygems_plugin.rb', __FILE__) unless Gem::Commands.const_defined? :InterruptCommand end def test_gem_help_bad diff --git a/test/rubygems/test_gem_commands_sources_command.rb b/test/rubygems/test_gem_commands_sources_command.rb index 36e6ee9293..b63fbce81f 100644 --- a/test/rubygems/test_gem_commands_sources_command.rb +++ b/test/rubygems/test_gem_commands_sources_command.rb @@ -74,6 +74,80 @@ class TestGemCommandsSourcesCommand < Gem::TestCase assert_equal '', @ui.error end + def test_execute_add_allow_typo_squatting_source + rubygems_org = "https://rubyems.org" + + spec_fetcher do |fetcher| + fetcher.spec("a", 1) + end + + specs = Gem::Specification.map do |spec| + [spec.name, spec.version, spec.original_platform] + end + + specs_dump_gz = StringIO.new + Zlib::GzipWriter.wrap(specs_dump_gz) do |io| + Marshal.dump(specs, io) + end + + @fetcher.data["#{rubygems_org}/specs.#{@marshal_version}.gz"] = specs_dump_gz.string + @cmd.handle_options %W[--add #{rubygems_org}] + ui = Gem::MockGemUi.new("y") + + use_ui ui do + @cmd.execute + end + + expected = "https://rubyems.org is too similar to https://rubygems.org\n\nDo you want to add this source? [yn] https://rubyems.org added to sources\n" + + assert_equal expected, ui.output + + source = Gem::Source.new(rubygems_org) + assert Gem.sources.include?(source) + + assert_empty ui.error + end + + def test_execute_add_deny_typo_squatting_source + rubygems_org = "https://rubyems.org" + + spec_fetcher do |fetcher| + fetcher.spec("a", 1) + end + + specs = Gem::Specification.map do |spec| + [spec.name, spec.version, spec.original_platform] + end + + specs_dump_gz = StringIO.new + Zlib::GzipWriter.wrap(specs_dump_gz) do |io| + Marshal.dump(specs, io) + end + + @fetcher.data["#{rubygems_org}/specs.#{@marshal_version}.gz"] = + specs_dump_gz.string + + @cmd.handle_options %W[--add #{rubygems_org}] + + ui = Gem::MockGemUi.new("n") + + use_ui ui do + + assert_raises Gem::MockGemUi::TermError do + @cmd.execute + end + end + + expected = "https://rubyems.org is too similar to https://rubygems.org\n\nDo you want to add this source? [yn] " + + assert_equal expected, ui.output + + source = Gem::Source.new(rubygems_org) + refute Gem.sources.include?(source) + + assert_empty ui.error + end + def test_execute_add_nonexistent_source spec_fetcher diff --git a/test/rubygems/test_gem_gem_runner.rb b/test/rubygems/test_gem_gem_runner.rb index 7c771de9e5..0c801847b0 100644 --- a/test/rubygems/test_gem_gem_runner.rb +++ b/test/rubygems/test_gem_gem_runner.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true require 'rubygems/test_case' -require 'rubygems/gem_runner' class TestGemGemRunner < Gem::TestCase @@ -8,6 +7,8 @@ class TestGemGemRunner < Gem::TestCase super @orig_args = Gem::Command.build_args + + require 'rubygems/gem_runner' @runner = Gem::GemRunner.new end diff --git a/test/rubygems/test_gem_indexer.rb b/test/rubygems/test_gem_indexer.rb index 43a04c3bb3..fdef33e464 100644 --- a/test/rubygems/test_gem_indexer.rb +++ b/test/rubygems/test_gem_indexer.rb @@ -3,7 +3,7 @@ require 'rubygems/test_case' require 'rubygems/indexer' unless defined?(Builder::XChar) - warn "Gem::Indexer tests are being skipped. Install builder gem." if $VERBOSE + warn "Gem::Indexer tests are being skipped. Install builder gem." end class TestGemIndexer < Gem::TestCase diff --git a/test/rubygems/test_gem_source.rb b/test/rubygems/test_gem_source.rb index 2ed9fc78e7..30b45ea267 100644 --- a/test/rubygems/test_gem_source.rb +++ b/test/rubygems/test_gem_source.rb @@ -235,4 +235,18 @@ class TestGemSource < Gem::TestCase refute @source.update_cache? end + def test_typo_squatting + rubygems_source = Gem::Source.new("https://rubgems.org") + assert rubygems_source.typo_squatting?("rubygems.org") + assert rubygems_source.typo_squatting?("rubyagems.org") + assert rubygems_source.typo_squatting?("rubyasgems.org") + refute rubygems_source.typo_squatting?("rubysertgems.org") + end + + def test_typo_squatting_custom_distance_threshold + rubygems_source = Gem::Source.new("https://rubgems.org") + distance_threshold = 5 + assert rubygems_source.typo_squatting?("rubysertgems.org", distance_threshold) + end + end diff --git a/test/rubygems/test_remote_fetch_error.rb b/test/rubygems/test_remote_fetch_error.rb index 780e5e79f7..766086756e 100644 --- a/test/rubygems/test_remote_fetch_error.rb +++ b/test/rubygems/test_remote_fetch_error.rb @@ -5,7 +5,7 @@ class TestRemoteFetchError < Gem::TestCase def test_password_redacted error = Gem::RemoteFetcher::FetchError.new('There was an error fetching', 'https://user:secret@gemsource.org') - refute_match error.to_s, 'secret' + refute_match 'secret', error.to_s end def test_invalid_url diff --git a/test/rubygems/test_require.rb b/test/rubygems/test_require.rb index 69df9093c7..aa2675af5d 100644 --- a/test/rubygems/test_require.rb +++ b/test/rubygems/test_require.rb @@ -365,19 +365,16 @@ class TestGemRequire < Gem::TestCase end def test_realworld_default_gem - begin - gem 'json' - rescue Gem::MissingSpecError - skip "default gems are only available after ruby installation" - end + testing_ruby_repo = !ENV["GEM_COMMAND"].nil? + skip "this test can't work under ruby-core setup" if testing_ruby_repo || java_platform? cmd = <<-RUBY $stderr = $stdout require "json" - puts Gem.loaded_specs["json"].default_gem? + puts Gem.loaded_specs["json"] RUBY output = Gem::Util.popen(Gem.ruby, "-e", cmd).strip - assert_equal "true", output + refute_empty output end def test_default_gem_and_normal_gem @@ -499,36 +496,38 @@ class TestGemRequire < Gem::TestCase end end - # uplevel is 2.5+ only and jruby has some issues with it - if RUBY_VERSION >= "2.5" && !java_platform? - def test_no_kernel_require_in_warn_with_uplevel - lib = File.realpath("../../../lib", __FILE__) - Dir.mktmpdir("warn_test") do |dir| - File.write(dir + "/sub.rb", "warn 'uplevel', 'test', uplevel: 1\n") - File.write(dir + "/main.rb", "require 'sub'\n") - _, err = capture_subprocess_io do - system(@@ruby, "-w", "-rpp", "--disable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") - end - assert_equal "main.rb:1: warning: uplevel\ntest\n", err - _, err = capture_subprocess_io do - system(@@ruby, "-w", "-rpp", "--enable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + # uplevel is 2.5+ only + if RUBY_VERSION >= "2.5" + ["", "Kernel."].each do |prefix| + define_method "test_no_kernel_require_in_#{prefix.tr(".", "_")}warn_with_uplevel" do + lib = File.realpath("../../../lib", __FILE__) + Dir.mktmpdir("warn_test") do |dir| + File.write(dir + "/sub.rb", "#{prefix}warn 'uplevel', 'test', uplevel: 1\n") + File.write(dir + "/main.rb", "require 'sub'\n") + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--disable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + end + assert_match(/main\.rb:1: warning: uplevel\ntest\n$/, err) + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--enable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + end + assert_match(/main\.rb:1: warning: uplevel\ntest\n$/, err) end - assert_equal "main.rb:1: warning: uplevel\ntest\n", err end - end - def test_no_other_behavioral_changes_with_kernel_warn - lib = File.realpath("../../../lib", __FILE__) - Dir.mktmpdir("warn_test") do |dir| - File.write(dir + "/main.rb", "warn({x:1}, {y:2}, [])\n") - _, err = capture_subprocess_io do - system(@@ruby, "-w", "-rpp", "--disable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") - end - assert_equal "{:x=>1}\n{:y=>2}\n", err - _, err = capture_subprocess_io do - system(@@ruby, "-w", "-rpp", "--enable=gems", "-I", lib, "-C", dir, "-I.", "main.rb") + define_method "test_no_other_behavioral_changes_with_#{prefix.tr(".", "_")}warn" do + lib = File.realpath("../../../lib", __FILE__) + Dir.mktmpdir("warn_test") do |dir| + File.write(dir + "/main.rb", "#{prefix}warn({x:1}, {y:2}, [])\n") + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--disable=gems", "-I", lib, "-C", dir, "main.rb") + end + assert_match(/{:x=>1}\n{:y=>2}\n$/, err) + _, err = capture_subprocess_io do + system(@@ruby, "-w", "--enable=gems", "-I", lib, "-C", dir, "main.rb") + end + assert_match(/{:x=>1}\n{:y=>2}\n$/, err) end - assert_equal "{:x=>1}\n{:y=>2}\n", err end end end |