eunit.mk 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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) $(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)$(ROOT_DIR),,apps-eunit) cover-data-dir
  55. ifneq ($(wildcard src/ $(TEST_DIR)),)
  56. $(gen_verbose) $(call erlang,$(call eunit.erl,[$(call comma_list,$(EUNIT_MODS))]),$(EUNIT_ERL_OPTS))
  57. endif
  58. ifneq ($(ALL_APPS_DIRS),)
  59. apps-eunit: test-build
  60. $(verbose) eunit_retcode=0 ; for app in $(ALL_APPS_DIRS); do $(MAKE) -C $$app eunit IS_APP=1; \
  61. [ $$? -ne 0 ] && eunit_retcode=1 ; done ; \
  62. exit $$eunit_retcode
  63. endif
  64. endif