Makefile 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # Copyright (c) 2014, Viktor Söderqvist <viktor@zuiderkwast.se>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. # Tests for erlang.mk targets. If any test fails or if you run a target other
  4. # than 'all', you must probably do 'make clean' before you can test again.
  5. # Verbosity.
  6. V ?= 0
  7. # Temporary files directory.
  8. ERLANG_MK_TMP=$(CURDIR)/tmp
  9. export ERLANG_MK_TMP
  10. # t = Verbosity control for tests
  11. # v = Verbosity control for erlang.mk
  12. # i = Command to display (or suppress) info messages
  13. ifeq ($V,0)
  14. # Show info messages only
  15. t = @
  16. v = V=0 >/dev/null 2>&1
  17. i = @echo
  18. else ifeq ($V,1)
  19. # Show test commands
  20. t =
  21. v = V=0 >/dev/null 2>&1
  22. i = @echo ==
  23. else ifeq ($V,2)
  24. # Show briefly what erlang.mk is doing
  25. t = @echo " TEST " $@;
  26. v = V=0
  27. i = @echo ==
  28. else
  29. # Show all commands with maximum verbosity
  30. t =
  31. v = V=1
  32. i = @echo ==
  33. endif
  34. .PHONY: all clean app ct eunit tests-cover docs
  35. .NOTPARALLEL:
  36. all: clean app ct eunit tests-cover docs pkgs
  37. $i '+---------------------+'
  38. $i '| All tests passed. |'
  39. $i '+---------------------+'
  40. clean:
  41. $t rm -rf app1 pkgs.log $(ERLANG_MK_TMP)
  42. app: app1
  43. $i "app: Testing the 'app' target."
  44. $t $(MAKE) -C app1 app $v
  45. $i "Checking the modules line in the generated .app file."
  46. $t [ `grep -E "{modules, *\['m'\]}" app1/ebin/app1.app | wc -l` == 1 ]
  47. $t [ -e app1/ebin/m.beam ]
  48. $i "Checking that '$(MAKE) clean-app' deletes ebin."
  49. $t $(MAKE) -C app1 clean-app $v
  50. $t [ ! -e app1/ebin ]
  51. $i "Checking that '$(MAKE) app' returns non-zero on compile errors."
  52. $t printf "%s\n" \
  53. "-module(syntax_error)." \
  54. "foo lorem_ipsum dolor sit amet." \
  55. > app1/src/syntax_error.erl
  56. $t if $(MAKE) -C app1 app $v ; then false ; fi
  57. $t rm app1/src/syntax_error.erl
  58. $i "Test 'app' passed."
  59. ct: app1
  60. $i "ct: Testing ct and related targets."
  61. $i "Setting up test suite."
  62. $t mkdir -p app1/test
  63. $t printf "%s\n" \
  64. "-module(m_SUITE)." \
  65. "-export([all/0, testcase1/1])." \
  66. "all() -> [testcase1]." \
  67. "testcase1(_) -> 2 = m:succ(1)." \
  68. > app1/test/m_SUITE.erl
  69. $t $(MAKE) -C app1 ct $v
  70. $i "Checking files created by '$(MAKE) ct'."
  71. $t [ -e app1/test/m_SUITE.beam ]
  72. $t [ -e app1/ebin/m.beam ]
  73. $t [ -e app1/logs ]
  74. $i "Checking that '$(MAKE) clean' does not delete logs."
  75. $t $(MAKE) -C app1 clean $v
  76. $t [ -e app1/logs ]
  77. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  78. $t $(MAKE) -C app1 ct-m $v
  79. $i "Checking that '$(MAKE) ct' returns non-zero for a failing suite."
  80. $t printf "%s\n" \
  81. "-module(failing_SUITE)." \
  82. "-export([all/0, testcase1/1])." \
  83. "all() -> [testcase1]." \
  84. "testcase1(_) -> 42 = m:succ(1)." \
  85. > app1/test/failing_SUITE.erl
  86. $t if $(MAKE) -C app1 ct-failing $v ; then false ; fi
  87. $i "Checking that '$(MAKE) distclean-ct' deletes logs."
  88. $t $(MAKE) -C app1 distclean-ct $v
  89. $t [ ! -e app1/logs ]
  90. $t [ -e app1/ebin/m.beam ]
  91. $i "Cleaning up test data."
  92. $t rm -rf app1/test
  93. $i "Test 'ct' passed."
  94. eunit: app1
  95. $i "eunit: Testing the 'eunit' target."
  96. $i "Running eunit test case inside module src/t.erl"
  97. $t $(call create-module-t)
  98. $t $(MAKE) -C app1 eunit $v
  99. $i "Checking that the eunit test in module t."
  100. $t echo t | cmp app1/test-eunit.log -
  101. $t rm app1/test-eunit.log
  102. $i "Running eunit tests in a separate directory."
  103. $t mkdir -p app1/eunit
  104. $t printf '%s\n' \
  105. '-module(t_tests).' \
  106. '-include_lib("eunit/include/eunit.hrl").' \
  107. 'succ_test() ->' \
  108. ' ?assertEqual(2, t:succ(1)),' \
  109. ' os:cmd("echo t_tests >> test-eunit.log").' \
  110. > app1/eunit/t_tests.erl
  111. $t printf '%s\n' \
  112. '-module(x_tests).' \
  113. '-include_lib("eunit/include/eunit.hrl").' \
  114. 'succ_test() ->' \
  115. ' ?assertEqual(2, t:succ(1)),' \
  116. ' os:cmd("echo x_tests >> test-eunit.log").' \
  117. > app1/eunit/x_tests.erl
  118. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  119. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  120. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  121. $t rm app1/test-eunit.log
  122. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  123. $t rm -f app1/eunit/*
  124. $t printf "%s\n" \
  125. "-module(t_tests)." \
  126. '-include_lib("eunit/include/eunit.hrl").' \
  127. "succ_test() ->" \
  128. " ?assertEqual(42, t:succ(1))." \
  129. > app1/eunit/t_tests.erl
  130. $t if $(MAKE) -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  131. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  132. $i "Test 'eunit' passed."
  133. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  134. tests-cover: app1
  135. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  136. $i "Setting up eunit and ct suites."
  137. $t $(call create-module-t)
  138. $t mkdir -p app1/test
  139. $t printf "%s\n" \
  140. "-module(m_SUITE)." \
  141. "-export([all/0, testcase1/1])." \
  142. "all() -> [testcase1]." \
  143. "testcase1(_) -> 2 = m:succ(1)." \
  144. > app1/test/m_SUITE.erl
  145. $i "Running tests with coverage analysis."
  146. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  147. $t [ -e app1/test-eunit.log ]
  148. $t [ -e app1/eunit.coverdata ]
  149. $t [ -e app1/ct.coverdata ]
  150. $i "Generating coverage report."
  151. $t $(MAKE) -C app1 cover-report COVER=1 $v
  152. $t [ -e app1/cover/m.COVER.html ]
  153. $t [ -e app1/cover/t.COVER.html ]
  154. $t [ -e app1/cover/index.html ]
  155. $i "Checking combined coverage from eunit and ct."
  156. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  157. $i "Checking that cover-report-clean removes cover report."
  158. $t $(MAKE) -C app1 cover-report-clean $v
  159. $t [ ! -e app1/cover ]
  160. $i "Checking that coverdata-clean removes cover data."
  161. $t $(MAKE) -C app1 coverdata-clean $v
  162. $t [ ! -e app1/eunit.coverdata ]
  163. @# clean up
  164. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  165. $t $(MAKE) -C app1 clean $v
  166. $i "Test 'tests-cover' passed."
  167. docs: app1
  168. $i "docs: Testing EDoc including DOC_DEPS."
  169. $t printf "%s\n" \
  170. "PROJECT = app1" \
  171. "DOC_DEPS = edown" \
  172. "dep_edown = git https://github.com/uwiger/edown.git 0.5" \
  173. "EDOC_OPTS = {doclet, edown_doclet}" \
  174. "include erlang.mk" \
  175. "distclean:: distclean-doc-md" \
  176. "distclean-doc-md:" \
  177. " rm -rf doc/*.md" \
  178. > app1/Makefile-doc
  179. $i "Downloading doc deps (edown) and building docs."
  180. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  181. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  182. $t [ -e app1/doc/m.md ]
  183. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  184. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  185. $t [ "`ls app1/doc/`" == "" ]
  186. $i "Cleaning up test data."
  187. $t rm app1/Makefile-doc
  188. $i "Test 'docs' passed."
  189. define app1_setup
  190. $i "Setting up app."
  191. $t mkdir -p app1
  192. $t cd .. && $(MAKE)
  193. $t cp ../erlang.mk app1/
  194. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  195. $t printf "%s\n" \
  196. "-module(m)." \
  197. "-export([succ/1])." \
  198. "succ(N) -> N + 1." \
  199. > app1/src/m.erl
  200. endef
  201. define pkg_test_target
  202. pkg-$(1)-clean:
  203. $t rm -rf app1 erl_crash.dump
  204. pkg-$(1)-app1:
  205. $(call app1_setup)
  206. # Running 'make' twice to make sure it recompiles fine.
  207. pkg-$(1): pkg-$(1)-clean pkg-$(1)-app1
  208. $i
  209. $i " pkgs: Checking that '$(1)' builds correctly"
  210. $i
  211. $t printf "%s\n" \
  212. "PROJECT = app1" \
  213. "DEPS = $(1)" \
  214. "include erlang.mk" \
  215. > app1/Makefile
  216. $t \
  217. if [ "$(1)" = "amqp_client" ]; then \
  218. virtualenv -p python2.7 --distribute temp-python; \
  219. . temp-python/bin/activate; \
  220. $(MAKE) -C app1 RABBITMQ_CLIENT_PATCH=1; \
  221. deactivate; \
  222. elif [ "$(1)" = "rabbit" ]; then \
  223. virtualenv -p python2.7 --distribute temp-python; \
  224. . temp-python/bin/activate; \
  225. $(MAKE) -C app1 RABBITMQ_SERVER_PATCH=1; \
  226. deactivate; \
  227. else \
  228. $(MAKE) -C app1; \
  229. fi; \
  230. if [ $$$$? -ne 0 ]; then \
  231. echo "$(1): make error" >> pkgs.log; \
  232. else \
  233. $(MAKE) -C app1; if [ $$$$? -ne 0 ]; then \
  234. echo "$(1): re-make error" >> pkgs.log; \
  235. else \
  236. find . -type f -name erl_crash.dump; if [ $$$$? -ne 0 ]; then \
  237. echo "$(1): erl_crash.dump found" >> pkgs.log; \
  238. else \
  239. erl +A0 -noinput -boot start_clean -pa app1/deps/*/ebin -eval " \
  240. Apps = [list_to_atom(App) || \"app1/deps/\" ++ App \
  241. <- filelib:wildcard(\"app1/deps/*\")], \
  242. [begin \
  243. io:format(\"Loading application ~p~n\", [App]), \
  244. case application:load(App) of \
  245. {error, _} -> ok; \
  246. ok -> \
  247. {ok, Mods} = application:get_key(App, modules), \
  248. [try io:format(\" Loading module ~p~n\", [Mod]), \
  249. {module, Mod} = code:load_file(Mod) \
  250. catch C:R -> timer:sleep(500), erlang:C(R) \
  251. end || Mod <- Mods] \
  252. end \
  253. end || App <- Apps], \
  254. halt()."; if [ $$$$? -ne 0 ]; then \
  255. echo "$(1): load error" >> pkgs.log; \
  256. fi \
  257. fi \
  258. fi \
  259. fi
  260. endef
  261. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  262. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_test_target,$(pkg))))
  263. pkgs: $(addprefix pkg-,$(PACKAGES))
  264. @if [ -f pkgs.log ]; then \
  265. echo "+-------------------------------+"; \
  266. echo "| ERRORS WHILE TESTING PACKAGES |"; \
  267. echo "+-------------------------------+"; \
  268. cat pkgs.log; \
  269. exit 33; \
  270. fi
  271. # Test application used for testing.
  272. app1:
  273. $(call app1_setup)
  274. # Extra module in app1 used for testing eunit
  275. define create-module-t
  276. printf '%s\n' \
  277. '-module(t).' \
  278. '-export([succ/1]).' \
  279. 'succ(N) -> N + 1.' \
  280. '-ifdef(TEST).' \
  281. '-include_lib("eunit/include/eunit.hrl").' \
  282. 'succ_test() ->' \
  283. ' ?assertEqual(2, succ(1)),' \
  284. ' os:cmd("echo t >> test-eunit.log").' \
  285. '-endif.' \
  286. > app1/src/t.erl
  287. endef