Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

203 linhas
9.2 KiB

  1. -module(rebar_xref_eunit).
  2. -compile(export_all).
  3. -include_lib("eunit/include/eunit.hrl").
  4. -define(REBAR_SCRIPT, "../rebar").
  5. -define(TMP_DIR, "tmp_xref_eunit/").
  6. xref_test_() ->
  7. {"Test the various xref warnings",
  8. setup, fun() -> setup_project(false), rebar("compile"), rebar("skip_deps=true xref") end,
  9. fun teardown/1,
  10. fun(RebarOut) ->
  11. [
  12. {"Undefined function", ?_assert(string:str(RebarOut,
  13. "myapp_somemod:notavailable/1 is undefined function") =/= 0)},
  14. {"Undefined function call", ?_assert(string:str(RebarOut,
  15. "myapp_othermod:somefunc/0 calls undefined function myapp_somemod:notavailable/1") =/= 0)},
  16. {"Deprecated function", ?_assert(string:str(RebarOut,
  17. "myapp_mymod:fdeprecated/0 is deprecated function") =/= 0)},
  18. {"Deprecated function call", ?_assert(string:str(RebarOut,
  19. "myapp_othermod:somefunc/0 calls deprecated function myapp_mymod:fdeprecated/0") =/= 0)},
  20. {"Unused local", ?_assert(string:str(RebarOut,
  21. "myapp_mymod:localfunc2/0 is unused local function") =/= 0)},
  22. {"Unused export 1", ?_assert(string:str(RebarOut,
  23. "myapp_behaviour1:behaviour_info/1 is unused export") =/= 0)},
  24. {"Unused export 2", ?_assert(string:str(RebarOut,
  25. "myapp_behaviour2:behaviour_info/1 is unused export") =/= 0)},
  26. {"Unused export 3", ?_assert(string:str(RebarOut,
  27. "myapp_mymod:other2/1 is unused export") =/= 0)},
  28. {"Unused export 4", ?_assert(string:str(RebarOut,
  29. "myapp_othermod:somefunc/0 is unused export") =/= 0)},
  30. {"Suppressed behaviour export 1", ?_assert(string:str(RebarOut,
  31. "myapp_mymod:bh1_a/1 is unused export") =:= 0)},
  32. {"Suppressed behaviour export 2", ?_assert(string:str(RebarOut,
  33. "myapp_mymod:bh1_b/1 is unused export") =:= 0)},
  34. {"Suppressed behaviour export 3", ?_assert(string:str(RebarOut,
  35. "myapp_mymod:bh2_a/1 is unused export") =:= 0)},
  36. {"Suppressed behaviour export 4", ?_assert(string:str(RebarOut,
  37. "myapp_mymod:bh2_b/1 is unused export") =:= 0)}
  38. ]
  39. end}.
  40. xref_ignore_test_() ->
  41. {"Test the suppression of xref warnings",
  42. setup, fun() -> setup_project(ignore_xref), rebar("compile"), rebar("skip_deps=true xref") end,
  43. fun teardown/1,
  44. fun(RebarOut) ->
  45. [
  46. {"Undefined function can not be suppressed.", ?_assert(string:str(RebarOut,
  47. "myapp_somemod:notavailable/1 is undefined function") =/= 0)},
  48. {"Supppressed undefined function call", ?_assert(string:str(RebarOut,
  49. "myapp_othermod:somefunc/0 calls undefined function myapp_somemod:notavailable/1") =:= 0)},
  50. {"Supppressed deprecated function", ?_assert(string:str(RebarOut,
  51. "myapp_mymod:fdeprecated/0 is deprecated function") =:= 0)},
  52. {"Supppressed deprecated function call", ?_assert(string:str(RebarOut,
  53. "myapp_othermod:somefunc/0 calls deprecated function myapp_mymod:fdeprecated/0") =:= 0)},
  54. {"Supppressed unused local", ?_assert(string:str(RebarOut,
  55. "myapp_mymod:localfunc2/0 is unused local function") =:= 0)},
  56. {"Supppressed unused export 1", ?_assert(string:str(RebarOut,
  57. "myapp_behaviour1:behaviour_info/1 is unused export") =:= 0)},
  58. {"Supppressed unused export 2", ?_assert(string:str(RebarOut,
  59. "myapp_behaviour2:behaviour_info/1 is unused export") =:= 0)},
  60. {"Supppressed unused export 3", ?_assert(string:str(RebarOut,
  61. "myapp_mymod:other2/1 is unused export") =:= 0)},
  62. {"Supppressed unused export 4", ?_assert(string:str(RebarOut,
  63. "myapp_othermod:somefunc/0 is unused export") =:= 0)},
  64. {"Suppressed behaviour export 1", ?_assert(string:str(RebarOut,
  65. "myapp_mymod:bh1_a/1 is unused export") =:= 0)},
  66. {"Suppressed behaviour export 2", ?_assert(string:str(RebarOut,
  67. "myapp_mymod:bh1_b/1 is unused export") =:= 0)},
  68. {"Suppressed behaviour export 3", ?_assert(string:str(RebarOut,
  69. "myapp_mymod:bh2_a/1 is unused export") =:= 0)},
  70. {"Suppressed behaviour export 4", ?_assert(string:str(RebarOut,
  71. "myapp_mymod:bh2_b/1 is unused export") =:= 0)}
  72. ]
  73. end}.
  74. %% ====================================================================
  75. %% Setup and Teardown
  76. %% ====================================================================
  77. -define(myapp_behaviour1,
  78. ["-module(myapp_behaviour1).\n",
  79. "-export([behaviour_info/1]).\n"]).
  80. -define(myapp_behaviour1_body,
  81. ["behaviour_info(callbacks) -> [{bh1_a,1},{bh1_b,1}];\n",
  82. "behaviour_info(_Other) -> undefined.\n"]).
  83. -define(myapp_behaviour1_ignorexref,
  84. ["-ignore_xref({behaviour_info,1}).\n"]).
  85. -define(myapp_behaviour2,
  86. ["-module(myapp_behaviour2).\n",
  87. "-export([behaviour_info/1]).\n"]).
  88. -define(myapp_behaviour2_body,
  89. ["behaviour_info(callbacks) -> [{bh2_a,1},{bh2_b,1}];\n",
  90. "behaviour_info(_Other) -> undefined.\n"]).
  91. -define(myapp_behaviour2_ignorexref,
  92. ["-ignore_xref({behaviour_info,1}).\n"]).
  93. -define(myapp_mymod,
  94. ["-module(myapp_mymod).\n",
  95. "-export([bh1_a/1,bh1_b/1,bh2_a/1,bh2_b/1,other1/1,other2/1,fdeprecated/0]).\n",
  96. "-behaviour(myapp_behaviour1).\n", % 2 behaviours
  97. "-behaviour(myapp_behaviour2).\n",
  98. "-deprecated({fdeprecated,0}).\n"]). % deprecated function
  99. -define(myapp_mymod_body,
  100. ["bh1_a(A) -> localfunc1(bh1_a, A).\n", % behaviour functions
  101. "bh1_b(A) -> localfunc1(bh1_b, A).\n",
  102. "bh2_a(A) -> localfunc1(bh2_a, A).\n",
  103. "bh2_b(A) -> localfunc1(bh2_b, A).\n",
  104. "other1(A) -> localfunc1(other1, A).\n", % regular exported functions
  105. "other2(A) -> localfunc1(other2, A).\n",
  106. "localfunc1(A, B) -> {A, B}.\n", % used local
  107. "localfunc2() -> ok.\n", % unused local
  108. "fdeprecated() -> ok.\n" % deprecated function
  109. ]).
  110. -define(myapp_mymod_ignorexref,
  111. ["-ignore_xref([{other2,1},{localfunc2,0},{fdeprecated,0}]).\n"]).
  112. -define(myapp_othermod,
  113. ["-module(myapp_othermod).\n",
  114. "-export([somefunc/0]).\n"]).
  115. -define(myapp_othermod_body,
  116. ["somefunc() ->\n",
  117. " myapp_mymod:other1(arg),\n",
  118. " myapp_somemod:notavailable(arg),\n",
  119. " myapp_mymod:fdeprecated().\n"
  120. ]).
  121. -define(myapp_othermod_ignorexref,
  122. ["-ignore_xref([{myapp_somemod,notavailable,1},{somefunc,0}]).\n",
  123. "-ignore_xref({myapp_mymod,fdeprecated,0}).\n"]).
  124. -define(myapp_rebarconfig,
  125. ["{erl_opts, [debug_info]}.\n",
  126. "{xref_checks, [deprecated_function_calls,deprecated_functions,\n",
  127. " undefined_function_calls,undefined_functions,\n",
  128. " exports_not_used,locals_not_used]}.\n"
  129. ]).
  130. setup_environment() ->
  131. ok = file:make_dir(?TMP_DIR),
  132. prepare_rebar_script(),
  133. ok = file:set_cwd(?TMP_DIR).
  134. prepare_project() ->
  135. setup_environment(),
  136. rebar("create-app appid=myapp"),
  137. ok = file:make_dir("ebin").
  138. setup_project(ignore_xref) ->
  139. prepare_project(),
  140. ok = file:write_file("src/myapp_behaviour1.erl", ?myapp_behaviour1 ++ ?myapp_behaviour1_ignorexref ++ ?myapp_behaviour1_body),
  141. ok = file:write_file("src/myapp_behaviour2.erl", ?myapp_behaviour2 ++ ?myapp_behaviour2_ignorexref++ ?myapp_behaviour2_body),
  142. ok = file:write_file("src/myapp_mymod.erl", ?myapp_mymod ++ ?myapp_mymod_ignorexref ++ ?myapp_mymod_body),
  143. ok = file:write_file("src/myapp_othermod.erl", ?myapp_othermod ++ ?myapp_othermod_ignorexref ++ ?myapp_othermod_body),
  144. ok = file:write_file("rebar.config", ?myapp_rebarconfig);
  145. setup_project(_) ->
  146. prepare_project(),
  147. ok = file:write_file("src/myapp_behaviour1.erl", ?myapp_behaviour1 ++ ?myapp_behaviour1_body),
  148. ok = file:write_file("src/myapp_behaviour2.erl", ?myapp_behaviour2 ++ ?myapp_behaviour2_body),
  149. ok = file:write_file("src/myapp_mymod.erl", ?myapp_mymod ++ ?myapp_mymod_body),
  150. ok = file:write_file("src/myapp_othermod.erl", ?myapp_othermod ++ ?myapp_othermod_body),
  151. ok = file:write_file("rebar.config", ?myapp_rebarconfig).
  152. teardown(_) ->
  153. ok = file:set_cwd(".."),
  154. ok = remove_tmp_dir().
  155. remove_tmp_dir() ->
  156. ok = rebar_file_utils:rm_rf(?TMP_DIR).
  157. %% ====================================================================
  158. %% Helper Functions
  159. %% ====================================================================
  160. prepare_rebar_script() ->
  161. Rebar = ?TMP_DIR ++ "rebar",
  162. {ok, _} = file:copy(?REBAR_SCRIPT, Rebar),
  163. case os:type() of
  164. {unix, _} ->
  165. [] = os:cmd("chmod u+x " ++ Rebar);
  166. {win32, _} ->
  167. {ok, _} = file:copy(?REBAR_SCRIPT ++ ".bat",
  168. ?TMP_DIR ++ "rebar.bat")
  169. end.
  170. rebar() ->
  171. rebar([]).
  172. rebar(Args) when is_list(Args) ->
  173. Out = os:cmd(filename:nativename("./rebar") ++ " " ++ Args),
  174. %% ?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"),
  175. Out.