plugin_eunit.mk 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # EUnit plugin.
  2. EUNIT_CASES = all apps-only 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. eunit-check: build clean
  62. $i "Bootstrap a new OTP application named $(APP)"
  63. $t mkdir $(APP)/
  64. $t cp ../erlang.mk $(APP)/
  65. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  66. $i "Generate a module containing EUnit tests"
  67. $t printf "%s\n" \
  68. "-module($(APP))." \
  69. "-ifdef(TEST)." \
  70. "-include_lib(\"eunit/include/eunit.hrl\")." \
  71. "ok_test() -> ok." \
  72. "-endif." > $(APP)/src/$(APP).erl
  73. $i "Check that EUnit runs on 'make check'"
  74. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  75. eunit-erl-opts: build clean
  76. $i "Bootstrap a new OTP application named $(APP)"
  77. $t mkdir $(APP)/
  78. $t cp ../erlang.mk $(APP)/
  79. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  80. $i "Set EUNIT_ERL_OPTS in the Makefile"
  81. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  82. $i "Generate a module containing EUnit tests"
  83. $t printf "%s\n" \
  84. "-module($(APP))." \
  85. "-ifdef(TEST)." \
  86. "-include_lib(\"eunit/include/eunit.hrl\")." \
  87. "ok_test() -> ok." \
  88. "-endif." > $(APP)/src/$(APP).erl
  89. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  90. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  91. eunit-fun: build clean
  92. $i "Bootstrap a new OTP application named $(APP)"
  93. $t mkdir $(APP)/
  94. $t cp ../erlang.mk $(APP)/
  95. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  96. $i "Generate a module containing EUnit tests"
  97. $t printf "%s\n" \
  98. "-module($(APP))." \
  99. "-ifdef(TEST)." \
  100. "-include_lib(\"eunit/include/eunit.hrl\")." \
  101. "ok_test() -> ok." \
  102. "bad_test() -> throw(fail)." \
  103. "-endif." > $(APP)/src/$(APP).erl
  104. $i "Check that we can run EUnit on a specific test"
  105. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  106. eunit-mod: build clean
  107. $i "Bootstrap a new OTP application named $(APP)"
  108. $t mkdir $(APP)/
  109. $t cp ../erlang.mk $(APP)/
  110. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  111. $i "Generate a module containing EUnit tests"
  112. $t printf "%s\n" \
  113. "-module($(APP))." \
  114. "-ifdef(TEST)." \
  115. "-include_lib(\"eunit/include/eunit.hrl\")." \
  116. "ok_test() -> ok." \
  117. "-endif." > $(APP)/src/$(APP).erl
  118. $i "Generate a module containing failing EUnit tests"
  119. $t printf "%s\n" \
  120. "-module($(APP)_fail)." \
  121. "-ifdef(TEST)." \
  122. "-include_lib(\"eunit/include/eunit.hrl\")." \
  123. "bad_test() -> throw(fail)." \
  124. "-endif." > $(APP)/src/$(APP)_fail.erl
  125. $i "Check that we can run EUnit on a specific module"
  126. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  127. eunit-priv: build clean
  128. $i "Bootstrap a new OTP application named $(APP)"
  129. $t mkdir $(APP)/
  130. $t cp ../erlang.mk $(APP)/
  131. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  132. $i "Generate a module containing EUnit tests"
  133. $t printf "%s\n" \
  134. "-module($(APP))." \
  135. "-ifdef(TEST)." \
  136. "-include_lib(\"eunit/include/eunit.hrl\")." \
  137. "ok_test() -> ?assert(is_list(code:priv_dir($(APP))) =:= true)." \
  138. "-endif." > $(APP)/src/$(APP).erl
  139. $i "Check that EUnit can resolve the priv_dir"
  140. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."
  141. eunit-test-dir: build clean
  142. $i "Bootstrap a new OTP application named $(APP)"
  143. $t mkdir $(APP)/
  144. $t cp ../erlang.mk $(APP)/
  145. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  146. $i "Generate a module containing EUnit tests"
  147. $t printf "%s\n" \
  148. "-module($(APP))." \
  149. "-ifdef(TEST)." \
  150. "-include_lib(\"eunit/include/eunit.hrl\")." \
  151. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  152. "-endif." > $(APP)/src/$(APP).erl
  153. $i "Generate a module containing EUnit tests in TEST_DIR"
  154. $t mkdir $(APP)/test
  155. $t printf "%s\n" \
  156. "-module($(APP)_tests)." \
  157. "-include_lib(\"eunit/include/eunit.hrl\")." \
  158. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  159. $i "Check that EUnit runs both tests"
  160. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  161. $i "Check that tests were both run only once"
  162. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  163. eunit-tests: build clean
  164. $i "Bootstrap a new OTP application named $(APP)"
  165. $t mkdir $(APP)/
  166. $t cp ../erlang.mk $(APP)/
  167. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  168. $i "Generate a module containing EUnit tests"
  169. $t printf "%s\n" \
  170. "-module($(APP))." \
  171. "-ifdef(TEST)." \
  172. "-include_lib(\"eunit/include/eunit.hrl\")." \
  173. "ok_test() -> ok." \
  174. "-endif." > $(APP)/src/$(APP).erl
  175. $i "Check that EUnit runs on 'make tests'"
  176. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."