Makefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  2. # Copyright (c) 2014, Viktor Söderqvist <viktor@zuiderkwast.se>
  3. # This file is part of erlang.mk and subject to the terms of the ISC License.
  4. # Temporary application name, taken from rule name.
  5. APP = $(subst -,_,$@)
  6. APP_TO_CLEAN = $(subst -,_,$(patsubst clean-%,%,$@))
  7. # Erlang, quickly!
  8. ERL = erl +A0 -noinput -boot start_clean
  9. # OTP master, for downloading files for testing.
  10. OTP_MASTER = https://raw.githubusercontent.com/erlang/otp/master
  11. # Verbosity.
  12. #
  13. # V=0: Show info messages only.
  14. # V=1: Show test commands.
  15. # V=2: Also show normal Erlang.mk output.
  16. # V=3: Also show verbose Erlang.mk output.
  17. V ?= 0
  18. # t: Verbosity control for tests.
  19. # v: Verbosity control for erlang.mk.
  20. # i: Command to display (or suppress) info messages.
  21. ifeq ($V,0)
  22. t = @
  23. v = V=0 >/dev/null 2>&1
  24. i = @echo $@:
  25. else ifeq ($V,1)
  26. t =
  27. v = V=0 >/dev/null 2>&1
  28. i = @echo == $@:
  29. else ifeq ($V,2)
  30. t = @echo " TEST " $@;
  31. v = V=0
  32. i = @echo == $@:
  33. else
  34. t =
  35. v = V=1
  36. i = @echo == $@:
  37. endif
  38. # Main targets.
  39. .PHONY: all clean build
  40. all:: core
  41. $i "Success!"
  42. clean:: clean-core
  43. build:
  44. $i "Generate a bleeding edge Erlang.mk"
  45. $t cd .. && $(MAKE) $v
  46. # Core.
  47. .PHONY: core clean-core
  48. define include_core
  49. core:: core-$1
  50. clean-core:: clean-core-$1
  51. include core_$1.mk
  52. endef
  53. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  54. # Plugins.
  55. define include_plugin
  56. all:: $1
  57. clean:: clean-$1
  58. include plugin_$1.mk
  59. endef
  60. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  61. # Tests that don't easily fit into other categories.
  62. core:: core-clean-crash-dump core-help
  63. clean-core:: clean-core-clean-crash-dump clean-core-help
  64. .PHONY: core-clean-crash-dump core-help clean-core-clean-crash-dump clean-core-help
  65. clean-core-clean-crash-dump clean-core-help:
  66. $t rm -rf $(APP_TO_CLEAN)/
  67. core-clean-crash-dump: build clean-core-clean-crash-dump
  68. $i "Bootstrap a new OTP library named $(APP)"
  69. $t mkdir $(APP)/
  70. $t cp ../erlang.mk $(APP)/
  71. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  72. $i "Create a fake erl_crash.dump file"
  73. $t touch $(APP)/erl_crash.dump
  74. $i "Clean the application"
  75. $t $(MAKE) -C $(APP) clean $v
  76. $i "Check that the crash dump is removed"
  77. $t test ! -e $(APP)/erl_crash.dump
  78. core-help: build clean-core-help
  79. $i "Bootstrap a new OTP library named $(APP)"
  80. $t mkdir $(APP)/
  81. $t cp ../erlang.mk $(APP)/
  82. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  83. $i "Run 'make help' and check that it prints help"
  84. $t test -n "`$(MAKE) -C $(APP) help` | grep Usage"
  85. # Legacy tests.
  86. #
  87. # The following tests are slowly being converted.
  88. # Do NOT use -j with legacy tests.
  89. .PHONY: legacy clean-legacy ct eunit tests-cover docs
  90. legacy: clean-legacy ct eunit tests-cover docs pkgs
  91. clean-legacy:
  92. $t rm -rf app1 pkgs.log
  93. ct: app1
  94. $i "ct: Testing ct and related targets."
  95. $i "Setting up test suite."
  96. $t mkdir -p app1/test
  97. $t printf "%s\n" \
  98. "-module(m_SUITE)." \
  99. "-export([all/0, testcase1/1])." \
  100. "all() -> [testcase1]." \
  101. "testcase1(_) -> 2 = m:succ(1)." \
  102. > app1/test/m_SUITE.erl
  103. $t $(MAKE) -C app1 ct $v
  104. $i "Checking files created by '$(MAKE) ct'."
  105. $t [ -e app1/test/m_SUITE.beam ]
  106. $t [ -e app1/ebin/m.beam ]
  107. $t [ -e app1/logs ]
  108. $i "Checking that '$(MAKE) clean' does not delete logs."
  109. $t $(MAKE) -C app1 clean $v
  110. $t [ -e app1/logs ]
  111. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  112. $t $(MAKE) -C app1 ct-m $v
  113. $i "Checking that '$(MAKE) ct' returns non-zero for a failing suite."
  114. $t printf "%s\n" \
  115. "-module(failing_SUITE)." \
  116. "-export([all/0, testcase1/1])." \
  117. "all() -> [testcase1]." \
  118. "testcase1(_) -> 42 = m:succ(1)." \
  119. > app1/test/failing_SUITE.erl
  120. $t if $(MAKE) -C app1 ct-failing $v ; then false ; fi
  121. $i "Checking that '$(MAKE) distclean-ct' deletes logs."
  122. $t $(MAKE) -C app1 distclean-ct $v
  123. $t [ ! -e app1/logs ]
  124. $t [ -e app1/ebin/m.beam ]
  125. $i "Cleaning up test data."
  126. $t rm -rf app1/test
  127. $i "Test 'ct' passed."
  128. eunit: app1
  129. $i "eunit: Testing the 'eunit' target."
  130. $i "Running eunit test case inside module src/t.erl"
  131. $t $(call create-module-t)
  132. $t $(MAKE) -C app1 distclean $v
  133. $t $(MAKE) -C app1 eunit $v
  134. $i "Checking that the eunit test in module t."
  135. $t echo t | cmp app1/test-eunit.log -
  136. $t rm app1/test-eunit.log
  137. $i "Running eunit tests in a separate directory."
  138. $t mkdir -p app1/eunit
  139. $t printf '%s\n' \
  140. '-module(t_tests).' \
  141. '-include_lib("eunit/include/eunit.hrl").' \
  142. 'succ_test() ->' \
  143. ' ?assertEqual(2, t:succ(1)),' \
  144. ' os:cmd("echo t_tests >> test-eunit.log").' \
  145. > app1/eunit/t_tests.erl
  146. $t printf '%s\n' \
  147. '-module(x_tests).' \
  148. '-include_lib("eunit/include/eunit.hrl").' \
  149. 'succ_test() ->' \
  150. ' ?assertEqual(2, t:succ(1)),' \
  151. ' os:cmd("echo x_tests >> test-eunit.log").' \
  152. > app1/eunit/x_tests.erl
  153. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  154. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  155. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  156. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  157. $t rm app1/test-eunit.log
  158. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  159. $t rm -f app1/eunit/*
  160. $t printf "%s\n" \
  161. "-module(t_tests)." \
  162. '-include_lib("eunit/include/eunit.hrl").' \
  163. "succ_test() ->" \
  164. " ?assertEqual(42, t:succ(1))." \
  165. > app1/eunit/t_tests.erl
  166. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  167. $t if $(MAKE) -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  168. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  169. $i "Test 'eunit' passed."
  170. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  171. tests-cover: app1
  172. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  173. $i "Setting up eunit and ct suites."
  174. $t $(call create-module-t)
  175. $t mkdir -p app1/test
  176. $t printf "%s\n" \
  177. "-module(m_SUITE)." \
  178. "-export([all/0, testcase1/1])." \
  179. "all() -> [testcase1]." \
  180. "testcase1(_) -> 2 = m:succ(1)." \
  181. > app1/test/m_SUITE.erl
  182. $i "Running tests with coverage analysis."
  183. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  184. $t [ -e app1/test-eunit.log ]
  185. $t [ -e app1/eunit.coverdata ]
  186. $t [ -e app1/ct.coverdata ]
  187. $i "Generating coverage report."
  188. $t $(MAKE) -C app1 cover-report COVER=1 $v
  189. $t [ -e app1/cover/m.COVER.html ]
  190. $t [ -e app1/cover/t.COVER.html ]
  191. $t [ -e app1/cover/index.html ]
  192. $i "Checking combined coverage from eunit and ct."
  193. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  194. $i "Checking that cover-report-clean removes cover report."
  195. $t $(MAKE) -C app1 cover-report-clean $v
  196. $t [ ! -e app1/cover ]
  197. $i "Checking that coverdata-clean removes cover data."
  198. $t $(MAKE) -C app1 coverdata-clean $v
  199. $t [ ! -e app1/eunit.coverdata ]
  200. @# clean up
  201. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  202. $t $(MAKE) -C app1 clean $v
  203. $i "Test 'tests-cover' passed."
  204. docs: app1
  205. $i "docs: Testing EDoc including DOC_DEPS."
  206. $t printf "%s\n" \
  207. "PROJECT = app1" \
  208. "DOC_DEPS = edown" \
  209. "dep_edown = git https://github.com/uwiger/edown.git 0.7" \
  210. "EDOC_OPTS = {doclet, edown_doclet}" \
  211. "include erlang.mk" \
  212. "distclean:: distclean-doc-md" \
  213. "distclean-doc-md:" \
  214. " rm -rf doc/*.md" \
  215. > app1/Makefile-doc
  216. $i "Downloading doc deps (edown) and building docs."
  217. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  218. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  219. $t [ -e app1/doc/m.md ]
  220. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  221. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  222. $t [ "`ls app1/doc/`" = "" ]
  223. $i "Cleaning up test data."
  224. $t rm app1/Makefile-doc
  225. $i "Test 'docs' passed."
  226. define app1_setup
  227. $i "Setting up app."
  228. $t mkdir -p app1
  229. $t cd .. && $(MAKE)
  230. $t cp ../erlang.mk app1/
  231. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  232. $t printf "%s\n" \
  233. "-module(m)." \
  234. "-export([succ/1])." \
  235. "succ(N) -> N + 1." \
  236. > app1/src/m.erl
  237. endef
  238. define pkg_test_target
  239. pkg-$(1)-clean:
  240. $t rm -rf app1 erl_crash.dump
  241. pkg-$(1)-app1:
  242. $(call app1_setup)
  243. # Running 'make' twice to make sure it recompiles fine.
  244. pkg-$(1): pkg-$(1)-clean pkg-$(1)-app1
  245. $i
  246. $i " pkgs: Checking that '$(1)' builds correctly"
  247. $i
  248. $t printf "%s\n" \
  249. "PROJECT = app1" \
  250. "DEPS = $(1)" \
  251. "include erlang.mk" \
  252. > app1/Makefile
  253. $t \
  254. if [ "$(1)" = "amqp_client" ]; then \
  255. $(MAKE) -C app1 RABBITMQ_CLIENT_PATCH=1; \
  256. elif [ "$(1)" = "rabbit" ]; then \
  257. $(MAKE) -C app1 RABBITMQ_SERVER_PATCH=1; \
  258. else \
  259. $(MAKE) -C app1; \
  260. fi; \
  261. if [ $$$$? -ne 0 ]; then \
  262. echo "$(1): make error" >> pkgs.log; \
  263. else \
  264. $(MAKE) -C app1; if [ $$$$? -ne 0 ]; then \
  265. echo "$(1): re-make error" >> pkgs.log; \
  266. else \
  267. find . -type f -name erl_crash.dump; if [ $$$$? -ne 0 ]; then \
  268. echo "$(1): erl_crash.dump found" >> pkgs.log; \
  269. else \
  270. erl +A0 -noinput -boot start_clean -pa app1/deps/*/ebin -eval " \
  271. Apps = [list_to_atom(App) || \"app1/deps/\" ++ App \
  272. <- filelib:wildcard(\"app1/deps/*\")], \
  273. [begin \
  274. io:format(\"Loading application ~p~n\", [App]), \
  275. case application:load(App) of \
  276. {error, _} -> ok; \
  277. ok -> \
  278. {ok, Mods} = application:get_key(App, modules), \
  279. [try io:format(\" Loading module ~p~n\", [Mod]), \
  280. {module, Mod} = code:load_file(Mod) \
  281. catch C:R -> timer:sleep(500), erlang:C(R) \
  282. end || Mod <- Mods] \
  283. end \
  284. end || App <- Apps], \
  285. halt()."; if [ $$$$? -ne 0 ]; then \
  286. echo "$(1): load error" >> pkgs.log; \
  287. fi \
  288. fi \
  289. fi \
  290. fi
  291. endef
  292. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  293. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_test_target,$(pkg))))
  294. pkgs: $(addprefix pkg-,$(PACKAGES))
  295. @if [ -f pkgs.log ]; then \
  296. echo "+-------------------------------+"; \
  297. echo "| ERRORS WHILE TESTING PACKAGES |"; \
  298. echo "+-------------------------------+"; \
  299. cat pkgs.log; \
  300. exit 33; \
  301. fi
  302. # Test application used for testing.
  303. app1:
  304. $(call app1_setup)
  305. # Extra module in app1 used for testing eunit
  306. define create-module-t
  307. printf '%s\n' \
  308. '-module(t).' \
  309. '-export([succ/1]).' \
  310. 'succ(N) -> N + 1.' \
  311. '-ifdef(TEST).' \
  312. '-include_lib("eunit/include/eunit.hrl").' \
  313. 'succ_test() ->' \
  314. ' ?assertEqual(2, succ(1)),' \
  315. ' os:cmd("echo t >> test-eunit.log").' \
  316. '-endif.' \
  317. > app1/src/t.erl
  318. endef