Skip to content

Commit 36de226

Browse files
committed
improvement: make router optional in select_endpoint
improvement: accept functions in warning/notice/issue assertions improvement: add `Igniter.Code.Common.variable?`
1 parent 845c1be commit 36de226

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

lib/igniter/code/common.ex

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ defmodule Igniter.Code.Common do
2424
end
2525
end
2626

27+
@doc "Returns true if the node represents a variable with the given name"
28+
@spec variable?(zipper :: Zipper.t(), name :: atom) :: boolean()
29+
def variable?(%{node: {name, _, ctx}}, name) when is_atom(ctx) do
30+
true
31+
end
32+
33+
def variable?(_, _), do: false
34+
2735
@doc "Returns true if the node represents a variable assignment"
2836
@spec variable_assignment?(zipper :: Zipper.t(), name :: atom) :: boolean()
2937
def variable_assignment?(%{node: {:=, _, [{name, _, ctx}, _]}}, name) when is_atom(ctx) do

lib/igniter/libs/phoenix.ex

+27-9
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,35 @@ defmodule Igniter.Libs.Phoenix do
114114
115115
If multiple endpoints are found, the user is prompted to select one of them.
116116
"""
117-
@spec select_endpoint(Igniter.t(), module(), String.t()) :: {Igniter.t(), module() | nil}
118-
def select_endpoint(igniter, router, label \\ "Which endpoint should be used") do
119-
case endpoints_for_router(igniter, router) do
120-
{igniter, []} ->
121-
{igniter, nil}
117+
@spec select_endpoint(Igniter.t(), module() | nil, String.t()) :: {Igniter.t(), module() | nil}
118+
def select_endpoint(igniter, router \\ nil, label \\ "Which endpoint should be used") do
119+
if router do
120+
case endpoints_for_router(igniter, router) do
121+
{igniter, []} ->
122+
{igniter, nil}
123+
124+
{igniter, [endpoint]} ->
125+
{igniter, endpoint}
126+
127+
{igniter, endpoints} ->
128+
{igniter, Igniter.Util.IO.select(label, endpoints, display: &inspect/1)}
129+
end
130+
else
131+
{igniter, endpoints} =
132+
Igniter.Project.Module.find_all_matching_modules(igniter, fn _mod, zipper ->
133+
Igniter.Code.Module.move_to_use(zipper, Phoenix.Endpoint) != :error
134+
end)
122135

123-
{igniter, [endpoint]} ->
124-
{igniter, endpoint}
136+
case endpoints do
137+
[] ->
138+
{igniter, nil}
125139

126-
{igniter, endpoints} ->
127-
{igniter, Igniter.Util.IO.select(label, endpoints, display: &inspect/1)}
140+
[endpoint] ->
141+
{igniter, endpoint}
142+
143+
endpoints ->
144+
{igniter, Igniter.Util.IO.select(label, endpoints, display: &inspect/1)}
145+
end
128146
end
129147
end
130148

lib/igniter/test.ex

+33-9
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ defmodule Igniter.Test do
208208
flunk("""
209209
Expected to find `mix #{task} #{Enum.join(argv, " ")}` in igniter tasks.
210210
211-
Found tasks:
211+
Found tasks:
212212
213213
#{Enum.map_join(igniter.tasks, "\n", fn {task, argv} -> "- mix #{task} #{Enum.join(argv)}" end)}
214214
""")
@@ -221,7 +221,13 @@ defmodule Igniter.Test do
221221

222222
defmacro assert_has_notice(igniter, notice) do
223223
quote bind_quoted: [igniter: igniter, notice: notice] do
224-
if notice not in igniter.notices do
224+
if !Enum.any?(igniter.notices, fn found_notice ->
225+
if is_binary(notice) do
226+
notice == found_notice
227+
else
228+
notice.(found_notice)
229+
end
230+
end) do
225231
if Enum.empty?(igniter.notices) do
226232
flunk("""
227233
Expected to find the following notice:
@@ -236,7 +242,7 @@ defmodule Igniter.Test do
236242
237243
#{notice}
238244
239-
Found notices:
245+
Found notices:
240246
241247
#{Enum.join(igniter.notices, "\n\b")}
242248
""")
@@ -249,7 +255,13 @@ defmodule Igniter.Test do
249255

250256
defmacro assert_has_warning(igniter, warning) do
251257
quote bind_quoted: [igniter: igniter, warning: warning] do
252-
if warning not in igniter.warnings do
258+
if !Enum.any?(igniter.warnings, fn found_warning ->
259+
if is_binary(warning) do
260+
warning == found_warning
261+
else
262+
warning.(found_warning)
263+
end
264+
end) do
253265
if Enum.empty?(igniter.warnings) do
254266
flunk("""
255267
Expected to find the following warning:
@@ -264,7 +276,7 @@ defmodule Igniter.Test do
264276
265277
#{warning}
266278
267-
Found warnings:
279+
Found warnings:
268280
269281
#{Enum.join(igniter.warnings, "\n\b")}
270282
""")
@@ -287,7 +299,13 @@ defmodule Igniter.Test do
287299
[]
288300
end
289301

290-
if issue not in issues do
302+
if !Enum.any?(igniter.issues, fn found_issue ->
303+
if is_binary(issue) do
304+
issue == found_issue
305+
else
306+
issue.(found_issue)
307+
end
308+
end) do
291309
if Enum.empty?(issues) do
292310
flunk("""
293311
Expected to find the following issue at path: #{inspect(path)}}
@@ -302,14 +320,20 @@ defmodule Igniter.Test do
302320
303321
#{issue}
304322
305-
Found issue:
323+
Found issue:
306324
307325
#{Enum.join(issues, "\n\b")}
308326
""")
309327
end
310328
end
311329
else
312-
if issue not in igniter.issues do
330+
if !Enum.any?(igniter.issues, fn found_issue ->
331+
if is_binary(issue) do
332+
issue == found_issue
333+
else
334+
issue.(found_issue)
335+
end
336+
end) do
313337
if Enum.empty?(igniter.issues) do
314338
flunk("""
315339
Expected to find the following issue:
@@ -324,7 +348,7 @@ defmodule Igniter.Test do
324348
325349
#{issue}
326350
327-
Found issues:
351+
Found issues:
328352
329353
#{Enum.join(igniter.issues, "\n\b")}
330354
""")

0 commit comments

Comments
 (0)