diff --git a/lib/ex_factor/remover.ex b/lib/ex_factor/remover.ex index ffeafe8..0d95edd 100644 --- a/lib/ex_factor/remover.ex +++ b/lib/ex_factor/remover.ex @@ -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)) @@ -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 @@ -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, @@ -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{ diff --git a/test/ex_factor/extractor_test.exs b/test/ex_factor/extractor_test.exs index 77b64bd..9a75bea 100644 --- a/test/ex_factor/extractor_test.exs +++ b/test/ex_factor/extractor_test.exs @@ -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 diff --git a/test/ex_factor/remover_test.exs b/test/ex_factor/remover_test.exs index 5ab55fe..6514984 100644 --- a/test/ex_factor/remover_test.exs +++ b/test/ex_factor/remover_test.exs @@ -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