Skip to content

Commit

Permalink
fix(ExFactor.Extractor): create path! for new modules
Browse files Browse the repository at this point in the history
Ensure we attempt create a new path as needed and raise exceptions on File failures.
  • Loading branch information
ckoch-cars committed Jul 20, 2022
1 parent 50f20fb commit 04d16d9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/ex_factor/extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ defmodule ExFactor.Extractor do
end

defp write_file(target_path, contents, module, _dry_run) do
_ = File.write(target_path, contents, [:write])
target_path
|> Path.dirname()
|> File.mkdir_p!()

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

%{
module: module,
Expand Down
54 changes: 42 additions & 12 deletions test/ex_factor/extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ defmodule ExFactor.ExtractorTest do
use ExUnit.Case
alias ExFactor.Extractor

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

on_exit(fn ->
File.rm_rf("lib/ex_factor/tmp")
File.rm_rf("test/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 @@ -32,6 +28,45 @@ defmodule ExFactor.ExtractorTest do
test "noop when no matching fns found in source" do
end

test "create the dir path if necessary" do
content = """
defmodule ExFactorSampleModule do
@somedoc "This is somedoc"
# a comment and no aliases
_docp = "here's an arbitrary module underscore"
@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",
source_function: :pub1,
arity: 1
]

Extractor.emplace(opts)
file = File.read!("lib/ex_factor/test/new_module.ex")

assert file =~ "def pub1(arg1) do"
assert file =~ "defmodule ExFactor.Test.NewModule do"
# includes additional attrs
assert file =~ "@spec pub1(term()) :: term()"
assert file =~ "@somedoc \"This is somedoc\""
# assert the added elements get flattened correctly
refute file =~ "[@somedoc \"This is somedoc\", "
# comments don't get moved
refute file =~ "# a comment and no aliases"

File.rm_rf("lib/ex_factor/test")
end

test "write a new file with the function" do
content = """
defmodule ExFactorSampleModule do
Expand All @@ -48,7 +83,6 @@ defmodule ExFactor.ExtractorTest do
File.write("test/tmp/source_module.ex", content)

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

opts = [
target_path: target_path,
Expand All @@ -71,8 +105,6 @@ defmodule ExFactor.ExtractorTest do
refute file =~ "[@somedoc \"This is somedoc\", "
# comments don't get moved
refute file =~ "# a comment and no aliases"
File.rm("test/tmp/source_module.ex")
File.rm("test/tmp/target_module.ex")
end

test "write a new file add a moduledoc comment" do
Expand All @@ -88,7 +120,6 @@ defmodule ExFactor.ExtractorTest do
File.write("test/tmp/source_module.ex", content)

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

opts = [
target_path: target_path,
Expand Down Expand Up @@ -122,7 +153,6 @@ defmodule ExFactor.ExtractorTest do
File.write("test/tmp/source_module.ex", content)

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

opts = [
target_path: target_path,
Expand Down
10 changes: 10 additions & 0 deletions test/ex_factor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ defmodule ExFactorTest do
end)
end

describe "path/1" do
test "convert a module name to a file path" do
assert "lib/test_module.ex" == ExFactor.path(TestModule)
end

test "convert a multi-atom module name to a file path" do
assert "lib/my/test/module_name.ex" == ExFactor.path(My.Test.ModuleName)
end
end

describe "refactor/1" do
test "it refactors the functions into a new module, specified in opts" do
File.rm("lib/ex_factor/tmp/source_module.ex")
Expand Down

0 comments on commit 04d16d9

Please sign in to comment.