Skip to content

Commit

Permalink
Handle edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Jul 15, 2022
1 parent 8bc6356 commit c23c93f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/ex_factor/remover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule ExFactor.Remover do
arity = Keyword.fetch!(opts, :arity)
source_path = Keyword.get(opts, :source_path, path(source_module))
dry_run = Keyword.get(opts, :dry_run, false)
guard_mismatch!(source_module, source_path)

{_ast, block_contents} = Parser.all_functions(source_path)
fns_to_remove = Enum.filter(block_contents, &(&1.name == source_function))
Expand All @@ -36,7 +37,22 @@ defmodule ExFactor.Remover do
|> List.insert_at(function.start_line - 1, comment(source_function, arity, function.defn))
end)
|> Enum.join("\n")
|> then(fn str -> write_file(source_path, str, source_module, dry_run) end)
|> then(fn str -> write_file(fns_to_remove, source_path, str, source_module, dry_run) end)
end

defp guard_mismatch!(module_string, source_path) when is_binary(module_string) do
source_path
|> File.read!()
|> String.match?(~r/#{module_string}/)
|> unless do
raise ArgumentError,
"Module name: #{module_string} not detected in source path: '#{source_path}'"
end
end

defp guard_mismatch!(source_module, source_path) do
module_string = Module.split(source_module) |> Enum.join(".")
guard_mismatch!(module_string, source_path)
end

defp comment(name, arity, "@spec") do
Expand All @@ -56,7 +72,7 @@ defmodule ExFactor.Remover do
"""
end

defp write_file(path, contents, source_module, true) do
defp write_file(_, path, contents, source_module, true) do
%ExFactor{
module: source_module,
path: path,
Expand All @@ -66,7 +82,17 @@ defmodule ExFactor.Remover do
}
end

defp write_file(path, contents, source_module, _) do
defp write_file([], path, contents, source_module, _) do
%ExFactor{
module: source_module,
path: path,
state: [:unchanged],
message: "function not matched",
file_contents: contents
}
end

defp write_file(_, path, contents, source_module, _) do
File.write(path, contents, [:write])

%ExFactor{
Expand Down
1 change: 1 addition & 0 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ defmodule ExFactor.ExtractorTest do
end

@tag :skip
# Need to implemnt this functionality
test "extract references to the function in the source module" do
content = """
defmodule ExFactorSampleModule do
Expand Down
76 changes: 76 additions & 0 deletions test/ex_factor/remover_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,85 @@ defmodule ExFactor.RemoverTest do
end

test "handles no functions found to remove, messages correctly" do
content = """
defmodule ExFactorSampleModule do
@somedoc "This is somedoc"
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
def pub1(arg1) do
:ok
end
end
"""

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

opts = [
source_module: ExFactorSampleModule,
source_path: "test/tmp/source_module.ex",
source_function: :pub2,
arity: 1
]

struct = Remover.remove(opts)

assert struct.state == [:unchanged]
assert struct.message == "function not matched"
end

test "handles no modules found to remove, messages correctly" do
content = """
defmodule ExFactorSampleModule do
@somedoc "This is somedoc"
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
def pub1(arg1) do
:ok
end
end
"""

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

opts = [
source_module: ExFactorSampleModule,
source_function: :pub1,
arity: 1
]

assert_raise File.Error,
"could not read file \"lib/ex_factor_sample_module.ex\": no such file or directory",
fn ->
Remover.remove(opts)
end
end

test "when the module name doesn't match the module in the path" do
content = """
defmodule ExFactorUnmatchedModule do
@somedoc "This is somedoc"
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
def pub1(arg1) do
:ok
end
end
"""

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

opts = [
source_module: ExFactorSampleModule,
source_path: "test/tmp/source_module.ex",
source_function: :pub1,
arity: 1
]

assert_raise ArgumentError,
"Module name: ExFactorSampleModule not detected in source path: 'test/tmp/source_module.ex'",
fn ->
Remover.remove(opts)
end
end
end
end

0 comments on commit c23c93f

Please sign in to comment.