Skip to content

Commit

Permalink
file cleanup and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jul 31, 2023
1 parent 97fb1e2 commit 2ad32bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Metrics/BlockLength:
- 'webdrivers.gemspec'

Metrics/ClassLength:
Max: 116
Max: 105
Exclude:
- 'lib/webdrivers/chromedriver.rb'
- 'lib/webdrivers/system.rb'

Metrics/CyclomaticComplexity:
Expand Down Expand Up @@ -96,3 +97,6 @@ RSpec/ExampleLength:

RSpec/MultipleExpectations:
Enabled: false

RSpec/NestedGroups:
Enabled: false
37 changes: 13 additions & 24 deletions lib/webdrivers/chromedriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,8 @@ def latest_point_release(version)
"#{msg} A network issue is preventing determination of latest chromedriver release."
end

url = if version >= normalize_version('115')
'https://googlechromelabs.github.io/chrome-for-testing'
else
'https://chromedriver.storage.googleapis.com/index.html'
end

msg = "#{msg} Please set `Webdrivers::Chromedriver.required_version = <desired driver version>` "\
"to a known chromedriver version: #{url}"
'to a known chromedriver version: https://chromedriver.chromium.org/downloads/version-selection'
Webdrivers.logger.debug msg
raise VersionError, msg
end
Expand All @@ -108,18 +102,16 @@ def apple_m1_compatible?(driver_version)
end

def apple_filename(driver_version)
if apple_m1_compatible?(driver_version)
driver_version >= normalize_version('106.0.5249.61') ? 'mac_arm64' : 'mac64_m1'
else
'mac64'
unless apple_m1_compatible?(driver_version)
return driver_version >= normalize_version('115') ? 'mac-x64' : 'mac64'
end
end

def apple_filename_for_api(driver_version)
if apple_m1_compatible?(driver_version)
driver_version >= normalize_version('106.0.5249.61') ? 'mac-arm64' : 'mac64-m1'
if driver_version < normalize_version('106.0.5249.61')
'mac64_m1'
elsif driver_version < normalize_version('115')
'mac_arm64'
else
'mac-x64'
'mac-arm64'
end
end

Expand All @@ -134,11 +126,7 @@ def driver_filename(driver_version)
elsif System.platform == 'linux'
'linux64'
elsif System.platform == 'mac'
if driver_version >= normalize_version('115')
apple_filename_for_api(driver_version)
else
apple_filename(driver_version)
end
apple_filename(driver_version)
else
raise 'Failed to determine driver filename to download for your OS.'
end
Expand All @@ -164,6 +152,7 @@ def current_build_version
def browser_build_version
normalize_version(browser_version.segments[0..2].join('.'))
end

alias chrome_build_version browser_build_version

# Returns true if an executable driver binary exists
Expand All @@ -174,11 +163,11 @@ def sufficient_binary?
end

def chrome_for_testing_base_url
'https://googlechromelabs.github.io'
'https://googlechromelabs.github.io/chrome-for-testing/'
end

def latest_patch_version(driver_version)
latest_patch_version = URI.join(chrome_for_testing_base_url, '/chrome-for-testing/latest-patch-versions-per-build.json')
latest_patch_version = URI.join(chrome_for_testing_base_url, 'latest-patch-versions-per-build.json')
.then { |url| Network.get(url) }
.then { |res| JSON.parse(res, symbolize_names: true) }
.then { |json| json.dig(:builds, :"#{driver_version}", :version) }
Expand All @@ -191,7 +180,7 @@ def latest_patch_version(driver_version)
def direct_url_from_api(driver_version)
return if normalize_version(driver_version) < normalize_version('115')

URI.join(chrome_for_testing_base_url, '/chrome-for-testing/known-good-versions-with-downloads.json')
URI.join(chrome_for_testing_base_url, 'known-good-versions-with-downloads.json')
.then { |url| Network.get(url) }
.then { |res| JSON.parse(res, symbolize_names: true) }
.then { |json| json[:versions].find { |e| e[:version] == driver_version.to_s } }
Expand Down
93 changes: 50 additions & 43 deletions spec/webdrivers/chromedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
it 'raises ConnectionError when offline, and no binary exists' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(chromedriver).to receive(:exists?).and_return(false)
chromedriver.required_version = '115.0.5790.102'

