Repository: phoenixframework/phoenix
Pull request #3694 (2020-03-04 10:23:10)

Add missing floki package for phx.new --live
The floki package is missing when generating a new phoenix project with the option --live.

Without this package you will get the following error when running `mix test:

```
...
** (UndefinedFunctionError) function Floki.parse_document/1 is undefined (module Floki is not available)
...
```


Patch:
diff --git a/installer/lib/phx_new/generator.ex b/installer/lib/phx_new/generator.ex
index 78e3dfc09..c2f26aea9 100644
--- a/installer/lib/phx_new/generator.ex
+++ b/installer/lib/phx_new/generator.ex
@@ -148,6 +148,7 @@ defmodule Phx.New.Generator do
       phoenix_path: phoenix_path,
       phoenix_webpack_path: phoenix_webpack_path(project, dev),
       phoenix_html_webpack_path: phoenix_html_webpack_path(project),
+      phoenix_live_view_webpack_path: phoenix_live_view_webpack_path(project),
       phoenix_static_path: phoenix_static_path(phoenix_path),
       pubsub_server: pubsub_server,
       secret_key_base: random_string(64),
@@ -288,6 +289,11 @@ defmodule Phx.New.Generator do
   defp phoenix_html_webpack_path(%Project{in_umbrella?: false}),
     do: "../deps/phoenix_html"
 
+  defp phoenix_live_view_webpack_path(%Project{in_umbrella?: true}),
+    do: "../../../deps/phoenix_live_view"
+  defp phoenix_live_view_webpack_path(%Project{in_umbrella?: false}),
+    do: "../deps/phoenix_live_view"
+
   # defp phoenix_dep("deps/phoenix"), do: ~s[{:phoenix, "~> #{@phoenix_version}"}]
   defp phoenix_dep("deps/phoenix"), do: ~s[{:phoenix, github: "phoenixframework/phoenix", override: true}]
   defp phoenix_dep(path), do: ~s[{:phoenix, path: #{inspect path}, override: true}]
diff --git a/installer/templates/phx_assets/package.json b/installer/templates/phx_assets/package.json
index 336a8280f..31c176432 100644
--- a/installer/templates/phx_assets/package.json
+++ b/installer/templates/phx_assets/package.json
@@ -9,7 +9,7 @@
   "dependencies": {
     "phoenix": "file:<%= phoenix_webpack_path %>"<%= if html do %>,
     "phoenix_html": "file:<%= phoenix_html_webpack_path %>"<% end %><%= if live do %>,
-    "phoenix_live_view": "file:../deps/phoenix_live_view",
+    "phoenix_live_view": "file:<%= phoenix_live_view_webpack_path %>",
     "nprogress": "^0.2.0"<% end %>
   },
   "devDependencies": {
diff --git a/installer/templates/phx_single/mix.exs b/installer/templates/phx_single/mix.exs
index 8f7f22439..b7084e855 100644
--- a/installer/templates/phx_single/mix.exs
+++ b/installer/templates/phx_single/mix.exs
@@ -41,7 +41,8 @@ defmodule <%= app_module %>.MixProject do
       {:phoenix_ecto, "~> 4.1"},
       {:ecto_sql, "~> 3.1"},
       {<%= inspect adapter_app %>, ">= 0.0.0"},<% end %><%= if html do %><%= if live do %>
-      {:phoenix_live_view, "~> 0.8.1"},<% end %>
+      {:phoenix_live_view, "~> 0.8.1"},
+      {:floki, ">= 0.0.0", only: :test},<% end %>
       {:phoenix_html, "~> 2.11"},
       {:phoenix_live_reload, "~> 1.2", only: :dev},<% end %><%= if gettext do %>
       {:gettext, "~> 0.11"},<% end %>
diff --git a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs
index a50cfd562..22b04fb1d 100644
--- a/installer/templates/phx_umbrella/apps/app_name_web/mix.exs
+++ b/installer/templates/phx_umbrella/apps/app_name_web/mix.exs
@@ -39,7 +39,8 @@ defmodule <%= web_namespace %>.MixProject do
     [
       <%= phoenix_dep %>,<%= if ecto do %>
       {:phoenix_ecto, "~> 4.0"},<% end %><%= if html do %><%= if live do %>
-      {:phoenix_live_view, "~> 0.8.1"},<% end %>
+      {:phoenix_live_view, "~> 0.8.1"},
+      {:floki, ">= 0.0.0", only: :test},<% end %>
       {:phoenix_html, "~> 2.11"},
       {:phoenix_live_reload, "~> 1.2", only: :dev},<% end %><%= if gettext do %>
       {:gettext, "~> 0.11"},<% end %><%= if app_name != web_app_name do %>


Test patch:
diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs
index 0a14cdf18..8481ab35c 100644
--- a/installer/test/phx_new_test.exs
+++ b/installer/test/phx_new_test.exs
@@ -140,6 +140,7 @@ defmodule Mix.Tasks.Phx.NewTest do
       refute_file "phx_blog/lib/phx_blog_web/live/page_live_view.ex"
       refute_file "phx_blog/assets/js/live.js"
       assert_file "phx_blog/mix.exs", &refute(&1 =~ ~r":phoenix_live_view")
+      assert_file "phx_blog/mix.exs", &refute(&1 =~ ~r":floki")
       assert_file "phx_blog/assets/package.json",
                   &refute(&1 =~ ~s["phoenix_live_view": "file:../deps/phoenix_live_view"])
 
@@ -284,6 +285,7 @@ defmodule Mix.Tasks.Phx.NewTest do
     in_tmp "new with live", fn ->
       Mix.Tasks.Phx.New.run([@app_name, "--live"])
       assert_file "phx_blog/mix.exs", &assert(&1 =~ ~r":phoenix_live_view")
+      assert_file "phx_blog/mix.exs", &assert(&1 =~ ~r":floki")
 
       refute_file "phx_blog/lib/phx_blog_web/controllers/page_controller.ex"
 
diff --git a/installer/test/phx_new_umbrella_test.exs b/installer/test/phx_new_umbrella_test.exs
index c69b8ae20..97787a23f 100644
--- a/installer/test/phx_new_umbrella_test.exs
+++ b/installer/test/phx_new_umbrella_test.exs
@@ -244,6 +244,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
 
       # No LiveView (in web_path)
       assert_file web_path(@app, "mix.exs"), &refute(&1 =~ ~r":phoenix_live_view")
+      assert_file web_path(@app, "mix.exs"), &refute(&1 =~ ~r":floki")
       refute File.exists?(web_path(@app, "lib/#{@app}_web/templates/page/hero.html.leex"))
 
       refute_file web_path(@app, "assets/js/live.js")
@@ -310,9 +311,10 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
       end
 
       assert_file web_path(@app, "mix.exs"), &assert(&1 =~ ~r":phoenix_live_view")
+      assert_file web_path(@app, "mix.exs"), &assert(&1 =~ ~r":floki")
 
       assert_file web_path(@app, "assets/package.json"),
-                  ~s["phoenix_live_view": "file:../deps/phoenix_live_view"]
+                  ~s["phoenix_live_view": "file:../../../deps/phoenix_live_view"]
 
       assert_file web_path(@app, "assets/js/app.js"), fn file ->
         assert file =~ ~s[import {LiveSocket} from "phoenix_live_view"]