Skip to content

Commit

Permalink
Update handling of test repo/migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed May 7, 2019
1 parent e0d0ebc commit 89aab27
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 69 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ erl_crash.dump
/tmp
.DS_Store
/.elixir_ls

/test/support/priv/migrations/*
!/test/support/priv/migrations/20170320052040_create_user.exs
9 changes: 1 addition & 8 deletions lib/mix/ex_oauth2_provider/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Mix.ExOauth2Provider.Migration do
base_name = "#{Macro.underscore(name)}.exs"
path =
repo
|> source_repo_priv()
|> Mix.EctoSQL.source_repo_priv()
|> Path.join("migrations")
|> maybe_create_directory()
timestamp = timestamp(path)
Expand Down Expand Up @@ -64,13 +64,6 @@ defmodule Mix.ExOauth2Provider.Migration do
defp pad(i) when i < 10, do: << ?0, ?0 + i >>
defp pad(i), do: to_string(i)

# TODO: Remove by 1.1.0 and only use Ecto 3.0
defp source_repo_priv(repo) do
mod = if Code.ensure_loaded?(Mix.EctoSQL), do: Mix.EctoSQL, else: Mix.Ecto

mod.source_repo_priv(repo)
end

@template """
defmodule <%= inspect migration.repo %>.Migrations.<%= migration.name %> do
use Ecto.Migration
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/tasks/ex_oauth2_provider.gen.config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.Config do

alias Mix.{Ecto, ExOauth2Provider, ExOauth2Provider.Config}

@switches [resource_owner: :string, config_file: :string]
@mix_task "ex_oauth2_provider.gen.migrations"
@switches [resource_owner: :string, config_file: :string]
@default_opts [resource_owner: "MyApp.Users.User", config_file: "config/config.exs"]
@mix_task "ex_oauth2_provider.gen.migrations"

@impl true
def run(args) do
Expand Down
10 changes: 7 additions & 3 deletions lib/mix/tasks/ex_oauth2_provider.gen.migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.Migration do

alias Mix.{Ecto, ExOauth2Provider, ExOauth2Provider.Migration}

@switches [binary_id: :boolean, namespace: :string]
@switches [binary_id: :boolean, namespace: :string]
@default_opts [binary_id: false, namespace: "oauth"]
@mix_task "ex_oauth2_provider.gen.migrations"
@mix_task "ex_oauth2_provider.gen.migrations"

@impl true
def run(args) do
Expand All @@ -47,7 +47,7 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.Migration do
defp create_migration_files(config, args) do
args
|> Ecto.parse_repo()
|> Enum.map(&Ecto.ensure_repo(&1, args))
|> Enum.map(&ensure_repo(&1, args))
|> Enum.map(&Map.put(config, :repo, &1))
|> Enum.each(&create_migration_files/1)
end
Expand All @@ -58,4 +58,8 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.Migration do

Migration.create_migration_file(repo, name, content)
end

defp ensure_repo(repo, args) do
Ecto.ensure_repo(repo, args ++ ~w(--no-deps-check))
end
end
4 changes: 2 additions & 2 deletions lib/mix/tasks/ex_oauth2_provider.gen.schemas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.Schemas do
alias ExOauth2Provider.Config
alias Mix.{ExOauth2Provider, ExOauth2Provider.Schema}

@switches [binary_id: :boolean, context_app: :string, namespace: :string]
@switches [binary_id: :boolean, context_app: :string, namespace: :string]
@default_opts [binary_id: false, namespace: "oauth"]
@mix_task "ex_oauth2_provider.gen.migrations"
@mix_task "ex_oauth2_provider.gen.migrations"

@impl true
def run(args) do
Expand Down
6 changes: 3 additions & 3 deletions lib/mix/tasks/ex_oauth2_provider.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ defmodule Mix.Tasks.ExOauth2Provider.Install do
alias Mix.ExOauth2Provider
alias Mix.Tasks.ExOauth2Provider.Gen.{Config, Migration, Schemas}

@switches [config: :boolean, migration: :boolean, schemas: :boolean]
@switches [config: :boolean, migration: :boolean, schemas: :boolean]
@default_opts [config: true, migration: true, schemas: true]
@mix_task "ex_oauth2_provider.install"
@mix_task "ex_oauth2_provider.install"

@doc false
@impl true
def run(args) do
ExOauth2Provider.no_umbrella!(@mix_task)

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule ExOauth2Provider.Mixfile do
defp elixirc_paths(_), do: ["lib"]

defp deps do
[{:ecto, "~> 2.1 or ~> 3.0"},
[{:ecto, "~> 3.0"},
{:plug, ">= 1.0.0 and < 1.8.0"},

# Dev and test dependencies
Expand Down
2 changes: 1 addition & 1 deletion test/mix/tasks/ex_oauth2_provider.gen.config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.ConfigTest do

@tmp_path Path.join(["tmp", inspect(Config)])
@config_file "config/config.exs"
@options ~w(-r #{to_string(Repo)})
@options ~w(-r #{inspect Repo})

setup do
File.rm_rf!(@tmp_path)
Expand Down
73 changes: 32 additions & 41 deletions test/mix/tasks/ex_oauth2_provider.gen.migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.MigrationTest do
end

@tmp_path Path.join(["tmp", inspect(Migration)])
@options ~w(-r #{to_string(Repo)})
@migrations_path Path.join(@tmp_path, "migrations")
@options ~w(-r #{inspect Repo})

setup do
File.rm_rf!(@tmp_path)
Expand All @@ -24,20 +24,21 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.MigrationTest do

assert [migration_file] = File.ls!(@migrations_path)
assert String.match?(migration_file, ~r/^\d{14}_create_oauth_tables\.exs$/)
assert_file Path.join(@migrations_path, migration_file), fn file ->
assert file =~ "defmodule #{inspect Repo}.Migrations.CreateOauthTables do"
assert file =~ "use Ecto.Migration"
assert file =~ "def change do"
assert file =~ "add :owner_id, references(:users, on_delete: :nothing)"
assert file =~ "add :resource_owner_id, references(:users, on_delete: :nothing)"
refute file =~ "add :owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
refute file =~ "add :resource_owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
refute file =~ ":oauth_applications, primary_key: false"
refute file =~ ":oauth_access_grants, primary_key: false"
refute file =~ ":oauth_access_tokens, primary_key: false"
refute file =~ "add :id, :binary_id, primary_key: true"
refute file =~ "add :application_id, references(:oauth_applications, on_delete: :nothing, type: binary_id)"
end

file = @migrations_path |> Path.join(migration_file) |> File.read!()

assert file =~ "defmodule #{inspect Repo}.Migrations.CreateOauthTables do"
assert file =~ "use Ecto.Migration"
assert file =~ "def change do"
assert file =~ "add :owner_id, references(:users, on_delete: :nothing)"
assert file =~ "add :resource_owner_id, references(:users, on_delete: :nothing)"
refute file =~ "add :owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
refute file =~ "add :resource_owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
refute file =~ ":oauth_applications, primary_key: false"
refute file =~ ":oauth_access_grants, primary_key: false"
refute file =~ ":oauth_access_tokens, primary_key: false"
refute file =~ "add :id, :binary_id, primary_key: true"
refute file =~ "add :application_id, references(:oauth_applications, on_delete: :nothing, type: binary_id)"
end)
end

Expand All @@ -46,38 +47,28 @@ defmodule Mix.Tasks.ExOauth2Provider.Gen.MigrationTest do
Migration.run(@options ++ ~w(--binary-id))

assert [migration_file] = File.ls!(@migrations_path)
assert_file Path.join(@migrations_path, migration_file), fn file ->
refute file =~ "add :owner_id, :integer, null: false"
refute file =~ "add :resource_owner_id, :integer"
assert file =~ "add :owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
assert file =~ "add :resource_owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
assert file =~ ":oauth_applications, primary_key: false"
assert file =~ ":oauth_access_grants, primary_key: false"
assert file =~ ":oauth_access_tokens, primary_key: false"
assert file =~ "add :id, :binary_id, primary_key: true"
assert file =~ "add :application_id, references(:oauth_applications, on_delete: :nothing, type: :binary_id)"
end

file = @migrations_path |> Path.join(migration_file) |> File.read!()

refute file =~ "add :owner_id, :integer, null: false"
refute file =~ "add :resource_owner_id, :integer"
assert file =~ "add :owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
assert file =~ "add :resource_owner_id, references(:users, on_delete: :nothing, type: :binary_id)"
assert file =~ ":oauth_applications, primary_key: false"
assert file =~ ":oauth_access_grants, primary_key: false"
assert file =~ ":oauth_access_tokens, primary_key: false"
assert file =~ "add :id, :binary_id, primary_key: true"
assert file =~ "add :application_id, references(:oauth_applications, on_delete: :nothing, type: :binary_id)"
end)
end

test "doesn't make duplicate timestamp migrations" do
test "doesn't make duplicate migrations" do
File.cd!(@tmp_path, fn ->
Mix.Tasks.Ecto.Gen.Migration.run(["test", "-r", to_string(Repo)])
Migration.run(@options)

assert [test_migration, migration_file] = @migrations_path |> File.ls!() |> Enum.sort()
date1 = Regex.run(~r/^(\d{14})_.*\.exs$/, test_migration)
date2 = Regex.run(~r/^(\d{14})_create_oauth_tables\.exs$/, migration_file)
assert date1 < date2
assert_raise Mix.Error, "migration can't be created, there is already a migration file with name CreateOauthTables.", fn ->
Migration.run(@options)
end
end)
end

defp assert_file(file) do
assert File.regular?(file), "Expected #{file} to exist, but does not"
end

defp assert_file(file, callback) when is_function(callback, 1) do
assert_file(file)
callback.(File.read!(file))
end
end
12 changes: 10 additions & 2 deletions test/support/mix/test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ defmodule ExOauth2Provider.Mix.TestCase do
@moduledoc false
use ExUnit.CaseTemplate

setup context do
setup_all do
clear_tmp_files()

:ok
end

setup _context do
current_shell = Mix.shell()

on_exit fn ->
Expand All @@ -11,6 +17,8 @@ defmodule ExOauth2Provider.Mix.TestCase do

Mix.shell(Mix.Shell.Process)

context
:ok
end

defp clear_tmp_files, do: File.rm_rf!("tmp")
end
6 changes: 6 additions & 0 deletions test/support/priv/migrations/2_create_oauth_tables.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require Mix.ExOauth2Provider.Migration

binary_id = if System.get_env("UUID"), do: true, else: false
"CreateOauthTables"
|> Mix.ExOauth2Provider.Migration.gen("oauth", %{repo: ExOauth2Provider.Test.Repo, binary_id: binary_id})
|> Code.eval_string()
3 changes: 0 additions & 3 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ File.rmdir(target)
:ok = :file.make_symlink(Path.expand(source), target)

# Set up database
Mix.shell.cmd("rm test/support/priv/migrations/*_create_oauth_tables.exs")
opts = if System.get_env("UUID"), do: ~w(--binary-id), else: ~w()
Mix.Task.run("ex_oauth2_provider.gen.migration", ~w(-r ExOauth2Provider.Test.Repo) ++ opts)
Mix.Task.run("ecto.drop", ~w(--quiet -r ExOauth2Provider.Test.Repo))
Mix.Task.run("ecto.create", ~w(--quiet -r ExOauth2Provider.Test.Repo))
Mix.Task.run("ecto.migrate", ~w(-r ExOauth2Provider.Test.Repo))
Expand Down

0 comments on commit 89aab27

Please sign in to comment.