Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
uses information from (https://askubuntu.com/questions/1177729/wsl-am-i-running-version-1-or-version-2) checking if the WSL kernel version is < 4.19 then it is WSL 1
  • Loading branch information
Rodrigo Urubatan committed Jan 13, 2021
1 parent 591cb8f commit c801b14
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
Binary file added lib/webdrivers/.system.rb.swp
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/webdrivers/chrome_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def mac_location
end

def linux_location
return wsl_location if System.wsl?
return wsl_location if System.wsl_v1?

directories = %w[/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /snap/bin /opt/google/chrome]
files = %w[google-chrome chrome chromium chromium-browser]
Expand All @@ -114,7 +114,7 @@ def wsl_version(location)
end

def linux_version(location)
return wsl_version(location) if System.wsl?
return wsl_version(location) if System.wsl_v1?

System.call(location, '--product-version')&.strip
end
Expand Down
4 changes: 2 additions & 2 deletions lib/webdrivers/chromedriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def latest_point_release(version)
end

def file_name
System.platform == 'win' || System.wsl? ? 'chromedriver.exe' : 'chromedriver'
System.platform == 'win' || System.wsl_v1? ? 'chromedriver.exe' : 'chromedriver'
end

def apple_m1_compatible?(version)
Expand All @@ -102,7 +102,7 @@ def download_url

apple_arch = apple_m1_compatible?(version) ? '_m1' : ''

file_name = System.platform == 'win' || System.wsl? ? 'win32' : "#{System.platform}64#{apple_arch}"
file_name = System.platform == 'win' || System.wsl_v1? ? 'win32' : "#{System.platform}64#{apple_arch}"
url = "#{base_url}/#{version}/chromedriver_#{file_name}.zip"
Webdrivers.logger.debug "chromedriver URL: #{url}"
@download_url = url
Expand Down
14 changes: 12 additions & 2 deletions lib/webdrivers/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,18 @@ def platform
end

# @return [TrueClass, FalseClass]
def wsl?
platform == 'linux' && File.open('/proc/version').read.downcase.include?('microsoft')
def wsl_v1?
if platform == 'linux'
version=File.open('/proc/version')&.read
if version.downcase.include?('microsoft')
ver = Gem::Version.new(version.gsub(/.*Linux version ([^ ]*).*/, '\1'))
ver.canonical_segments[0] <= 4 && ver.canonical_segments[1] < 19
else
false
end
else
false
end
end

# @param [String] path
Expand Down
Binary file added spec/webdrivers/.system_spec.rb.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/webdrivers/chrome_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
before do
skip "The current platform cannot be WSL, as it's not Linux" unless Selenium::WebDriver::Platform.linux?

allow(Webdrivers::System).to receive(:wsl?).and_return(true)
allow(Webdrivers::System).to receive(:wsl_v1?).and_return(true)
allow(Webdrivers::System).to receive(:to_wsl_path).and_return('')
allow(Webdrivers::System).to receive(:to_win32_path).and_return('')
end
Expand Down
24 changes: 21 additions & 3 deletions spec/webdrivers/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

require 'spec_helper'

wslv2_proc_contents = [
'Linux version 4.19.0-18362-Microsoft',
'(Microsoft@Microsoft.com)',
'(gcc version 5.4.0 (GCC) )',
'#836-Microsoft',
'Mon May 05 16:04:00 PST 2020'
].join ' '

wsl_proc_contents = [
'Linux version 4.4.0-18362-Microsoft',
'(Microsoft@Microsoft.com)',
Expand All @@ -11,14 +19,24 @@
].join ' '

describe Webdrivers::System do
describe '#wsl?' do
describe '#wsl_v1?' do
context 'when the current platform is linux but WSLv2' do
before { allow(described_class).to receive(:platform).and_return 'linux' }

it 'checks /proc/version' do
allow(File).to receive(:open).with('/proc/version').and_return(StringIO.new(wslv2_proc_contents))

expect(described_class.wsl_v1?).to eq false
end
end

context 'when the current platform is linux' do
before { allow(described_class).to receive(:platform).and_return 'linux' }

it 'checks /proc/version' do
allow(File).to receive(:open).with('/proc/version').and_return(StringIO.new(wsl_proc_contents))

expect(described_class.wsl?).to eq true
expect(described_class.wsl_v1?).to eq true
end
end

Expand All @@ -28,7 +46,7 @@
it 'does not bother checking proc' do
allow(File).to receive(:open).and_call_original

expect(described_class.wsl?).to eq false
expect(described_class.wsl_v1?).to eq false

expect(File).not_to have_received(:open).with('/proc/version')
end
Expand Down

0 comments on commit c801b14

Please sign in to comment.