Makefile 10 KB

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