Skip to content

Commit

Permalink
Cleanup test setup to remove dangling modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Jul 15, 2022
1 parent 8c7e712 commit 8bc6356
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 42 deletions.
19 changes: 12 additions & 7 deletions lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,21 @@ defmodule ExFactor.Extractor do
_ ->
target_mod = Module.concat([target_module])

module_contents = quote generated: true do
defmodule unquote(target_mod) do
@moduledoc "This module created with ExFactor"
module_contents =
quote generated: true do
defmodule unquote(target_mod) do
@moduledoc "This module created with ExFactor"
end
end
end
|> Macro.to_string()
|> Macro.to_string()

list = String.split(module_contents, "\n")
{:ok, ast} = Code.string_to_quoted(module_contents, token_metadata: true)
{:defmodule, [do: [line: _begin_line], end: [line: end_line], closing: _, line: _], _} = ast
insert_code(list, end_line, string_fns, target_path, target_module, dry_run)

{:defmodule, [do: [line: _begin_line], end: [line: end_line], closing: _, line: _], _} =
ast

insert_code(list, end_line, string_fns, target_path, target_module, dry_run)
end
end

Expand Down
8 changes: 3 additions & 5 deletions test/ex_factor/changer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ defmodule ExFactor.ChangerTest do
use ExUnit.Case
alias ExFactor.Changer

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

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

describe "change/1" do
Expand Down
68 changes: 40 additions & 28 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ defmodule ExFactor.ExtractorTest do

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

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

setup do
File.rm_rf("lib/ex_factor/tmp")
File.mkdir_p("lib/ex_factor/tmp")
:ok
end

describe "emplace/1" do
test "requires some options" do
opts = [
Expand All @@ -36,7 +40,7 @@ defmodule ExFactor.ExtractorTest do
_docp = "here's an arbitrary module underscore"
@spec pub1(term()) :: term()
def pub1(arg1) do
:ok
:pub1_ok
end
end
"""
Expand Down Expand Up @@ -71,6 +75,37 @@ defmodule ExFactor.ExtractorTest do
File.rm("test/tmp/target_module.ex")
end

test "write a new file add a moduledoc comment" do
content = """
defmodule ExFactorSampleModule do
def pub1(arg1) do
:pub1_ok
end
end
"""

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

target_path = "test/tmp/target_module.ex"
File.rm(target_path)

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

Extractor.emplace(opts)

file = File.read!(target_path)
assert file =~ "def(pub1(arg1))"
assert file =~ "@moduledoc(\"This module created with ExFactor\")"
end

test " with dry_run option, don't write the file." do
content = """
defmodule ExFactorSampleModule do
Expand Down Expand Up @@ -107,9 +142,6 @@ defmodule ExFactor.ExtractorTest do
end

test "write a new file with the function, infer some defaults" 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
@somedoc "This is somedoc"
Expand All @@ -133,15 +165,9 @@ defmodule ExFactor.ExtractorTest do
file = File.read!("lib/ex_factor/tmp/target_module.ex")
assert file =~ "def(pub1(arg1))"
assert file =~ "defmodule(ExFactor.Tmp.TargetModule) do"

File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")
end

test "write the function into an existing module" 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
@somedoc "This is somedoc"
Expand Down Expand Up @@ -178,15 +204,9 @@ defmodule ExFactor.ExtractorTest do
assert file =~ "def(refactor1(arg1)) do"
assert file =~ "def pub_exists(arg_exists) do"
assert file =~ "defmodule ExFactor.Tmp.TargetModule do"

File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")
end

test "write multiple functions, into an existing module" 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
@somedoc "This is somedoc"
Expand Down Expand Up @@ -227,15 +247,9 @@ defmodule ExFactor.ExtractorTest do
assert file =~ "def pub_exists(arg_exists) do"
assert file =~ "def pub_exists(:error) do"
assert file =~ "defmodule ExFactor.Tmp.TargetModule do"

File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")
end

test "write multiple functions and their docs, into an existing module" 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
@somedoc "This is somedoc"
Expand Down Expand Up @@ -280,11 +294,9 @@ defmodule ExFactor.ExtractorTest do
assert file =~ "def(refactor1(arg1)) do"
assert file =~ "def(refactor1([])) do"
assert file =~ " @doc \"some docs\""

File.rm("lib/ex_factor/tmp/source_module.ex")
File.rm("lib/ex_factor/tmp/target_module.ex")
end

@tag :skip
test "extract references to the function in the source module" do
content = """
defmodule ExFactorSampleModule do
Expand Down Expand Up @@ -316,7 +328,7 @@ defmodule ExFactor.ExtractorTest do
opts = [
target_module: "ExFactor.Tmp.TargetModule",
source_module: "ExFactor.Tmp.SourceModule",
source_function: :refactor1,
source_function: :pub1,
arity: 1
]

Expand Down
18 changes: 18 additions & 0 deletions test/ex_factor/neighbors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ defmodule ExFactor.NeighborsTest do
] = Neighbors.walk(block, :pub1)
end

test "it should handle the first function without anything else" do
module =
"""
defmodule ExFactorSampleModule do
def pub1(arg1) do
:ok
end
end
"""
|> Code.string_to_quoted()

{_ast, block} = Parser.block_contents(module)

assert [
{:def, _, [{:pub1, _, [{:arg1, _, nil}]}, _]}
] = Neighbors.walk(block, :pub1)
end

test "it finds the target when the last function and function is a string" do
module =
"""
Expand Down
15 changes: 15 additions & 0 deletions test/ex_factor/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,21 @@ defmodule ExFactor.ParserTest do
{:defp, _, [{:priv1, _, [{:arg1, _, nil}]}, [do: :ok]]}
] = block
end

test "it parses a dead-simple module" do
content = """
defmodule ExFactorSampleModule do
def pub1(arg1) do
:ok
end
end
"""

File.write("test/tmp/test_module.ex", content)
{_ast, [f1]} = Parser.block_contents("test/tmp/test_module.ex")

assert {:def, _, [{:pub1, _, [{:arg1, _, nil}]}, _]} = f1
end
end

describe "read_file/1" do
Expand Down
2 changes: 0 additions & 2 deletions test/ex_factor/remover_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ defmodule ExFactor.RemoverTest do
assert file =~ "defmodule ExFactorSampleModule do"
assert file =~ "_docp = \"here's an arbitrary module underscore"
refute file =~ "def pub1(arg1) do"
File.rm("test/support/source_module.ex")
end

test "remove the function leave a comment in place" do
Expand Down Expand Up @@ -71,7 +70,6 @@ defmodule ExFactor.RemoverTest do
assert file =~ "_docp = \"here's an arbitrary module underscore"
assert file =~ "# Function: pub1/1 removed by ExFactor"
refute file =~ "def pub1(arg1) do"
File.rm("test/support/source_module.ex")
end

test "it rewrites the source file and removes code blocks when function is a string" do
Expand Down

0 comments on commit 8bc6356

Please sign in to comment.