From 193a864478bd880d86a5399d421a5640b35f2e5e Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Fri, 21 Feb 2025 10:38:02 -0500 Subject: [PATCH] fix: find app_name when kw list is defined at the end Close #235 --- lib/igniter/project/application.ex | 6 ++- test/igniter/project/application_test.exs | 50 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/igniter/project/application.ex b/lib/igniter/project/application.ex index 26d65b33..c6503240 100644 --- a/lib/igniter/project/application.ex +++ b/lib/igniter/project/application.ex @@ -26,8 +26,10 @@ defmodule Igniter.Project.Application do |> Sourceror.Zipper.zip() with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :project, 0), - zipper <- Igniter.Code.Common.rightmost(zipper), - true <- Igniter.Code.List.list?(zipper), + {:ok, zipper} <- + Igniter.Code.Common.move_to(zipper, &match?({:__block__, _, [:app]}, &1.node)), + zipper = Zipper.up(zipper), + zipper = Zipper.up(zipper), {:ok, zipper} <- Igniter.Code.Keyword.get_key(zipper, :app), {:ok, app_name} when is_atom(app_name) <- expand_key_value(zipper) do app_name diff --git a/test/igniter/project/application_test.exs b/test/igniter/project/application_test.exs index 493ea7ea..9e875dad 100644 --- a/test/igniter/project/application_test.exs +++ b/test/igniter/project/application_test.exs @@ -231,6 +231,56 @@ defmodule Igniter.Project.ApplicationTest do assert Igniter.Project.Application.app_name(igniter) == :igniter_test end + test "it returns the application name when keyword list is defined at the end" do + igniter = + test_project( + files: %{ + "mix.exs" => """ + defmodule IgniterTest.MixProject do + use Mix.Project + + def project do + releases = [ + test: [] + ] + + [ + app: :igniter_test, + releases: releases + ] + end + end + """ + } + ) + + assert Igniter.Project.Application.app_name(igniter) == :igniter_test + end + + test "it returns the application name when keyword list is assigned to a variable" do + igniter = + test_project( + files: %{ + "mix.exs" => """ + defmodule IgniterTest.MixProject do + use Mix.Project + + def project do + project = [ + app: :igniter_test, + releases: releases + ] + + project + end + end + """ + } + ) + + assert Igniter.Project.Application.app_name(igniter) == :igniter_test + end + test "it raises if the application name can't be resolved" do igniter = test_project(