Skip to content

Commit

Permalink
Refactor & test extended chrome args (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
maltoe authored Jul 25, 2024
1 parent 9bebccf commit fd1c01f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
24 changes: 9 additions & 15 deletions lib/chromic_pdf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ defmodule ChromicPDF do
## Chrome options
By default, ChromicPDF will try to run a Chrome instance in the local environment. The following options allow to customize the generated command line.
By default, ChromicPDF will try to run a Chrome instance in the local environment. The
following options allow to customize the generated command line.
### Custom command line switches
Expand All @@ -278,22 +279,15 @@ defmodule ChromicPDF do
[chrome_args: "--font-render-hinting=none"]
end
In some cases, chromic_pdf's default args may conflict with the ones you would like to add.
E.g. `--disable-gpu`
The problem is that you can't simply override them like so:
E.g. in default args within chromic_pdf's code, `--disable-gpu` and then
passing `chrome_args: "--enable-gpu"` won't work.
In this case, use Keyword form of the `:chrome_args` option which
allows targeted removing of problematic default args so that they don't
disrupt your chrome configuration.
In some cases, ChromicPDF's default arguments (e.g. `--disable-gpu`) may conflict with the ones
you would like to add. In this case, use can supply a keyword list to the `:chrome_args` option
which allows targeted removing of default arguments.
defp chromic_pdf_opts do
[chrome_args: [append: "--headless=new --angle=swiftshader",
remove: ["--headless", "--disable-gpu"]]]
[chrome_args: [
append: "--headless=new --angle=swiftshader",
remove: ["--headless", "--disable-gpu"]
]]
end
The `:chrome_executable` option allows to specify a custom Chrome/Chromium executable.
Expand Down
20 changes: 13 additions & 7 deletions lib/chromic_pdf/pdf/chrome_runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ defmodule ChromicPDF.ChromeRunner do
)
end

defp shell_command(extra_args, opts) do
# Public for unit tests.
@doc false
@spec shell_command(keyword()) :: binary()
@spec shell_command(binary() | [binary()], keyword()) :: binary()
def shell_command(extra_args \\ "", opts) do
Enum.join([~s("#{executable(opts)}") | args(extra_args, opts)], " ")
end

Expand Down Expand Up @@ -154,21 +158,23 @@ defmodule ChromicPDF.ChromeRunner do
end

defp append_if(list, _value, false), do: list
defp append_if(list, value, true), do: list ++ List.wrap(value)
defp append_if(list, value, true), do: append(list, value)

defp append(list, value), do: list ++ List.wrap(value)

defp apply_chrome_args(list, nil), do: list

defp apply_chrome_args(list, chrome_args) when is_binary(chrome_args) do
append_if(list, chrome_args, true)
append(list, chrome_args)
end

defp apply_chrome_args(list, extended) when is_list(extended) do
conflicting = List.wrap(Keyword.get(extended, :remove, []))
append = List.wrap(Keyword.get(extended, :append, []))
append = Keyword.get(extended, :append, [])
remove = Keyword.get(extended, :remove, []) |> List.wrap()

list
|> Enum.reject(&Enum.member?(conflicting, &1))
|> append_if(append, true)
|> Enum.reject(&Enum.member?(remove, &1))
|> append(append)
end

defp no_sandbox?(opts), do: Keyword.get(opts, :no_sandbox, false)
Expand Down
25 changes: 25 additions & 0 deletions test/unit/chromic_pdf/pdf/chrome_runner_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-License-Identifier: Apache-2.0

defmodule ChromicPDF.ChromeRunnerTest do
use ExUnit.Case, async: true
alias ChromicPDF.ChromeRunner

describe ":chrome_args option" do
test "additional arguments passed as binary" do
command = ChromeRunner.shell_command(chrome_args: "--some-extra-arg")
assert command =~ "--some-extra-arg"
end

test "removal of default arguments via extended :chrome_args" do
assert ChromeRunner.shell_command([]) =~ "--no-first-run"

command =
ChromeRunner.shell_command(
chrome_args: [remove: "--no-first-run", append: "--some-extra-arg"]
)

assert command =~ "--some-extra-arg"
refute command =~ "--no-first-run"
end
end
end

0 comments on commit fd1c01f

Please sign in to comment.