Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run mix format on generated files by tasks phx.gen.* #6015

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ erl_crash.dump
phoenix-*.ez

.DS_Store

/.vscode/
ShPakvel marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 8 additions & 18 deletions installer/test/mix_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ defmodule MixHelper do
end

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

Expand All @@ -38,24 +37,14 @@ defmodule MixHelper do

File.cd!(path, fn ->
File.touch!("mix.exs")

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

function.()
with_generator_env([format_extensions: []], function)
end)
after
File.rm_rf!(base)
Application.put_env(:phoenix, :generators, conf_before)
end
end

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

Expand All @@ -72,9 +61,10 @@ defmodule MixHelper do
File.write!(Path.join(config_path, file), "import Config\n")
end

File.cd!(apps_path, function)
File.cd!(apps_path, fn ->
with_generator_env([format_extensions: []], function)
end)
after
Application.put_env(:phoenix, :generators, conf_before)
File.rm_rf!(base)
end
end
Expand Down Expand Up @@ -131,15 +121,15 @@ defmodule MixHelper do
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)
config_before = Application.get_env(app_name, :generators)
Application.put_env(app_name, :generators, Keyword.merge(config_before || [], 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)
nil -> Application.delete_env(app_name, :generators)
config -> Application.put_env(app_name, :generators, config)
end
end
end
Expand Down
37 changes: 34 additions & 3 deletions integration_test/test/code_generation/app_with_defaults_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
with_installer_tmp("app_with_defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.html Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down Expand Up @@ -74,7 +84,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
with_installer_tmp("app_with_defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog")

mix_run!(~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.json Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down Expand Up @@ -121,7 +141,18 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithDefaultsTest do
with_installer_tmp("app_with_defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--live"])

mix_run!(~w(phx.gen.live Blog Post posts title:unique body:string p:boolean s:enum:a:b:c), app_root_path)
mix_run!(
~w(phx.gen.live Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
public:boolean
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
with_installer_tmp("new with defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)
mix_run!(
~w(phx.gen.html Accounts Group groups
name
details
other_very_important_attribute
one_more_attribute_with_long_name),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/phx_blog_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down Expand Up @@ -159,7 +166,8 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
@tag database: :mssql
test "has a passing test suite (--no-live)" do
with_installer_tmp("app_with_defaults", fn tmp_dir ->
{app_root_path, _} = generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "phx_blog", ["--database", "mssql", "--live"])

mix_run!(~w(phx.gen.html Accounts Group groups name), app_root_path)

Expand All @@ -174,7 +182,10 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMSSQLAdapterTest do
""")
end)

mix_run!(~w(phx.gen.auth Accounts User users --hashing-lib pbkdf2 --merge-with-existing-context --no-live), app_root_path)
mix_run!(
~w(phx.gen.auth Accounts User users --hashing-lib pbkdf2 --merge-with-existing-context --no-live),
app_root_path
)

drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql"])

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.html Blog Post posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -21,6 +31,9 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
""")
end)

# TODO: Uncomment when fix unformatted code from `phx.new` generator.
# assert_no_compilation_warnings(app_root_path)
# assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand All @@ -34,7 +47,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql"])

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.json Blog Post posts posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -47,6 +70,9 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
""")
end)

# TODO: Uncomment when fix unformatted code from `phx.new` generator.
# assert_no_compilation_warnings(app_root_path)
# assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand All @@ -60,7 +86,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_mysql_app", ["--database", "mysql", "--live"])

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.live Blog Post posts posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_mysql_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -76,6 +112,9 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithMySqlAdapterTest do
""")
end)

# TODO: Uncomment when fix unformatted code from `phx.new` generator.
# assert_no_compilation_warnings(app_root_path)
# assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand Down
42 changes: 39 additions & 3 deletions integration_test/test/code_generation/app_with_sqlite3_adapter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3"])

mix_run!(~w(phx.gen.html Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.html Blog Post posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -21,6 +31,8 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
""")
end)

assert_no_compilation_warnings(app_root_path)
assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand All @@ -34,7 +46,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3"])

mix_run!(~w(phx.gen.json Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.json Blog Post posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -47,6 +69,8 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
""")
end)

assert_no_compilation_warnings(app_root_path)
assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand All @@ -60,7 +84,17 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
{app_root_path, _} =
generate_phoenix_app(tmp_dir, "default_sqlite3_app", ["--database", "sqlite3", "--live"])

mix_run!(~w(phx.gen.live Blog Post posts title body:string status:enum:unpublished:published:deleted), app_root_path)
mix_run!(
~w(phx.gen.live Blog Post posts
title
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
app_root_path
)

modify_file(Path.join(app_root_path, "lib/default_sqlite3_app_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand All @@ -76,6 +110,8 @@ defmodule Phoenix.Integration.CodeGeneration.AppWithSQLite3AdapterTest do
""")
end)

assert_no_compilation_warnings(app_root_path)
assert_passes_formatter_check(app_root_path)
drop_test_database(app_root_path)
assert_tests_pass(app_root_path)
end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.html Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)
mix_run!(
~w(phx.gen.html Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
web_root_path
)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down Expand Up @@ -78,7 +88,17 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.json Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)
mix_run!(
~w(phx.gen.json Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
web_root_path
)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down Expand Up @@ -127,7 +147,17 @@ defmodule Phoenix.Integration.CodeGeneration.UmbrellaAppWithDefaultsTest do
{app_root_path, _} = generate_phoenix_app(tmp_dir, "rainy_day", ["--umbrella", "--live"])
web_root_path = Path.join(app_root_path, "apps/rainy_day_web")

mix_run!(~w(phx.gen.live Blog Post posts title:unique body:string status:enum:unpublished:published:deleted), web_root_path)
mix_run!(
~w(phx.gen.live Blog Post posts
title:unique
body:string
preface
author_name
author_email
other_very_important_attribute
status:enum:unpublished:published:deleted),
web_root_path
)

modify_file(Path.join(web_root_path, "lib/rainy_day_web/router.ex"), fn file ->
inject_before_final_end(file, """
Expand Down
Loading
Loading