-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
169 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Browser | ||
module Binary | ||
module_function | ||
|
||
def find(commands) | ||
enum(commands).first | ||
end | ||
|
||
def all(commands) | ||
enum(commands).force | ||
end | ||
|
||
def enum(commands) | ||
paths, exts = prepare_paths | ||
cmds = Array(commands).product(paths, exts) | ||
lazy_find(cmds) | ||
end | ||
|
||
def prepare_paths | ||
exts = (ENV.key?("PATHEXT") ? ENV.fetch("PATHEXT").split(";") : []) << "" | ||
paths = ENV["PATH"].split(File::PATH_SEPARATOR) | ||
raise EmptyPathError if paths.empty? | ||
|
||
[paths, exts] | ||
end | ||
|
||
def lazy_find(cmds) | ||
cmds.lazy.filter_map do |cmd, path, ext| | ||
cmd = File.expand_path("#{cmd}#{ext}", path) unless File.absolute_path?(cmd) | ||
|
||
next unless File.executable?(cmd) | ||
next if File.directory?(cmd) | ||
|
||
cmd | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cliver" | ||
require "net/http" | ||
require "json" | ||
require "addressable" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Browser | ||
describe Binary do | ||
let(:tmp_bin) { "#{PROJECT_ROOT}/spec/tmp/bin" } | ||
let(:bin1) { File.join(tmp_bin, "bin1") } | ||
let(:bin1_exe) { File.join(tmp_bin, "bin1.exe") } | ||
let(:bin2_no_x) { File.join(tmp_bin, "/bin2") } | ||
let(:bin3) { File.join(tmp_bin, "/bin3") } | ||
let(:bin4_exe) { File.join(tmp_bin, "/bin4.exe") } | ||
|
||
before do | ||
FileUtils.mkdir_p(tmp_bin) | ||
FileUtils.touch([bin1, bin1_exe, bin2_no_x, bin3, bin4_exe]) | ||
FileUtils.chmod("u=rwx,go=rx", bin1) | ||
FileUtils.chmod("u=rwx,go=rx", bin1_exe) | ||
FileUtils.chmod("u=rw,go=r", bin2_no_x) | ||
FileUtils.chmod("u=rwx,go=rx", bin3) | ||
FileUtils.chmod("u=rwx,go=rx", bin4_exe) | ||
|
||
@original_env_path = ENV.fetch("PATH", nil) | ||
@original_env_pathext = ENV.fetch("PATHEXT", nil) | ||
ENV["PATH"] = "#{tmp_bin}#{File::PATH_SEPARATOR}#{@original_env_path}" | ||
end | ||
|
||
after do | ||
FileUtils.rm_rf(tmp_bin) | ||
ENV["PATH"] = @original_env_path | ||
ENV["PATHEXT"] = @original_env_pathext | ||
end | ||
|
||
context "#find" do | ||
it "finds one binary" do | ||
expect(Binary.find("bin1")).to eq(bin1) | ||
end | ||
|
||
it "finds first binary when list is passed" do | ||
expect(Binary.find(%w[bin1 bin3])).to eq(bin1) | ||
end | ||
|
||
it "finds binary with PATHEXT" do | ||
ENV["PATHEXT"] = ".com;.exe" | ||
|
||
expect(Binary.find(%w[bin4])).to eq(bin4_exe) | ||
end | ||
|
||
it "finds binary without ext" do | ||
ENV["PATHEXT"] = ".com;.exe" | ||
|
||
expect(Binary.find("bin1")).to eq(bin1_exe) | ||
FileUtils.rm_rf(bin1_exe) | ||
expect(Binary.find("bin1")).to eq(bin1) | ||
end | ||
|
||
it "raises an error" do | ||
ENV["PATH"] = "" | ||
|
||
expect { Binary.find(%w[bin1]) }.to raise_error(Ferrum::EmptyPathError) | ||
end | ||
end | ||
|
||
context "#all" do | ||
it "finds one binary" do | ||
expect(Binary.all("bin1")).to eq([bin1]) | ||
end | ||
|
||
it "finds multiple binaries with ext" do | ||
ENV["PATHEXT"] = ".com;.exe" | ||
|
||
expect(Binary.all("bin1")).to eq([bin1_exe, bin1]) | ||
end | ||
|
||
it "finds all binary when list passed" do | ||
expect(Binary.all(%w[bin1 bin3])).to eq([bin1, bin3]) | ||
end | ||
|
||
it "finds binary with PATHEXT" do | ||
ENV["PATHEXT"] = ".com;.exe" | ||
|
||
expect(Binary.all(%w[bin4])).to eq([bin4_exe]) | ||
end | ||
|
||
it "raises an error" do | ||
ENV["PATH"] = "" | ||
|
||
expect { Binary.all(%w[bin1]) }.to raise_error(Ferrum::EmptyPathError) | ||
end | ||
end | ||
|
||
it "works lazily" do | ||
enum = Binary.lazy_find(%w[ls which none]) | ||
|
||
expect(enum.instance_of?(Enumerator::Lazy)).to be_truthy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters