Makefile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. all: clean app ct eunit tests-cover docs
  33. $i '+---------------------+'
  34. $i '| All tests passed. |'
  35. $i '+---------------------+'
  36. clean:
  37. $t rm -rf app1
  38. app: app1
  39. $i "app: Testing the 'app' target."
  40. $t make -C app1 app $v
  41. $i "Checking the modules line in the generated .app file."
  42. $t [ `grep -E "{modules, *\['m'\]}" app1/ebin/app1.app | wc -l` == 1 ]
  43. $t [ -e app1/ebin/m.beam ]
  44. $i "Checking that 'make clean-app' deletes ebin."
  45. $t make -C app1 clean-app $v
  46. $t [ ! -e app1/ebin ]
  47. $i "Checking that 'make app' returns non-zero on compile errors."
  48. $t printf "%s\n" \
  49. "-module(syntax_error)." \
  50. "foo lorem_ipsum dolor sit amet." \
  51. > app1/src/syntax_error.erl
  52. $t if make -C app1 app $v ; then false ; fi
  53. $t rm app1/src/syntax_error.erl
  54. $i "Test 'app' passed."
  55. ct: app1
  56. $i "ct: Testing ct and related targets."
  57. $i "Setting up test suite."
  58. $t mkdir -p app1/test
  59. $t printf "%s\n" \
  60. "-module(m_SUITE)." \
  61. "-export([all/0, testcase1/1])." \
  62. "all() -> [testcase1]." \
  63. "testcase1(_) -> 2 = m:succ(1)." \
  64. > app1/test/m_SUITE.erl
  65. $t make -C app1 ct $v
  66. $i "Checking files created by 'make ct'."
  67. $t [ -e app1/test/m_SUITE.beam ]
  68. $t [ -e app1/ebin/m.beam ]
  69. $t [ -e app1/logs ]
  70. $i "Checking that 'make clean' does not delete logs."
  71. $t make -C app1 clean $v
  72. $t [ -e app1/logs ]
  73. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  74. $t make -C app1 ct-m $v
  75. $i "Checking that 'make ct' returns non-zero for a failing suite."
  76. $t printf "%s\n" \
  77. "-module(failing_SUITE)." \
  78. "-export([all/0, testcase1/1])." \
  79. "all() -> [testcase1]." \
  80. "testcase1(_) -> 42 = m:succ(1)." \
  81. > app1/test/failing_SUITE.erl
  82. $t if make -C app1 ct-failing $v ; then false ; fi
  83. $i "Checking that 'make distclean-ct' deletes logs."
  84. $t make -C app1 distclean-ct $v
  85. $t [ ! -e app1/logs ]
  86. $t [ -e app1/ebin/m.beam ]
  87. $i "Cleaning up test data."
  88. $t rm -rf app1/test
  89. $i "Test 'ct' passed."
  90. eunit: app1
  91. $i "eunit: Testing the 'eunit' target."
  92. $i "Running eunit test case inside module src/t.erl"
  93. $t $(call create-module-t)
  94. $t make -C app1 eunit $v
  95. $i "Checking that the eunit test in module t."
  96. $t echo t | cmp app1/test-eunit.log -
  97. $t rm app1/test-eunit.log
  98. $i "Running eunit tests in a separate directory."
  99. $t mkdir -p app1/eunit
  100. $t printf '%s\n' \
  101. '-module(t_tests).' \
  102. '-include_lib("eunit/include/eunit.hrl").' \
  103. 'succ_test() ->' \
  104. ' ?assertEqual(2, t:succ(1)),' \
  105. ' os:cmd("echo t_tests >> test-eunit.log").' \
  106. > app1/eunit/t_tests.erl
  107. $t printf '%s\n' \
  108. '-module(x_tests).' \
  109. '-include_lib("eunit/include/eunit.hrl").' \
  110. 'succ_test() ->' \
  111. ' ?assertEqual(2, t:succ(1)),' \
  112. ' os:cmd("echo x_tests >> test-eunit.log").' \
  113. > app1/eunit/x_tests.erl
  114. $t make -C app1 eunit TEST_DIR=eunit $v
  115. $i "Checking that 'make eunit' didn't run the tests in t_tests twice, etc."
  116. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  117. $t rm app1/test-eunit.log
  118. $i "Checking that 'make eunit' returns non-zero for a failing test."
  119. $t rm -f app1/eunit/*
  120. $t printf "%s\n" \
  121. "-module(t_tests)." \
  122. '-include_lib("eunit/include/eunit.hrl").' \
  123. "succ_test() ->" \
  124. " ?assertEqual(42, t:succ(1))." \
  125. > app1/eunit/t_tests.erl
  126. $t if make -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  127. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  128. $i "Test 'eunit' passed."
  129. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  130. tests-cover: app1
  131. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  132. $i "Setting up eunit and ct suites."
  133. $t $(call create-module-t)
  134. $t mkdir -p app1/test
  135. $t printf "%s\n" \
  136. "-module(m_SUITE)." \
  137. "-export([all/0, testcase1/1])." \
  138. "all() -> [testcase1]." \
  139. "testcase1(_) -> 2 = m:succ(1)." \
  140. > app1/test/m_SUITE.erl
  141. $i "Running tests with coverage analysis."
  142. $t make -C app1 eunit ct COVER=1 $v
  143. $t [ -e app1/test-eunit.log ]
  144. $t [ -e app1/eunit.coverdata ]
  145. $t [ -e app1/ct.coverdata ]
  146. $i "Generating coverage report."
  147. $t make -C app1 cover-report COVER=1 $v
  148. $t [ -e app1/cover/m.COVER.html ]
  149. $t [ -e app1/cover/t.COVER.html ]
  150. $t [ -e app1/cover/index.html ]
  151. $i "Checking combined coverage from eunit and ct."
  152. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  153. $i "Checking that cover-report-clean removes cover report."
  154. $t make -C app1 cover-report-clean $v
  155. $t [ ! -e app1/cover ]
  156. $i "Checking that coverdata-clean removes cover data."
  157. $t make -C app1 coverdata-clean $v
  158. $t [ ! -e app1/eunit.coverdata ]
  159. @# clean up
  160. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  161. $t make -C app1 clean $v
  162. $i "Test 'tests-cover' passed."
  163. docs: app1
  164. $i "docs: Testing EDoc including DOC_DEPS."
  165. $t printf "%s\n" \
  166. "PROJECT = app1" \
  167. "DOC_DEPS = edown" \
  168. "dep_edown = git https://github.com/uwiger/edown.git 0.5" \
  169. "EDOC_OPTS = {doclet, edown_doclet}" \
  170. "include erlang.mk" \
  171. "distclean:: distclean-doc-md" \
  172. "distclean-doc-md:" \
  173. " rm -rf doc/*.md" \
  174. > app1/Makefile-doc
  175. $i "Downloading doc deps (edown) and building docs."
  176. $t make -C app1 -f Makefile-doc docs $v
  177. $i "Checking that 'make docs' using edown generated a markdown file."
  178. $t [ -e app1/doc/m.md ]
  179. $i "Checking that 'make distclean' deletes all generated doc files."
  180. $t make -C app1 -f Makefile-doc distclean $v
  181. $t [ "`ls app1/doc/`" == "" ]
  182. $i "Cleaning up test data."
  183. $t rm app1/Makefile-doc
  184. $i "Test 'docs' passed."
  185. # Test application used for testing.
  186. app1:
  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. # Extra module in app1 used for testing eunit
  198. define create-module-t
  199. printf '%s\n' \
  200. '-module(t).' \
  201. '-export([succ/1]).' \
  202. 'succ(N) -> N + 1.' \
  203. '-ifdef(TEST).' \
  204. '-include_lib("eunit/include/eunit.hrl").' \
  205. 'succ_test() ->' \
  206. ' ?assertEqual(2, succ(1)),' \
  207. ' os:cmd("echo t >> test-eunit.log").' \
  208. '-endif.' \
  209. > app1/src/t.erl
  210. endef