Skip to content

Commit

Permalink
Steal from Phoenix
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardarella committed Mar 12, 2024
1 parent 4a70f1e commit 7e7b811
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 16 deletions.
175 changes: 163 additions & 12 deletions lib/mix/lvn.test_helper.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
Mix.Project.deps_paths[:phx_new]
|> Path.join("test/mix_helper.exs")
|> Code.require_file()

defmodule Mix.Lvn.TestHelper do
import MixHelper, except: [
assert_file: 1,
assert_file: 2,
refute_file: 1
]
@moduledoc false
# stolen from phx_new

import ExUnit.Assertions
import ExUnit.CaptureIO

def tmp_path do
Path.expand("../../tmp", __DIR__)
end

defp random_string(len) do
len |> :crypto.strong_rand_bytes() |> Base.encode64() |> binary_part(0, len)
end

def in_tmp(which, function) do
path = Path.join([tmp_path(), random_string(10) <> to_string(which)])

try do
File.rm_rf!(path)
File.mkdir_p!(path)
File.cd!(path, function)
after
File.rm_rf!(path)
end
end

def in_tmp_project(which, function) do
conf_before = Application.get_env(:phoenix, :generators) || []
path = Path.join([tmp_path(), random_string(10) <> to_string(which)])

try do
File.rm_rf!(path)
File.mkdir_p!(path)

defdelegate assert_file(file), to: MixHelper
defdelegate assert_file(file, match), to: MixHelper
defdelegate refute_file(file), to: MixHelper
File.cd!(path, fn ->
File.touch!("mix.exs")

File.write!(".formatter.exs", """
[
import_deps: [:phoenix, :ecto, :ecto_sql],
inputs: ["*.exs"]
]
""")

function.()
end)
after
File.rm_rf!(path)
Application.put_env(:phoenix, :generators, conf_before)
end
end

def in_tmp_live_project(test, func) do
in_tmp_project(test, fn ->
Expand All @@ -22,6 +60,30 @@ defmodule Mix.Lvn.TestHelper do
end)
end

def in_tmp_umbrella_project(which, function) do
conf_before = Application.get_env(:phoenix, :generators) || []
path = Path.join([tmp_path(), random_string(10) <> to_string(which)])

try do
apps_path = Path.join(path, "apps")
config_path = Path.join(path, "config")
File.rm_rf!(path)
File.mkdir_p!(path)
File.mkdir_p!(apps_path)
File.mkdir_p!(config_path)
File.touch!(Path.join(path, "mix.exs"))

for file <- ~w(config.exs dev.exs test.exs prod.exs) do
File.write!(Path.join(config_path, file), "import Config\n")
end

File.cd!(apps_path, function)
after
Application.put_env(:phoenix, :generators, conf_before)
File.rm_rf!(path)
end
end

def in_tmp_live_umbrella_project(test, func) do
in_tmp_umbrella_project(test, fn ->
File.mkdir_p!("live_view_native/lib")
Expand All @@ -31,4 +93,93 @@ defmodule Mix.Lvn.TestHelper do
func.()
end)
end

def in_project(app, path, fun) do
%{name: name, file: file} = Mix.Project.pop()

try do
capture_io(:stderr, fn ->
Mix.Project.in_project(app, path, [], fun)
end)
after
Mix.Project.push(name, file)
end
end

def assert_file(file) do
assert File.regular?(file), "Expected #{file} to exist, but does not"
end

def refute_file(file) do
refute File.regular?(file), "Expected #{file} to not exist, but it does"
end

def assert_file(file, match) do
cond do
is_list(match) ->
assert_file(file, &Enum.each(match, fn m -> assert &1 =~ m end))

is_binary(match) or is_struct(match, Regex) ->
assert_file(file, &assert(&1 =~ match))

is_function(match, 1) ->
assert_file(file)
match.(File.read!(file))

true ->
raise inspect({file, match})
end
end

def modify_file(path, function) when is_binary(path) and is_function(function, 1) do
path
|> File.read!()
|> function.()
|> write_file!(path)
end

defp write_file!(content, path) do
File.write!(path, content)
end

def with_generator_env(app_name \\ :phoenix, new_env, fun) do
config_before = Application.fetch_env(app_name, :generators)
Application.put_env(app_name, :generators, new_env)

try do
fun.()
after
case config_before do
{:ok, config} -> Application.put_env(app_name, :generators, config)
:error -> Application.delete_env(app_name, :generators)
end
end
end

