Makefile 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 distclean $v
  99. $t $(MAKE) -C app1 eunit $v
  100. $i "Checking that the eunit test in module t."
  101. $t echo t | cmp app1/test-eunit.log -
  102. $t rm app1/test-eunit.log
  103. $i "Running eunit tests in a separate directory."
  104. $t mkdir -p app1/eunit
  105. $t printf '%s\n' \
  106. '-module(t_tests).' \
  107. '-include_lib("eunit/include/eunit.hrl").' \
  108. 'succ_test() ->' \
  109. ' ?assertEqual(2, t:succ(1)),' \
  110. ' os:cmd("echo t_tests >> test-eunit.log").' \
  111. > app1/eunit/t_tests.erl
  112. $t printf '%s\n' \
  113. '-module(x_tests).' \
  114. '-include_lib("eunit/include/eunit.hrl").' \
  115. 'succ_test() ->' \
  116. ' ?assertEqual(2, t:succ(1)),' \
  117. ' os:cmd("echo x_tests >> test-eunit.log").' \
  118. > app1/eunit/x_tests.erl
  119. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  120. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  121. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  122. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  123. $t rm app1/test-eunit.log
  124. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  125. $t rm -f app1/eunit/*
  126. $t printf "%s\n" \
  127. "-module(t_tests)." \
  128. '-include_lib("eunit/include/eunit.hrl").' \
  129. "succ_test() ->" \
  130. " ?assertEqual(42, t:succ(1))." \
  131. > app1/eunit/t_tests.erl
  132. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  133. $t if $(MAKE) -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  134. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  135. $i "Test 'eunit' passed."
  136. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  137. tests-cover: app1
  138. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  139. $i "Setting up eunit and ct suites."
  140. $t $(call create-module-t)
  141. $t mkdir -p app1/test
  142. $t printf "%s\n" \
  143. "-module(m_SUITE)." \
  144. "-export([all/0, testcase1/1])." \
  145. "all() -> [testcase1]." \
  146. "testcase1(_) -> 2 = m:succ(1)." \
  147. > app1/test/m_SUITE.erl
  148. $i "Running tests with coverage analysis."
  149. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  150. $t [ -e app1/test-eunit.log ]
  151. $t [ -e app1/eunit.coverdata ]
  152. $t [ -e app1/ct.coverdata ]
  153. $i "Generating coverage report."
  154. $t $(MAKE) -C app1 cover-report COVER=1 $v
  155. $t [ -e app1/cover/m.COVER.html ]
  156. $t [ -e app1/cover/t.COVER.html ]
  157. $t [ -e app1/cover/index.html ]
  158. $i "Checking combined coverage from eunit and ct."
  159. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  160. $i "Checking that cover-report-clean removes cover report."
  161. $t $(MAKE) -C app1 cover-report-clean $v
  162. $t [ ! -e app1/cover ]
  163. $i "Checking that coverdata-clean removes cover data."
  164. $t $(MAKE) -C app1 coverdata-clean $v
  165. $t [ ! -e app1/eunit.coverdata ]
  166. @# clean up
  167. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  168. $t $(MAKE) -C app1 clean $v
  169. $i "Test 'tests-cover' passed."
  170. docs: app1
  171. $i "docs: Testing EDoc including DOC_DEPS."
  172. $t printf "%s\n" \
  173. "PROJECT = app1" \
  174. "DOC_DEPS = edown" \
  175. "dep_edown = git https://github.com/uwiger/edown.git 0.5" \
  176. "EDOC_OPTS = {doclet, edown_doclet}" \
  177. "include erlang.mk" \
  178. "distclean:: distclean-doc-md" \
  179. "distclean-doc-md:" \
  180. " rm -rf doc/*.md" \
  181. > app1/Makefile-doc
  182. $i "Downloading doc deps (edown) and building docs."
  183. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  184. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  185. $t [ -e app1/doc/m.md ]
  186. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  187. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  188. $t [ "`ls app1/doc/`" == "" ]
  189. $i "Cleaning up test data."
  190. $t rm app1/Makefile-doc
  191. $i "Test 'docs' passed."
  192. define app1_setup
  193. $i "Setting up app."
  194. $t mkdir -p app1
  195. $t cd .. && $(MAKE)
  196. $t cp ../erlang.mk app1/
  197. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  198. $t printf "%s\n" \
  199. "-module(m)." \
  200. "-export([succ/1])." \
  201. "succ(N) -> N + 1." \
  202. > app1/src/m.erl
  203. endef
  204. define pkg_test_target
  205. pkg-$(1)-clean:
  206. $t rm -rf app1 erl_crash.dump
  207. pkg-$(1)-app1:
  208. $(call app1_setup)
  209. # Running 'make' twice to make sure it recompiles fine.
  210. pkg-$(1): pkg-$(1)-clean pkg-$(1)-app1
  211. $i
  212. $i " pkgs: Checking that '$(1)' builds correctly"
  213. $i
  214. $t printf "%s\n" \
  215. "PROJECT = app1" \
  216. "DEPS = $(1)" \
  217. "include erlang.mk" \
  218. > app1/Makefile
  219. $t \
  220. if [ "$(1)" = "amqp_client" ]; then \
  221. virtualenv -p python2.7 --distribute temp-python; \
  222. . temp-python/bin/activate; \
  223. $(MAKE) -C app1 RABBITMQ_CLIENT_PATCH=1; \
  224. deactivate; \
  225. elif [ "$(1)" = "rabbit" ]; then \
  226. virtualenv -p python2.7 --distribute temp-python; \
  227. . temp-python/bin/activate; \
  228. $(MAKE) -C app1 RABBITMQ_SERVER_PATCH=1; \
  229. deactivate; \
  230. else \
  231. $(MAKE) -C app1; \
  232. fi; \
  233. if [ $$$$? -ne 0 ]; then \
  234. echo "$(1): make error" >> pkgs.log; \
  235. else \
  236. $(MAKE) -C app1; if [ $$$$? -ne 0 ]; then \
  237. echo "$(1): re-make error" >> pkgs.log; \
  238. else \
  239. find . -type f -name erl_crash.dump; if [ $$$$? -ne 0 ]; then \
  240. echo "$(1): erl_crash.dump found" >> pkgs.log; \
  241. else \
  242. erl +A0 -noinput -boot start_clean -pa app1/deps/*/ebin -eval " \
  243. Apps = [list_to_atom(App) || \"app1/deps/\" ++ App \
  244. <- filelib:wildcard(\"app1/deps/*\")], \
  245. [begin \
  246. io:format(\"Loading application ~p~n\", [App]), \
  247. case application:load(App) of \
  248. {error, _} -> ok; \
  249. ok -> \
  250. {ok, Mods} = application:get_key(App, modules), \
  251. [try io:format(\" Loading module ~p~n\", [Mod]), \
  252. {module, Mod} = code:load_file(Mod) \
  253. catch C:R -> timer:sleep(500), erlang:C(R) \
  254. end || Mod <- Mods] \
  255. end \
  256. end || App <- Apps], \
  257. halt()."; if [ $$$$? -ne 0 ]; then \
  258. echo "$(1): load error" >> pkgs.log; \
  259. fi \
  260. fi \
  261. fi \
  262. fi
  263. endef
  264. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  265. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_test_target,$(pkg))))
  266. pkgs: $(addprefix pkg-,$(PACKAGES))
  267. @if [ -f pkgs.log ]; then \
  268. echo "+-------------------------------+"; \
  269. echo "| ERRORS WHILE TESTING PACKAGES |"; \
  270. echo "+-------------------------------+"; \
  271. cat pkgs.log; \
  272. exit 33; \
  273. fi
  274. # Test application used for testing.
  275. app1:
  276. $(call app1_setup)
  277. # Extra module in app1 used for testing eunit
  278. define create-module-t
  279. printf '%s\n' \
  280. '-module(t).' \
  281. '-export([succ/1]).' \
  282. 'succ(N) -> N + 1.' \
  283. '-ifdef(TEST).' \
  284. '-include_lib("eunit/include/eunit.hrl").' \
  285. 'succ_test() ->' \
  286. ' ?assertEqual(2, succ(1)),' \
  287. ' os:cmd("echo t >> test-eunit.log").' \
  288. '-endif.' \
  289. > app1/src/t.erl
  290. endef