From a9ab030d9bf8d6247e68ba32d33ae0bffe53de25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C5=82uszniak?= Date: Fri, 1 Mar 2024 09:39:47 +0100 Subject: [PATCH] Fix umbrella app order for Elixir 1.16 + refine test setup --- lib/ex_check/check/compiler.ex | 4 +- lib/ex_check/manifest.ex | 2 +- .../project_cases/application_mod_test.exs | 2 +- test/ex_check/project_cases/base_test.exs | 2 +- test/ex_check/project_cases/compiler_test.exs | 5 +- .../project_cases/config_and_scripts_test.exs | 15 +-- test/ex_check/project_cases/deps_test.exs | 4 +- test/ex_check/project_cases/detect_test.exs | 2 +- .../project_cases/external_tools_test.exs | 3 +- .../project_cases/formatter_issue_test.exs | 2 +- .../project_cases/gen_config_test.exs | 6 +- test/ex_check/project_cases/manifest_test.exs | 30 +++--- .../missing_test_helper_test.exs | 2 +- .../asset_testing_test.exs | 4 +- .../umbrella_project_cases/base_test.exs | 4 +- .../non_recursive_test.exs | 4 +- .../only_specific_apps_test.exs | 8 +- .../sequential_test.exs | 49 +++++---- test/support/ex_check/case_helpers.ex | 101 ++++++++++++++++++ test/support/ex_check/project_case.ex | 86 +-------------- .../support/ex_check/umbrella_project_case.ex | 90 +--------------- 21 files changed, 184 insertions(+), 241 deletions(-) create mode 100644 test/support/ex_check/case_helpers.ex diff --git a/lib/ex_check/check/compiler.ex b/lib/ex_check/check/compiler.ex index e806228..14ebedb 100644 --- a/lib/ex_check/check/compiler.ex +++ b/lib/ex_check/check/compiler.ex @@ -58,7 +58,9 @@ defmodule ExCheck.Check.Compiler do else: actual_apps_paths tool_instances = - Enum.map(apps_paths, fn {app_name, app_dir} -> + apps_paths + |> Enum.sort_by(&elem(&1, 0)) + |> Enum.map(fn {app_name, app_dir} -> final_tool_opts = Keyword.update(tool_opts, :cd, app_dir, &Path.join(app_dir, &1)) {{tool_name, app_name}, final_tool_opts} end) diff --git a/lib/ex_check/manifest.ex b/lib/ex_check/manifest.ex index 9b2728a..c68917b 100644 --- a/lib/ex_check/manifest.ex +++ b/lib/ex_check/manifest.ex @@ -61,7 +61,7 @@ defmodule ExCheck.Manifest do defp serialize_tool_name({tool, app}), do: "#{app}/#{tool}" defp serialize_tool_name(tool), do: tool - @escape Enum.map(' [~#%&*{}\\:<>?/+|"]', &<<&1::utf8>>) + @escape Enum.map(~c" [~#%&*{}\\:<>?/+|\"]", &<<&1::utf8>>) defp get_path(opts) do Keyword.get_lazy(opts, :manifest, fn -> diff --git a/test/ex_check/project_cases/application_mod_test.exs b/test/ex_check/project_cases/application_mod_test.exs index ff12334..88edaa5 100644 --- a/test/ex_check/project_cases/application_mod_test.exs +++ b/test/ex_check/project_cases/application_mod_test.exs @@ -46,7 +46,7 @@ defmodule ExCheck.ProjectCases.ApplicationModTest do application_test_path = Path.join([project_dir, "test", "application_test.exs"]) File.write!(application_test_path, @application_test) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/project_cases/base_test.exs b/test/ex_check/project_cases/base_test.exs index d6d176e..036d986 100644 --- a/test/ex_check/project_cases/base_test.exs +++ b/test/ex_check/project_cases/base_test.exs @@ -2,7 +2,7 @@ defmodule ExCheck.ProjectCases.BaseTest do use ExCheck.ProjectCase, async: true test "base", %{project_dir: project_dir} do - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/project_cases/compiler_test.exs b/test/ex_check/project_cases/compiler_test.exs index fc5f18b..ee95efd 100644 --- a/test/ex_check/project_cases/compiler_test.exs +++ b/test/ex_check/project_cases/compiler_test.exs @@ -17,12 +17,13 @@ defmodule ExCheck.ProjectCases.CompilerTest do end """) - assert {output, 1} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(1) assert output =~ "compiler error code 1" assert output =~ "variable \"a\" is unused" - assert {output, 0} = System.cmd("mix", ~w[check --except compiler --no-retry], cd: project_dir) + output = + System.cmd("mix", ~w[check --except compiler --no-retry], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/project_cases/config_and_scripts_test.exs b/test/ex_check/project_cases/config_and_scripts_test.exs index 786bde7..7561ba1 100644 --- a/test/ex_check/project_cases/config_and_scripts_test.exs +++ b/test/ex_check/project_cases/config_and_scripts_test.exs @@ -56,13 +56,14 @@ defmodule ExCheck.ProjectCases.ConfigAndScriptsTest do supports_erl_config = Version.match?(System.version(), ">= 1.9.0") - assert {output, 0} = - System.cmd( - "elixir", - ["-e", "Application.put_env(:elixir, :ansi_enabled, true)", "-S", "mix", "check"], - cd: project_dir, - stderr_to_stdout: true - ) + output = + System.cmd( + "elixir", + ["-e", "Application.put_env(:elixir, :ansi_enabled, true)", "-S", "mix", "check"], + cd: project_dir, + stderr_to_stdout: true + ) + |> cmd_exit(0) plain_output = String.replace(output, @ansi_code_regex, "") diff --git a/test/ex_check/project_cases/deps_test.exs b/test/ex_check/project_cases/deps_test.exs index 991cbae..95d455f 100644 --- a/test/ex_check/project_cases/deps_test.exs +++ b/test/ex_check/project_cases/deps_test.exs @@ -42,7 +42,7 @@ defmodule ExCheck.ProjectCases.DepsTest do config_path = Path.join(project_dir, ".check.exs") File.write!(config_path, @config) - assert {output, 1} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(1) assert output =~ "a success" assert output =~ "b success" @@ -63,7 +63,7 @@ defmodule ExCheck.ProjectCases.DepsTest do config_path = Path.join(project_dir, ".check.exs") File.write!(config_path, @config) - assert {output, 1} = System.cmd("mix", ~w[check --no-parallel], cd: project_dir) + output = System.cmd("mix", ~w[check --no-parallel], cd: project_dir) |> cmd_exit(1) assert output =~ "a success" assert output =~ "b success" diff --git a/test/ex_check/project_cases/detect_test.exs b/test/ex_check/project_cases/detect_test.exs index b588321..5f3e668 100644 --- a/test/ex_check/project_cases/detect_test.exs +++ b/test/ex_check/project_cases/detect_test.exs @@ -15,7 +15,7 @@ defmodule ExCheck.ProjectCases.DetectTest do config_path = Path.join(project_dir, ".check.exs") File.write!(config_path, @config) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(0) assert output =~ "credo skipped due to missing package credo" assert output =~ "bad_dir skipped due to missing directory bad_directory" diff --git a/test/ex_check/project_cases/external_tools_test.exs b/test/ex_check/project_cases/external_tools_test.exs index f934b8c..3fcde9a 100644 --- a/test/ex_check/project_cases/external_tools_test.exs +++ b/test/ex_check/project_cases/external_tools_test.exs @@ -11,7 +11,8 @@ defmodule ExCheck.ProjectCases.ExternalToolsTest do set_mix_deps(project_dir, tools) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_dir, env: %{"MIX_ENV" => "dev"}) + output = + System.cmd("mix", ~w[check], cd: project_dir, env: %{"MIX_ENV" => "dev"}) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/project_cases/formatter_issue_test.exs b/test/ex_check/project_cases/formatter_issue_test.exs index 4743b9e..d741fbf 100644 --- a/test/ex_check/project_cases/formatter_issue_test.exs +++ b/test/ex_check/project_cases/formatter_issue_test.exs @@ -9,7 +9,7 @@ defmodule ExCheck.ProjectCases.FormatterIssueTest do File.write!(invalid_file_path, "IO.inspect( 1 )") - assert {output, 1} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(1) assert output =~ "compiler success" assert output =~ "formatter error code 1" diff --git a/test/ex_check/project_cases/gen_config_test.exs b/test/ex_check/project_cases/gen_config_test.exs index 3d0d1b0..2e4f63d 100644 --- a/test/ex_check/project_cases/gen_config_test.exs +++ b/test/ex_check/project_cases/gen_config_test.exs @@ -4,15 +4,15 @@ defmodule ExCheck.ProjectCases.GenConfigTest do test "gen config", %{project_dir: project_dir} do File.rm!(Path.join(project_dir, ".check.exs")) - assert {output, 0} = System.cmd("mix", ~w[check.gen.config], cd: project_dir) + output = System.cmd("mix", ~w[check.gen.config], cd: project_dir) |> cmd_exit(0) assert output =~ "creating .check.exs" - assert {output, 0} = System.cmd("mix", ~w[check.gen.config], cd: project_dir) + output = System.cmd("mix", ~w[check.gen.config], cd: project_dir) |> cmd_exit(0) assert output =~ ".check.exs already exists, skipped" - assert {output, 0} = System.cmd("mix", ~w[check --no-fix], cd: project_dir) + output = System.cmd("mix", ~w[check --no-fix], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/project_cases/manifest_test.exs b/test/ex_check/project_cases/manifest_test.exs index 473ad18..3372220 100644 --- a/test/ex_check/project_cases/manifest_test.exs +++ b/test/ex_check/project_cases/manifest_test.exs @@ -9,7 +9,7 @@ defmodule ExCheck.ProjectCases.ManifestTest do File.write!(invalid_file_path, "IO.inspect( 1 )") - assert {output, 1} = System.cmd("mix", ~w[check --manifest manifest.txt], cd: project_dir) + output = System.cmd("mix", ~w[check --manifest manifest.txt], cd: project_dir) |> cmd_exit(1) assert output =~ "compiler success" assert output =~ "formatter error code 1" @@ -47,7 +47,7 @@ defmodule ExCheck.ProjectCases.ManifestTest do assert manifest |> String.split("\n") |> Enum.sort() == expected_manifest - assert {output, 1} = System.cmd("mix", ~w[check --manifest manifest.txt], cd: project_dir) + output = System.cmd("mix", ~w[check --manifest manifest.txt], cd: project_dir) |> cmd_exit(1) assert output =~ "retrying automatically" assert output =~ "compiler success" @@ -59,8 +59,8 @@ defmodule ExCheck.ProjectCases.ManifestTest do refute output =~ "ex_doc skipped due to missing package ex_doc" refute output =~ "mix_audit skipped due to missing package mix_audit" - assert {output, 1} = - System.cmd("mix", ~w[check --manifest manifest.txt --retry], cd: project_dir) + output = + System.cmd("mix", ~w[check --manifest manifest.txt --retry], cd: project_dir) |> cmd_exit(1) refute output =~ "retrying automatically" assert output =~ "compiler success" @@ -72,8 +72,9 @@ defmodule ExCheck.ProjectCases.ManifestTest do refute output =~ "ex_doc skipped due to missing package ex_doc" refute output =~ "mix_audit skipped due to missing package mix_audit" - assert {output, 1} = - System.cmd("mix", ~w[check --manifest manifest.txt --no-retry], cd: project_dir) + output = + System.cmd("mix", ~w[check --manifest manifest.txt --no-retry], cd: project_dir) + |> cmd_exit(1) refute output =~ "retrying automatically" assert output =~ "compiler success" @@ -85,8 +86,9 @@ defmodule ExCheck.ProjectCases.ManifestTest do assert output =~ "ex_doc skipped due to missing package ex_doc" assert output =~ "mix_audit skipped due to missing package mix_audit" - assert {output, 0} = - System.cmd("mix", ~w[check --manifest manifest.txt --retry --fix], cd: project_dir) + output = + System.cmd("mix", ~w[check --manifest manifest.txt --retry --fix], cd: project_dir) + |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter fix success" @@ -97,8 +99,8 @@ defmodule ExCheck.ProjectCases.ManifestTest do refute output =~ "ex_doc skipped due to missing package ex_doc" refute output =~ "mix_audit skipped due to missing package mix_audit" - assert {output, 0} = - System.cmd("mix", ~w[check --manifest manifest.txt --retry], cd: project_dir) + output = + System.cmd("mix", ~w[check --manifest manifest.txt --retry], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" refute output =~ "formatter success" @@ -124,14 +126,14 @@ defmodule ExCheck.ProjectCases.ManifestTest do end """) - assert {output, 1} = - System.cmd("mix", ~w[check --only ex_unit --only formatter], cd: project_dir) + output = + System.cmd("mix", ~w[check --only ex_unit --only formatter], cd: project_dir) |> cmd_exit(1) assert output =~ "formatter success" assert output =~ "ex_unit error code" assert output =~ "2 tests, 1 failure" - assert {output, 1} = System.cmd("mix", ~w[check --retry], cd: project_dir) + output = System.cmd("mix", ~w[check --retry], cd: project_dir) |> cmd_exit(1) refute output =~ "formatter" assert output =~ "ex_unit error code" @@ -142,7 +144,7 @@ defmodule ExCheck.ProjectCases.ManifestTest do File.read!(failing_test_path) |> String.replace(":universe", ":world") ) - assert {output, 0} = System.cmd("mix", ~w[check --retry], cd: project_dir) + output = System.cmd("mix", ~w[check --retry], cd: project_dir) |> cmd_exit(0) refute output =~ "formatter" assert output =~ "ex_unit retry success" diff --git a/test/ex_check/project_cases/missing_test_helper_test.exs b/test/ex_check/project_cases/missing_test_helper_test.exs index 9d10b26..dbcd8f5 100644 --- a/test/ex_check/project_cases/missing_test_helper_test.exs +++ b/test/ex_check/project_cases/missing_test_helper_test.exs @@ -5,7 +5,7 @@ defmodule ExCheck.ProjectCases.MissingTestHelperTest do test_dir_path = Path.join(project_dir, "test") File.rm_rf!(test_dir_path) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_dir) + output = System.cmd("mix", ~w[check], cd: project_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/umbrella_project_cases/asset_testing_test.exs b/test/ex_check/umbrella_project_cases/asset_testing_test.exs index 160ae20..5caa7fe 100644 --- a/test/ex_check/umbrella_project_cases/asset_testing_test.exs +++ b/test/ex_check/umbrella_project_cases/asset_testing_test.exs @@ -14,8 +14,8 @@ defmodule ExCheck.UmbrellaProjectCases.AssetTestingTest do File.mkdir_p!(Path.dirname(package_json_path)) File.write!(package_json_path, @package_json) - assert {_, 0} = System.cmd("mix", ~w[compile], cd: project_root_dir) - assert {output, 1} = System.cmd("mix", ~w[check], cd: project_root_dir) + System.cmd("mix", ~w[compile], cd: project_root_dir) |> cmd_exit(0) + output = System.cmd("mix", ~w[check], cd: project_root_dir) |> cmd_exit(1) assert output =~ "npm_test in child_a error code 1" refute output =~ "npm_test in child_b" diff --git a/test/ex_check/umbrella_project_cases/base_test.exs b/test/ex_check/umbrella_project_cases/base_test.exs index b318e1b..d5396d9 100644 --- a/test/ex_check/umbrella_project_cases/base_test.exs +++ b/test/ex_check/umbrella_project_cases/base_test.exs @@ -2,8 +2,8 @@ defmodule ExCheck.UmbrellaProjectCases.BaseTest do use ExCheck.UmbrellaProjectCase, async: true test "base", %{project_dirs: [project_root_dir | _]} do - assert {_, 0} = System.cmd("mix", ~w[compile], cd: project_root_dir) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_root_dir) + System.cmd("mix", ~w[compile], cd: project_root_dir) |> cmd_exit(0) + output = System.cmd("mix", ~w[check], cd: project_root_dir) |> cmd_exit(0) assert output =~ "compiler success" assert output =~ "formatter success" diff --git a/test/ex_check/umbrella_project_cases/non_recursive_test.exs b/test/ex_check/umbrella_project_cases/non_recursive_test.exs index 0150b87..c0c0ed0 100644 --- a/test/ex_check/umbrella_project_cases/non_recursive_test.exs +++ b/test/ex_check/umbrella_project_cases/non_recursive_test.exs @@ -13,8 +13,8 @@ defmodule ExCheck.UmbrellaProjectCases.NonRecursiveTest do config_path = Path.join(project_root_dir, ".check.exs") File.write!(config_path, @config) - assert {_, 0} = System.cmd("mix", ~w[compile], cd: project_root_dir) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_root_dir) + System.cmd("mix", ~w[compile], cd: project_root_dir) |> cmd_exit(0) + output = System.cmd("mix", ~w[check], cd: project_root_dir) |> cmd_exit(0) assert output =~ "ex_unit success" refute output =~ "ex_unit in child_a success" diff --git a/test/ex_check/umbrella_project_cases/only_specific_apps_test.exs b/test/ex_check/umbrella_project_cases/only_specific_apps_test.exs index bc8867c..04d6048 100644 --- a/test/ex_check/umbrella_project_cases/only_specific_apps_test.exs +++ b/test/ex_check/umbrella_project_cases/only_specific_apps_test.exs @@ -13,18 +13,18 @@ defmodule ExCheck.UmbrellaProjectCases.OnlySpecificAppsTest do config_path = Path.join(project_root_dir, ".check.exs") File.write!(config_path, @config) - assert {_, 0} = System.cmd("mix", ~w[compile], cd: project_root_dir) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_root_dir) + System.cmd("mix", ~w[compile], cd: project_root_dir) |> cmd_exit(0) + output = System.cmd("mix", ~w[check], cd: project_root_dir) |> cmd_exit(0) refute output =~ "ex_unit success" assert output =~ "ex_unit in child_a success" refute output =~ "ex_unit in child_b success" - assert {output, 0} = System.cmd("mix", ~w[check], cd: child_a_dir) + output = System.cmd("mix", ~w[check], cd: child_a_dir) |> cmd_exit(0) assert output =~ "ex_unit success" - assert {output, 0} = System.cmd("mix", ~w[check], cd: child_b_dir) + output = System.cmd("mix", ~w[check], cd: child_b_dir) |> cmd_exit(0) refute output =~ "ex_unit" end diff --git a/test/ex_check/umbrella_project_cases/sequential_test.exs b/test/ex_check/umbrella_project_cases/sequential_test.exs index 797620e..4867d39 100644 --- a/test/ex_check/umbrella_project_cases/sequential_test.exs +++ b/test/ex_check/umbrella_project_cases/sequential_test.exs @@ -1,35 +1,42 @@ defmodule ExCheck.UmbrellaProjectCases.SequentialTest do use ExCheck.UmbrellaProjectCase, async: true - @a_script """ - Process.sleep(1_000) - File.write!(Path.join("..", "a_out"), "a_out") - """ - - @b_script """ - IO.puts(File.read!(Path.join("..", "a_out"))) - """ - - @config """ - [ - tools: [ - {:seq, "elixir script.exs", umbrella: [parallel: false]}, + test "sequential", %{project_dirs: [project_root_dir, child_a_dir, child_b_dir]} do + script_path = Path.join(project_root_dir, "a_out") + + a_script = """ + IO.inspect({:a1, DateTime.utc_now()}) + Process.sleep(1_000) + File.write!("#{script_path}", "a_out") + IO.inspect({:a2, DateTime.utc_now()}) + """ + + b_script = """ + IO.inspect({:b1, DateTime.utc_now()}) + Process.sleep(1_000) + IO.puts(File.read!("#{script_path}")) + IO.inspect({:b2, DateTime.utc_now()}) + """ + + config = """ + [ + tools: [ + {:seq, "elixir script.exs", umbrella: [parallel: false]}, + ] ] - ] - """ + """ - test "sequential", %{project_dirs: [project_root_dir, child_a_dir, child_b_dir]} do config_path = Path.join(project_root_dir, ".check.exs") - File.write!(config_path, @config) + File.write!(config_path, config) child_a_script_path = Path.join(child_a_dir, "script.exs") - File.write!(child_a_script_path, @a_script) + File.write!(child_a_script_path, a_script) child_b_script_path = Path.join(child_b_dir, "script.exs") - File.write!(child_b_script_path, @b_script) + File.write!(child_b_script_path, b_script) - assert {_, 0} = System.cmd("mix", ~w[compile], cd: project_root_dir) - assert {output, 0} = System.cmd("mix", ~w[check], cd: project_root_dir) + System.cmd("mix", ~w[compile], cd: project_root_dir) |> cmd_exit(0) + output = System.cmd("mix", ~w[check], cd: project_root_dir) |> cmd_exit(0) assert output =~ "seq in child_a success" assert output =~ "seq in child_b success" diff --git a/test/support/ex_check/case_helpers.ex b/test/support/ex_check/case_helpers.ex new file mode 100644 index 0000000..652d90f --- /dev/null +++ b/test/support/ex_check/case_helpers.ex @@ -0,0 +1,101 @@ +defmodule ExCheck.CaseHelpers do + @moduledoc false + + use ExUnit.CaseTemplate + + @default_config """ + [ + fix: false + ] + """ + + def cmd_exit({output, code}, expected_code) do + assert code == expected_code, + "unexpected exit code #{code} (expected #{expected_code}):\n\n#{output}" + + output + end + + def create_tmp_directory do + timestamp = DateTime.utc_now() |> DateTime.to_unix(:microsecond) + unique_id = :crypto.strong_rand_bytes(12) |> Base.url_encode64() + + tmp_dir = + System.tmp_dir!() + |> Path.join("ex_check_test") + |> Path.join("#{timestamp}-#{unique_id}") + + File.mkdir_p!(tmp_dir) + + tmp_dir + end + + def remove_tmp_directory(tmp_dir) do + File.rm_rf!(tmp_dir) + end + + def set_mix_deps(project_dirs, deps) when is_list(project_dirs) do + Enum.map(project_dirs, &set_mix_deps(&1, deps)) + end + + def set_mix_deps(project_dir, deps) do + config_path = "#{project_dir}/mix.exs" + deps_from = ~r/ *defp deps.*end\n/Us + + deps_list = + Enum.map(deps, fn + :ex_check -> + "{:ex_check, path: \"#{File.cwd!()}\", only: [:dev, :test], runtime: false}" + + dep -> + "{:#{dep}, \">= 0.0.0\", only: :dev, runtime: false}" + end) + + deps_to = """ + defp deps do + [ + #{Enum.join(deps_list, ",\n ")} + ] + end + """ + + new_config = + config_path + |> File.read!() + |> String.replace(deps_from, deps_to) + + unless String.contains?(new_config, "ex_check"), do: raise("unable to add ex_check dep") + + File.write!(config_path, new_config) + {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) + {_, 0} = System.cmd("mix", ~w[deps.get], cd: project_dir) + end + + def set_mix_app_mod(project_dir, mod) do + config_path = "#{project_dir}/mix.exs" + app_from = ~r/ *def application.*end\n/Us + + app_to = """ + def application do + [ + mod: {#{mod}, []} + ] + end + """ + + new_config = + config_path + |> File.read!() + |> String.replace(app_from, app_to) + + unless String.contains?(new_config, mod), do: raise("unable to set #{mod} app mod") + + File.write!(config_path, new_config) + {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) + end + + def write_default_config(project_dir) do + config_path = Path.join(project_dir, ".check.exs") + File.write!(config_path, @default_config) + end +end diff --git a/test/support/ex_check/project_case.ex b/test/support/ex_check/project_case.ex index b46e081..2c189a3 100644 --- a/test/support/ex_check/project_case.ex +++ b/test/support/ex_check/project_case.ex @@ -3,14 +3,9 @@ defmodule ExCheck.ProjectCase do use ExUnit.CaseTemplate - @default_config """ - [ - fix: false - ] - """ - using do quote do + import ExCheck.CaseHelpers import ExCheck.ProjectCase @moduletag timeout: 5 * 60 * 1_000 @@ -28,88 +23,9 @@ defmodule ExCheck.ProjectCase do end end - def create_tmp_directory do - timestamp = DateTime.utc_now() |> DateTime.to_unix(:microsecond) - unique_id = :crypto.strong_rand_bytes(12) |> Base.url_encode64() - - tmp_dir = - System.tmp_dir!() - |> Path.join("ex_check_test") - |> Path.join("#{timestamp}-#{unique_id}") - - File.mkdir_p!(tmp_dir) - - tmp_dir - end - - def remove_tmp_directory(tmp_dir) do - File.rm_rf!(tmp_dir) - end - def create_mix_project(root_dir) do System.cmd("mix", ~w(new test_project), cd: root_dir) Path.join(root_dir, "test_project") end - - def set_mix_deps(project_dir, deps) do - config_path = "#{project_dir}/mix.exs" - deps_from = ~r/ *defp deps.*end\n/Us - - deps_list = - Enum.map(deps, fn - :ex_check -> - "{:ex_check, path: \"#{File.cwd!()}\", only: [:dev, :test], runtime: false}" - - dep -> - "{:#{dep}, \">= 0.0.0\", only: :dev, runtime: false}" - end) - - deps_to = """ - defp deps do - [ - #{Enum.join(deps_list, ",\n ")} - ] - end - """ - - new_config = - config_path - |> File.read!() - |> String.replace(deps_from, deps_to) - - unless String.contains?(new_config, "ex_check"), do: raise("unable to add ex_check dep") - - File.write!(config_path, new_config) - {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) - {_, 0} = System.cmd("mix", ~w[deps.get], cd: project_dir) - end - - def set_mix_app_mod(project_dir, mod) do - config_path = "#{project_dir}/mix.exs" - app_from = ~r/ *def application.*end\n/Us - - app_to = """ - def application do - [ - mod: {#{mod}, []} - ] - end - """ - - new_config = - config_path - |> File.read!() - |> String.replace(app_from, app_to) - - unless String.contains?(new_config, mod), do: raise("unable to set #{mod} app mod") - - File.write!(config_path, new_config) - {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) - end - - def write_default_config(project_dir) do - config_path = Path.join(project_dir, ".check.exs") - File.write!(config_path, @default_config) - end end diff --git a/test/support/ex_check/umbrella_project_case.ex b/test/support/ex_check/umbrella_project_case.ex index 18afd03..f662ceb 100644 --- a/test/support/ex_check/umbrella_project_case.ex +++ b/test/support/ex_check/umbrella_project_case.ex @@ -3,14 +3,9 @@ defmodule ExCheck.UmbrellaProjectCase do use ExUnit.CaseTemplate - @default_config """ - [ - fix: false - ] - """ - using do quote do + import ExCheck.CaseHelpers import ExCheck.UmbrellaProjectCase @moduletag timeout: 5 * 60 * 1_000 @@ -32,24 +27,6 @@ defmodule ExCheck.UmbrellaProjectCase do end end - def create_tmp_directory do - timestamp = DateTime.utc_now() |> DateTime.to_unix(:microsecond) - unique_id = :crypto.strong_rand_bytes(12) |> Base.url_encode64() - - tmp_dir = - System.tmp_dir!() - |> Path.join("ex_check_test") - |> Path.join("#{timestamp}-#{unique_id}") - - File.mkdir_p!(tmp_dir) - - tmp_dir - end - - def remove_tmp_directory(tmp_dir) do - File.rm_rf!(tmp_dir) - end - def create_mix_project(root_dir, opts \\ []) do name = Keyword.get(opts, :name, "test_project") umbrella = Keyword.get(opts, :umbrella, false) @@ -59,69 +36,4 @@ defmodule ExCheck.UmbrellaProjectCase do Path.join(root_dir, name) end - - def set_mix_deps(project_dirs, deps) when is_list(project_dirs) do - Enum.map(project_dirs, &set_mix_deps(&1, deps)) - end - - def set_mix_deps(project_dir, deps) do - config_path = "#{project_dir}/mix.exs" - deps_from = ~r/ *defp deps.*end\n/Us - - deps_list = - Enum.map(deps, fn - :ex_check -> - "{:ex_check, path: \"#{File.cwd!()}\", only: [:dev, :test], runtime: false}" - - dep -> - "{:#{dep}, \">= 0.0.0\", only: :dev, runtime: false}" - end) - - deps_to = """ - defp deps do - [ - #{Enum.join(deps_list, ",\n ")} - ] - end - """ - - new_config = - config_path - |> File.read!() - |> String.replace(deps_from, deps_to) - - unless String.contains?(new_config, "ex_check"), do: raise("unable to add ex_check dep") - - File.write!(config_path, new_config) - {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) - {_, 0} = System.cmd("mix", ~w[deps.get], cd: project_dir) - end - - def set_mix_app_mod(project_dir, mod) do - config_path = "#{project_dir}/mix.exs" - app_from = ~r/ *def application.*end\n/Us - - app_to = """ - def application do - [ - mod: {#{mod}, []} - ] - end - """ - - new_config = - config_path - |> File.read!() - |> String.replace(app_from, app_to) - - unless String.contains?(new_config, mod), do: raise("unable to set #{mod} app mod") - - File.write!(config_path, new_config) - {_, 0} = System.cmd("mix", ~w[format], cd: project_dir) - end - - def write_default_config(project_dir) do - config_path = Path.join(project_dir, ".check.exs") - File.write!(config_path, @default_config) - end end