improvement: better confirmation message experience
Some checks are pending
CI / ash-ci (push) Waiting to run

chore: update tests for phoenix extensions
This commit is contained in:
Zach Daniel 2024-09-16 09:08:56 -04:00
parent 896a63865a
commit 86c93968b1
4 changed files with 25 additions and 7 deletions

View file

@ -651,7 +651,7 @@ defmodule Igniter do
"These dependencies #{IO.ANSI.yellow()}should#{IO.ANSI.reset()} be installed before continuing. Modify mix.exs and install?"
end
if opts[:yes] || Mix.shell().yes?(message) do
if opts[:yes] || Igniter.Util.IO.yes?(message) do
rewrite =
case Rewrite.write(rewrite, "mix.exs", :force) do
{:ok, rewrite} -> rewrite
@ -685,7 +685,7 @@ defmodule Igniter do
"These dependencies #{IO.ANSI.yellow()}should#{IO.ANSI.reset()} be installed before continuing. Modify mix.exs and install?"
end
if Mix.shell().yes?(message) do
if Igniter.Util.IO.yes?(message) do
rewrite =
case Rewrite.write(igniter.rewrite, "mix.exs", :force) do
{:ok, rewrite} -> rewrite
@ -793,7 +793,7 @@ defmodule Igniter do
result_of_dry_run
else
if opts[:yes] ||
Mix.shell().yes?(opts[:confirmation_message] || "Proceed with changes?") do
Igniter.Util.IO.yes?(opts[:confirmation_message] || "Proceed with changes?") do
igniter.rewrite
|> Enum.any?(fn source ->
Rewrite.Source.from?(source, :string) || Rewrite.Source.updated?(source)
@ -901,7 +901,7 @@ defmodule Igniter do
Igniter.assign(igniter, :prompt_on_git_changes?, false)
_ ->
if Mix.shell().yes?("Uncommitted changes detected in the project. Continue?") do
if Igniter.Util.IO.yes?("Uncommitted changes detected in the project. Continue?") do
Igniter.assign(igniter, :prompt_on_git_changes?, false)
else
exit({:shutdown, 1})

View file

@ -50,7 +50,7 @@ defmodule Igniter.Project.Deps do
igniter
else
if opts[:yes?] ||
Mix.shell().yes?("""
Igniter.Util.IO.yes?("""
Dependency #{name} is already in mix.exs. Should we replace it?
Desired: `#{inspect(desired)}`

18
lib/igniter/util/io.ex Normal file
View file

@ -0,0 +1,18 @@
defmodule Igniter.Util.IO do
@moduledoc "Helpers for working with input/output"
@doc "Prompts the user for yes or no, repeating the prompt until a satisfactory answer is given"
def yes?(prompt) do
case String.trim(Mix.shell().prompt(prompt <> " [Yn]")) do
yes when yes in ["y", "Y", "yes", "YES"] ->
true
no when no in ["n", "N", "no", "NO"] ->
false
value ->
Mix.shell().info("Please enter one of [Yn]. Got: #{value}")
yes?(prompt)
end
end
end

View file

@ -45,7 +45,7 @@ defmodule Igniter.Extensions.PhoenixTest do
Igniter.Extensions.Phoenix.proper_location(igniter, TestWeb.FooHTML, [])
end
test "when not belonging to a controller, we instruct to keep its current location" do
test "when not belonging to a controller, we say we don't know where it goes" do
igniter =
test_project()
|> Igniter.create_new_file("lib/test_web/controllers/foo_html.ex", """
@ -54,7 +54,7 @@ defmodule Igniter.Extensions.PhoenixTest do
end
""")
assert :keep =
assert :error =
Igniter.Extensions.Phoenix.proper_location(igniter, TestWeb.FooHTML, [])
end