plugin_eunit.mk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. # EUnit plugin.
  2. EUNIT_TARGETS = $(call list_targets,eunit)
  3. .PHONY: eunit $(EUNIT_TARGETS)
  4. eunit: $(EUNIT_TARGETS)
  5. eunit-all: init
  6. $i "Bootstrap a new OTP application named $(APP)"
  7. $t mkdir $(APP)/
  8. $t cp ../erlang.mk $(APP)/
  9. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  10. $i "Check that EUnit detects no tests"
  11. $t $(MAKE) -C $(APP) eunit | grep -c "There were no tests to run." | grep -q 1
  12. $i "Generate a module containing EUnit tests"
  13. $t printf "%s\n" \
  14. "-module($(APP))." \
  15. "-ifdef(TEST)." \
  16. "-include_lib(\"eunit/include/eunit.hrl\")." \
  17. "ok_test() -> ok." \
  18. "-endif." > $(APP)/src/$(APP).erl
  19. $i "Build the project cleanly"
  20. $t $(MAKE) -C $(APP) clean $v
  21. $t $(MAKE) -C $(APP) $v
  22. $i "Check that no EUnit test cases were exported"
  23. $t $(ERL) -pa $(APP)/ebin -eval 'code:load_file($(APP)), false = erlang:function_exported($(APP), ok_test, 0), halt()'
  24. $i "Check that EUnit runs tests"
  25. $t $(MAKE) -C $(APP) eunit | grep -c "Test passed." | grep -q 1
  26. $i "Add a failing test to the module"
  27. $t printf "%s\n" \
  28. "-ifdef(TEST)." \
  29. "bad_test() -> throw(fail)." \
  30. "-endif." >> $(APP)/src/$(APP).erl
  31. $i "Check that EUnit errors out"
  32. $t ! $(MAKE) -C $(APP) eunit $v
  33. eunit-apps-include-lib: init
  34. $i "Bootstrap a new OTP library named $(APP)"
  35. $t mkdir $(APP)/
  36. $t cp ../erlang.mk $(APP)/
  37. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  38. $i "Create new libraries the_app and test_helpers"
  39. $t $(MAKE) -C $(APP) new-lib in=the_app $v
  40. $t $(MAKE) -C $(APP) new-lib in=test_helpers $v
  41. $i "Generate .erl file"
  42. $t echo '-module(the). -export([thing/0]). thing() -> true.' > $(APP)/apps/the_app/src/the.erl
  43. $t mkdir -p $(APP)/apps/the_app/test
  44. $i "Generate test .erl file with helper include_lib()"
  45. $t echo '-module(the_test).' > $(APP)/apps/the_app/test/the_test.erl
  46. $t echo '-include_lib("eunit/include/eunit.hrl").' >> $(APP)/apps/the_app/test/the_test.erl
  47. $t echo '-include_lib("test_helpers/include/test_helpers.hrl").' >> $(APP)/apps/the_app/test/the_test.erl
  48. $t echo 'thing_test() -> ?assertEqual(true, the:thing()).' >> $(APP)/apps/the_app/test/the_test.erl
  49. $t mkdir -p $(APP)/apps/test_helpers/include
  50. $t echo '%% test_helpers' > $(APP)/apps/test_helpers/include/test_helpers.hrl
  51. $i "Build the application"
  52. $t $(MAKE) -C $(APP) $v
  53. $i "Run eunit"
  54. $t $(MAKE) -C $(APP) $v
  55. $i "Distclean the application"
  56. $t $(MAKE) -C $(APP) distclean $v
  57. $i "Run eunit on a subdirectory"
  58. $t $(MAKE) -C $(APP)/apps/the_app eunit $v
  59. $i "Distclean the application"
  60. $t $(MAKE) -C $(APP) distclean $v
  61. eunit-apps-one-app-tested: init
  62. $i "Bootstrap a new OTP library named $(APP)"
  63. $t mkdir $(APP)/
  64. $t cp ../erlang.mk $(APP)/
  65. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  66. $i "Create a new application named my_app"
  67. $t $(MAKE) -C $(APP) new-app in=my_app $v
  68. $i "Create a new library named my_lib"
  69. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  70. $i "Generate a module containing EUnit tests in my_app"
  71. $t printf "%s\n" \
  72. "-module(my_app)." \
  73. "-ifdef(TEST)." \
  74. "-include_lib(\"eunit/include/eunit.hrl\")." \
  75. "ok_test() -> ok." \
  76. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  77. $i "Generate a module containing EUnit tests in my_lib"
  78. $t printf "%s\n" \
  79. "-module(my_lib)." \
  80. "-ifdef(TEST)." \
  81. "-include_lib(\"eunit/include/eunit.hrl\")." \
  82. "ok_test() -> ok." \
  83. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  84. $i "Run EUnit on my_app only"
  85. $t $(MAKE) -C $(APP)/apps/my_app eunit | grep -c "Test passed." | grep -q 1
  86. eunit-apps-only: init
  87. $i "Create a multi application repository with no root application"
  88. $t mkdir $(APP)/
  89. $t cp ../erlang.mk $(APP)/
  90. $t echo "include erlang.mk" > $(APP)/Makefile
  91. $i "Create a new application named my_app"
  92. $t $(MAKE) -C $(APP) new-app in=my_app $v
  93. $i "Create a new library named my_lib"
  94. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  95. $i "Check that EUnit detects no tests"
  96. $t $(MAKE) -C $(APP) eunit | grep -c "There were no tests to run." | grep -q 2
  97. $i "Generate a module containing EUnit tests in my_app"
  98. $t printf "%s\n" \
  99. "-module(my_app)." \
  100. "-ifdef(TEST)." \
  101. "-include_lib(\"eunit/include/eunit.hrl\")." \
  102. "ok_test() -> ok." \
  103. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  104. $i "Generate a module containing EUnit tests in my_lib"
  105. $t printf "%s\n" \
  106. "-module(my_lib)." \
  107. "-ifdef(TEST)." \
  108. "-include_lib(\"eunit/include/eunit.hrl\")." \
  109. "ok_test() -> ok." \
  110. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  111. $i "Check that EUnit runs tests"
  112. $t $(MAKE) -C $(APP) eunit | grep -c "Test passed." | grep -q 2
  113. eunit-apps-only-error: init
  114. $i "Create a multi application repository with no root application"
  115. $t mkdir $(APP)/
  116. $t cp ../erlang.mk $(APP)/
  117. $t echo "include erlang.mk" > $(APP)/Makefile
  118. $i "Create a new application named my_app1"
  119. $t $(MAKE) -C $(APP) new-app in=my_app1 $v
  120. $i "Create a new application named my_app2"
  121. $t $(MAKE) -C $(APP) new-app in=my_app2 $v
  122. $i "Create a new library named my_lib"
  123. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  124. $i "Check that EUnit detects no tests"
  125. $t $(MAKE) -C $(APP) eunit | grep -c "There were no tests to run." | grep -q 3
  126. $i "Generate a module containing broken EUnit tests in my_app1"
  127. $t printf "%s\n" \
  128. "-module(my_app1)." \
  129. "-ifdef(TEST)." \
  130. "-include_lib(\"eunit/include/eunit.hrl\")." \
  131. "bad_test() -> ?assert(0 =:= 1)." \
  132. "-endif." > $(APP)/apps/my_app1/src/my_app1.erl
  133. $i "Generate a module containing EUnit good tests in my_app2"
  134. $t printf "%s\n" \
  135. "-module(my_app2)." \
  136. "-ifdef(TEST)." \
  137. "-include_lib(\"eunit/include/eunit.hrl\")." \
  138. "ok_test() -> ok." \
  139. "-endif." > $(APP)/apps/my_app2/src/my_app2.erl
  140. $i "Generate a module containing EUnit tests in my_lib"
  141. $t printf "%s\n" \
  142. "-module(my_lib)." \
  143. "-ifdef(TEST)." \
  144. "-include_lib(\"eunit/include/eunit.hrl\")." \
  145. "ok_test() -> ok." \
  146. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  147. $i "Check exit code of EUnit for the module with broken test should be non-zero"
  148. $t ! $(MAKE) -C $(APP)/apps/my_app1 eunit $v
  149. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  150. $t ! $(MAKE) -C $(APP) eunit $v
  151. eunit-check: init
  152. $i "Bootstrap a new OTP application named $(APP)"
  153. $t mkdir $(APP)/
  154. $t cp ../erlang.mk $(APP)/
  155. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  156. $i "Generate a module containing EUnit tests"
  157. $t printf "%s\n" \
  158. "-module($(APP))." \
  159. "-ifdef(TEST)." \
  160. "-include_lib(\"eunit/include/eunit.hrl\")." \
  161. "ok_test() -> ok." \
  162. "-endif." > $(APP)/src/$(APP).erl
  163. $i "Check that EUnit runs on 'make check'"
  164. $t $(MAKE) -C $(APP) check | grep -c "Test passed." | grep -q 1
  165. eunit-erl-opts: init
  166. $i "Bootstrap a new OTP application named $(APP)"
  167. $t mkdir $(APP)/
  168. $t cp ../erlang.mk $(APP)/
  169. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  170. $i "Set EUNIT_ERL_OPTS in the Makefile"
  171. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  172. $i "Generate a module containing EUnit tests"
  173. $t printf "%s\n" \
  174. "-module($(APP))." \
  175. "-ifdef(TEST)." \
  176. "-include_lib(\"eunit/include/eunit.hrl\")." \
  177. "ok_test() -> ok." \
  178. "-endif." > $(APP)/src/$(APP).erl
  179. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  180. $t $(MAKE) -C $(APP) eunit | grep -c "hello" | grep -q 1
  181. eunit-fun: init
  182. $i "Bootstrap a new OTP application named $(APP)"
  183. $t mkdir $(APP)/
  184. $t cp ../erlang.mk $(APP)/
  185. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  186. $i "Generate a module containing EUnit tests"
  187. $t printf "%s\n" \
  188. "-module($(APP))." \
  189. "-ifdef(TEST)." \
  190. "-include_lib(\"eunit/include/eunit.hrl\")." \
  191. "ok_test() -> ok." \
  192. "bad_test() -> throw(fail)." \
  193. "-endif." > $(APP)/src/$(APP).erl
  194. $i "Check that we can run EUnit on a specific test"
  195. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  196. eunit-mod: init
  197. $i "Bootstrap a new OTP application named $(APP)"
  198. $t mkdir $(APP)/
  199. $t cp ../erlang.mk $(APP)/
  200. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  201. $i "Generate a module containing EUnit tests"
  202. $t printf "%s\n" \
  203. "-module($(APP))." \
  204. "-ifdef(TEST)." \
  205. "-include_lib(\"eunit/include/eunit.hrl\")." \
  206. "ok_test() -> ok." \
  207. "-endif." > $(APP)/src/$(APP).erl
  208. $i "Generate a module containing failing EUnit tests"
  209. $t printf "%s\n" \
  210. "-module($(APP)_fail)." \
  211. "-ifdef(TEST)." \
  212. "-include_lib(\"eunit/include/eunit.hrl\")." \
  213. "bad_test() -> throw(fail)." \
  214. "-endif." > $(APP)/src/$(APP)_fail.erl
  215. $i "Check that we can run EUnit on a specific module"
  216. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  217. eunit-priv: init
  218. $i "Bootstrap a new OTP application named $(APP)"
  219. $t mkdir $(APP)/
  220. $t cp ../erlang.mk $(APP)/
  221. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  222. $i "Generate a module containing EUnit tests"
  223. $t printf "%s\n" \
  224. "-module($(APP))." \
  225. "-ifdef(TEST)." \
  226. "-include_lib(\"eunit/include/eunit.hrl\")." \
  227. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  228. "-endif." > $(APP)/src/$(APP).erl
  229. $i "Check that EUnit can resolve the priv_dir"
  230. $t $(MAKE) -C $(APP) tests | grep -c "Test passed." | grep -q 1
  231. eunit-test-dir: init
  232. $i "Bootstrap a new OTP application named $(APP)"
  233. $t mkdir $(APP)/
  234. $t cp ../erlang.mk $(APP)/
  235. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  236. $i "Generate a module containing EUnit tests"
  237. $t printf "%s\n" \
  238. "-module($(APP))." \
  239. "-ifdef(TEST)." \
  240. "-include_lib(\"eunit/include/eunit.hrl\")." \
  241. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  242. "-endif." > $(APP)/src/$(APP).erl
  243. $i "Generate a module containing EUnit tests in TEST_DIR"
  244. $t mkdir $(APP)/test
  245. $t printf "%s\n" \
  246. "-module($(APP)_tests)." \
  247. "-include_lib(\"eunit/include/eunit.hrl\")." \
  248. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  249. $i "Check that EUnit runs both tests"
  250. $t $(MAKE) -C $(APP) eunit | grep -c "2 tests passed." | grep -q 1
  251. $i "Check that tests were both run only once"
  252. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  253. eunit-tests: init
  254. $i "Bootstrap a new OTP application named $(APP)"
  255. $t mkdir $(APP)/
  256. $t cp ../erlang.mk $(APP)/
  257. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  258. $i "Generate a module containing EUnit tests"
  259. $t printf "%s\n" \
  260. "-module($(APP))." \
  261. "-ifdef(TEST)." \
  262. "-include_lib(\"eunit/include/eunit.hrl\")." \
  263. "ok_test() -> ok." \
  264. "-endif." > $(APP)/src/$(APP).erl
  265. $i "Check that EUnit runs on 'make tests'"
  266. $t $(MAKE) -C $(APP) tests | grep -c "Test passed." | grep -q 1