diff --git a/lib/igniter.ex b/lib/igniter.ex index fe1c997..da14e87 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -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}) diff --git a/lib/igniter/project/deps.ex b/lib/igniter/project/deps.ex index 4d48273..9dc5279 100644 --- a/lib/igniter/project/deps.ex +++ b/lib/igniter/project/deps.ex @@ -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)}` diff --git a/lib/igniter/util/io.ex b/lib/igniter/util/io.ex new file mode 100644 index 0000000..4b60520 --- /dev/null +++ b/lib/igniter/util/io.ex @@ -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 diff --git a/test/igniter/extensions/phoenix_test.exs b/test/igniter/extensions/phoenix_test.exs index 8af7ef9..17d2e96 100644 --- a/test/igniter/extensions/phoenix_test.exs +++ b/test/igniter/extensions/phoenix_test.exs @@ -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