Skip to content

Commit

Permalink
feature(CLI interface): add CLI for moduelonly
Browse files Browse the repository at this point in the history
Add tests for moduleonly CLI interface.
  • Loading branch information
ckoch-cars committed Aug 4, 2022
1 parent 9b2f5a1 commit e2ee4b8
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 9 deletions.
15 changes: 15 additions & 0 deletions lib/ex_factor.ex
Expand Up @@ -39,6 +39,21 @@ defmodule ExFactor do
format(%{additions: emplace, changes: changes, removals: removals}, dry_run, opts)
end

def refactor_module(opts) do
source_module = Keyword.fetch!(opts, :source_module)
target_module = Keyword.fetch!(opts, :target_module)
dry_run = Keyword.get(opts, :dry_run, false)

opts =
opts
|> Keyword.put_new(:target_path, path(target_module))
|> Keyword.put_new(:source_path, path(source_module))

changes = Changer.rename_module(opts)

format(%{additions: %ExFactor{}, changes: changes, removals: %ExFactor{}}, dry_run, opts)
end

def path(module) do
Path.join(["lib", Macro.underscore(module) <> ".ex"])
end
Expand Down
14 changes: 12 additions & 2 deletions lib/mix/tasks/ex_factor.ex
Expand Up @@ -56,6 +56,7 @@ defmodule Mix.Tasks.ExFactor do
function: :string,
key: :string,
module: :string,
moduleonly: :boolean,
source_path: :string,
target: :string,
target_path: :string
Expand All @@ -64,13 +65,22 @@ defmodule Mix.Tasks.ExFactor do

parsed_opts
|> options()
|> ExFactor.refactor()
|> choose_your_path()
|> cli_output(parsed_opts)
end

defp choose_your_path(opts) do
if Keyword.get(opts, :moduleonly, false) do
ExFactor.refactor_module(opts)
else
opts
|> Keyword.put(:source_function, Keyword.fetch!(opts, :function))
|> ExFactor.refactor()
end
end

defp options(opts) do
opts
|> Keyword.put(:source_function, Keyword.fetch!(opts, :function))
|> Keyword.put(:source_module, Keyword.fetch!(opts, :module))
|> Keyword.put(:target_module, Keyword.fetch!(opts, :target))
|> Keyword.put(:dry_run, Keyword.get(opts, :dryrun, false))
Expand Down
35 changes: 33 additions & 2 deletions test/ex_factor/cli_test.exs
Expand Up @@ -16,12 +16,13 @@ defmodule ExFactor.CLITest do
function: "pub1",
arity: 1
]

argv = OptionParser.to_argv(opts)

capture_io fn ->
capture_io(fn ->
{_, exit_status} = System.cmd("mix", ["ex_factor" | argv])
refute exit_status == 0
end
end)
end

test "with dry run" do
Expand Down Expand Up @@ -94,4 +95,34 @@ defmodule ExFactor.CLITest do
file = File.read!(target_path)
assert file =~ "\n@spec pub1(term()) :: term()\ndef pub1(arg1) do\n :pub1_ok\nend\nend"
end

test "with --moduleonly" do
File.mkdir_p("lib/ex_factor/tmp")

content = """
defmodule ExFactor.Module do
def pub1(arg1) do
ExFactorSampleModule.call_some_function(arg1)
end
end
"""

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

opts = [
target: "ExFactor.NewMod",
module: "ExFactorSampleModule",
moduleonly: true
]

argv = OptionParser.to_argv(opts)

{_cli_output, exit_status} = System.cmd("mix", ["ex_factor" | argv])
assert exit_status == 0
file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "alias ExFactor.NewMod"
assert file =~ "def pub1(arg1) do\n NewMod.call_some_function(arg1)"

File.rm_rf("lib/ex_factor/tmp")
end
end
47 changes: 42 additions & 5 deletions test/ex_factor/mix_task_test.exs
Expand Up @@ -17,6 +17,7 @@ defmodule ExFactor.MixTaskTest do
function: "pub1",
arity: 1
]

