Skip to content

Commit

Permalink
Use a struct, update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 21, 2021
1 parent cb5eff2 commit 4d8cf30
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
32 changes: 29 additions & 3 deletions lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ defmodule ExFactor do
new module's name.
"""

_docp = "results struct"
defstruct [:module, :path, :message, :file_contents, :state]

alias ExFactor.Changer
alias ExFactor.Extractor
alias ExFactor.Formatter
alias ExFactor.Remover

@doc """
Expand All @@ -28,12 +32,34 @@ defmodule ExFactor do
emplace = Extractor.emplace(opts)
changes = Changer.change(opts)
# remove should be last (before format)
remove = Remover.remove(opts)
{emplace, remove, changes}
|> IO.inspect(label: "refactor all changes")
removals = Remover.remove(opts)

format(%{additions: emplace, changes: changes, removals: removals})
end

def path(module) do
Path.join(["lib", Macro.underscore(module) <> ".ex"])
end

defp format(%{path: nil} = struct), do: struct

defp format(%{additions: adds, changes: changes, removals: removals}) do
%{
additions: format(adds),
changes: format(changes),
removals: format(removals)
}
end

defp format(list) when is_list(list) do
Enum.map(list, fn elem ->
format(elem)
Map.get_and_update(elem, :state, fn val -> {val, [:formatted | val]} end)
end)
end

defp format(struct) do
Formatter.format([struct.path])
Map.get_and_update(struct, :state, fn val -> {val, [:formatted | val]} end)
end
end
8 changes: 4 additions & 4 deletions lib/ex_factor/changer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule ExFactor.Changer do
|> update_callers(opts)
end

defp update_callers([], _), do: []
defp update_callers([], _), do: %ExFactor{}

