Makefile 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. # t = Verbosity control for tests
  8. # v = Verbosity control for erlang.mk
  9. # i = Command to display (or suppress) info messages
  10. ifeq ($V,0)
  11. # Show info messages only
  12. t = @
  13. v = V=0 &>/dev/null
  14. i = @echo
  15. else ifeq ($V,1)
  16. # Show test commands
  17. t =
  18. v = V=0 &>/dev/null
  19. i = @echo ==
  20. else ifeq ($V,2)
  21. # Show briefly what erlang.mk is doing
  22. t = @echo " TEST " $@;
  23. v = V=0
  24. i = @echo ==
  25. else
  26. # Show all commands with maximum verbosity
  27. t =
  28. v = V=1
  29. i = @echo ==
  30. endif
  31. .PHONY: all clean app ct eunit tests-cover docs
  32. .NOTPARALLEL:
  33. all: clean app ct eunit tests-cover docs pkgs
  34. $i '+---------------------+'
  35. $i '| All tests passed. |'
  36. $i '+---------------------+'
  37. clean:
  38. $t rm -rf app1
  39. app: app1
  40. $i "app: Testing the 'app' target."
  41. $t make -C app1 app $v
  42. $i "Checking the modules line in the generated .app file."
  43. $t [ `grep -E "{modules, *\['m'\]}" app1/ebin/app1.app | wc -l` == 1 ]
  44. $t [ -e app1/ebin/m.beam ]
  45. $i "Checking that 'make clean-app' deletes ebin."
  46. $t make -C app1 clean-app $v
  47. $t [ ! -e app1/ebin ]
  48. $i "Checking that 'make app' returns non-zero on compile errors."
  49. $t printf "%s\n" \
  50. "-module(syntax_error)." \
  51. "foo lorem_ipsum dolor sit amet." \
  52. > app1/src/syntax_error.erl
  53. $t if make -C app1 app $v ; then false ; fi
  54. $t rm app1/src/syntax_error.erl
  55. $i "Test 'app' passed."
  56. ct: app1
  57. $i "ct: Testing ct and related targets."
  58. $i "Setting up test suite."
  59. $t mkdir -p app1/test
  60. $t printf "%s\n" \
  61. "-module(m_SUITE)." \
  62. "-export([all/0, testcase1/1])." \
  63. "all() -> [testcase1]." \
  64. "testcase1(_) -> 2 = m:succ(1)." \
  65. > app1/test/m_SUITE.erl
  66. $t make -C app1 ct $v
  67. $i "Checking files created by 'make ct'."
  68. $t [ -e app1/test/m_SUITE.beam ]
  69. $t [ -e app1/ebin/m.beam ]
  70. $t [ -e app1/logs ]
  71. $i "Checking that 'make clean' does not delete logs."
  72. $t make -C app1 clean $v
  73. $t [ -e app1/logs ]
  74. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  75. $t make -C app1 ct-m $v
  76. $i "Checking that 'make ct' returns non-zero for a failing suite."
  77. $t printf "%s\n" \
  78. "-module(failing_SUITE)." \
  79. "-export([all/0, testcase1/1])." \
  80. "all() -> [testcase1]." \
  81. "testcase1(_) -> 42 = m:succ(1)." \
  82. > app1/test/failing_SUITE.erl
  83. $t if make -C app1 ct-failing $v ; then false ; fi
  84. $i "Checking that 'make distclean-ct' deletes logs."
  85. $t make -C app1 distclean-ct $v
  86. $t [ ! -e app1/logs ]
  87. $t [ -e app1/ebin/m.beam ]
  88. $i "Cleaning up test data."
  89. $t rm -rf app1/test
  90. $i "Test 'ct' passed."
  91. eunit: app1
  92. $i "eunit: Testing the 'eunit' target."
  93. $i "Running eunit test case inside module src/t.erl"
  94. $t $(call create-module-t)
  95. $t make -C app1 eunit $v
  96. $i "Checking that the eunit test in module t."
  97. $t echo t | cmp app1/test-eunit.log -
  98. $t rm app1/test-eunit.log
  99. $i "Running eunit tests in a separate directory."
  100. $t mkdir -p app1/eunit
  101. $t printf '%s\n' \
  102. '-module(t_tests).' \
  103. '-include_lib("eunit/include/eunit.hrl").' \
  104. 'succ_test() ->' \
  105. ' ?assertEqual(2, t:succ(1)),' \
  106. ' os:cmd("echo t_tests >> test-eunit.log").' \
  107. > app1/eunit/t_tests.erl
  108. $t printf '%s\n' \
  109. '-module(x_tests).' \
  110. '-include_lib("eunit/include/eunit.hrl").' \
  111. 'succ_test() ->' \
  112. ' ?assertEqual(2, t:succ(1)),' \
  113. ' os:cmd("echo x_tests >> test-eunit.log").' \
  114. > app1/eunit/x_tests.erl
  115. $t make -C app1 eunit TEST_DIR=eunit $v
  116. $i "Checking that 'make eunit' didn't run the tests in t_tests twice, etc."
  117. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  118. $t rm app1/test-eunit.log
  119. $i "Checking that 'make eunit' returns non-zero for a failing test."
  120. $t rm -f app1/eunit/*
  121. $t printf "%s\n" \
  122. "-module(t_tests)." \
  123. '-include_lib("eunit/include/eunit.hrl").' \
  124. "succ_test() ->" \
  125. " ?assertEqual(42, t:succ(1))." \
  126. > app1/eunit/t_tests.erl
  127. $t if make -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  128. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  129. $i "Test 'eunit' passed."
  130. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  131. tests-cover: app1
  132. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  133. $i "Setting up eunit and ct suites."
  134. $t $(call create-module-t)
  135. $t mkdir -p app1/test
  136. $t printf "%s\n" \
  137. "-module(m_SUITE)." \
  138. "-export([all/0, testcase1/1])." \
  139. "all() -> [testcase1]." \
  140. "testcase1(_) -> 2 = m:succ(1)." \
  141. > app1/test/m_SUITE.erl
  142. $i "Running tests with coverage analysis."
  143. $t make -C app1 eunit ct COVER=1 $v
  144. $t [ -e app1/test-eunit.log ]
  145. $t [ -e app1/eunit.coverdata ]
  146. $t [ -e app1/ct.coverdata ]
  147. $i "Generating coverage report."
  148. $t make -C app1 cover-report COVER=1 $v
  149. $t [ -e app1/cover/m.COVER.html ]
  150. $t [ -e app1/cover/t.COVER.html ]
  151. $t [ -e app1/cover/index.html ]
  152. $i "Checking combined coverage from eunit and ct."
  153. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  154. $i "Checking that cover-report-clean removes cover report."
  155. $t make -C app1 cover-report-clean $v
  156. $t [ ! -e app1/cover ]
  157. $i "Checking that coverdata-clean removes cover data."
  158. $t make -C app1 coverdata-clean $v
  159. $t [ ! -e app1/eunit.coverdata ]
  160. @# clean up
  161. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  162. $t make -C app1 clean $v
  163. $i "Test 'tests-cover' passed."
  164. docs: app1
  165. $i "docs: Testing EDoc including DOC_DEPS."
  166. $t printf "%s\n" \
  167. "PROJECT = app1" \
  168. "DOC_DEPS = edown" \
  169. "dep_edown = git https://github.com/uwiger/edown.git 0.5" \
  170. "EDOC_OPTS = {doclet, edown_doclet}" \
  171. "include erlang.mk" \
  172. "distclean:: distclean-doc-md" \
  173. "distclean-doc-md:" \
  174. " rm -rf doc/*.md" \
  175. > app1/Makefile-doc
  176. $i "Downloading doc deps (edown) and building docs."
  177. $t make -C app1 -f Makefile-doc docs $v
  178. $i "Checking that 'make docs' using edown generated a markdown file."
  179. $t [ -e app1/doc/m.md ]
  180. $i "Checking that 'make distclean' deletes all generated doc files."
  181. $t make -C app1 -f Makefile-doc distclean $v
  182. $t [ "`ls app1/doc/`" == "" ]
  183. $i "Cleaning up test data."
  184. $t rm app1/Makefile-doc
  185. $i "Test 'docs' passed."
  186. define app1_setup
  187. $i "Setting up app."
  188. $t mkdir -p app1
  189. $t cd .. && make
  190. $t cp ../erlang.mk app1/
  191. $t make -C app1 -f erlang.mk bootstrap-lib
  192. $t printf "%s\n" \
  193. "-module(m)." \
  194. "-export([succ/1])." \
  195. "succ(N) -> N + 1." \
  196. > app1/src/m.erl
  197. endef
  198. define pkg_test_target
  199. pkg-$(1)-clean:
  200. $t rm -rf app1
  201. pkg-$(1)-app1:
  202. $(call app1_setup)
  203. # Running 'make' twice to make sure it recompiles fine.
  204. pkg-$(1): pkg-$(1)-clean pkg-$(1)-app1
  205. $i
  206. $i " pkgs: Checking that '$(1)' builds correctly"
  207. $i
  208. $t printf "%s\n" \
  209. "PROJECT = app1" \
  210. "DEPS = $(1)" \
  211. "include erlang.mk" \
  212. > app1/Makefile
  213. cp ../packages.v2.tsv app1/.erlang.mk.packages.v2
  214. $t make -C app1
  215. $t make -C app1
  216. endef
  217. $(foreach pkg,$(shell awk '{print $$1}' ../packages.v2.tsv),$(eval $(call pkg_test_target,$(pkg))))
  218. pkgs: $(foreach pkg,$(shell awk '{print $$1}' ../packages.v2.tsv),pkg-$(pkg))
  219. # Test application used for testing.
  220. app1:
  221. $(call app1_setup)
  222. # Extra module in app1 used for testing eunit
  223. define create-module-t
  224. printf '%s\n' \
  225. '-module(t).' \
  226. '-export([succ/1]).' \
  227. 'succ(N) -> N + 1.' \
  228. '-ifdef(TEST).' \
  229. '-include_lib("eunit/include/eunit.hrl").' \
  230. 'succ_test() ->' \
  231. ' ?assertEqual(2, succ(1)),' \
  232. ' os:cmd("echo t >> test-eunit.log").' \
  233. '-endif.' \
  234. > app1/src/t.erl
  235. endef