From 2d1c7d48ceb85d1d2d411f8e2e03e2bfa4f0ca5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Wed, 7 Aug 2024 16:38:00 +0200 Subject: [PATCH] chore: Cleanup Test Output (#366) --- .../migration_generator.ex | 4 +- test/migration_generator_test.exs | 81 +++++++++---------- test/mix_squash_snapshots_test.exs | 14 +++- 3 files changed, 52 insertions(+), 47 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index a6138eb..ff8f1d4 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -379,7 +379,7 @@ defmodule AshPostgres.MigrationGenerator do operations -> if opts.check do - IO.puts(""" + Mix.shell().error(""" Migrations would have been generated, but the --check flag was provided. To see what migration would have been generated, run with the `--dry-run` @@ -1028,7 +1028,7 @@ defmodule AshPostgres.MigrationGenerator do end rescue exception -> - IO.puts(""" + Mix.shell().error(""" Exception while formatting: #{inspect(exception)} diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index de8e12a..a5648c4 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -4,7 +4,17 @@ defmodule AshPostgres.MigrationGeneratorTest do import ExUnit.CaptureLog - defmacrop defposts(mod \\ Post, do: body) do + setup do + current_shell = Mix.shell() + + :ok = Mix.shell(Mix.Shell.Process) + + on_exit(fn -> + Mix.shell(current_shell) + end) + end + + defmacrop defresource(mod, do: body) do quote do Code.compiler_options(ignore_module_conflict: true) @@ -13,6 +23,16 @@ defmodule AshPostgres.MigrationGeneratorTest do domain: nil, data_layer: AshPostgres.DataLayer + unquote(body) + end + + Code.compiler_options(ignore_module_conflict: false) + end + end + + defmacrop defposts(mod \\ Post, do: body) do + quote do + defresource unquote(mod) do postgres do table "posts" repo(AshPostgres.TestRepo) @@ -30,8 +50,23 @@ defmodule AshPostgres.MigrationGeneratorTest do unquote(body) end + end + end - Code.compiler_options(ignore_module_conflict: false) + defmacrop defcomments(mod \\ Comment, do: body) do + quote do + defresource unquote(mod) do + postgres do + table "comments" + repo(AshPostgres.TestRepo) + end + + actions do + defaults([:create, :read, :update, :destroy]) + end + + unquote(body) + end end end @@ -108,8 +143,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post]) - Mix.shell(Mix.Shell.Process) - AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", migration_path: "test_migration_path", @@ -208,8 +241,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post]) - Mix.shell(Mix.Shell.Process) - {:ok, _} = Ecto.Adapters.SQL.query( AshPostgres.TestRepo, @@ -291,7 +322,6 @@ defmodule AshPostgres.MigrationGeneratorTest do end defdomain([Post]) - Mix.shell(Mix.Shell.Process) AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", @@ -336,7 +366,6 @@ defmodule AshPostgres.MigrationGeneratorTest do end defdomain([Post]) - Mix.shell(Mix.Shell.Process) AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", @@ -378,8 +407,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post]) - Mix.shell(Mix.Shell.Process) - AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", migration_path: "test_migration_path", @@ -470,8 +497,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post]) - Mix.shell(Mix.Shell.Process) - AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", migration_path: "test_migration_path", @@ -833,8 +858,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post]) - Mix.shell(Mix.Shell.Process) - AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", migration_path: "test_migration_path", @@ -1635,11 +1658,7 @@ defmodule AshPostgres.MigrationGeneratorTest do File.rm_rf!("test_migration_path") end) - defmodule Comment do - use Ash.Resource, - domain: nil, - data_layer: AshPostgres.DataLayer - + defcomments do postgres do polymorphic?(true) repo(AshPostgres.TestRepo) @@ -1832,16 +1851,7 @@ defmodule AshPostgres.MigrationGeneratorTest do end end - defmodule Comment do - use Ash.Resource, - domain: nil, - data_layer: AshPostgres.DataLayer - - postgres do - table "comments" - repo AshPostgres.TestRepo - end - + defcomments do attributes do uuid_primary_key(:id) end @@ -1855,8 +1865,6 @@ defmodule AshPostgres.MigrationGeneratorTest do defdomain([Post, Comment]) - Mix.shell(Mix.Shell.Process) - AshPostgres.MigrationGenerator.generate(Domain, snapshot_path: "test_snapshots_path", migration_path: "test_migration_path", @@ -1881,16 +1889,7 @@ defmodule AshPostgres.MigrationGeneratorTest do end end - defmodule Comment do - use Ash.Resource, - domain: nil, - data_layer: AshPostgres.DataLayer - - postgres do - table "comments" - repo AshPostgres.TestRepo - end - + defcomments do attributes do uuid_primary_key(:id) end diff --git a/test/mix_squash_snapshots_test.exs b/test/mix_squash_snapshots_test.exs index 5c934b2..f282722 100644 --- a/test/mix_squash_snapshots_test.exs +++ b/test/mix_squash_snapshots_test.exs @@ -2,6 +2,16 @@ defmodule AshPostgres.MixSquashSnapshotsTest do use AshPostgres.RepoCase, async: false @moduletag :migration + setup do + current_shell = Mix.shell() + + :ok = Mix.shell(Mix.Shell.Process) + + on_exit(fn -> + Mix.shell(current_shell) + end) + end + defmacrop defposts(mod \\ Post, do: body) do quote do Code.compiler_options(ignore_module_conflict: true) @@ -67,8 +77,6 @@ defmodule AshPostgres.MixSquashSnapshotsTest do File.rm_rf!("test_migration_path") end) - Mix.shell(Mix.Shell.Process) - defposts do identities do identity(:title, [:title]) @@ -154,8 +162,6 @@ defmodule AshPostgres.MixSquashSnapshotsTest do File.rm_rf!("test_migration_path") end) - Mix.shell(Mix.Shell.Process) - defposts do identities do identity(:title, [:title])