Makefile 11 KB

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