emqx.eunit.ex 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. defmodule Mix.Tasks.Emqx.Eunit do
  2. use Mix.Task
  3. # Code.require_file("emqx.ct.ex", __DIR__)
  4. alias Mix.Tasks.Emqx.Ct, as: ECt
  5. # todo: invoke the equivalent of `make merge-config` as a requirement...
  6. @requirements ["compile", "loadpaths"]
  7. @impl true
  8. def run(args) do
  9. Mix.debug(true)
  10. IO.inspect(args)
  11. Enum.each([:common_test, :eunit, :mnesia], &ECt.add_to_path_and_cache/1)
  12. ECt.ensure_whole_emqx_project_is_loaded!()
  13. ECt.unload_emqx_applications!()
  14. {_, 0} = System.cmd("epmd", ["-daemon"])
  15. node_name = :"test@127.0.0.1"
  16. :net_kernel.start([node_name, :longnames])
  17. # unmangle PROFILE env because some places (`:emqx_conf.resolve_schema_module`) expect
  18. # the version without the `-test` suffix.
  19. System.fetch_env!("PROFILE")
  20. |> String.replace_suffix("-test", "")
  21. |> then(& System.put_env("PROFILE", &1))
  22. discover_tests()
  23. |> :eunit.test(
  24. verbose: true,
  25. print_depth: 100
  26. )
  27. |> case do
  28. :ok -> :ok
  29. :error -> Mix.raise("errors found in tests")
  30. end
  31. end
  32. defp add_to_path_and_cache(lib_name) do
  33. :code.lib_dir()
  34. |> Path.join("#{lib_name}-*")
  35. |> Path.wildcard()
  36. |> hd()
  37. |> Path.join("ebin")
  38. |> to_charlist()
  39. |> :code.add_path(:cache)
  40. end
  41. ## TODO: allow filtering modules and test names
  42. defp discover_tests() do
  43. Mix.Dep.Umbrella.cached()
  44. |> Enum.map(& {:application, &1.app})
  45. end
  46. end