plugin_eunit.mk 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # EUnit plugin.
  2. EUNIT_TARGETS = $(call list_targets,eunit)
  3. .PHONY: eunit $(EUNIT_TARGETS)
  4. eunit: $(EUNIT_TARGETS)
  5. eunit-all: build clean
  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 -q "There were no tests to run."
  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 -q "Test passed."
  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: build clean
  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-only: build clean
  62. $i "Create a multi application repository with no root application"
  63. $t mkdir $(APP)/
  64. $t cp ../erlang.mk $(APP)/
  65. $t echo "include erlang.mk" > $(APP)/Makefile
  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 "Check that EUnit detects no tests"
  71. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  72. $i "Generate a module containing EUnit tests in my_app"
  73. $t printf "%s\n" \
  74. "-module(my_app)." \
  75. "-ifdef(TEST)." \
  76. "-include_lib(\"eunit/include/eunit.hrl\")." \
  77. "ok_test() -> ok." \
  78. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  79. $i "Generate a module containing EUnit tests in my_lib"
  80. $t printf "%s\n" \
  81. "-module(my_lib)." \
  82. "-ifdef(TEST)." \
  83. "-include_lib(\"eunit/include/eunit.hrl\")." \
  84. "ok_test() -> ok." \
  85. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  86. $i "Check that EUnit runs tests"
  87. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  88. eunit-apps-only-error: build clean
  89. $i "Create a multi application repository with no root application"
  90. $t mkdir $(APP)/
  91. $t cp ../erlang.mk $(APP)/
  92. $t echo "include erlang.mk" > $(APP)/Makefile
  93. $i "Create a new application named my_app1"
  94. $t $(MAKE) -C $(APP) new-app in=my_app1 $v
  95. $i "Create a new application named my_app2"
  96. $t $(MAKE) -C $(APP) new-app in=my_app2 $v
  97. $i "Create a new library named my_lib"
  98. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  99. $i "Check that EUnit detects no tests"
  100. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  101. $i "Generate a module containing broken EUnit tests in my_app1"
  102. $t printf "%s\n" \
  103. "-module(my_app1)." \
  104. "-ifdef(TEST)." \
  105. "-include_lib(\"eunit/include/eunit.hrl\")." \
  106. "bad_test() -> ?assert(0 =:= 1)." \
  107. "-endif." > $(APP)/apps/my_app1/src/my_app1.erl
  108. $i "Generate a module containing EUnit good tests in my_app2"
  109. $t printf "%s\n" \
  110. "-module(my_app2)." \
  111. "-ifdef(TEST)." \
  112. "-include_lib(\"eunit/include/eunit.hrl\")." \
  113. "ok_test() -> ok." \
  114. "-endif." > $(APP)/apps/my_app2/src/my_app2.erl
  115. $i "Generate a module containing EUnit tests in my_lib"
  116. $t printf "%s\n" \
  117. "-module(my_lib)." \
  118. "-ifdef(TEST)." \
  119. "-include_lib(\"eunit/include/eunit.hrl\")." \
  120. "ok_test() -> ok." \
  121. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  122. $i "Check exit code of EUnit for the module with broken test should be non-zero"
  123. $t ! $(MAKE) -C $(APP)/apps/my_app1 eunit $v
  124. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  125. $t ! $(MAKE) -C $(APP) eunit $v
  126. eunit-check: build clean
  127. $i "Bootstrap a new OTP application named $(APP)"
  128. $t mkdir $(APP)/
  129. $t cp ../erlang.mk $(APP)/
  130. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  131. $i "Generate a module containing EUnit tests"
  132. $t printf "%s\n" \
  133. "-module($(APP))." \
  134. "-ifdef(TEST)." \
  135. "-include_lib(\"eunit/include/eunit.hrl\")." \
  136. "ok_test() -> ok." \
  137. "-endif." > $(APP)/src/$(APP).erl
  138. $i "Check that EUnit runs on 'make check'"
  139. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  140. eunit-erl-opts: build clean
  141. $i "Bootstrap a new OTP application named $(APP)"
  142. $t mkdir $(APP)/
  143. $t cp ../erlang.mk $(APP)/
  144. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  145. $i "Set EUNIT_ERL_OPTS in the Makefile"
  146. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  147. $i "Generate a module containing EUnit tests"
  148. $t printf "%s\n" \
  149. "-module($(APP))." \
  150. "-ifdef(TEST)." \
  151. "-include_lib(\"eunit/include/eunit.hrl\")." \
  152. "ok_test() -> ok." \
  153. "-endif." > $(APP)/src/$(APP).erl
  154. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  155. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  156. eunit-fun: build clean
  157. $i "Bootstrap a new OTP application named $(APP)"
  158. $t mkdir $(APP)/
  159. $t cp ../erlang.mk $(APP)/
  160. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  161. $i "Generate a module containing EUnit tests"
  162. $t printf "%s\n" \
  163. "-module($(APP))." \
  164. "-ifdef(TEST)." \
  165. "-include_lib(\"eunit/include/eunit.hrl\")." \
  166. "ok_test() -> ok." \
  167. "bad_test() -> throw(fail)." \
  168. "-endif." > $(APP)/src/$(APP).erl
  169. $i "Check that we can run EUnit on a specific test"
  170. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  171. eunit-mod: build clean
  172. $i "Bootstrap a new OTP application named $(APP)"
  173. $t mkdir $(APP)/
  174. $t cp ../erlang.mk $(APP)/
  175. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  176. $i "Generate a module containing EUnit tests"
  177. $t printf "%s\n" \
  178. "-module($(APP))." \
  179. "-ifdef(TEST)." \
  180. "-include_lib(\"eunit/include/eunit.hrl\")." \
  181. "ok_test() -> ok." \
  182. "-endif." > $(APP)/src/$(APP).erl
  183. $i "Generate a module containing failing EUnit tests"
  184. $t printf "%s\n" \
  185. "-module($(APP)_fail)." \
  186. "-ifdef(TEST)." \
  187. "-include_lib(\"eunit/include/eunit.hrl\")." \
  188. "bad_test() -> throw(fail)." \
  189. "-endif." > $(APP)/src/$(APP)_fail.erl
  190. $i "Check that we can run EUnit on a specific module"
  191. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  192. eunit-priv: build clean
  193. $i "Bootstrap a new OTP application named $(APP)"
  194. $t mkdir $(APP)/
  195. $t cp ../erlang.mk $(APP)/
  196. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  197. $i "Generate a module containing EUnit tests"
  198. $t printf "%s\n" \
  199. "-module($(APP))." \
  200. "-ifdef(TEST)." \
  201. "-include_lib(\"eunit/include/eunit.hrl\")." \
  202. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  203. "-endif." > $(APP)/src/$(APP).erl
  204. $i "Check that EUnit can resolve the priv_dir"
  205. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."
  206. eunit-test-dir: build clean
  207. $i "Bootstrap a new OTP application named $(APP)"
  208. $t mkdir $(APP)/
  209. $t cp ../erlang.mk $(APP)/
  210. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  211. $i "Generate a module containing EUnit tests"
  212. $t printf "%s\n" \
  213. "-module($(APP))." \
  214. "-ifdef(TEST)." \
  215. "-include_lib(\"eunit/include/eunit.hrl\")." \
  216. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  217. "-endif." > $(APP)/src/$(APP).erl
  218. $i "Generate a module containing EUnit tests in TEST_DIR"
  219. $t mkdir $(APP)/test
  220. $t printf "%s\n" \
  221. "-module($(APP)_tests)." \
  222. "-include_lib(\"eunit/include/eunit.hrl\")." \
  223. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  224. $i "Check that EUnit runs both tests"
  225. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  226. $i "Check that tests were both run only once"
  227. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  228. eunit-tests: build clean
  229. $i "Bootstrap a new OTP application named $(APP)"
  230. $t mkdir $(APP)/
  231. $t cp ../erlang.mk $(APP)/
  232. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  233. $i "Generate a module containing EUnit tests"
  234. $t printf "%s\n" \
  235. "-module($(APP))." \
  236. "-ifdef(TEST)." \
  237. "-include_lib(\"eunit/include/eunit.hrl\")." \
  238. "ok_test() -> ok." \
  239. "-endif." > $(APP)/src/$(APP).erl
  240. $i "Check that EUnit runs on 'make tests'"
  241. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."