Skip to content

Commit

Permalink
Test oban.install igniter task
Browse files Browse the repository at this point in the history
  • Loading branch information
sorentwo committed Dec 27, 2024
1 parent 8fc9176 commit d3b8875
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/mix/tasks/oban.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ if Code.ensure_loaded?(Igniter) do
opts = igniter.args.options

case extract_repo(igniter, app_name, opts[:repo]) do
{nil, igniter} ->
igniter

{repo, igniter} ->
{:ok, repo} ->
engine = parse_engine(repo, opts[:engine])
notifier = parse_notifier(repo, opts[:notifier])

conf_code = [engine: engine, notifier: notifier, queues: [default: 10], repo: repo]
test_code = [testing: :manual]
tree_code = "Application.fetch_env!(#{app_name}, Oban)"

tree_code =
quote do
Application.fetch_env!(unquote(app_name), Oban)
end

migration = """
use Ecto.Migration
Expand All @@ -90,31 +91,38 @@ if Code.ensure_loaded?(Igniter) do
|> Igniter.Project.Deps.add_dep({:oban, "~> 2.18"})
|> Igniter.Project.Config.configure("config.exs", app_name, [Oban], {:code, conf_code})
|> Igniter.Project.Config.configure("test.exs", app_name, [Oban], {:code, test_code})
|> Igniter.Project.Application.add_new_child({Oban, {:code, tree_code}}, after: repo)
|> Igniter.Project.Application.add_new_child({Oban, {:code, tree_code}}, after: [repo])
|> Igniter.Project.Formatter.import_dep(:oban)
|> Igniter.Libs.Ecto.gen_migration(repo, "add_oban", body: migration)

{:error, igniter} ->
igniter
end
end

defp extract_repo(igniter, app_name, nil) do
case Application.get_env(app_name, :ecto_repos, []) do
[repo | _] ->
{repo, igniter}
{:ok, repo}

_ ->
{nil, Igniter.add_issue(igniter, "No ecto repos found for #{inspect(app_name)}")}
issue = """
No ecto repos found for #{inspect(app_name)}.
Ensure `:ecto` is installed and configured for the current application.
"""

{:error, Igniter.add_issue(igniter, issue)}
end
end

defp extract_repo(igniter, _app_name, module) do
repo = Igniter.Project.Module.parse(module)

case Igniter.Project.Module.module_exists(igniter, repo) do
{true, igniter} ->
{repo, igniter}

{_boo, igniter} ->
{nil, Igniter.add_issue(igniter, "The provided repo (#{inspect(repo)}) doesn't exist")}
if Code.ensure_loaded?(repo) do
{:ok, repo}
else
{:error, Igniter.add_issue(igniter, "The provided repo (#{inspect(repo)}) doesn't exist")}
end
end

Expand Down
66 changes: 66 additions & 0 deletions test/mix/tasks/oban.install_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
defmodule Mix.Tasks.Oban.InstallTest do
use ExUnit.Case, async: true

import Igniter.Test

alias Mix.Tasks.Oban.Install

describe "install" do
test "installing without an available ecto repo" do
assert {:error, [warning]} =
test_project()
|> Install.igniter()
|> apply_igniter()

assert warning =~ "No ecto repos found for :test"
end

test "installing with an ecto repo available" do
application = """
defmodule Oban.Application do
use Application
def start(_type, _args) do
children = [
Oban.Test.Repo,
{Ecto.Migrator, migrator_opts()},
MyWeb.Endpoint
]
end
end
"""

[app_name: :oban, files: %{"lib/oban/application.ex" => application}]
|> test_project()
|> Install.igniter()
|> assert_has_patch("config/config.exs", """
| config :oban, Oban,
| engine: Oban.Engines.Basic,
| notifier: Oban.Notifiers.Postgres,
| queues: [default: 10],
| repo: Oban.Test.Repo
""")
|> assert_has_patch("config/test.exs", """
| config :oban, Oban, testing: :manual
""")
|> assert_has_patch("lib/oban/application.ex", """
| {Oban, Application.fetch_env!(:oban, Oban)},
| MyWeb.Endpoint
""")
end

test "installing selects correct config with alternate repo" do
[app_name: :oban]
|> test_project()
|> Map.replace!(:args, %{options: [repo: "Oban.Test.LiteRepo"]})
|> Install.igniter()
|> assert_has_patch("config/config.exs", """
| config :oban, Oban,
| engine: Oban.Engines.Lite,
| notifier: Oban.Notifiers.PG,
| queues: [default: 10],
| repo: Oban.Test.LiteRepo
""")
end
end
end

0 comments on commit d3b8875

Please sign in to comment.