Skip to content

Commit

Permalink
[API] Fixed, that Utils.__report_unsupported_parameters and `Utils.…
Browse files Browse the repository at this point in the history
…__report_unsupported_method` use `Kernel.warn` so they can be suppressed

Closes #399
  • Loading branch information
karmiq committed Jan 17, 2018
1 parent 331b796 commit 698065a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions elasticsearch-api/lib/elasticsearch/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ def __report_unsupported_parameters(arguments, params=[])
end

unless messages.empty?
messages << "Suppress this warning by the `-WO` command line flag."

if STDERR.tty?
STDERR.puts messages.map { |m| "\e[31;1m#{m}\e[0m" }.join("\n")
Kernel.warn messages.map { |m| "\e[31;1m#{m}\e[0m" }.join("\n")
else
STDERR.puts messages.join("\n")
Kernel.warn messages.join("\n")
end
end
end
Expand All @@ -250,12 +252,12 @@ def __report_unsupported_method(name)
message += " in `#{source}`"
end

message += ". This method is not supported in the version you're using: #{Elasticsearch::API::VERSION}, and will be removed in the next release."
message += ". This method is not supported in the version you're using: #{Elasticsearch::API::VERSION}, and will be removed in the next release. Suppress this warning by the `-WO` command line flag."

if STDERR.tty?
STDERR.puts "\e[31;1m#{message}\e[0m"
Kernel.warn "\e[31;1m#{message}\e[0m"
else
STDERR.puts message
Kernel.warn message
end
end

Expand Down
16 changes: 8 additions & 8 deletions elasticsearch-api/test/unit/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ class UtilsTest < UnitTest
arguments = { :foo => 'bar', :moo => 'bam', :baz => 'qux' }
unsupported_params = [:foo, :moo]

STDERR.expects(:puts).with do |message|
assert_equal 2, message.split("\n").size
Kernel.expects(:warn).with do |message|
assert_equal 2, message.split("\n").reject { |l| l.include? 'Suppress this warning' }.size
true
end

Expand All @@ -269,9 +269,9 @@ class UtilsTest < UnitTest
arguments = { :foo => 'bar', :moo => 'bam', :baz => 'qux' }
unsupported_params = [ { :foo => { :explanation => 'NOT_SUPPORTED' } } ]

STDERR.expects(:puts).with do |message|
Kernel.expects(:warn).with do |message|
assert_match /NOT_SUPPORTED/, message
assert_equal 1, message.split("\n").size
assert_equal 1, message.split("\n").reject { |l| l.include? 'Suppress this warning' }.size
true
end

Expand All @@ -282,9 +282,9 @@ class UtilsTest < UnitTest
arguments = { :foo => 'bar', :moo => 'bam', :baz => 'qux' }
unsupported_params = [ { :foo => { :explanation => 'NOT_SUPPORTED'} }, :moo ]

STDERR.expects(:puts).with do |message|
Kernel.expects(:warn).with do |message|
assert_match /NOT_SUPPORTED/, message
assert_equal 2, message.split("\n").size
assert_equal 2, message.split("\n").reject { |l| l.include? 'Suppress this warning' }.size
true
end

Expand All @@ -295,15 +295,15 @@ class UtilsTest < UnitTest
arguments = { :moo => 'bam', :baz => 'qux' }
unsupported_params = [:foo]

STDERR.expects(:puts).never
Kernel.expects(:warn).never

__report_unsupported_parameters(arguments, unsupported_params)
end
end

context "__report_unsupported_method" do
should "print the warning" do
STDERR.expects(:puts).with do |message|
Kernel.expects(:warn).with do |message|
assert_match /foo/, message
true
end
Expand Down

0 comments on commit 698065a

Please sign in to comment.