2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2021-06-01 23:32:47 -04:00
|
|
|
require_relative 'helper'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/deprecate'
|
|
|
|
|
|
|
|
class TestDeprecate < Gem::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
@original_skip = Gem::Deprecate.skip
|
|
|
|
Gem::Deprecate.skip = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
|
|
|
|
|
|
|
Gem::Deprecate.skip = @original_skip
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_defaults
|
|
|
|
assert_equal false, @original_skip
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment
|
|
|
|
Gem::Deprecate.skip = false
|
|
|
|
assert_equal false, Gem::Deprecate.skip
|
|
|
|
|
|
|
|
Gem::Deprecate.skip = true
|
|
|
|
assert_equal true, Gem::Deprecate.skip
|
|
|
|
|
|
|
|
Gem::Deprecate.skip = nil
|
|
|
|
assert([true,false].include? Gem::Deprecate.skip)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip
|
|
|
|
Gem::Deprecate.skip_during do
|
|
|
|
assert_equal true, Gem::Deprecate.skip
|
|
|
|
end
|
|
|
|
|
|
|
|
Gem::Deprecate.skip = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
class Thing
|
|
|
|
extend Gem::Deprecate
|
|
|
|
attr_accessor :message
|
|
|
|
def foo
|
|
|
|
@message = "foo"
|
|
|
|
end
|
|
|
|
def bar
|
|
|
|
@message = "bar"
|
|
|
|
end
|
2020-04-19 09:41:54 -04:00
|
|
|
rubygems_deprecate :foo, :bar
|
2021-04-21 07:54:29 -04:00
|
|
|
|
|
|
|
def foo_arg(msg)
|
|
|
|
@message = "foo" + msg
|
|
|
|
end
|
|
|
|
def bar_arg(msg)
|
|
|
|
@message = "bar" + msg
|
|
|
|
end
|
|
|
|
rubygems_deprecate :foo_arg, :bar_arg
|
|
|
|
|
|
|
|
def foo_kwarg(message:)
|
|
|
|
@message = "foo" + message
|
|
|
|
end
|
|
|
|
def bar_kwarg(message:)
|
|
|
|
@message = "bar" + message
|
|
|
|
end
|
|
|
|
rubygems_deprecate :foo_kwarg, :bar_kwarg
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2020-04-19 10:18:29 -04:00
|
|
|
class OtherThing
|
|
|
|
extend Gem::Deprecate
|
|
|
|
attr_accessor :message
|
|
|
|
def foo
|
|
|
|
@message = "foo"
|
|
|
|
end
|
|
|
|
def bar
|
|
|
|
@message = "bar"
|
|
|
|
end
|
|
|
|
deprecate :foo, :bar, 2099, 3
|
2021-04-21 07:54:29 -04:00
|
|
|
|
|
|
|
def foo_arg(msg)
|
|
|
|
@message = "foo" + msg
|
|
|
|
end
|
|
|
|
def bar_arg(msg)
|
|
|
|
@message = "bar" + msg
|
|
|
|
end
|
|
|
|
deprecate :foo_arg, :bar_arg, 2099, 3
|
|
|
|
|
|
|
|
def foo_kwarg(message:)
|
|
|
|
@message = "foo" + message
|
|
|
|
end
|
|
|
|
def bar_kwarg(message:)
|
|
|
|
@message = "bar" + message
|
|
|
|
end
|
|
|
|
deprecate :foo_kwarg, :bar_kwarg, 2099, 3
|
2020-04-19 10:18:29 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_deprecated_method_calls_the_old_method
|
2019-06-29 07:16:49 -04:00
|
|
|
capture_output do
|
2012-11-29 01:52:18 -05:00
|
|
|
thing = Thing.new
|
|
|
|
thing.foo
|
|
|
|
assert_equal "foo", thing.message
|
2021-04-21 07:54:29 -04:00
|
|
|
thing.foo_arg("msg")
|
|
|
|
assert_equal "foomsg", thing.message
|
|
|
|
thing.foo_kwarg(message: "msg")
|
|
|
|
assert_equal "foomsg", thing.message
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecated_method_outputs_a_warning
|
2019-06-29 07:16:49 -04:00
|
|
|
out, err = capture_output do
|
2012-11-29 01:52:18 -05:00
|
|
|
thing = Thing.new
|
|
|
|
thing.foo
|
2021-04-21 07:54:29 -04:00
|
|
|
thing.foo_arg("msg")
|
|
|
|
thing.foo_kwarg(message: "msg")
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "", out
|
|
|
|
assert_match(/Thing#foo is deprecated; use bar instead\./, err)
|
2021-04-21 07:54:29 -04:00
|
|
|
assert_match(/Thing#foo_arg is deprecated; use bar_arg instead\./, err)
|
|
|
|
assert_match(/Thing#foo_kwarg is deprecated; use bar_kwarg instead\./, err)
|
2020-03-28 20:11:40 -04:00
|
|
|
assert_match(/in Rubygems [0-9]+/, err)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2020-04-19 09:41:54 -04:00
|
|
|
def test_rubygems_deprecate_command
|
2020-01-31 21:14:04 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
foo_command = Class.new(Gem::Command) do
|
|
|
|
extend Gem::Deprecate
|
|
|
|
|
2020-04-19 09:41:54 -04:00
|
|
|
rubygems_deprecate_command
|
2020-01-31 21:14:04 -05:00
|
|
|
|
|
|
|
def execute
|
|
|
|
puts "pew pew!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gem::Commands.send(:const_set, :FooCommand, foo_command)
|
|
|
|
assert Gem::Commands::FooCommand.new("foo").deprecated?
|
|
|
|
ensure
|
|
|
|
Gem::Commands.send(:remove_const, :FooCommand)
|
|
|
|
end
|
|
|
|
|
2020-04-19 10:18:29 -04:00
|
|
|
def test_deprecated_method_outputs_a_warning_old_way
|
2020-05-25 08:58:27 -04:00
|
|
|
out, err = capture_output do
|
2020-04-19 10:18:29 -04:00
|
|
|
thing = OtherThing.new
|
|
|
|
thing.foo
|
2021-04-21 07:54:29 -04:00
|
|
|
thing.foo_arg("msg")
|
|
|
|
thing.foo_kwarg(message: "msg")
|
2020-04-19 10:18:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "", out
|
2021-04-21 07:54:29 -04:00
|
|
|
assert_match(/OtherThing#foo is deprecated; use bar instead\./, err)
|
|
|
|
assert_match(/OtherThing#foo_arg is deprecated; use bar_arg instead\./, err)
|
|
|
|
assert_match(/OtherThing#foo_kwarg is deprecated; use bar_kwarg instead\./, err)
|
|
|
|
assert_match(/on or after 2099-03/, err)
|
2020-04-19 10:18:29 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|