Skip to content

Commit

Permalink
chore(Cleanup): fill in test
Browse files Browse the repository at this point in the history
add docs
refactor extractor output fn.
  • Loading branch information
ckoch-cars committed Aug 4, 2022
1 parent 407f9aa commit db25c9f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Use at your peril, _for now._
## Roadmap TODO

- [] Add test for one file containing more than one `defmodule`
- [] Add test for nested defmodules.
- [] How does this work with macro code? Does that even make sense as a case to handle?
- [] Update .exs files too?
- [] update test file refs by CLI option
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.3.1
34 changes: 18 additions & 16 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,22 @@ defmodule ExFactor.Extractor do

defp refactor_message, do: "#refactored function moved with ExFactor"

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

defp write_file(target_path, contents, module, _dry_run) do
defp write_file(target_path, contents, target_module, _dry_run) do
target_path
|> Path.dirname()
|> File.mkdir_p!()

File.write!(target_path, contents, [:write])

%{
module: module,
path: target_path,
state: [:additions_made],
message: "changes made",
file_contents: contents
}
output(target_path, contents, target_module, [:additions_made], "changes made")
end

defp insert_code(_list, _end_line, "", target_path, target_module, _dry_run) do
output(target_path, "", target_module, [:unchanged], "function not detected in source.")
end

defp insert_code(list, end_line, string_fns, target_path, target_module, dry_run) do
Expand All @@ -107,4 +99,14 @@ defmodule ExFactor.Extractor do
|> Enum.join("\n")
|> then(fn contents -> write_file(target_path, contents, target_module, dry_run) end)
end

defp output(path, contents, module, state, message) do
%{
module: module,
path: path,
state: state,
message: message,
file_contents: contents
}
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule ExFactor.MixProject do

defp package() do
[
description: description(),
licenses: ["CC-BY-NC-ND-4.0"],
links: %{"GitHub" => @source_url}
]
Expand Down
74 changes: 31 additions & 43 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ defmodule ExFactor.ExtractorTest do
end

test "noop when no matching fns found in source" do
content = """
defmodule ExFactorSampleModule do
@spec pub1(term()) :: term()
def pub1(arg1) do
:pub1_ok
end
end
"""

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

opts = [
target_module: "ExFactor.Test.NewModule",
source_module: "ExFactorSampleModule",
source_path: "test/tmp/source_module.ex",
target_path: "test/tmp/target_module.ex",
source_function: :pub2,
arity: 1
]

changes = Extractor.emplace(opts)

assert changes.state == [:unchanged]
assert changes.message == "function not detected in source."
assert changes.file_contents == ""
# file =
assert_raise File.Error,
"could not read file \"test/tmp/target_module.ex\": no such file or directory",
fn ->
File.read!("test/tmp/target_module.ex")
end
end

test "create the dir path if necessary" do
Expand Down Expand Up @@ -325,48 +356,5 @@ defmodule ExFactor.ExtractorTest do
assert file =~ "def refactor1([]) do"
assert file =~ " @doc \"some docs\""
end

@tag :skip
# Need to implemnt this functionality
test "extract references to the function in the source module" do
content = """
defmodule ExFactorSampleModule do
def pub1(arg1) do
arg1
end
def pub2(arg2) do
pub1(arg2)
end
end
"""

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

content = """
defmodule ExFactor.Tmp.TargetModule do
def pub_exists(arg_exists) do
:ok
end
def pub_exists(:error) do
:error
end
end
"""

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

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

Extractor.emplace(opts)

file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "def pub2(arg2) do\n TargetModule.pub1(arg2)"
end
end
end

0 comments on commit db25c9f

Please sign in to comment.