argv = OptionParser.to_argv(opts)

assert_raise KeyError, "key :target not found in: #{inspect(opts)}", fn ->
Expand Down Expand Up @@ -51,7 +52,7 @@ defmodule ExFactor.MixTaskTest do

argv = OptionParser.to_argv(opts)

capture_io fn -> Mix.Tasks.ExFactor.run(argv) end
capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

file = File.read!(target_path)

Expand Down Expand Up @@ -89,7 +90,7 @@ defmodule ExFactor.MixTaskTest do
]

argv = OptionParser.to_argv(opts)
capture_io fn -> Mix.Tasks.ExFactor.run(argv) end
capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

file = File.read!(target_path)
assert file =~ "def pub1(arg1)"
Expand Down Expand Up @@ -124,7 +125,7 @@ defmodule ExFactor.MixTaskTest do

argv = OptionParser.to_argv(opts)

output = capture_io fn -> Mix.Tasks.ExFactor.run(argv) end
output = capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

# no new file gets written
assert {:error, :enoent} = File.read(target_path)
Expand Down Expand Up @@ -177,13 +178,49 @@ defmodule ExFactor.MixTaskTest do

argv = OptionParser.to_argv(opts)

capture_io fn -> Mix.Tasks.ExFactor.run(argv) end
capture_io(fn -> Mix.Tasks.ExFactor.run(argv) end)

file = File.read!(target_path)
assert file =~ "def refactor1(arg1) do"
assert file =~ "def refactor1([]) do"
assert file =~ " @doc \"some docs\""
end

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

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

test "with --moduleonly" do
content = """
defmodule ExFactor.Module do
def pub1(arg1) do
ExFactorSampleModule.call_some_function(arg1)
end
end
"""

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

opts = [
target: "ExFactor.NewMod",
module: "ExFactorSampleModule",
moduleonly: true
]

argv = OptionParser.to_argv(opts)

capture_io(fn ->
Mix.Tasks.ExFactor.run(argv)
end)

file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "def pub1(arg1) do\n NewMod.call_some_function(arg1)\n end"
assert file =~ "alias ExFactor.NewMod"
end

end
end
end
57 changes: 57 additions & 0 deletions test/ex_factor_test.exs
Expand Up @@ -174,4 +174,61 @@ defmodule ExFactorTest do
assert file =~ "def refactor1([]) do"
end
end

describe "refactor_module/1" do
test "it refactors the refs to a module name only" do
File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")

content = """
defmodule ExFactor.Tmp.SourceModule do
@doc "this is some documentation for refactor1/1"
def refactor1([]) do
ExFactor.Tmp.TargetModule.pub_exists({})
end
def refactor1(arg1) do
ExFactor.Tmp.TargetModule.pub_exists(arg1)
end
end
"""

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

content = """
defmodule ExFactor.Tmp.TargetModule do
@doc "some docs"
def pub_exists(:error) do
:error
end
def pub_exists(arg_exists) do
arg_exists
end
end
"""

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

opts = [
target_module: "ExFactor.Tmp.NewModule",
source_module: "ExFactor.Tmp.TargetModule"
]

%{additions: additions, changes: [changes], removals: removals} =
ExFactor.refactor_module(opts)

assert additions == %ExFactor{}
assert removals == %ExFactor{}

assert %ExFactor{
module: ExFactor.Tmp.SourceModule,
path: "lib/ex_factor/tmp/source_module.ex",
state: [:alias_added, :changed, :changed]
} = changes

file = File.read!("lib/ex_factor/tmp/source_module.ex")
assert file =~ "alias ExFactor.Tmp.NewModule"
assert file =~ "NewModule.pub_exists({})"
assert file =~ "NewModule.pub_exists(arg1)"
end
end
end

0 comments on commit e2ee4b8

Please sign in to comment.