plugin_eunit.mk 7.9 KB

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