-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e382b11
commit 8c58d8c
Showing
7 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule ExFactor.CLITest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureIO | ||
|
||
setup_all do | ||
File.mkdir_p("test/tmp") | ||
|
||
on_exit(fn -> | ||
File.rm_rf("test/tmp") | ||
end) | ||
end | ||
|
||
test "missing required options exit != 0" do | ||
opts = [ | ||
module: "ExFactorSampleModule", | ||
function: "pub1", | ||
arity: 1 | ||
] | ||
argv = OptionParser.to_argv(opts) | ||
|
||
capture_io fn -> | ||
{_, exit_status} = System.cmd("mix", ["ex_factor" | argv]) | ||
refute exit_status == 0 | ||
end | ||
end | ||
|
||
test "with dry run" 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) | ||
target_path = "test/tmp/target_module.ex" | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target: "ExFactor.NewMod", | ||
module: "ExFactorSampleModule", | ||
source_path: "test/tmp/source_module.ex", | ||
function: :pub1, | ||
arity: 1, | ||
dryrun: true | ||
] | ||
|
||
argv = OptionParser.to_argv(opts) | ||
|
||
{cli_output, exit_status} = System.cmd("mix", ["ex_factor" | argv]) | ||
assert exit_status == 0 | ||
|
||
assert cli_output =~ "Message: --dry_run changes to make" | ||
|
||
# no new file gets written | ||
assert {:error, :enoent} = File.read(target_path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
defmodule ExFactor.MixTaskTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureIO | ||
|
||
setup do | ||
File.mkdir_p("test/tmp") | ||
|
||
on_exit(fn -> | ||
File.rm_rf("test/tmp") | ||
end) | ||
end | ||
|
||
describe "mix task" do | ||
test "requires some options" do | ||
opts = [ | ||
module: "ExFactorSampleModule", | ||
function: "pub1", | ||
arity: 1 | ||
] | ||
argv = OptionParser.to_argv(opts) | ||
|
||
assert_raise KeyError, "key :target not found in: #{inspect(opts)}", fn -> | ||
Mix.Tasks.ExFactor.run(argv) | ||
end | ||
end | ||
|
||
test "write a new file with the function" 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) | ||
target_path = "test/tmp/target_module.ex" | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target: "ExFactor.NewMod", | ||
module: "ExFactorSampleModule", | ||
source_path: "test/tmp/source_module.ex", | ||
function: :pub1, | ||
arity: 1 | ||
] | ||
|
||
argv = OptionParser.to_argv(opts) | ||
|
||
capture_io fn -> Mix.Tasks.ExFactor.run(argv) end | ||
|
||
file = File.read!(target_path) | ||
|
||
assert file =~ "def pub1(arg1) do" | ||
assert file =~ "defmodule ExFactor.NewMod 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" | ||
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" | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target: "ExFactor.NewMod", | ||
module: "ExFactorSampleModule", | ||
source_path: "test/tmp/source_module.ex", | ||
function: :pub1, | ||
arity: 1 | ||
] | ||
|
||
argv = OptionParser.to_argv(opts) | ||
capture_io fn -> Mix.Tasks.ExFactor.run(argv) end | ||
|
||
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 | ||
@somedoc "This is somedoc" | ||
# a comment and no aliases | ||
_docp = "here's an arbitrary module underscore" | ||
@spec pub1(term()) :: term() | ||
def pub1(arg1) do | ||
:ok | ||
end | ||
end | ||
""" | ||
|
||
File.write("test/tmp/source_module.ex", content) | ||
target_path = "test/tmp/target_module.ex" | ||
|
||
opts = [ | ||
target_path: target_path, | ||
target: "ExFactor.NewMod", | ||
module: "ExFactorSampleModule", | ||
source_path: "test/tmp/source_module.ex", | ||
function: :pub1, | ||
arity: 1, | ||
dryrun: true | ||
] | ||
|
||
argv = OptionParser.to_argv(opts) | ||
|
||
output = capture_io fn -> Mix.Tasks.ExFactor.run(argv) end | ||
|
||
# no new file gets written | ||
assert {:error, :enoent} = File.read(target_path) | ||
# assert output.file_contents | ||
assert output =~ "--dry_run changes to make" | ||
assert output =~ "ExFactor.NewMod" | ||
end | ||
|
||
test "write multiple functions and their docs, into an existing module" do | ||
content = """ | ||
defmodule ExFactor.Tmp.SourceModule do | ||
@somedoc "This is somedoc" | ||
@doc "this is some documentation for refactor1/1" | ||
def refactor1(arg1) do | ||
:ok | ||
end | ||
def refactor1([]) do | ||
:empty | ||
end | ||
end | ||
""" | ||
|
||
File.write("test/tmp/source_module.ex", content) | ||
|
||
content = """ | ||
defmodule ExFactor.Tmp.TargetModule do | ||
@moduledoc false | ||
@somedoc "This is somedoc TargetModule" | ||
@doc "some docs" | ||
def pub_exists(arg_exists) do | ||
:ok | ||
end | ||
def pub_exists(:error) do | ||
:error | ||
end | ||
end | ||
""" | ||
|
||
target_path = "test/tmp/target_module.ex" | ||
File.write(target_path, content) | ||
|
||
opts = [ | ||
target: "ExFactor.Tmp.TargetModule", | ||
module: "ExFactor.Tmp.SourceModule", | ||
target_path: target_path, | ||
source_path: "test/tmp/source_module.ex", | ||
function: :refactor1, | ||
arity: 1 | ||
] | ||
|
||
argv = OptionParser.to_argv(opts) | ||
|
||
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 | ||
|
||
end | ||
end |