Skip to content

Commit

Permalink
Automatically detect WSL distro
Browse files Browse the repository at this point in the history
  • Loading branch information
dersnek committed Jul 10, 2020
1 parent 01879a8 commit e1c4822
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 43 deletions.
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PATH
remote: .
specs:
capybara-wsl (0.2.1)
capybara (>= 2.0)
launchy (>= 2.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
capybara (3.33.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
launchy (2.5.0)
addressable (~> 2.7)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
public_suffix (4.0.5)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
regexp_parser (1.7.1)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
capybara-wsl!

BUNDLED WITH
2.1.4
35 changes: 8 additions & 27 deletions lib/capybara/wsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,32 @@

require "capybara"
require "capybara/dsl"
require_relative "wsl/distros"
require_relative "wsl/dsl"
require_relative "wsl/path"

module Capybara
module WSL
class CapybaraWSLError < StandardError; end
class DistroKeyNotSupported < CapybaraWSLError; end

def self.save_and_open_page(path = nil)
Capybara.current_session.save_page(path).tap do |s_path|
wsl_path = modify_path(s_path)
Capybara.current_session.send(:open_file, wsl_path)
open_file(s_path)
end
end

def self.save_and_open_screenshot(path = nil, **options)
Capybara.current_session.save_screenshot(path, options).tap do |s_path|
wsl_path = modify_path(s_path)
Capybara.current_session.send(:open_file, wsl_path)
end
end

def self.distro
@distro || :ubuntu
end

def self.distro=(name)
if name.is_a?(Symbol) && DISTROS[name].nil?
raise(Capybara::WSL::DistroKeyNotSupported,
"This distro key is not supported. Please use supported key or pass a string with exact distro folder name.")
open_file(s_path)
end

@distro = name
end

private

def self.modify_path(path)
path.prepend("file://///wsl$/#{distro_folder_name}")
def self.open_file(path)
wsl_path = modify_path(path)
Capybara.current_session.send(:open_file, wsl_path)
end

def self.distro_folder_name
return distro if distro.is_a?(String)

DISTROS[distro]
def self.modify_path(path)
path.prepend(Path.get)
end
end
end
Expand Down
16 changes: 0 additions & 16 deletions lib/capybara/wsl/distros.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/capybara/wsl/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Capybara
module WSL
module DSL
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/wsl/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Capybara
module WSL
class CapybaraWSLError < StandardError; end
class CannotDetectWSLPath < CapybaraWSLError; end
end
end
20 changes: 20 additions & 0 deletions lib/capybara/wsl/path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require_relative "errors"

module Capybara
module WSL
module Path
def self.get
path = `wslpath -m "/"`&.strip

if path.empty?
raise(Capybara::WSL::CannotDetectWSLPath,
"Cannot detect WSL path. Are you sure you're running WSL?")
end

"file:///#{path}"
end
end
end
end

0 comments on commit e1c4822

Please sign in to comment.