Skip to content

Commit

Permalink
Add formatter module
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoch-cars committed Sep 18, 2021
1 parent d923f0b commit 4a90b3e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/ex_factor/callers.ex
Expand Up @@ -2,6 +2,7 @@ defmodule ExFactor.Callers do
@moduledoc """
Documentation for `ExFactor.Callers`.
"""
import ExUnit.CaptureIO

alias ExFactor.Parser

Expand All @@ -11,8 +12,7 @@ defmodule ExFactor.Callers do
use `mix xref` list all the callers of a given module.
"""
def callers(mod) do
System.cmd("mix", ["xref", "callers", "#{mod}"], env: [{"MIX_ENV", "test"}])
|> elem(0)
capture_io(fn -> Mix.Tasks.Xref.run(["callers", mod]) end)
|> String.trim()
|> String.split("\n")
|> mangle_list()
Expand Down
10 changes: 10 additions & 0 deletions lib/ex_factor/formatter.ex
@@ -0,0 +1,10 @@
defmodule ExFactor.Formatter do
@moduledoc """
Documentation for `ExFactor.Formatter`.
Format a list of files
"""

def format(args) do
Mix.Tasks.Format.run(args)
end
end
1 change: 0 additions & 1 deletion lib/ex_factor/parser.ex
Expand Up @@ -39,7 +39,6 @@ defmodule ExFactor.Parser do
def all_functions({:ok, _ast} = input) do
{_ast, public_functions} = public_functions(input)
{ast, private_functions} = private_functions(input)
{ast, public_functions ++ private_functions}
all_fns = public_functions ++ private_functions
{ast, Enum.uniq(all_fns)}
end
Expand Down
29 changes: 29 additions & 0 deletions test/ex_factor/formatter_test.exs
@@ -0,0 +1,29 @@
defmodule ExFactor.FormatterTest do
use ExUnit.Case
alias ExFactor.Formatter

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

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

describe "format/1" do
test "it should format the specified files" do
content = """
defmodule ExFactorSampleModule do
# unindented line
# overindented line
end
"""

File.write("test/tmp/test_module.ex", content)
Formatter.format(["test/tmp/test_module.ex"])
{:ok, formatted_file} = File.read("test/tmp/test_module.ex")
assert formatted_file =~ "\n # unindented line"
assert formatted_file =~ "\n # overindented line"
end
end
end

0 comments on commit 4a90b3e

Please sign in to comment.