Skip to content

Commit

Permalink
Merge pull request #269 from StuartApp/main
Browse files Browse the repository at this point in the history
Add `env` option
  • Loading branch information
route authored May 25, 2022
2 parents 6a6017c + 970ec8c commit 024ad04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Ferrum::Browser.new(options)
than this will cause a `Ferrum::DeadBrowserError`.
* `:proxy` (Hash) - Specify proxy settings, [read more](https://github.com/rubycdp/ferrum#proxy)
* `:save_path` (String) - Path to save attachments with [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header.
* `:env` (Hash) - Environment variables you'd like to pass through to the process


## Navigation
Expand Down
4 changes: 3 additions & 1 deletion lib/ferrum/browser/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def initialize(options)

@logger = options[:logger]
@process_timeout = options.fetch(:process_timeout, PROCESS_TIMEOUT)
@env = Hash(options[:env])

tmpdir = Dir.mktmpdir("ferrum_user_data_dir_")
ObjectSpace.define_finalizer(self, self.class.directory_remover(tmpdir))
Expand All @@ -95,7 +96,8 @@ def start
ObjectSpace.define_finalizer(self, self.class.process_killer(@xvfb.pid))
end

@pid = ::Process.spawn(Hash(@xvfb&.to_env), *@command.to_a, process_options)
env = Hash(@xvfb&.to_env).merge(@env)
@pid = ::Process.spawn(env, *@command.to_a, process_options)
ObjectSpace.define_finalizer(self, self.class.process_killer(@pid))

parse_ws_url(read_io, @process_timeout)
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ class Browser
subject.quit
end
end

context "env variables" do
subject { Browser.new(env: { "LD_PRELOAD" => "some.so" }) }

it "passes through env" do
allow(::Process).to receive(:wait).and_return(nil)
allow(Client).to receive(:new).and_return(double.as_null_object)

allow(::Process).to receive(:spawn).with({ "LD_PRELOAD" => "some.so" }, any_args)

allow_any_instance_of(Process).to receive(:parse_ws_url)

subject.send(:start)
subject.quit
end
end
end
end
end

0 comments on commit 024ad04

Please sign in to comment.