From 698065a83f0cd1cc5a16aef40393ba300b494284 Mon Sep 17 00:00:00 2001 From: Karel Minarik Date: Wed, 17 Jan 2018 15:30:11 +0100 Subject: [PATCH] [API] Fixed, that `Utils.__report_unsupported_parameters` and `Utils.__report_unsupported_method` use `Kernel.warn` so they can be suppressed Closes #399 --- elasticsearch-api/lib/elasticsearch/api/utils.rb | 12 +++++++----- elasticsearch-api/test/unit/utils_test.rb | 16 ++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/elasticsearch-api/lib/elasticsearch/api/utils.rb b/elasticsearch-api/lib/elasticsearch/api/utils.rb index 638bfe6020..d92967060e 100644 --- a/elasticsearch-api/lib/elasticsearch/api/utils.rb +++ b/elasticsearch-api/lib/elasticsearch/api/utils.rb @@ -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 @@ -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 diff --git a/elasticsearch-api/test/unit/utils_test.rb b/elasticsearch-api/test/unit/utils_test.rb index e0bf3041a3..db198e20da 100644 --- a/elasticsearch-api/test/unit/utils_test.rb +++ b/elasticsearch-api/test/unit/utils_test.rb @@ -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 @@ -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 @@ -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 @@ -295,7 +295,7 @@ 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 @@ -303,7 +303,7 @@ class UtilsTest < UnitTest 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