-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
What changed? ============== This introduces a new `open_browser/1` helper to open the default browser to display current HTML of `session`. The `Live` implementation mostly wraps LiveView's `open_browser/1` implementation. But for the `Static` implementation, we bring a lot of the work that was done in LiveView into `PhoenixTest`. The biggest difference is that we do not bring LiveView's unique integer generation. Instead, we rely on `System.unique_integer` to help us generate unique names for the files. Finally, we opt for using the name `open_browser/1` (as opposed to the original suggestion of `preview`) to try to align with LiveView's existing language so it's (hopefully) more intuitive to LiveView users who are already accustomed to the behavior.
- Loading branch information
1 parent
d699792
commit b9d8347
Showing
8 changed files
with
162 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule PhoenixTest.OpenBrowser do | ||
@moduledoc false | ||
|
||
# This module contains private functionality ported over from | ||
# `Phoenix.LiveViewTest` to make `open_browser` work with `Static` tests. | ||
|
||
@doc """ | ||
Fully qualifies static assets paths so the browser can find them. | ||
""" | ||
def prefix_static_paths(node, endpoint) do | ||
static_path = static_path(endpoint) | ||
|
||
case node do | ||
# Remove script tags | ||
{"script", _, _} -> nil | ||
# Skip prefixing src attributes on anchor tags | ||
{"a", _, _} = link -> link | ||
{el, attrs, children} -> {el, maybe_prefix_static_path(attrs, static_path), children} | ||
el -> el | ||
end | ||
end | ||
|
||
defp static_path(endpoint) do | ||
static_url = endpoint.config(:static_url) || [] | ||
priv_dir = :otp_app |> endpoint.config() |> Application.app_dir("priv") | ||
|
||
if Keyword.get(static_url, :path) do | ||
priv_dir | ||
else | ||
Path.join(priv_dir, "static") | ||
end | ||
end | ||
|
||
defp maybe_prefix_static_path(attrs, nil), do: attrs | ||
|
||
defp maybe_prefix_static_path(attrs, static_path) do | ||
Enum.map(attrs, fn | ||
{"src", path} -> {"src", prefix_static_path(path, static_path)} | ||
{"href", path} -> {"href", prefix_static_path(path, static_path)} | ||
attr -> attr | ||
end) | ||
end | ||
|
||
defp prefix_static_path(<<"//" <> _::binary>> = url, _prefix), do: url | ||
|
||
defp prefix_static_path(<<"/" <> _::binary>> = path, prefix) do | ||
"file://#{Path.join([prefix, path])}" | ||
end | ||
|
||
defp prefix_static_path(url, _), do: url | ||
|
||
@doc """ | ||
System agnostic function to open the default browser with the given `path`. | ||
This is ripped verbatim from `Phoenix.LiveViewTest`. | ||
""" | ||
def open_with_system_cmd(path) do | ||
{cmd, args} = | ||
case :os.type() do | ||
{:win32, _} -> | ||
{"cmd", ["/c", "start", path]} | ||
|
||
{:unix, :darwin} -> | ||
{"open", [path]} | ||
|
||
{:unix, _} -> | ||
if System.find_executable("cmd.exe") do | ||
{"cmd.exe", ["/c", "start", path]} | ||
else | ||
{"xdg-open", [path]} | ||
end | ||
end | ||
|
||
System.cmd(cmd, args) | ||
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