msg = %r{Can not reach https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json}
msg = %r{Can not reach https://googlechromelabs.github.io/chrome-for-testing}
expect { chromedriver.update }.to raise_error(Webdrivers::ConnectionError, msg)
end
end
Expand Down Expand Up @@ -100,8 +101,9 @@

it 'raises ConnectionError if offline' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
chromedriver.required_version = '115.0.5790.102'

msg = %r{Can not reach https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json}
msg = %r{Can not reach https://googlechromelabs.github.io/chrome-for-testing}
expect { chromedriver.update }.to raise_error(Webdrivers::ConnectionError, msg)
end
end
Expand Down Expand Up @@ -142,49 +144,53 @@
allow(Webdrivers::System).to receive(:download)
end

it 'uses the correct chromedriver filename suffix for Intel' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(false)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('106.0.5249.61'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_mac64.zip'), anything)
end

it 'uses the correct chromedriver filename suffix from version 106.0.5249.61 for Silicon' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('106.0.5249.61'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_arm64.zip'), anything)
end
context 'with Intel architecture' do
it 'v114 uses correct suffix' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(false)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('114.0.5735.90'))
chromedriver.required_version = nil

it 'uses the correct chromedriver filename suffix for versions less than 106.0.5249.61 for Silicon' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('106.0.5249.21'))
chromedriver.required_version = nil
chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_mac64.zip'), anything)
end

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_mac64_m1.zip'), anything)
end

it 'uses the correct chromedriver filename suffix for versions greater than 115 for Intel' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(false)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('115.0.5790.102'))
chromedriver.required_version = nil
it 'v115 uses correct suffix' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(false)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('115.0.5790.102'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('-mac-x64.zip'), anything)
chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('-mac-x64.zip'), anything)
end
end

it 'uses the correct chromedriver filename suffix for versions greater than 115 for Silicon' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('115.0.5790.102'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('-mac-arm64.zip'), anything)
context 'with Apple architecture' do
it 'v106.0.5249.61 uses correct suffix' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('106.0.5249.61'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_arm64.zip'), anything)
end

it 'less than v106.0.5249.61 uses correct suffix' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('106.0.5249.21'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('_mac64_m1.zip'), anything)
end

it 'v115 uses correct suffix' do
allow(Webdrivers::System).to receive(:apple_m1_architecture?).and_return(true)
allow(chromedriver).to receive(:latest_version).and_return(Gem::Version.new('115.0.5790.102'))
chromedriver.required_version = nil

chromedriver.update
expect(Webdrivers::System).to have_received(:download).with(end_with('-mac-arm64.zip'), anything)
end
end
end
end
Expand Down Expand Up @@ -224,7 +230,7 @@
msg = 'Unable to find latest point release version for 999.0.0. '\
'You appear to be using a non-production version of Chrome. '\
'Please set `Webdrivers::Chromedriver.required_version = <desired driver version>` '\
'to a known chromedriver version: https://googlechromelabs.github.io/chrome-for-testing'
'to a known chromedriver version: https://chromedriver.chromium.org/downloads/version-selection'

expect { chromedriver.latest_version }.to raise_exception(Webdrivers::VersionError, msg)
end
Expand All @@ -233,15 +239,16 @@
allow(chromedriver).to receive(:browser_version).and_return Gem::Version.new('72.0.9999.0000')
msg = 'Unable to find latest point release version for 72.0.9999. '\
'Please set `Webdrivers::Chromedriver.required_version = <desired driver version>` '\
'to a known chromedriver version: https://chromedriver.storage.googleapis.com/index.html'
'to a known chromedriver version: https://chromedriver.chromium.org/downloads/version-selection'

expect { chromedriver.latest_version }.to raise_exception(Webdrivers::VersionError, msg)
end

it 'raises ConnectionError when offline' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
chromedriver.required_version = '115.0.5790.102'

msg = %r{^Can not reach https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json}
msg = %r{^Can not reach https://googlechromelabs.github.io/chrome-for-testing}
expect { chromedriver.latest_version }.to raise_error(Webdrivers::ConnectionError, msg)
end

Expand Down
2 changes: 2 additions & 0 deletions spec/webdrivers/edgedriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
before { allow(edgedriver).to receive(:correct_binary?).and_return(false) }

it 'downloads binary' do
allow(Webdrivers::EdgeFinder).to receive(:version).and_return('115.0.1901.188')
edgedriver.update

expect(edgedriver.current_version).not_to be_nil
Expand Down Expand Up @@ -241,6 +242,7 @@

describe '#remove' do
it 'removes existing edgedriver' do
allow(Webdrivers::EdgeFinder).to receive(:version).and_return('115.0.1901.188')
edgedriver.update

edgedriver.remove
Expand Down

0 comments on commit 2ad32bd

Please sign in to comment.