Skip to content

Commit

Permalink
[rb] add custom matchers for deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Dec 2, 2019
1 parent 61ccd71 commit 3c038ec
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 40 deletions.
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def service_url(opts)
next unless opts.key? key

WebDriver.logger.deprecate(":#{key}", ':service with an instance of Selenium::WebDriver::Service',
id: :service)
id: "service_#{key}".to_sym)
end
@service ||= Service.send(browser,
args: opts.delete(:driver_opts),
Expand Down
9 changes: 6 additions & 3 deletions rb/lib/selenium/webdriver/common/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ def warn(message, id: [])
# @yield appends additional message to end of provided template
#
def deprecate(old, new = nil, id: [], &block)
return if @ignored.include?(:deprecations) || (@ignored & Array(id)).any?
id = Array(id)
return if @ignored.include?(:deprecations) || (@ignored & id).any?

ids = id.empty? ? '' : "[#{id.map(&:inspect).join(', ')}] "

message = +"[DEPRECATION] #{old} is deprecated"
message = +"[DEPRECATION] #{ids}#{old} is deprecated"
message << if new
". Use #{new} instead."
else
' and will be removed in a future release.'
end
warn message, id: id, &block
warn message, &block
end

private
Expand Down
2 changes: 1 addition & 1 deletion rb/spec/integration/selenium/webdriver/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module WebDriver
path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.jpg"
message = "name used for saved screenshot does not match file type. "\
"It should end with .png extension"
expect(WebDriver.logger).to receive(:warn).with(message)
expect(WebDriver.logger).to receive(:warn).with(message, id: :screenshot)

save_screenshot_and_assert(path)
end
Expand Down
3 changes: 2 additions & 1 deletion rb/spec/integration/selenium/webdriver/manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module WebDriver
expect(entries.first).to be_kind_of(LogEntry)
end

it 'can get the driver log' do
# Chrome - turned off by default
it 'can get the driver log', except: {browser: %i[chrome edge_chrome]} do
driver.navigate.to url_for('simpleTest.html')

entries = driver.manage.logs.get(:driver)
Expand Down
1 change: 1 addition & 0 deletions rb/spec/integration/selenium/webdriver/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

require 'selenium-webdriver'
require_relative 'spec_support'
require_relative '../../../rspec_matchers'

include Selenium # rubocop:disable Style/MixinUsage

Expand Down
48 changes: 48 additions & 0 deletions rb/spec/rspec_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

RSpec::Matchers.define :have_deprecated do |deprecation|
match do |actual|
# Not sure how else to capture stdout here
expect {
actual.call
std_out = File.read $stdout if $stdout.is_a?(File)
@deprecations_found = std_out&.scan(/DEPRECATION\] \[:([^\]]*)\]/)&.flatten&.map(&:to_sym)
}.to output.to_stdout_from_any_process
expect(Array(deprecation).sort).to eq(@deprecations_found&.sort)
end

failure_message do
but_message = if @deprecations_found.empty? || @deprecations_found.nil?
'no deprecations were found'
else
"instead these deprecations were found: [#{@deprecations_found.join(', ')}]"
end
"expected :#{deprecation} to have been deprecated, but #{but_message}"
end

failure_message_when_negated do
but_message = "it was found among these deprecations: [#{@deprecations_found.join(', ')}]"
"expected :#{deprecation} not to have been deprecated, but #{but_message}"
end

def supports_block_expectations?
true
end
end
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/chrome/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ module WebDriver

