eunit.mk 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) 2015-2016, Loïc Hoguin <essen@ninenines.eu>
  2. # Copyright (c) 2014, Enrique Fernandez <enrique.fernandez@erlang-solutions.com>
  3. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  4. .PHONY: eunit apps-eunit
  5. # Configuration
  6. EUNIT_OPTS ?=
  7. EUNIT_ERL_OPTS ?=
  8. # Core targets.
  9. tests:: eunit
  10. help::
  11. $(verbose) printf "%s\n" "" \
  12. "EUnit targets:" \
  13. " eunit Run all the EUnit tests for this project"
  14. # Plugin-specific targets.
  15. define eunit.erl
  16. Enabled = case "$(COVER)" of
  17. "" -> false;
  18. _ ->
  19. case filelib:is_dir("ebin") of
  20. false -> false;
  21. true ->
  22. case cover:compile_beam_directory("ebin") of
  23. {error, _} -> halt(1);
  24. _ -> true
  25. end
  26. end
  27. end,
  28. case eunit:test($1, [$(EUNIT_OPTS)]) of
  29. ok -> ok;
  30. error -> halt(2)
  31. end,
  32. case {Enabled, "$(COVER)"} of
  33. {false, _} -> ok;
  34. {_, ""} -> ok;
  35. _ ->
  36. cover:export("$(COVER_DATA_DIR)/eunit.coverdata")
  37. end,
  38. halt()
  39. endef
  40. EUNIT_ERL_OPTS += -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin $(APPS_DIR)/*/ebin $(CURDIR)/ebin
  41. ifdef t
  42. ifeq (,$(findstring :,$(t)))
  43. eunit: test-build cover-data-dir
  44. $(gen_verbose) $(call erlang,$(call eunit.erl,['$(t)']),$(EUNIT_ERL_OPTS))
  45. else
  46. eunit: test-build cover-data-dir
  47. $(gen_verbose) $(call erlang,$(call eunit.erl,fun $(t)/0),$(EUNIT_ERL_OPTS))
  48. endif
  49. else
  50. EUNIT_EBIN_MODS = $(notdir $(basename $(ERL_FILES) $(BEAM_FILES)))
  51. EUNIT_TEST_MODS = $(notdir $(basename $(call core_find,$(TEST_DIR)/,*.erl)))
  52. EUNIT_MODS = $(foreach mod,$(EUNIT_EBIN_MODS) $(filter-out \
  53. $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(EUNIT_TEST_MODS)),'$(mod)')
  54. eunit: test-build $(if $(IS_APP),,apps-eunit) cover-data-dir
  55. $(gen_verbose) $(call erlang,$(call eunit.erl,[$(call comma_list,$(EUNIT_MODS))]),$(EUNIT_ERL_OPTS))
  56. ifneq ($(ALL_APPS_DIRS),)
  57. apps-eunit:
  58. $(verbose) eunit_retcode=0 ; for app in $(ALL_APPS_DIRS); do $(MAKE) -C $$app eunit IS_APP=1; \
  59. [ $$? -ne 0 ] && eunit_retcode=1 ; done ; \
  60. exit $$eunit_retcode
  61. endif
  62. endif