|
| 1 | +# frozen_string_literal: true |
| 2 | +require_relative "helper" |
| 3 | +require "rubygems/command" |
| 4 | +require "rubygems/update_suggestion" |
| 5 | + |
| 6 | +class TestUpdateSuggestion < Gem::TestCase |
| 7 | + def setup |
| 8 | + super |
| 9 | + |
| 10 | + @cmd = Gem::Command.new "dummy", "dummy" |
| 11 | + @cmd.extend Gem::UpdateSuggestion |
| 12 | + end |
| 13 | + |
| 14 | + def with_eglible_environment(**params) |
| 15 | + self.class.with_eglible_environment(**params) do |
| 16 | + yield |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + def self.with_eglible_environment( |
| 21 | + tty: true, |
| 22 | + rubygems_version: Gem::Version.new("1.2.3"), |
| 23 | + latest_rubygems_version: Gem::Version.new("2.0.0"), |
| 24 | + ci: false, |
| 25 | + cmd: |
| 26 | + ) |
| 27 | + original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], nil |
| 28 | + original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], nil |
| 29 | + original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, nil |
| 30 | + Gem.configuration[:last_update_check] = nil |
| 31 | + |
| 32 | + Gem.ui.stub :tty?, tty do |
| 33 | + Gem.stub :rubygems_version, rubygems_version do |
| 34 | + Gem.stub :latest_rubygems_version, latest_rubygems_version do |
| 35 | + cmd.stub :ci?, ci do |
| 36 | + yield |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + ensure |
| 42 | + Gem.configuration[:prevent_update_suggestion] = original_config |
| 43 | + ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env |
| 44 | + Gem.disable_system_update_message = original_disable |
| 45 | + end |
| 46 | + |
| 47 | + def test_update_suggestion |
| 48 | + Gem.stub :rubygems_version, Gem::Version.new("1.2.3") do |
| 49 | + Gem.stub :latest_rubygems_version, Gem::Version.new("2.0.0") do |
| 50 | + assert_equal @cmd.update_suggestion, <<~SUGGESTION |
| 51 | +
|
| 52 | + A new release of RubyGems is available: 1.2.3 → 2.0.0! |
| 53 | + Run `gem update --system 2.0.0` to update your installation. |
| 54 | +
|
| 55 | + SUGGESTION |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + def test_eglible_for_update |
| 61 | + with_eglible_environment(cmd: @cmd) do |
| 62 | + Time.stub :now, 123456789 do |
| 63 | + assert @cmd.eglible_for_update? |
| 64 | + assert_equal Gem.configuration[:last_update_check], 123456789 |
| 65 | + |
| 66 | + # test last check is written to config file |
| 67 | + assert File.read(Gem.configuration.config_file_name).match("last_update_check: 123456789") |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + def test_eglible_for_update_prevent_config |
| 73 | + with_eglible_environment(cmd: @cmd) do |
| 74 | + begin |
| 75 | + original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], true |
| 76 | + refute @cmd.eglible_for_update? |
| 77 | + ensure |
| 78 | + Gem.configuration[:prevent_update_suggestion] = original_config |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def test_eglible_for_update_prevent_env |
| 84 | + with_eglible_environment(cmd: @cmd) do |
| 85 | + begin |
| 86 | + original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], "yes" |
| 87 | + refute @cmd.eglible_for_update? |
| 88 | + ensure |
| 89 | + ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def test_eglible_for_update_non_tty |
| 95 | + with_eglible_environment(tty: false, cmd: @cmd) do |
| 96 | + refute @cmd.eglible_for_update? |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def test_eglible_for_update_for_prerelease |
| 101 | + with_eglible_environment(rubygems_version: Gem::Version.new("1.0.0-rc1"), cmd: @cmd) do |
| 102 | + refute @cmd.eglible_for_update? |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def test_eglible_for_update_disabled_update |
| 107 | + with_eglible_environment(cmd: @cmd) do |
| 108 | + begin |
| 109 | + original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, "disabled" |
| 110 | + refute @cmd.eglible_for_update? |
| 111 | + ensure |
| 112 | + Gem.disable_system_update_message = original_disable |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + def test_eglible_for_update_on_ci |
| 118 | + with_eglible_environment(ci: true, cmd: @cmd) do |
| 119 | + refute @cmd.eglible_for_update? |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + def test_eglible_for_update_unwrittable_config |
| 124 | + with_eglible_environment(ci: true, cmd: @cmd) do |
| 125 | + Gem.configuration.stub :config_file_writable?, false do |
| 126 | + refute @cmd.eglible_for_update? |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + def test_eglible_for_update_notification_delay |
| 132 | + with_eglible_environment(cmd: @cmd) do |
| 133 | + Gem.configuration[:last_update_check] = Time.now.to_i |
| 134 | + refute @cmd.eglible_for_update? |
| 135 | + end |
| 136 | + end |
| 137 | +end |
0 commit comments