plugin_eunit.mk 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # EUnit plugin.
  2. EUNIT_CASES = all apps-only check erl-opts fun mod priv test-dir tests multiapps-no-root-check-exit-code
  3. EUNIT_TARGETS = $(addprefix eunit-,$(EUNIT_CASES))
  4. .PHONY: eunit $(EUNIT_TARGETS)
  5. eunit: $(EUNIT_TARGETS)
  6. eunit-all: build clean
  7. $i "Bootstrap a new OTP application named $(APP)"
  8. $t mkdir $(APP)/
  9. $t cp ../erlang.mk $(APP)/
  10. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  11. $i "Check that EUnit detects no tests"
  12. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  13. $i "Generate a module containing EUnit tests"
  14. $t printf "%s\n" \
  15. "-module($(APP))." \
  16. "-ifdef(TEST)." \
  17. "-include_lib(\"eunit/include/eunit.hrl\")." \
  18. "ok_test() -> ok." \
  19. "-endif." > $(APP)/src/$(APP).erl
  20. $i "Build the project cleanly"
  21. $t $(MAKE) -C $(APP) clean $v
  22. $t $(MAKE) -C $(APP) $v
  23. $i "Check that no EUnit test cases were exported"
  24. $t $(ERL) -pa $(APP)/ebin -eval 'code:load_file($(APP)), false = erlang:function_exported($(APP), ok_test, 0), halt()'
  25. $i "Check that EUnit runs tests"
  26. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  27. $i "Add a failing test to the module"
  28. $t printf "%s\n" \
  29. "-ifdef(TEST)." \
  30. "bad_test() -> throw(fail)." \
  31. "-endif." >> $(APP)/src/$(APP).erl
  32. $i "Check that EUnit errors out"
  33. $t ! $(MAKE) -C $(APP) eunit $v
  34. eunit-apps-only: build clean
  35. $i "Create a multi application repository with no root application"
  36. $t mkdir $(APP)/
  37. $t cp ../erlang.mk $(APP)/
  38. $t echo "include erlang.mk" > $(APP)/Makefile
  39. $i "Create a new application named my_app"
  40. $t $(MAKE) -C $(APP) new-app in=my_app $v
  41. $i "Create a new library named my_lib"
  42. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  43. $i "Check that EUnit detects no tests"
  44. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  45. $i "Generate a module containing EUnit tests in my_app"
  46. $t printf "%s\n" \
  47. "-module(my_app)." \
  48. "-ifdef(TEST)." \
  49. "-include_lib(\"eunit/include/eunit.hrl\")." \
  50. "ok_test() -> ok." \
  51. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  52. $i "Generate a module containing EUnit tests in my_lib"
  53. $t printf "%s\n" \
  54. "-module(my_lib)." \
  55. "-ifdef(TEST)." \
  56. "-include_lib(\"eunit/include/eunit.hrl\")." \
  57. "ok_test() -> ok." \
  58. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  59. $i "Check that EUnit runs tests"
  60. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  61. $i "Check that EUnit runs tests"
  62. eunit-multiapps-no-root-check-exit-code: build clean
  63. $i "Create a multi application repository with no root application"
  64. $t mkdir $(APP)/
  65. $t cp ../erlang.mk $(APP)/
  66. $t echo "include erlang.mk" > $(APP)/Makefile
  67. $i "Create a new application named my_app1"
  68. $t $(MAKE) -C $(APP) new-app in=my_app1 $v
  69. $i "Create a new application named my_app2"
  70. $t $(MAKE) -C $(APP) new-app in=my_app2 $v
  71. $i "Create a new library named my_lib"
  72. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  73. $i "Check that EUnit detects no tests"
  74. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  75. $i "Generate a module containing broken EUnit tests in my_app1"
  76. $t printf "%s\n" \
  77. "-module(my_app1)." \
  78. "-ifdef(TEST)." \
  79. "-include_lib(\"eunit/include/eunit.hrl\")." \
  80. "bad_test() -> ?assert(0 =:= 1)." \
  81. "-endif." > $(APP)/apps/my_app1/src/my_app1.erl
  82. $i "Generate a module containing EUnit good tests in my_app2"
  83. $t printf "%s\n" \
  84. "-module(my_app2)." \
  85. "-ifdef(TEST)." \
  86. "-include_lib(\"eunit/include/eunit.hrl\")." \
  87. "ok_test() -> ok." \
  88. "-endif." > $(APP)/apps/my_app2/src/my_app2.erl
  89. $i "Generate a module containing EUnit tests in my_lib"
  90. $t printf "%s\n" \
  91. "-module(my_lib)." \
  92. "-ifdef(TEST)." \
  93. "-include_lib(\"eunit/include/eunit.hrl\")." \
  94. "ok_test() -> ok." \
  95. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  96. $i "Check exit code of EUnit for the module with broken test should be non-zero"
  97. $t ( set +e; $(MAKE) -C $(APP)/apps/my_app1 eunit ; if [ $$? -ne 0 ] ; then \
  98. echo "Test passed exit-code is non-zero" ; \
  99. else \
  100. echo "Test failed exit-code is zero" ; \
  101. exit 100; \
  102. fi )
  103. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  104. $t ( set +e; $(MAKE) -C $(APP) eunit ; if [ $$? -ne 0 ] ; then \
  105. echo "Test passed exit-code is non-zero" ; \
  106. else \
  107. echo "Test failed exit-code is zero" ; \
  108. exit 100; \
  109. fi )
  110. eunit-check: build clean
  111. $i "Bootstrap a new OTP application named $(APP)"
  112. $t mkdir $(APP)/
  113. $t cp ../erlang.mk $(APP)/
  114. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  115. $i "Generate a module containing EUnit tests"
  116. $t printf "%s\n" \
  117. "-module($(APP))." \
  118. "-ifdef(TEST)." \
  119. "-include_lib(\"eunit/include/eunit.hrl\")." \
  120. "ok_test() -> ok." \
  121. "-endif." > $(APP)/src/$(APP).erl
  122. $i "Check that EUnit runs on 'make check'"
  123. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  124. eunit-erl-opts: build clean
  125. $i "Bootstrap a new OTP application named $(APP)"
  126. $t mkdir $(APP)/
  127. $t cp ../erlang.mk $(APP)/
  128. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  129. $i "Set EUNIT_ERL_OPTS in the Makefile"
  130. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  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 uses EUNIT_ERL_OPTS"
  139. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  140. eunit-fun: 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 "Generate a module containing EUnit tests"
  146. $t printf "%s\n" \
  147. "-module($(APP))." \
  148. "-ifdef(TEST)." \
  149. "-include_lib(\"eunit/include/eunit.hrl\")." \
  150. "ok_test() -> ok." \
  151. "bad_test() -> throw(fail)." \
  152. "-endif." > $(APP)/src/$(APP).erl
  153. $i "Check that we can run EUnit on a specific test"
  154. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  155. eunit-mod: build clean
  156. $i "Bootstrap a new OTP application named $(APP)"
  157. $t mkdir $(APP)/
  158. $t cp ../erlang.mk $(APP)/
  159. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  160. $i "Generate a module containing EUnit tests"
  161. $t printf "%s\n" \
  162. "-module($(APP))." \
  163. "-ifdef(TEST)." \
  164. "-include_lib(\"eunit/include/eunit.hrl\")." \
  165. "ok_test() -> ok." \
  166. "-endif." > $(APP)/src/$(APP).erl
  167. $i "Generate a module containing failing EUnit tests"
  168. $t printf "%s\n" \
  169. "-module($(APP)_fail)." \
  170. "-ifdef(TEST)." \
  171. "-include_lib(\"eunit/include/eunit.hrl\")." \
  172. "bad_test() -> throw(fail)." \
  173. "-endif." > $(APP)/src/$(APP)_fail.erl
  174. $i "Check that we can run EUnit on a specific module"
  175. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  176. eunit-priv: build clean
  177. $i "Bootstrap a new OTP application named $(APP)"
  178. $t mkdir $(APP)/
  179. $t cp ../erlang.mk $(APP)/
  180. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  181. $i "Generate a module containing EUnit tests"
  182. $t printf "%s\n" \
  183. "-module($(APP))." \
  184. "-ifdef(TEST)." \
  185. "-include_lib(\"eunit/include/eunit.hrl\")." \
  186. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  187. "-endif." > $(APP)/src/$(APP).erl
  188. $i "Check that EUnit can resolve the priv_dir"
  189. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."
  190. eunit-test-dir: build clean
  191. $i "Bootstrap a new OTP application named $(APP)"
  192. $t mkdir $(APP)/
  193. $t cp ../erlang.mk $(APP)/
  194. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  195. $i "Generate a module containing EUnit tests"
  196. $t printf "%s\n" \
  197. "-module($(APP))." \
  198. "-ifdef(TEST)." \
  199. "-include_lib(\"eunit/include/eunit.hrl\")." \
  200. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  201. "-endif." > $(APP)/src/$(APP).erl
  202. $i "Generate a module containing EUnit tests in TEST_DIR"
  203. $t mkdir $(APP)/test
  204. $t printf "%s\n" \
  205. "-module($(APP)_tests)." \
  206. "-include_lib(\"eunit/include/eunit.hrl\")." \
  207. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  208. $i "Check that EUnit runs both tests"
  209. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  210. $i "Check that tests were both run only once"
  211. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  212. eunit-tests: build clean
  213. $i "Bootstrap a new OTP application named $(APP)"
  214. $t mkdir $(APP)/
  215. $t cp ../erlang.mk $(APP)/
  216. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  217. $i "Generate a module containing EUnit tests"
  218. $t printf "%s\n" \
  219. "-module($(APP))." \
  220. "-ifdef(TEST)." \
  221. "-include_lib(\"eunit/include/eunit.hrl\")." \
  222. "ok_test() -> ok." \
  223. "-endif." > $(APP)/src/$(APP).erl
  224. $i "Check that EUnit runs on 'make tests'"
  225. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."