eunit.mk 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2014, Enrique Fernandez <enrique.fernandez@erlang-solutions.com>
  2. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  3. .PHONY: help-eunit build-eunit eunit distclean-eunit
  4. # Configuration
  5. EUNIT_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard -DTEST=1 -DEXTRA=1
  6. EUNIT_DIR ?=
  7. EUNIT_DIRS = $(sort $(EUNIT_DIR) ebin)
  8. ifeq ($(strip $(EUNIT_DIR)),)
  9. TAGGED_EUNIT_TESTS = {dir,"ebin"}
  10. else
  11. # All modules in EUNIT_DIR
  12. EUNIT_DIR_MODS = $(notdir $(basename $(shell find $(EUNIT_DIR) -type f -name *.beam)))
  13. # All modules in 'ebin'
  14. EUNIT_EBIN_MODS = $(notdir $(basename $(shell find ebin -type f -name *.beam)))
  15. # Only those modules in EUNIT_DIR with no matching module in 'ebin'.
  16. # This is done to avoid some tests being executed twice.
  17. EUNIT_MODS = $(filter-out $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(EUNIT_DIR_MODS))
  18. TAGGED_EUNIT_TESTS = {dir,"ebin"} $(foreach mod,$(EUNIT_MODS),$(shell echo $(mod) | sed -e 's/\(.*\)/{module,\1}/g'))
  19. endif
  20. EUNIT_OPTS ?= verbose
  21. # Utility functions
  22. define str-join
  23. $(shell echo '$(strip $(1))' | sed -e "s/ /,/g")
  24. endef
  25. # Core targets.
  26. help:: help-eunit
  27. tests:: eunit
  28. clean:: clean-eunit
  29. # Plugin-specific targets.
  30. EUNIT_RUN = erl \
  31. -no_auto_compile \
  32. -noshell \
  33. -pa $(realpath $(EUNIT_DIR)) $(DEPS_DIR)/*/ebin \
  34. -pz $(realpath ebin) \
  35. -eval 'case eunit:test([$(call str-join,$(TAGGED_EUNIT_TESTS))], [$(EUNIT_OPTS)]) of ok -> erlang:halt(0); error -> erlang:halt(1) end.'
  36. help-eunit:
  37. @printf "%s\n" "" \
  38. "EUnit targets:" \
  39. " eunit Run all the EUnit tests for this project"
  40. ifeq ($(strip $(EUNIT_DIR)),)
  41. build-eunit:
  42. else ifeq ($(strip $(EUNIT_DIR)),ebin)
  43. build-eunit:
  44. else
  45. build-eunit:
  46. $(gen_verbose) erlc -v $(EUNIT_ERLC_OPTS) -I include/ -o $(EUNIT_DIR) \
  47. $(wildcard $(EUNIT_DIR)/*.erl $(EUNIT_DIR)/*/*.erl) -pa ebin/
  48. endif
  49. eunit: ERLC_OPTS = $(EUNIT_ERLC_OPTS)
  50. eunit: clean deps app build-eunit
  51. $(gen_verbose) $(EUNIT_RUN)
  52. clean-eunit:
  53. $(gen_verbose) $(foreach dir,$(EUNIT_DIRS),rm -rf $(dir)/*.beam)