Skip to content

Commit

Permalink
improvement: more module helpers
Browse files Browse the repository at this point in the history
test: add regression test
improvement: wrap code in `==code==` so you can tell what is being `puts`
  • Loading branch information
zachdaniel committed Jun 2, 2024
1 parent 99ac8f0 commit 57288d3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/common.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ defmodule Igniter.Common do
|> Zipper.subtree()
|> Zipper.root()
|> Sourceror.to_string()
|> then(&"==code==\n#{&1}\n==code==\n")
|> IO.puts()

zipper
Expand Down Expand Up @@ -539,8 +540,10 @@ defmodule Igniter.Common do
|> Zipper.subtree()
|> Zipper.root()
|> case do
{:__block__, _, [_]} ->
Zipper.down(zipper)
{:__block__, _, _} ->
zipper
|> Zipper.down()
|> maybe_move_to_block()

_ ->
zipper
Expand Down
19 changes: 19 additions & 0 deletions lib/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ defmodule Igniter.Module do
Module.concat(module_name_prefix(), suffix)
end

def proper_location(module_name) do
path =
module_name
|> Module.split()
|> Enum.map(&to_string/1)
|> Enum.map(&Macro.underscore/1)

last = List.last(path)
leading = :lists.droplast(path)

Path.join(["lib" | leading] ++ ["#{last}.ex"])
end

def parse(module_name) do
module_name
|> String.split(".")
|> Module.concat()
end

def module_name_prefix do
Mix.Project.get!()
|> Module.split()
Expand Down
25 changes: 25 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ defmodule Igniter.ConfigTest do
"""
end

@tag :regression
test "it merges the spark formatter plugins" do
%{rewrite: rewrite} =
Igniter.new()
|> Igniter.Config.configure(
"fake.exs",
:spark,
[:formatter, :"Ash.Resource"],
[],
fn x ->
x
end
)
|> Igniter.Config.configure("fake.exs", :spark, [:formatter, :"Ash.Domain"], [], fn x ->
x
end)

config_file = Rewrite.source!(rewrite, "config/fake.exs")

assert Source.get(config_file, :content) == """
import Config
config :spark, formatter: ["Ash.Domain": [], "Ash.Resource": []]
"""
end

test "it merges with 2 arg version of existing config with a single path item" do
%{rewrite: rewrite} =
Igniter.new()
Expand Down

0 comments on commit 57288d3

Please sign in to comment.