Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
tjchambers committed Nov 26, 2024
1 parent d5dd04d commit ef0c034
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 102 deletions.
7 changes: 5 additions & 2 deletions lib/ex_unit_jumpstart/create_files.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ defmodule ExUnitJumpstart.CreateFiles do
def create_missing_test_files(config, code_files, test_files) do
code_files
|> Enum.map(fn code_file ->
test_file = test_files
|> Enum.find(fn test_file -> String.replace(test_file.path, "_test.exs", ".ex") == code_file.path end)
test_file =
test_files
|> Enum.find(fn test_file ->
String.replace(test_file.path, "_test.exs", ".ex") == code_file.path
end)

unless test_file do
IO.puts("Creating test file for #{code_file.path}")
Expand Down
20 changes: 13 additions & 7 deletions lib/ex_unit_jumpstart/get_code_files.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ defmodule ExUnitJumpstart.GetCodeFiles do
@moduledoc """
Get a list of code files and their modules.
"""

@doc """
Get a list of code files and their modules.
"""
@spec get_code_files(Keyword.t()) :: list(ExUnitJumpstart.CodeFile.t())
def get_code_files(config) do
paths = Path.wildcard("#{config[:code_dir]}/**/*.ex")

paths
|> Enum.map(fn path ->
%ExUnitJumpstart.CodeFile{
%ExUnitJumpstart.CodeFile{
path: path |> String.replace(config[:code_dir] <> "/", ""),
modules: modules(path)
}
end)
end

defp modules(path) do
content = File.read!(path)
Regex.scan(~r/defmodule (.*?) do/, content)
|> Enum.flat_map(fn [_, y] -> [y] end)
end
end
defp modules(path) do
content = File.read!(path)

Regex.scan(~r/defmodule (.*?) do/, content)
|> Enum.flat_map(fn [_, y] -> [y] end)
end
end
13 changes: 11 additions & 2 deletions lib/ex_unit_jumpstart/get_test_files.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
defmodule ExUnitJumpstart.GetTestFiles do
@moduledoc """
Get a list of test files and their modules.
"""

@doc """
Get a list of test files and their modules.
"""
@spec get_test_files(Keyword.t()) :: list(ExUnitJumpstart.TestFile.t())
def get_test_files(config) do
paths = Path.wildcard("#{config[:test_dir]}/**/*.exs")
paths = Path.wildcard("#{config[:test_dir]}/**/*.exs")

paths
|> Enum.map(fn path ->
%ExUnitJumpstart.TestFile{
%ExUnitJumpstart.TestFile{
path: path |> String.replace(config[:test_dir] <> "/", ""),
modules: modules(path)
}
Expand All @@ -13,6 +21,7 @@ defmodule ExUnitJumpstart.GetTestFiles do

defp modules(path) do
content = File.read!(path)

Regex.scan(~r/defmodule (.*?) do/, content)
|> Enum.flat_map(fn [_, y] -> [y] end)
end
Expand Down
29 changes: 22 additions & 7 deletions lib/ex_unit_jumpstart/move_files.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ defmodule ExUnitJumpstart.MoveFiles do
@moduledoc """
Move incorrectly located test files to the appropriate test directory.
"""

@doc """
Move test files that are not in the correct location to the appropriate test directory.
"""
@spec move_misplaced_test_files(
Keyword.t(),
list(ExUnitJumpstart.CodeFile.t()),
list(ExUnitJumpstart.TestFile.t())
) :: list()
def move_misplaced_test_files(config, code_files, test_files) do
test_files
|> Enum.filter(fn test_file -> test_file_is_misplaced?(test_file, code_files) end)
|> Enum.map(fn test_file -> move_test_file(code_files, test_file, config) end)
end

def test_file_is_misplaced?(test_file, code_files) do
path_to_test = String.replace(test_file.path, "_test.exs",".ex" )
defp test_file_is_misplaced?(test_file, code_files) do
path_to_test = String.replace(test_file.path, "_test.exs", ".ex")

code_files
|> Enum.find(fn code_file -> code_file.path == path_to_test end)
|> is_nil
|> is_nil()
end

def move_test_file(code_files, test_file, config) do
IO.puts "Moving #{test_file.path} "
defp move_test_file(code_files, test_file, config) do
IO.puts("Moving #{test_file.path}")

code_file =
code_files
|> Enum.find(fn code_file ->
Expand All @@ -27,10 +37,15 @@ defmodule ExUnitJumpstart.MoveFiles do
end)

if code_file do
new_path = Path.join(config[:test_dir], code_file.path) |> String.replace(".ex", "_test.exs")
IO.puts "Moving #{test_file.path} to #{new_path}"
new_path =
Path.join(config[:test_dir], code_file.path) |> String.replace(".ex", "_test.exs")

IO.puts("Moving #{test_file.path} to #{new_path}")
unless config.dry_run do

File.mkdir_p!(Path.dirname(new_path))
File.rename!(Path.join(config[:test_dir], test_file.path), new_path)
end
end
end
end
86 changes: 8 additions & 78 deletions lib/mix/tasks/ex_unit_jumpstart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ defmodule Mix.Tasks.ExUnitJumpstart do

@app :ex_unit_jumpstart

@defaults [
test_dir: "test",
code_dir: "lib",
template_dir: "priv/templates",
dry_run: false
]

@doc "Generate cfg from mix.exs and app config"
@spec parse_args(OptionParser.argv()) :: Keyword.t()
def parse_args(argv) do
Expand All @@ -18,14 +25,8 @@ defmodule Mix.Tasks.ExUnitJumpstart do
@doc "Generate cfg based on params"
@spec create_config(Keyword.t(), Keyword.t()) :: Keyword.t()
def create_config(_mix_config, user_config) do
defaults = [
test_dir: "test",
code_dir: "lib",
template_dir: "priv/templates"
]

# Override values from user config
cfg = Keyword.merge(defaults, user_config)
cfg = Keyword.merge(@defaults, user_config)

# Calculate values from other things

Expand Down Expand Up @@ -80,74 +81,3 @@ defmodule Mix.Tasks.ExUnitJumpstart do

def expand_vars(value, _cfg), do: to_string(value)
end

defmodule Mix.Tasks.ExUnitJumpstart.Init do
@moduledoc """
Initialize template files.
## Command line options
* `--test_dir` - target directory
## Usage
# Copy default templates into your project
mix ex_unit_jumpstart.init
"""
@shortdoc "Initialize template files"
use Mix.Task

@app :ex_unit_jumpstart

@impl Mix.Task
def run(args) do
cfg = Mix.Tasks.ExUnitJumpstart.parse_args(args)

template_dir = cfg[:template_dir]
app_dir = Application.app_dir(@app, ["priv", "templates"])

IO.puts("Copying templates to #{template_dir}")
:ok = File.mkdir_p(template_dir)
{:ok, _files} = File.cp_r(app_dir, template_dir)
end
end

defmodule Mix.Tasks.ExUnitJumpstart.Generate do
@moduledoc """
Create ExUnitJumpstart test files and update existing ones.
## Usage
mix ex_unit_jumpstart.generate
"""
@shortdoc "Create ExUnitJumpstart scripts and files"
use Mix.Task

# alias MixExUnitJumpstart.Templates

@impl Mix.Task
def run(args) do
cfg = Mix.Tasks.ExUnitJumpstart.parse_args(args)

code_files = ExUnitJumpstart.GetCodeFiles.get_code_files(cfg)
test_files = ExUnitJumpstart.GetTestFiles.get_test_files(cfg)

ExUnitJumpstart.MoveFiles.move_misplaced_test_files(cfg, code_files, test_files)
ExUnitJumpstart.CreateFiles.create_missing_test_files(cfg, code_files, test_files)

# refetch test files after moving and creating
test_files = ExUnitJumpstart.GetTestFiles.get_test_files(cfg)

ExUnitJumpstart.UnitTestGenerator.create_unit_tests(cfg, code_files, test_files)
end

# defp write_template(cfg, dest_dir, template),
# do: write_template(cfg, dest_dir, template, template)

# defp write_template(cfg, dest_dir, template, file) do
# output_file = cfg[:target_prefix] <> file
# # target_file = Path.join(dest_dir, output_file)
# # Mix.shell.info "Generating #{target_file} from template #{template}"
# Templates.write_template(cfg, dest_dir, template, output_file)
# end
end
41 changes: 41 additions & 0 deletions lib/mix/tasks/generate.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Mix.Tasks.ExUnitJumpstart.Generate do
@moduledoc """
Create ExUnitJumpstart test files and update existing ones.
## Usage
mix ex_unit_jumpstart.generate
"""
@shortdoc "Create ExUnitJumpstart scripts and files"
use Mix.Task

# alias MixExUnitJumpstart.Templates

@impl Mix.Task
def run(args) do
cfg = Mix.Tasks.ExUnitJumpstart.parse_args(args)

IO.inspect(cfg)

code_files = ExUnitJumpstart.GetCodeFiles.get_code_files(cfg)
test_files = ExUnitJumpstart.GetTestFiles.get_test_files(cfg)

ExUnitJumpstart.MoveFiles.move_misplaced_test_files(cfg, code_files, test_files)
# ExUnitJumpstart.CreateFiles.create_missing_test_files(cfg, code_files, test_files)

# refetch test files after moving and creating
_test_files = ExUnitJumpstart.GetTestFiles.get_test_files(cfg)

# ExUnitJumpstart.UnitTestGenerator.create_unit_tests(cfg, code_files, test_files)
end

# defp write_template(cfg, dest_dir, template),
# do: write_template(cfg, dest_dir, template, template)

# defp write_template(cfg, dest_dir, template, file) do
# output_file = cfg[:target_prefix] <> file
# # target_file = Path.join(dest_dir, output_file)
# # Mix.shell.info "Generating #{target_file} from template #{template}"
# Templates.write_template(cfg, dest_dir, template, output_file)
# end
end
30 changes: 30 additions & 0 deletions lib/mix/tasks/init.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Mix.Tasks.ExUnitJumpstart.Init do
@moduledoc """
Initialize template files.
## Command line options
* `--test_dir` - target directory
## Usage
# Copy default templates into your project
mix ex_unit_jumpstart.init
"""
@shortdoc "Initialize template files"
use Mix.Task

@app :ex_unit_jumpstart

@impl Mix.Task
def run(args) do
cfg = Mix.Tasks.ExUnitJumpstart.parse_args(args)

template_dir = cfg[:template_dir]
app_dir = Application.app_dir(@app, ["priv", "templates"])

IO.puts("Copying templates to #{template_dir}")
:ok = File.mkdir_p(template_dir)
{:ok, _files} = File.cp_r(app_dir, template_dir)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExUnitJumpstart.MixProject do
use Mix.Project

@github "https://github.com/tjchambers/ex_unit_jumpstart"
@version "0.0.3"
@version "0.0.4"

def project do
[
Expand Down
8 changes: 4 additions & 4 deletions test/ex_unit_jumpstart/get_code_files_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule ExUnitJumpstart.GetCodeFilesTest do

last = code_files |> List.last()

assert length(code_files) == 9
assert last.path == "mix/tasks/ex_unit_jumpstart.ex"
assert length(last.modules) == 3
assert last.modules |> List.last() == "Mix.Tasks.ExUnitJumpstart.Generate"
assert length(code_files) >= 9
assert last.path == "mix/tasks/init.ex"
assert length(last.modules) == 1
assert last.modules |> List.last() == "Mix.Tasks.ExUnitJumpstart.Init"
end
end
1 change: 0 additions & 1 deletion test/ex_unit_jumpstart/unit_test_generator_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
defmodule ExUnitJumpstart.UnitTestGeneratorTest do
use ExUnit.Case, async: true

end

0 comments on commit ef0c034

Please sign in to comment.