plugin_eunit.mk 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-only: build clean
  34. $i "Create a multi application repository with no root application"
  35. $t mkdir $(APP)/
  36. $t cp ../erlang.mk $(APP)/
  37. $t echo "include erlang.mk" > $(APP)/Makefile
  38. $i "Create a new application named my_app"
  39. $t $(MAKE) -C $(APP) new-app in=my_app $v
  40. $i "Create a new library named my_lib"
  41. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  42. $i "Check that EUnit detects no tests"
  43. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  44. $i "Generate a module containing EUnit tests in my_app"
  45. $t printf "%s\n" \
  46. "-module(my_app)." \
  47. "-ifdef(TEST)." \
  48. "-include_lib(\"eunit/include/eunit.hrl\")." \
  49. "ok_test() -> ok." \
  50. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  51. $i "Generate a module containing EUnit tests in my_lib"
  52. $t printf "%s\n" \
  53. "-module(my_lib)." \
  54. "-ifdef(TEST)." \
  55. "-include_lib(\"eunit/include/eunit.hrl\")." \
  56. "ok_test() -> ok." \
  57. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  58. $i "Check that EUnit runs tests"
  59. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  60. $i "Check that EUnit runs tests"
  61. eunit-apps-only-error: 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_app1"
  67. $t $(MAKE) -C $(APP) new-app in=my_app1 $v
  68. $i "Create a new application named my_app2"
  69. $t $(MAKE) -C $(APP) new-app in=my_app2 $v
  70. $i "Create a new library named my_lib"
  71. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  72. $i "Check that EUnit detects no tests"
  73. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  74. $i "Generate a module containing broken EUnit tests in my_app1"
  75. $t printf "%s\n" \
  76. "-module(my_app1)." \
  77. "-ifdef(TEST)." \
  78. "-include_lib(\"eunit/include/eunit.hrl\")." \
  79. "bad_test() -> ?assert(0 =:= 1)." \
  80. "-endif." > $(APP)/apps/my_app1/src/my_app1.erl
  81. $i "Generate a module containing EUnit good tests in my_app2"
  82. $t printf "%s\n" \
  83. "-module(my_app2)." \
  84. "-ifdef(TEST)." \
  85. "-include_lib(\"eunit/include/eunit.hrl\")." \
  86. "ok_test() -> ok." \
  87. "-endif." > $(APP)/apps/my_app2/src/my_app2.erl
  88. $i "Generate a module containing EUnit tests in my_lib"
  89. $t printf "%s\n" \
  90. "-module(my_lib)." \
  91. "-ifdef(TEST)." \
  92. "-include_lib(\"eunit/include/eunit.hrl\")." \
  93. "ok_test() -> ok." \
  94. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  95. $i "Check exit code of EUnit for the module with broken test should be non-zero"
  96. $t ! $(MAKE) -C $(APP)/apps/my_app1 eunit $v
  97. $i "Check exit code of EUnit for the whole project with broken test should be non-zero"
  98. $t ! $(MAKE) -C $(APP) eunit $v
  99. eunit-check: build clean
  100. $i "Bootstrap a new OTP application named $(APP)"
  101. $t mkdir $(APP)/
  102. $t cp ../erlang.mk $(APP)/
  103. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  104. $i "Generate a module containing EUnit tests"
  105. $t printf "%s\n" \
  106. "-module($(APP))." \
  107. "-ifdef(TEST)." \
  108. "-include_lib(\"eunit/include/eunit.hrl\")." \
  109. "ok_test() -> ok." \
  110. "-endif." > $(APP)/src/$(APP).erl
  111. $i "Check that EUnit runs on 'make check'"
  112. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  113. eunit-erl-opts: build clean
  114. $i "Bootstrap a new OTP application named $(APP)"
  115. $t mkdir $(APP)/
  116. $t cp ../erlang.mk $(APP)/
  117. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  118. $i "Set EUNIT_ERL_OPTS in the Makefile"
  119. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  120. $i "Generate a module containing EUnit tests"
  121. $t printf "%s\n" \
  122. "-module($(APP))." \
  123. "-ifdef(TEST)." \
  124. "-include_lib(\"eunit/include/eunit.hrl\")." \
  125. "ok_test() -> ok." \
  126. "-endif." > $(APP)/src/$(APP).erl
  127. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  128. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  129. eunit-fun: build clean
  130. $i "Bootstrap a new OTP application named $(APP)"
  131. $t mkdir $(APP)/
  132. $t cp ../erlang.mk $(APP)/
  133. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  134. $i "Generate a module containing EUnit tests"
  135. $t printf "%s\n" \
  136. "-module($(APP))." \
  137. "-ifdef(TEST)." \
  138. "-include_lib(\"eunit/include/eunit.hrl\")." \
  139. "ok_test() -> ok." \
  140. "bad_test() -> throw(fail)." \
  141. "-endif." > $(APP)/src/$(APP).erl
  142. $i "Check that we can run EUnit on a specific test"
  143. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  144. eunit-mod: build clean
  145. $i "Bootstrap a new OTP application named $(APP)"
  146. $t mkdir $(APP)/
  147. $t cp ../erlang.mk $(APP)/
  148. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  149. $i "Generate a module containing EUnit tests"
  150. $t printf "%s\n" \
  151. "-module($(APP))." \
  152. "-ifdef(TEST)." \
  153. "-include_lib(\"eunit/include/eunit.hrl\")." \
  154. "ok_test() -> ok." \
  155. "-endif." > $(APP)/src/$(APP).erl
  156. $i "Generate a module containing failing EUnit tests"
  157. $t printf "%s\n" \
  158. "-module($(APP)_fail)." \
  159. "-ifdef(TEST)." \
  160. "-include_lib(\"eunit/include/eunit.hrl\")." \
  161. "bad_test() -> throw(fail)." \
  162. "-endif." > $(APP)/src/$(APP)_fail.erl
  163. $i "Check that we can run EUnit on a specific module"
  164. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  165. eunit-priv: build clean
  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 "Generate a module containing EUnit tests"
  171. $t printf "%s\n" \
  172. "-module($(APP))." \
  173. "-ifdef(TEST)." \
  174. "-include_lib(\"eunit/include/eunit.hrl\")." \
  175. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  176. "-endif." > $(APP)/src/$(APP).erl
  177. $i "Check that EUnit can resolve the priv_dir"
  178. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."
  179. eunit-test-dir: build clean
  180. $i "Bootstrap a new OTP application named $(APP)"
  181. $t mkdir $(APP)/
  182. $t cp ../erlang.mk $(APP)/
  183. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  184. $i "Generate a module containing EUnit tests"
  185. $t printf "%s\n" \
  186. "-module($(APP))." \
  187. "-ifdef(TEST)." \
  188. "-include_lib(\"eunit/include/eunit.hrl\")." \
  189. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  190. "-endif." > $(APP)/src/$(APP).erl
  191. $i "Generate a module containing EUnit tests in TEST_DIR"
  192. $t mkdir $(APP)/test
  193. $t printf "%s\n" \
  194. "-module($(APP)_tests)." \
  195. "-include_lib(\"eunit/include/eunit.hrl\")." \
  196. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  197. $i "Check that EUnit runs both tests"
  198. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  199. $i "Check that tests were both run only once"
  200. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  201. eunit-tests: build clean
  202. $i "Bootstrap a new OTP application named $(APP)"
  203. $t mkdir $(APP)/
  204. $t cp ../erlang.mk $(APP)/
  205. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  206. $i "Generate a module containing EUnit tests"
  207. $t printf "%s\n" \
  208. "-module($(APP))." \
  209. "-ifdef(TEST)." \
  210. "-include_lib(\"eunit/include/eunit.hrl\")." \
  211. "ok_test() -> ok." \
  212. "-endif." > $(APP)/src/$(APP).erl
  213. $i "Check that EUnit runs on 'make tests'"
  214. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."