plugin_eunit.mk 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # EUnit plugin.
  2. EUNIT_CASES = all apps-only apps-only-error check erl-opts fun mod priv test-dir tests
  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-apps-only-error: 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 ! $(MAKE) -C $(APP)/apps/my_app1 eunit $v
  98. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  99. $t ! $(MAKE) -C $(APP) eunit $v
  100. eunit-check: build clean
  101. $i "Bootstrap a new OTP application named $(APP)"
  102. $t mkdir $(APP)/
  103. $t cp ../erlang.mk $(APP)/
  104. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  105. $i "Generate a module containing EUnit tests"
  106. $t printf "%s\n" \
  107. "-module($(APP))." \
  108. "-ifdef(TEST)." \
  109. "-include_lib(\"eunit/include/eunit.hrl\")." \
  110. "ok_test() -> ok." \
  111. "-endif." > $(APP)/src/$(APP).erl
  112. $i "Check that EUnit runs on 'make check'"
  113. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  114. eunit-erl-opts: build clean
  115. $i "Bootstrap a new OTP application named $(APP)"
  116. $t mkdir $(APP)/
  117. $t cp ../erlang.mk $(APP)/
  118. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  119. $i "Set EUNIT_ERL_OPTS in the Makefile"
  120. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  121. $i "Generate a module containing EUnit tests"
  122. $t printf "%s\n" \
  123. "-module($(APP))." \
  124. "-ifdef(TEST)." \
  125. "-include_lib(\"eunit/include/eunit.hrl\")." \
  126. "ok_test() -> ok." \
  127. "-endif." > $(APP)/src/$(APP).erl
  128. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  129. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  130. eunit-fun: build clean
  131. $i "Bootstrap a new OTP application named $(APP)"
  132. $t mkdir $(APP)/
  133. $t cp ../erlang.mk $(APP)/
  134. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  135. $i "Generate a module containing EUnit tests"
  136. $t printf "%s\n" \
  137. "-module($(APP))." \
  138. "-ifdef(TEST)." \
  139. "-include_lib(\"eunit/include/eunit.hrl\")." \
  140. "ok_test() -> ok." \
  141. "bad_test() -> throw(fail)." \
  142. "-endif." > $(APP)/src/$(APP).erl
  143. $i "Check that we can run EUnit on a specific test"
  144. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  145. eunit-mod: build clean
  146. $i "Bootstrap a new OTP application named $(APP)"
  147. $t mkdir $(APP)/
  148. $t cp ../erlang.mk $(APP)/
  149. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  150. $i "Generate a module containing EUnit tests"
  151. $t printf "%s\n" \
  152. "-module($(APP))." \
  153. "-ifdef(TEST)." \
  154. "-include_lib(\"eunit/include/eunit.hrl\")." \
  155. "ok_test() -> ok." \
  156. "-endif." > $(APP)/src/$(APP).erl
  157. $i "Generate a module containing failing EUnit tests"
  158. $t printf "%s\n" \
  159. "-module($(APP)_fail)." \
  160. "-ifdef(TEST)." \
  161. "-include_lib(\"eunit/include/eunit.hrl\")." \
  162. "bad_test() -> throw(fail)." \
  163. "-endif." > $(APP)/src/$(APP)_fail.erl
  164. $i "Check that we can run EUnit on a specific module"
  165. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  166. eunit-priv: build clean
  167. $i "Bootstrap a new OTP application named $(APP)"
  168. $t mkdir $(APP)/
  169. $t cp ../erlang.mk $(APP)/
  170. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  171. $i "Generate a module containing EUnit tests"
  172. $t printf "%s\n" \
  173. "-module($(APP))." \
  174. "-ifdef(TEST)." \
  175. "-include_lib(\"eunit/include/eunit.hrl\")." \
  176. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  177. "-endif." > $(APP)/src/$(APP).erl
  178. $i "Check that EUnit can resolve the priv_dir"
  179. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."
  180. eunit-test-dir: build clean
  181. $i "Bootstrap a new OTP application named $(APP)"
  182. $t mkdir $(APP)/
  183. $t cp ../erlang.mk $(APP)/
  184. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  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. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  191. "-endif." > $(APP)/src/$(APP).erl
  192. $i "Generate a module containing EUnit tests in TEST_DIR"
  193. $t mkdir $(APP)/test
  194. $t printf "%s\n" \
  195. "-module($(APP)_tests)." \
  196. "-include_lib(\"eunit/include/eunit.hrl\")." \
  197. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  198. $i "Check that EUnit runs both tests"
  199. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  200. $i "Check that tests were both run only once"
  201. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  202. eunit-tests: build clean
  203. $i "Bootstrap a new OTP application named $(APP)"
  204. $t mkdir $(APP)/
  205. $t cp ../erlang.mk $(APP)/
  206. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  207. $i "Generate a module containing EUnit tests"
  208. $t printf "%s\n" \
  209. "-module($(APP))." \
  210. "-ifdef(TEST)." \
  211. "-include_lib(\"eunit/include/eunit.hrl\")." \
  212. "ok_test() -> ok." \
  213. "-endif." > $(APP)/src/$(APP).erl
  214. $i "Check that EUnit runs on 'make tests'"
  215. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."