plugin_eunit.mk 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # EUnit plugin.
  2. EUNIT_CASES = all apps-only check erl-opts fun mod test-dir tests
  3. EUNIT_TARGETS = $(addprefix eunit-,$(EUNIT_CASES))
  4. EUNIT_CLEAN_TARGETS = $(addprefix clean-,$(EUNIT_TARGETS))
  5. .PHONY: eunit $(EUNIT_TARGETS) clean-eunit $(EUNIT_CLEAN_TARGETS)
  6. clean-eunit: $(EUNIT_CLEAN_TARGETS)
  7. $(EUNIT_CLEAN_TARGETS):
  8. $t rm -rf $(APP_TO_CLEAN)
  9. eunit: $(EUNIT_TARGETS)
  10. eunit-all: build clean-eunit-all
  11. $i "Bootstrap a new OTP application named $(APP)"
  12. $t mkdir $(APP)/
  13. $t cp ../erlang.mk $(APP)/
  14. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  15. $i "Check that EUnit detects no tests"
  16. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  17. $i "Generate a module containing EUnit tests"
  18. $t printf "%s\n" \
  19. "-module($(APP))." \
  20. "-ifdef(TEST)." \
  21. "-include_lib(\"eunit/include/eunit.hrl\")." \
  22. "ok_test() -> ok." \
  23. "-endif." > $(APP)/src/$(APP).erl
  24. $i "Build the project cleanly"
  25. $t $(MAKE) -C $(APP) clean $v
  26. $t $(MAKE) -C $(APP) $v
  27. $i "Check that no EUnit test cases were exported"
  28. $t $(ERL) -pa $(APP)/ebin -eval 'code:load_file($(APP)), false = erlang:function_exported($(APP), ok_test, 0), halt()'
  29. $i "Check that EUnit runs tests"
  30. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  31. $i "Add a failing test to the module"
  32. $t printf "%s\n" \
  33. "-ifdef(TEST)." \
  34. "bad_test() -> throw(fail)." \
  35. "-endif." >> $(APP)/src/$(APP).erl
  36. $i "Check that EUnit errors out"
  37. $t ! $(MAKE) -C $(APP) eunit $v
  38. eunit-apps-only: build clean-eunit-apps-only
  39. $i "Create a multi application repository with no root application"
  40. $t mkdir $(APP)/
  41. $t cp ../erlang.mk $(APP)/
  42. $t echo "include erlang.mk" > $(APP)/Makefile
  43. $i "Create a new application named my_app"
  44. $t $(MAKE) -C $(APP) new-app in=my_app $v
  45. $i "Create a new library named my_lib"
  46. $t $(MAKE) -C $(APP) new-lib in=my_lib $v
  47. $i "Check that EUnit detects no tests"
  48. $t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
  49. $i "Generate a module containing EUnit tests in my_app"
  50. $t printf "%s\n" \
  51. "-module(my_app)." \
  52. "-ifdef(TEST)." \
  53. "-include_lib(\"eunit/include/eunit.hrl\")." \
  54. "ok_test() -> ok." \
  55. "-endif." > $(APP)/apps/my_app/src/my_app.erl
  56. $i "Generate a module containing EUnit tests in my_lib"
  57. $t printf "%s\n" \
  58. "-module(my_lib)." \
  59. "-ifdef(TEST)." \
  60. "-include_lib(\"eunit/include/eunit.hrl\")." \
  61. "ok_test() -> ok." \
  62. "-endif." > $(APP)/apps/my_lib/src/my_lib.erl
  63. $i "Check that EUnit runs tests"
  64. $t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
  65. eunit-check: build clean-eunit-check
  66. $i "Bootstrap a new OTP application named $(APP)"
  67. $t mkdir $(APP)/
  68. $t cp ../erlang.mk $(APP)/
  69. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  70. $i "Generate a module containing EUnit tests"
  71. $t printf "%s\n" \
  72. "-module($(APP))." \
  73. "-ifdef(TEST)." \
  74. "-include_lib(\"eunit/include/eunit.hrl\")." \
  75. "ok_test() -> ok." \
  76. "-endif." > $(APP)/src/$(APP).erl
  77. $i "Check that EUnit runs on 'make check'"
  78. $t $(MAKE) -C $(APP) check | grep -q "Test passed."
  79. eunit-erl-opts: build clean-eunit-erl-opts
  80. $i "Bootstrap a new OTP application named $(APP)"
  81. $t mkdir $(APP)/
  82. $t cp ../erlang.mk $(APP)/
  83. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  84. $i "Set EUNIT_ERL_OPTS in the Makefile"
  85. $t perl -ni.bak -e 'print;if ($$.==1) {print "EUNIT_ERL_OPTS = -eval \"erlang:display(hello).\" \n"}' $(APP)/Makefile
  86. $i "Generate a module containing EUnit tests"
  87. $t printf "%s\n" \
  88. "-module($(APP))." \
  89. "-ifdef(TEST)." \
  90. "-include_lib(\"eunit/include/eunit.hrl\")." \
  91. "ok_test() -> ok." \
  92. "-endif." > $(APP)/src/$(APP).erl
  93. $i "Check that EUnit uses EUNIT_ERL_OPTS"
  94. $t $(MAKE) -C $(APP) eunit | grep -q "hello"
  95. eunit-fun: build clean-eunit-fun
  96. $i "Bootstrap a new OTP application named $(APP)"
  97. $t mkdir $(APP)/
  98. $t cp ../erlang.mk $(APP)/
  99. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  100. $i "Generate a module containing EUnit tests"
  101. $t printf "%s\n" \
  102. "-module($(APP))." \
  103. "-ifdef(TEST)." \
  104. "-include_lib(\"eunit/include/eunit.hrl\")." \
  105. "ok_test() -> ok." \
  106. "bad_test() -> throw(fail)." \
  107. "-endif." > $(APP)/src/$(APP).erl
  108. $i "Check that we can run EUnit on a specific test"
  109. $t $(MAKE) -C $(APP) eunit t=$(APP):ok_test $v
  110. eunit-mod: build clean-eunit-mod
  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 "Generate a module containing failing EUnit tests"
  123. $t printf "%s\n" \
  124. "-module($(APP)_fail)." \
  125. "-ifdef(TEST)." \
  126. "-include_lib(\"eunit/include/eunit.hrl\")." \
  127. "bad_test() -> throw(fail)." \
  128. "-endif." > $(APP)/src/$(APP)_fail.erl
  129. $i "Check that we can run EUnit on a specific module"
  130. $t $(MAKE) -C $(APP) eunit t=$(APP) $v
  131. eunit-test-dir: build clean-eunit-test-dir
  132. $i "Bootstrap a new OTP application named $(APP)"
  133. $t mkdir $(APP)/
  134. $t cp ../erlang.mk $(APP)/
  135. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  136. $i "Generate a module containing EUnit tests"
  137. $t printf "%s\n" \
  138. "-module($(APP))." \
  139. "-ifdef(TEST)." \
  140. "-include_lib(\"eunit/include/eunit.hrl\")." \
  141. "log_test() -> os:cmd(\"echo $(APP) >> eunit.log\")." \
  142. "-endif." > $(APP)/src/$(APP).erl
  143. $i "Generate a module containing EUnit tests in TEST_DIR"
  144. $t mkdir $(APP)/test
  145. $t printf "%s\n" \
  146. "-module($(APP)_tests)." \
  147. "-include_lib(\"eunit/include/eunit.hrl\")." \
  148. "log_test() -> os:cmd(\"echo $(APP)_tests >> eunit.log\")." > $(APP)/test/$(APP)_tests.erl
  149. $i "Check that EUnit runs both tests"
  150. $t $(MAKE) -C $(APP) eunit | grep -q "2 tests passed."
  151. $i "Check that tests were both run only once"
  152. $t printf "%s\n" $(APP) $(APP)_tests | cmp $(APP)/eunit.log -
  153. eunit-tests: build clean-eunit-tests
  154. $i "Bootstrap a new OTP application named $(APP)"
  155. $t mkdir $(APP)/
  156. $t cp ../erlang.mk $(APP)/
  157. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
  158. $i "Generate a module containing EUnit tests"
  159. $t printf "%s\n" \
  160. "-module($(APP))." \
  161. "-ifdef(TEST)." \
  162. "-include_lib(\"eunit/include/eunit.hrl\")." \
  163. "ok_test() -> ok." \
  164. "-endif." > $(APP)/src/$(APP).erl
  165. $i "Check that EUnit runs on 'make tests'"
  166. $t $(MAKE) -C $(APP) tests | grep -q "Test passed."