plugin_eunit.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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) eunit $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-include-lib-deps: 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 new library the_app"
  67. $t $(MAKE) -C $(APP) new-lib in=the_app $v
  68. $i "Add Cowlib to the list of dependencies of the_app"
  69. $t perl -ni.bak -e 'print;if ($$.==1) {print "DEPS = cowlib\ndep_cowlib_commit = master\n"}' $(APP)/apps/the_app/Makefile
  70. $i "Generate .erl file that uses include_lib()"
  71. $t echo '-module(the). -include_lib("cowlib/include/cow_parse.hrl"). -export([thing/0]). thing() -> true.' > $(APP)/apps/the_app/src/the.erl
  72. $i "Run eunit"
  73. $t $(MAKE) -C $(APP) eunit $v
  74. eunit-apps-one-app-tested: init
  75. $i "Bootstrap a new OTP library named $(APP)"
  76. $t mkdir $(APP)/
  77. $t cp ../erlang.mk $(APP)/
  78. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  79. $i "Create a new application named my_app"
  80. $t $(MAKE) -C $(APP) new-app in=my_app $v
  81. $i "Create a new library named my_lib"
  82. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  83. $i "Generate a module containing EUnit tests in my_app"
  84. $t printf "%s\n" \
  85. "-module(my_app)." \
  86. "-ifdef(TEST)." \
  87. "-include_lib(\"eunit/include/eunit.hrl\")." \
  88. "ok_test() -> ok." \
  89. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  90. $i "Generate a module containing EUnit tests in my_lib"
  91. $t printf "%s\n" \
  92. "-module(my_lib)." \
  93. "-ifdef(TEST)." \
  94. "-include_lib(\"eunit/include/eunit.hrl\")." \
  95. "ok_test() -> ok." \
  96. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  97. $i "Run EUnit on my_app only"
  98. $t $(MAKE) -C $(APP)/apps/my_app eunit | grep -c "Test passed." | grep -q 1
  99. eunit-apps-only: init
  100. $i "Create a multi application repository with no root application"
  101. $t mkdir $(APP)/
  102. $t cp ../erlang.mk $(APP)/
  103. $t echo "include erlang.mk" > $(APP)/Makefile
  104. $i "Create a new application named my_app"
  105. $t $(MAKE) -C $(APP) new-app in=my_app $v
  106. $i "Create a new library named my_lib"
  107. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  108. $i "Check that EUnit detects no tests"
  109. $t $(MAKE) -C $(APP) eunit | grep -c "There were no tests to run." | grep -q 2
  110. $i "Generate a module containing EUnit tests in my_app"
  111. $t printf "%s\n" \
  112. "-module(my_app)." \
  113. "-ifdef(TEST)." \
  114. "-include_lib(\"eunit/include/eunit.hrl\")." \
  115. "ok_test() -> ok." \
  116. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  117. $i "Generate a module containing EUnit tests in my_lib"
  118. $t printf "%s\n" \
  119. "-module(my_lib)." \
  120. "-ifdef(TEST)." \
  121. "-include_lib(\"eunit/include/eunit.hrl\")." \
  122. "ok_test() -> ok." \
  123. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  124. $i "Check that EUnit runs tests"
  125. $t $(MAKE) -C $(APP) eunit | grep -c "Test passed." | grep -q 2
  126. eunit-apps-only-error: init
  127. $i "Create a multi application repository with no root application"
  128. $t mkdir $(APP)/
  129. $t cp ../erlang.mk $(APP)/
  130. $t echo "include erlang.mk" > $(APP)/Makefile
  131. $i "Create a new application named my_app1"
  132. $t $(MAKE) -C $(APP) new-app in=my_app1 $v
  133. $i "Create a new application named my_app2"
  134. $t $(MAKE) -C $(APP) new-app in=my_app2 $v
  135. $i "Create a new library named my_lib"
  136. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  137. $i "Check that EUnit detects no tests"
  138. $t $(MAKE) -C $(APP) eunit | grep -c "There were no tests to run." | grep -q 3
  139. $i "Generate a module containing broken EUnit tests in my_app1"
  140. $t printf "%s\n" \
  141. "-module(my_app1)." \
  142. "-ifdef(TEST)." \
  143. "-include_lib(\"eunit/include/eunit.hrl\")." \
  144. "bad_test() -> ?assert(0 =:= 1)." \
  145. "-endif." > $(APP)/apps/my_app1/src/my_app1.erl
  146. $i "Generate a module containing EUnit good tests in my_app2"
  147. $t printf "%s\n" \
  148. "-module(my_app2)." \
  149. "-ifdef(TEST)." \
  150. "-include_lib(\"eunit/include/eunit.hrl\")." \
  151. "ok_test() -> ok." \
  152. "-endif." > $(APP)/apps/my_app2/src/my_app2.erl
  153. $i "Generate a module containing EUnit tests in my_lib"
  154. $t printf "%s\n" \
  155. "-module(my_lib)." \
  156. "-ifdef(TEST)." \
  157. "-include_lib(\"eunit/include/eunit.hrl\")." \
  158. "ok_test() -> ok." \
  159. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  160. $i "Check exit code of EUnit for the module with broken test should be non-zero"
  161. $t ! $(MAKE) -C $(APP)/apps/my_app1 eunit $v
  162. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  163. $t ! $(MAKE) -C $(APP) eunit $v
  164. eunit-check: init
  165. $i "Bootstrap a new OTP application named $(APP)"
  166. $t mkdir $(APP)/
  167. $t cp ../erlang.mk $(APP)/
  168. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  169. $i "Generate a module containing EUnit tests"
  170. $t printf "%s\n" \
  171. "-module($(APP))." \
  172. "-ifdef(TEST)." \
  173. "-include_lib(\"eunit/include/eunit.hrl\")." \
  174. "ok_test() -> ok." \
  175. "-endif." > $(APP)/src/$(APP).erl
  176. $i "Check that EUnit runs on 'make check'"
  177. $t $(MAKE) -C $(APP) check | grep -c "Test passed." | grep -q 1
  178. eunit-erl-opts: init
  179. $i "Bootstrap a new OTP application named $(APP)"
  180. $t mkdir $(APP)/
  181. $t cp ../erlang.mk $(APP)/
  182. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  183. $i "Set EUNIT_ERL_OPTS in the Makefile"
  184. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  185. $i "Generate a module containing EUnit tests"
  186. $t printf "%s\n" \
  187. "-module($(APP))." \
  188. "-ifdef(TEST)." \
  189. "-include_lib(\"eunit/include/eunit.hrl\")." \
  190. "ok_test() -> ok." \
  191. "-endif." > $(APP)/src/$(APP).erl
  192. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  193. $t $(MAKE) -C $(APP) eunit | grep -c "hello" | grep -q 1
  194. eunit-fun: init
  195. $i "Bootstrap a new OTP application named $(APP)"
  196. $t mkdir $(APP)/
  197. $t cp ../erlang.mk $(APP)/
  198. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  199. $i "Generate a module containing EUnit tests"
  200. $t printf "%s\n" \
  201. "-module($(APP))." \
  202. "-ifdef(TEST)." \
  203. "-include_lib(\"eunit/include/eunit.hrl\")." \
  204. "ok_test() -> ok." \
  205. "bad_test() -> throw(fail)." \
  206. "-endif." > $(APP)/src/$(APP).erl
  207. $i "Check that we can run EUnit on a specific test"
  208. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  209. eunit-mod: init
  210. $i "Bootstrap a new OTP application named $(APP)"
  211. $t mkdir $(APP)/
  212. $t cp ../erlang.mk $(APP)/
  213. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  214. $i "Generate a module containing EUnit tests"
  215. $t printf "%s\n" \
  216. "-module($(APP))." \
  217. "-ifdef(TEST)." \
  218. "-include_lib(\"eunit/include/eunit.hrl\")." \
  219. "ok_test() -> ok." \
  220. "-endif." > $(APP)/src/$(APP).erl
  221. $i "Generate a module containing failing EUnit tests"
  222. $t printf "%s\n" \
  223. "-module($(APP)_fail)." \
  224. "-ifdef(TEST)." \
  225. "-include_lib(\"eunit/include/eunit.hrl\")." \
  226. "bad_test() -> throw(fail)." \
  227. "-endif." > $(APP)/src/$(APP)_fail.erl
  228. $i "Check that we can run EUnit on a specific module"
  229. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  230. eunit-priv: init
  231. $i "Bootstrap a new OTP application named $(APP)"
  232. $t mkdir $(APP)/
  233. $t cp ../erlang.mk $(APP)/
  234. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  235. $i "Generate a module containing EUnit tests"
  236. $t printf "%s\n" \
  237. "-module($(APP))." \
  238. "-ifdef(TEST)." \
  239. "-include_lib(\"eunit/include/eunit.hrl\")." \
  240. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  241. "-endif." > $(APP)/src/$(APP).erl
  242. $i "Check that EUnit can resolve the priv_dir"
  243. $t $(MAKE) -C $(APP) tests | grep -c "Test passed." | grep -q 1
  244. eunit-test-dir: init
  245. $i "Bootstrap a new OTP application named $(APP)"
  246. $t mkdir $(APP)/
  247. $t cp ../erlang.mk $(APP)/
  248. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  249. $i "Generate a module containing EUnit tests"
  250. $t printf "%s\n" \
  251. "-module($(APP))." \
  252. "-ifdef(TEST)." \
  253. "-include_lib(\"eunit/include/eunit.hrl\")." \
  254. "log_test() -> file:write_file(\"eunit.log\", \"$(APP)\n\", [append])." \
  255. "-endif." > $(APP)/src/$(APP).erl
  256. $i "Generate a module containing EUnit tests in TEST_DIR"
  257. $t mkdir $(APP)/test
  258. $t printf "%s\n" \
  259. "-module($(APP)_tests)." \
  260. "-include_lib(\"eunit/include/eunit.hrl\")." \
  261. "log_test() -> file:write_file(\"eunit.log\", \"$(APP)_tests\n\", [append])." > $(APP)/test/$(APP)_tests.erl
  262. $i "Check that EUnit runs both tests"
  263. $t $(MAKE) -C $(APP) eunit | grep -c "2 tests passed." | grep -q 1
  264. $i "Check that tests were both run only once"
  265. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  266. eunit-tests: init
  267. $i "Bootstrap a new OTP application named $(APP)"
  268. $t mkdir $(APP)/
  269. $t cp ../erlang.mk $(APP)/
  270. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  271. $i "Generate a module containing EUnit tests"
  272. $t printf "%s\n" \
  273. "-module($(APP))." \
  274. "-ifdef(TEST)." \
  275. "-include_lib(\"eunit/include/eunit.hrl\")." \
  276. "ok_test() -> ok." \
  277. "-endif." > $(APP)/src/$(APP).erl
  278. $i "Check that EUnit runs on 'make tests'"
  279. $t $(MAKE) -C $(APP) tests | grep -c "Test passed." | grep -q 1