def umbrella_mixfile_contents do
"""
defmodule Umbrella.MixProject do
use Mix.Project
def project do
[
apps_path: "apps",
deps: deps()
]
end
defp deps do
[]
end
end
"""
end

def flush do
receive do
_ -> flush()
after
0 -> :ok
end
end
end
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ defmodule LiveViewNative.MixProject do
defp deps do
[
{:phoenix, "~> 1.7.0"},
{:phx_new, github: "phoenixframework/phoenix", sparse: "installer", branch: "main", only: :test},
{:phoenix_view, "~> 2.0"},
{:phoenix_live_view, "~> 0.20.10"},
{:phoenix_live_reload, "~> 1.4", only: :test},
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"castore": {:hex, :castore, "1.0.5", "9eeebb394cc9a0f3ae56b813459f990abb0a3dedee1be6b27fdb50301930502f", [:mix], [], "hexpm", "8d7c597c3e4a64c395980882d4bca3cebb8d74197c590dc272cfd3b6a6310578"},
"castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"},
"cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"},
Expand All @@ -11,7 +11,7 @@
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"live_view_native_test": {:git, "https://github.com/liveview-native/live_view_native_test.git", "7922516d80a5279e9920227109265bbfbc0b7f66", [tag: "v0.3.0-alpha.4"]},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_eex": {:hex, :makeup_eex, "0.1.1", "89352d5da318d97ae27bbcc87201f274504d2b71ede58ca366af6a5fbed9508d", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.16", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_html, "~> 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d111a0994eaaab09ef1a4b3b313ef806513bb4652152c26c0d7ca2be8402a964"},
"makeup_eex": {:hex, :makeup_eex, "0.1.2", "93a5ef3d28ed753215dba2d59cb40408b37cccb4a8205e53ef9b5319a992b700", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.16 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_html, "~> 0.1.0 or ~> 1.0", [hex: :makeup_html, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "6140eafb28215ad7182282fd21d9aa6dcffbfbe0eb876283bc6b768a6c57b0c3"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"},
"makeup_html": {:hex, :makeup_html, "0.1.1", "c3d4abd39d5f7e925faca72ada6e9cc5c6f5fa7cd5bc0158315832656cf14d7f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "44f2a61bc5243645dd7fafeaa6cc28793cd22f3c76b861e066168f9a5b2c26a4"},
Expand All @@ -20,7 +20,7 @@
"owl": {:hex, :owl, "0.9.0", "9b33d64734bd51d3fc1d6ed01b12f8c2ed23e1fbf8c43658a6dfbff62578bd03", [:mix], [{:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: true]}], "hexpm", "cd70b55327985f8f24d38cb7de5bf8a8d24040e1b49cca2345508f8119ce81fd"},
"phoenix": {:hex, :phoenix, "1.7.11", "1d88fc6b05ab0c735b250932c4e6e33bfa1c186f76dcf623d8dd52f07d6379c7", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "b1ec57f2e40316b306708fe59b92a16b9f6f4bf50ccfa41aa8c7feb79e0ec02a"},
"phoenix_html": {:hex, :phoenix_html, "4.1.1", "4c064fd3873d12ebb1388425a8f2a19348cef56e7289e1998e2d2fa758aa982e", [:mix], [], "hexpm", "f2f2df5a72bc9a2f510b21497fd7d2b86d932ec0598f0210fed4114adc546c6f"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.5.1", "6ab463cf43938ee9906067b33c8d66782343de4280a70084cd5617accc6345a8", [:mix], [{:file_system, "~> 0.3 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "e8467d308b61f294f68afe12c81bf585584c7ceed40ec8adde88ec176d480a78"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.5.2", "354460993a480656b71c3887f5565f612b3bdbdd8688c83f9e6f512307067dd4", [:mix], [{:file_system, "~> 0.3 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "2bb3722f327e14a7aa47b1acf27ed633c8cd27b167e18b8237954b9b4804af39"},
"phoenix_live_view": {:hex, :phoenix_live_view, "0.20.12", "3f4b5849b8018023c01e41a7da2e6c986222cc3f0282858f8af11221638645cb", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ae3a143cc33325f3a4c192b7da1726e6665e154c50e1461af4cd7d561ccfd9ab"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
Expand Down

0 comments on commit 7e7b811

Please sign in to comment.