expect {
Selenium::WebDriver::Chrome.driver_path = path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Chrome#driver_path=/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

expect {
expect(Selenium::WebDriver::Chrome.driver_path).to eq path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Chrome#driver_path/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

service = Service.chrome

Expand Down Expand Up @@ -139,7 +139,7 @@ module WebDriver

expect {
driver.new(driver_path: driver_path)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_path)
end

it 'accepts :port but throws deprecation notice' do
Expand All @@ -151,7 +151,7 @@ module WebDriver

expect {
driver.new(port: driver_port)
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
}.to have_deprecated(:service_port)
end

it 'accepts :driver_opts but throws deprecation notice' do
Expand All @@ -164,7 +164,7 @@ module WebDriver

expect {
driver.new(driver_opts: driver_opts)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_opts)
end

it 'accepts :service without creating a new instance' do
Expand Down
24 changes: 17 additions & 7 deletions rb/spec/unit/selenium/webdriver/common/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module WebDriver

describe '#output' do
it 'outputs to stdout by default' do
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stdout
expect { logger.warn('message') }.to output(/WARN Selenium message/).to_stdout_from_any_process
end

it 'allows output to file' do
Expand All @@ -83,34 +83,44 @@ module WebDriver

describe '#warn' do
it 'logs with String' do
expect { logger.warn "String Value" }.to output(/WARN Selenium String Value/).to_stdout
expect { logger.warn "String Value" }.to output(/WARN Selenium String Value/).to_stdout_from_any_process
end

it 'logs single id when set' do
msg = /WARN Selenium \[:foo\] warning message/
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout_from_any_process
end

it 'logs multiple ids when set' do
msg = /WARN Selenium \[:foo, :bar\] warning message/
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout_from_any_process
end
end

describe '#deprecate' do
it 'allows to deprecate functionality with replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\./
expect { logger.deprecate('#old', '#new') }.to output(message).to_stdout
expect { logger.deprecate('#old', '#new') }.to output(message).to_stdout_from_any_process
end

it 'allows to deprecate functionality without replacement' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated and will be removed in a future release\./
expect { logger.deprecate('#old') }.to output(message).to_stdout
expect { logger.deprecate('#old') }.to output(message).to_stdout_from_any_process
end

it 'appends deprecation message with provided block' do
message = /WARN Selenium \[DEPRECATION\] #old is deprecated\. Use #new instead\. More Details\./
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stdout
expect { logger.deprecate('#old', '#new') { 'More Details.' } }.to output(message).to_stdout_from_any_process
end

it 'logs single id when set' do
msg = /WARN Selenium \[:foo\] warning message/
expect { logger.warn('warning message', id: :foo) }.to output(msg).to_stdout_from_any_process
end

it 'logs multiple ids when set' do
msg = /WARN Selenium \[:foo, :bar\] warning message/
expect { logger.warn('warning message', id: %i[foo bar]) }.to output(msg).to_stdout_from_any_process
end
end

Expand Down
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/edge/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ module WebDriver
path = '/path/to/driver'
expect {
Selenium::WebDriver::Edge.driver_path = path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Edge#driver_path=/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

expect {
expect(Selenium::WebDriver::Edge.driver_path).to eq path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Edge#driver_path/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

service = Service.edge

Expand Down Expand Up @@ -140,7 +140,7 @@ module WebDriver

expect {
driver.new(driver_path: driver_path)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_path)
end

it 'accepts :port but throws deprecation notice' do
Expand All @@ -152,7 +152,7 @@ module WebDriver

expect {
driver.new(port: driver_port)
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
}.to have_deprecated(:service_port)
end

it 'accepts :driver_opts but throws deprecation notice' do
Expand All @@ -165,7 +165,7 @@ module WebDriver

expect {
driver.new(driver_opts: driver_opts)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_opts)
end

it 'accepts :service without creating a new instance' do
Expand Down
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/firefox/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ module WebDriver

expect {
Selenium::WebDriver::Firefox.driver_path = path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Firefox#driver_path=/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

expect {
expect(Selenium::WebDriver::Firefox.driver_path).to eq path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Firefox#driver_path/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

service = Service.firefox

Expand Down Expand Up @@ -141,7 +141,7 @@ module WebDriver

expect {
driver.new(driver_path: driver_path)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_path)
end

it 'accepts :port but throws deprecation notice' do
Expand All @@ -153,7 +153,7 @@ module WebDriver

expect {
driver.new(port: driver_port)
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
}.to have_deprecated(:service_port)
end

it 'accepts :driver_opts but throws deprecation notice' do
Expand All @@ -166,7 +166,7 @@ module WebDriver

expect {
driver.new(driver_opts: driver_opts)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_opts)
end

it 'accepts :service without creating a new instance' do
Expand Down
4 changes: 2 additions & 2 deletions rb/spec/unit/selenium/webdriver/ie/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def expect_request(body: nil, endpoint: nil)
expect_request(body: {capabilities: {firstMatch: ["browserName": "internet explorer",
"platformName": "windows",
"invalid": "foobar",
"se:ieOptions":{"nativeEvents":true,
"ie.browserCommandLineSwitches":"-f"}]}})
"se:ieOptions": {"nativeEvents": true,
"ie.browserCommandLineSwitches": "-f"}]}})

expect {
Driver.new(options: Options.new(browser_opts), desired_capabilities: caps)
Expand Down
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/ie/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ module WebDriver

expect {
Selenium::WebDriver::IE.driver_path = path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::IE#driver_path=/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

expect {
expect(Selenium::WebDriver::IE.driver_path).to eq path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::IE#driver_path/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

service = Service.ie

Expand Down Expand Up @@ -141,7 +141,7 @@ module WebDriver

expect {
driver.new(driver_path: driver_path)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_path)
end

it 'accepts :port but throws deprecation notice' do
Expand All @@ -153,7 +153,7 @@ module WebDriver

expect {
driver.new(port: driver_port)
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
}.to have_deprecated(:service_port)
end

it 'accepts :driver_opts but throws deprecation notice' do
Expand All @@ -166,7 +166,7 @@ module WebDriver

expect {
driver.new(driver_opts: driver_opts)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_opts)
end

it 'accepts :service without creating a new instance' do
Expand Down
10 changes: 5 additions & 5 deletions rb/spec/unit/selenium/webdriver/safari/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ module WebDriver

expect {
Selenium::WebDriver::Safari.driver_path = path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Safari#driver_path=/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

expect {
expect(Selenium::WebDriver::Safari.driver_path).to eq path
}.to output(/WARN Selenium \[DEPRECATION\] Selenium::WebDriver::Safari#driver_path/).to_stdout_from_any_process
}.to have_deprecated(:driver_path)

service = Service.safari

Expand Down Expand Up @@ -131,7 +131,7 @@ module WebDriver

expect {
driver.new(driver_path: driver_path)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_path/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_path)
end

it 'accepts :port but throws deprecation notice' do
Expand All @@ -143,7 +143,7 @@ module WebDriver

expect {
driver.new(port: driver_port)
}.to output(/WARN Selenium \[DEPRECATION\] :port/).to_stdout_from_any_process
}.to have_deprecated(:service_port)
end

it 'accepts :driver_opts but throws deprecation notice' do
Expand All @@ -156,7 +156,7 @@ module WebDriver

expect {
driver.new(driver_opts: driver_opts)
}.to output(/WARN Selenium \[DEPRECATION\] :driver_opts/).to_stdout_from_any_process
}.to have_deprecated(:service_driver_opts)
end

it 'accepts :service without creating a new instance' do
Expand Down
1 change: 1 addition & 0 deletions rb/spec/unit/selenium/webdriver/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'selenium-webdriver'
require 'securerandom'
require 'pathname'
require_relative '../../../rspec_matchers'

module Selenium
module WebDriver
Expand Down

0 comments on commit 3c038ec

Please sign in to comment.