Makefile 9.9 KB

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