diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e4f251f --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/lib/capybara/wsl.rb b/lib/capybara/wsl.rb index 67fb74c..6d69a4c 100644 --- a/lib/capybara/wsl.rb +++ b/lib/capybara/wsl.rb @@ -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 diff --git a/lib/capybara/wsl/distros.rb b/lib/capybara/wsl/distros.rb deleted file mode 100644 index 6bab4ee..0000000 --- a/lib/capybara/wsl/distros.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -module Capybara - module WSL - DISTROS = { - alpine: "Alpine-WSL", - centos: "CentOS", - debian: "Debian", - fedors: "Fedora-Remix-for-WSL", - kali: "kali-linux", - opensuse: "openSUSE-Leap-15-1", - pengwin: "Pengwin", - ubuntu: "Ubuntu", - }.freeze - end -end diff --git a/lib/capybara/wsl/dsl.rb b/lib/capybara/wsl/dsl.rb index 14c1b52..2f2c4d2 100644 --- a/lib/capybara/wsl/dsl.rb +++ b/lib/capybara/wsl/dsl.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Capybara module WSL module DSL diff --git a/lib/capybara/wsl/errors.rb b/lib/capybara/wsl/errors.rb new file mode 100644 index 0000000..89f9205 --- /dev/null +++ b/lib/capybara/wsl/errors.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Capybara + module WSL + class CapybaraWSLError < StandardError; end + class CannotDetectWSLPath < CapybaraWSLError; end + end +end diff --git a/lib/capybara/wsl/path.rb b/lib/capybara/wsl/path.rb new file mode 100644 index 0000000..28f6d5b --- /dev/null +++ b/lib/capybara/wsl/path.rb @@ -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