Skip to content

Commit

Permalink
Change tests, so cleanup is better.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 18, 2021
1 parent e00edef commit 59b486b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ to a new file/module. At this time, ExFactor cannot change the function arity.
- [] update test file refs by CLI option
- [] format changes
- [] CLI output, list files changed and created.
- [] dry-run option
- [X] dry-run option
- [] ElixirLS integration for VSCode?
- [] Write the module code to rename usages of the refactored function
- [] guthub actions, run test suite


## Installation
Expand Down
7 changes: 6 additions & 1 deletion lib/ex_factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ defmodule ExFactor do
"""

alias ExFactor.Extractor
alias ExFactor.Remover

@doc """
Call Extractor module emaplce/1
"""
def refactor(opts), do: Extractor.emplace(opts)
def refactor(opts) do
emplace = Extractor.emplace(opts)
remove = Remover.remove(opts)
{emplace, remove}
end
end
2 changes: 1 addition & 1 deletion lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule ExFactor.Extractor do
to_extract = block_contents
|> Neighbors.walk(source_function, arity)
|> Enum.map(&(Macro.to_string(&1)))
|> IO.inspect(label: "to string")
# |> IO.inspect(label: "to string")

string_fns = Enum.join(to_extract, "\n")

Expand Down
7 changes: 6 additions & 1 deletion lib/ex_factor/remover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ defmodule ExFactor.Remover do
"""
def remove(opts) do
source_function = Keyword.fetch!(opts, :source_function)
source_module = Keyword.get(opts, :source_module)
arity = Keyword.fetch!(opts, :arity)
source_path = Keyword.fetch!(opts, :source_path)
source_path = Keyword.get(opts, :source_path, path(source_module))
dry_run = Keyword.get(opts, :dry_run, false)
# |> IO.inspect(label: "REMOVE source_path")

Expand Down Expand Up @@ -60,4 +61,8 @@ defmodule ExFactor.Remover do
defp write_file(path, contents, _) do
File.write(path, contents, [:write])
end

defp path(module) do
Path.join(["lib", Macro.underscore(module) <> ".ex"])
end
end
4 changes: 2 additions & 2 deletions lib/mix/tasks/ex_factor/refactor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ defmodule Mix.Tasks.ExFactor.Refactor do
]
)

parsed_opts
|> IO.inspect(label: "PARSED ARGS")
# parsed_opts
# |> IO.inspect(label: "PARSED ARGS")

# Mix.Task.run("app.start")
parsed_opts
Expand Down
2 changes: 1 addition & 1 deletion test/ex_factor/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ defmodule ExFactor.ParserTest do

assert List.first(list) == "defmodule ExFactorSampleModule do"
assert Enum.at(list, -2) == "end"
assert {:defmodule, [do: [line: 1], end: [line: 8], line: 1], _} = ast
assert {:defmodule, [do: [line: 1], end: [line: 9], line: 1], _} = ast
end

test "raises an exception for an invalid file path" do
Expand Down
35 changes: 21 additions & 14 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
defmodule ExFactorTest do
use ExUnit.Case

setup_all do
File.mkdir_p("lib/ex_factor/tmp")

on_exit(fn ->
File.rm_rf("lib/ex_factor/tmp")
end)
end

describe "refactor/1" do
test "it refactors the functions into a new module, specified in opts" do
File.rm("lib/ex_factor/source_module.ex")
File.rm("lib/ex_factor/target_module.ex")
File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")

content = """
defmodule ExFactor.SourceModule do
defmodule ExFactor.Tmp.SourceModule do
@somedoc "This is somedoc"
@doc "this is some documentation for refactor1/1"
def refactor1(arg1) do
Expand All @@ -19,10 +27,10 @@ defmodule ExFactorTest do
end
"""

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

content = """
defmodule ExFactor.TargetModule do
defmodule ExFactor.Tmp.TargetModule do
@somedoc "This is somedoc TargetModule"
@doc "some docs"
def pub_exists(arg_exists) do
Expand All @@ -34,30 +42,29 @@ defmodule ExFactorTest do
end
"""

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

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

ExFactor.refactor(opts)
results = ExFactor.refactor(opts)

file = File.read!("lib/ex_factor/target_module.ex")
file = File.read!("lib/ex_factor/tmp/target_module.ex")
assert file =~ "def(refactor1(arg1)) do"
assert file =~ "def(refactor1([])) do"
assert file =~ " @doc \"some docs\""
assert file =~ "def pub_exists(arg_exists) do"

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

# File.rm("lib/ex_factor/source_module.ex")
# File.rm("lib/ex_factor/target_module.ex")
results |> IO.inspect(label: "ExFactorTest results")
end
end
end

0 comments on commit 59b486b

Please sign in to comment.