defp update_callers(callers, opts) do
Enum.map(callers, fn caller ->
Expand Down Expand Up @@ -94,7 +94,7 @@ defmodule ExFactor.Changer do
end

defp write_file({:unchanged, contents_list}, source_function, target_path, true) do
%{
%ExFactor{
path: target_path,
state: [:unchanged],
message: "#{source_function} not found, no changes to make",
Expand All @@ -103,7 +103,7 @@ defmodule ExFactor.Changer do
end

defp write_file({state, contents_list}, _, target_path, true) do
%{
%ExFactor{
path: target_path,
state: [:dry_run, state],
message: "--dry_run changes to make",
Expand All @@ -115,7 +115,7 @@ defmodule ExFactor.Changer do
contents = list_to_string(contents_list)
File.write(target_path, contents, [:write])

%{
%ExFactor{
path: target_path,
state: [state],
message: "changes made",
Expand Down
4 changes: 3 additions & 1 deletion lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule ExFactor.Extractor do
%{
module: module,
path: target_path,
state: [:dry_run],
message: "--dry_run changes to make",
file_contents: contents
}
Expand All @@ -81,7 +82,8 @@ defmodule ExFactor.Extractor do
%{
module: module,
path: target_path,
message: "changes to make",
state: [:additions_made],
message: "changes made",
file_contents: contents
}
end
Expand Down
12 changes: 10 additions & 2 deletions lib/ex_factor/remover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ defmodule ExFactor.Remover do
end

defp write_file(path, contents, source_module, true) do
%{
%ExFactor{
module: source_module,
path: path,
state: [:dry_run],
message: "--dry_run changes to make",
file_contents: contents
}
end

defp write_file(path, contents, _source_module, _) do
defp write_file(path, contents, source_module, _) do
File.write(path, contents, [:write])
%ExFactor{
module: source_module,
path: path,
state: [:removed],
message: "changes made",
file_contents: contents
}
end

defp path(module), do: ExFactor.path(module)
Expand Down
37 changes: 32 additions & 5 deletions test/ex_factor/changer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ defmodule ExFactor.ChangerTest do

File.write("lib/ex_factor/tmp/caller_module.ex", content)

content = """
defmodule ExFactor.Tmp.CallerTwoModule do
alias ExFactor.Tmp.SourceMod
def pub1(arg_a) do
SourceMod.refactor1(arg_a)
end
def pub2(), do: Other
end
"""

File.write("lib/ex_factor/tmp/caller_two_module.ex", content)

opts = [
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceMod",
source_function: :refactor1,
arity: 1
]

Changer.change(opts)
changes = Changer.change(opts)

caller = File.read!("lib/ex_factor/tmp/caller_module.ex")
assert caller =~ "alias ExFactor.Tmp.TargetModule"
Expand All @@ -57,10 +69,17 @@ defmodule ExFactor.ChangerTest do
assert caller =~ "TargetModule.refactor1(arg_a)"
# asser the function uses the alias
refute caller =~ "ExFactor.Tmp.TargetModule.refactor1(arg_a)"
end

# update the annoying alias style: alias Foo.{Bar, Baz, Biz}
# find and update when the module is used but not aliased
caller_two = File.read!("lib/ex_factor/tmp/caller_two_module.ex")
assert caller_two =~ "alias ExFactor.Tmp.TargetModule"
# ensure we don't match dumbly
assert caller_two =~ "TargetModule.refactor1(arg_a)"
# asser the function uses the alias
refute caller_two =~ "ExFactor.Tmp.TargetModule.refactor1(arg_a)"

assert Enum.find(changes, &(&1.path == "lib/ex_factor/tmp/caller_module.ex"))
assert Enum.find(changes, &(&1.path == "lib/ex_factor/tmp/caller_two_module.ex"))
end

test "only add alias entry if it's missing" do
content = """
Expand Down Expand Up @@ -204,6 +223,15 @@ defmodule ExFactor.ChangerTest do
test "changes multiple functions" do
end

test "handles no functions found to change, messages correctly" do
end

test "handles no modules found to change, messages correctly" do
end

# update the annoying alias style: alias Foo.{Bar, Baz, Biz}
# find and update when the module is used but not aliased

test "takes a dry_run argument and doesn't update the files" do
content = """
defmodule ExFactor.Tmp.SourceMod do
Expand Down Expand Up @@ -235,7 +263,6 @@ defmodule ExFactor.ChangerTest do
]

[change_map] = Changer.change(opts)
# |> IO.inspect(label: "")

caller = File.read!("lib/ex_factor/tmp/caller_module.ex")

Expand Down
10 changes: 7 additions & 3 deletions test/ex_factor/remover_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ defmodule ExFactor.RemoverTest do
arity: 1
]

changes =
Remover.remove(opts)
|> IO.inspect(label: "")
changes = Remover.remove(opts)

unchanged_file = File.read!("test/tmp/source_module.ex")
assert unchanged_file =~ "def pub1(arg1) do"
Expand All @@ -171,5 +169,11 @@ defmodule ExFactor.RemoverTest do
assert changes.module == ExFactorSampleModule
assert changes.message == "--dry_run changes to make"
end

test "handles no functions found to remove, messages correctly" do
end

test "handles no modules found to remove, messages correctly" do
end
end
end
9 changes: 2 additions & 7 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ExFactorTest do
arity: 1
]

{_, _, _} = _results = ExFactor.refactor(opts)
%{additions: _, changes: _, removals: _} = _results = ExFactor.refactor(opts)

file = File.read!("lib/ex_factor/tmp/target_module.ex")
assert file =~ "def(refactor1(arg1)) do"
Expand All @@ -58,10 +58,8 @@ defmodule ExFactorTest do
assert file =~ "def pub_exists(arg_exists) do"

file = File.read!("lib/ex_factor/tmp/source_module.ex")
# |> IO.inspect(label: "")
refute file =~ "def refactor1(arg1) do"
refute file =~ "def refactor1([]) do"
# assert results == {:ok, :ok}
end

test "it returns the results of the dry_run changes" do
Expand Down Expand Up @@ -106,7 +104,7 @@ defmodule ExFactorTest do
dry_run: true
]

{_extract_results, _remove_results, _change_results} = ExFactor.refactor(opts)
%{additions: _, changes: _, removals: _} = ExFactor.refactor(opts)

# assert that the original files are unchanged
file = File.read!("lib/ex_factor/tmp/target_module.ex")
Expand All @@ -116,9 +114,6 @@ defmodule ExFactorTest do
file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "def refactor1(arg1) do"
assert file =~ "def refactor1([]) do"

# extract_contents |> IO.inspect(label: "")
# remove_contents |> IO.inspect(label: "")
end
end
end

0 comments on commit 4d8cf30

Please sign in to comment.