Makefile 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (c) 2014, Viktor Söderqvist <viktor@zuiderkwast.se>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. # Tests for erlang.mk targets. If any test fails or if you run a target other
  4. # than 'all', you must probably do 'make clean' before you can test again.
  5. # Verbosity.
  6. V ?= 0
  7. # t = Verbosity control for tests
  8. # v = Verbosity control for erlang.mk
  9. # i = Command to display (or suppress) info messages
  10. ifeq ($V,0)
  11. # Show info messages only
  12. t = @
  13. v = V=0 &>/dev/null
  14. i = @echo
  15. else ifeq ($V,1)
  16. # Show test commands
  17. t =
  18. v = V=0 &>/dev/null
  19. i = @echo ==
  20. else ifeq ($V,2)
  21. # Show briefly what erlang.mk is doing
  22. t = @echo " TEST " $@;
  23. v = V=0
  24. i = @echo ==
  25. else
  26. # Show all commands with maximum verbosity
  27. t =
  28. v = V=1
  29. i = @echo ==
  30. endif
  31. .PHONY: all clean app ct eunit
  32. all: app ct eunit clean
  33. $i '+---------------------+'
  34. $i '| All tests passed. |'
  35. $i '+---------------------+'
  36. clean:
  37. $t rm -rf app1
  38. app: app1
  39. $i "app: Testing the 'app' target."
  40. $t make -C app1 app $v
  41. $i "Checking the modules line in the generated .app file."
  42. $t [ `grep -E "{modules, *\['m'\]}" app1/ebin/app1.app | wc -l` == 1 ]
  43. $t [ -e app1/ebin/m.beam ]
  44. $i "Checking that 'make clean-app' deletes ebin."
  45. $t make -C app1 clean-app $v
  46. $t [ ! -e app1/ebin ]
  47. $i "Checking that 'make app' returns non-zero on compile errors."
  48. $t printf "%s\n" \
  49. "-module(syntax_error)." \
  50. "foo lorem_ipsum dolor sit amet." \
  51. > app1/src/syntax_error.erl
  52. $t if make -C app1 app $v ; then false ; fi
  53. $t rm app1/src/syntax_error.erl
  54. $i "Test 'app' passed."
  55. ct: app1
  56. $i "ct: Testing tests-ct and related targets."
  57. $i "Setting up test suite."
  58. $t mkdir -p app1/test
  59. $t printf "%s\n" \
  60. "-module(m_SUITE)." \
  61. "-export([all/0, testcase1/1])." \
  62. "all() -> [testcase1]." \
  63. "testcase1(_) -> 2 = m:succ(1)." \
  64. > app1/test/m_SUITE.erl
  65. $t make -C app1 tests-ct $v
  66. $i "Checking files created by 'make tests-ct'."
  67. $t [ ! -e app1/test/m_SUITE.beam ]
  68. $t [ -e app1/ebin/m.beam ]
  69. $t [ -e app1/logs ]
  70. $i "Checking that 'make clean-ct' does not delete logs."
  71. $t make -C app1 clean-ct $v
  72. $t [ -e app1/logs ]
  73. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  74. $t make -C app1 ct-m $v
  75. $i "Checking that 'make tests-ct' returns non-zero for a failing suite."
  76. $t printf "%s\n" \
  77. "-module(failing_SUITE)." \
  78. "-export([all/0, testcase1/1])." \
  79. "all() -> [testcase1]." \
  80. "testcase1(_) -> 42 = m:succ(1)." \
  81. > app1/test/failing_SUITE.erl
  82. $t if make -C app1 ct-failing $v ; then false ; fi
  83. $i "Checking that 'make ct-distclean' deletes logs."
  84. $t make -C app1 distclean-ct $v
  85. $t [ ! -e app1/logs ]
  86. $t [ -e app1/ebin/m.beam ]
  87. $i "Cleaning up test data."
  88. $t rm -rf app1/test
  89. $i "Test 'ct' passed."
  90. eunit: app1
  91. $i "eunit: Testing the 'eunit' target."
  92. $t mkdir -p eunit
  93. $i "Running eunit test case inside module src/t.erl"
  94. $t printf '%s\n' \
  95. '-module(t).' \
  96. '-export([succ/1]).' \
  97. 'succ(N) -> N + 1.' \
  98. '-ifdef(TEST).' \
  99. '-include_lib("eunit/include/eunit.hrl").' \
  100. 'succ_test() ->' \
  101. ' ?assertEqual(2, succ(1)),' \
  102. ' os:cmd("echo t >> test-eunit.log").' \
  103. '-endif.' \
  104. > app1/src/t.erl
  105. $t make -C app1 eunit $v
  106. $i "Checking that the eunit test in module t."
  107. $t echo t | cmp app1/test-eunit.log -
  108. $t rm app1/test-eunit.log
  109. $i "Running eunit tests in a separate directory."
  110. $t mkdir -p app1/eunit
  111. $t printf '%s\n' \
  112. '-module(t_tests).' \
  113. '-include_lib("eunit/include/eunit.hrl").' \
  114. 'succ_test() ->' \
  115. ' ?assertEqual(2, t:succ(1)),' \
  116. ' os:cmd("echo t_tests >> test-eunit.log").' \
  117. > app1/eunit/t_tests.erl
  118. $t printf '%s\n' \
  119. '-module(x_tests).' \
  120. '-include_lib("eunit/include/eunit.hrl").' \
  121. 'succ_test() ->' \
  122. ' ?assertEqual(2, t:succ(1)),' \
  123. ' os:cmd("echo x_tests >> test-eunit.log").' \
  124. > app1/eunit/x_tests.erl
  125. $t make -C app1 eunit EUNIT_DIR=eunit $v
  126. $i "Checking that 'make eunit' didn't run the tests in t_tests twice, etc."
  127. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  128. $t rm app1/test-eunit.log
  129. $i "Checking that 'make eunit' returns non-zero for a failing test."
  130. $t rm -f app1/eunit/*
  131. $t printf "%s\n" \
  132. "-module(t_tests)." \
  133. '-include_lib("eunit/include/eunit.hrl").' \
  134. "succ_test() ->" \
  135. " ?assertEqual(42, t:succ(1))." \
  136. > app1/eunit/t_tests.erl
  137. $t if make -C app1 eunit EUNIT_DIR=eunit $v ; then false ; fi
  138. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  139. $i "Test 'eunit' passed."
  140. # Test application used for testing.
  141. app1:
  142. $i "Setting up app."
  143. $t mkdir -p app1
  144. $t cp ../erlang.mk app1/
  145. $t make -C app1 -f erlang.mk bootstrap-lib
  146. $t printf "%s\n" \
  147. "-module(m)." \
  148. "-export([succ/1])." \
  149. "succ(N) -> N + 1." \
  150. > app1/src/m.erl