You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. -module(rebar_dialyzer_SUITE).
  2. -export([suite/0,
  3. init_per_suite/1,
  4. end_per_suite/1,
  5. init_per_testcase/2,
  6. all/0,
  7. update_base_plt/1,
  8. update_app_plt/1,
  9. build_release_plt/1,
  10. plt_apps_option/1]).
  11. -include_lib("common_test/include/ct.hrl").
  12. -include_lib("eunit/include/eunit.hrl").
  13. -include_lib("kernel/include/file.hrl").
  14. suite() ->
  15. [].
  16. init_per_suite(Config) ->
  17. Config.
  18. end_per_suite(_Config) ->
  19. ok.
  20. init_per_testcase(Testcase, Config) ->
  21. PrivDir = ?config(priv_dir, Config),
  22. Prefix = ec_cnv:to_list(Testcase),
  23. BasePrefix = Prefix ++ "_base",
  24. Opts = [{plt_prefix, Prefix},
  25. {plt_location, PrivDir},
  26. {base_plt_prefix, BasePrefix},
  27. {base_plt_location, PrivDir},
  28. {base_plt_apps, [erts]}],
  29. Suffix = "_" ++ rebar_utils:otp_release() ++ "_plt",
  30. [{plt, filename:join(PrivDir, Prefix ++ Suffix)},
  31. {base_plt, filename:join(PrivDir, BasePrefix ++ Suffix)},
  32. {rebar_config, [{dialyzer, Opts}]} |
  33. rebar_test_utils:init_rebar_state(Config)].
  34. all() ->
  35. [update_base_plt, update_app_plt, build_release_plt, plt_apps_option].
  36. update_base_plt(Config) ->
  37. AppDir = ?config(apps, Config),
  38. RebarConfig = ?config(rebar_config, Config),
  39. BasePlt = ?config(base_plt, Config),
  40. Plt = ?config(plt, Config),
  41. Name = rebar_test_utils:create_random_name("app1_"),
  42. Vsn = rebar_test_utils:create_random_vsn(),
  43. rebar_test_utils:create_app(AppDir, Name, Vsn, [erts]),
  44. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  45. {ok, [{app, Name}]}),
  46. ErtsFiles = erts_files(),
  47. {ok, BasePltFiles} = plt_files(BasePlt),
  48. ?assertEqual(ErtsFiles, BasePltFiles),
  49. alter_plt(BasePlt),
  50. ok = file:delete(Plt),
  51. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  52. {ok, [{app, Name}]}),
  53. {ok, BasePltFiles2} = plt_files(BasePlt),
  54. ?assertEqual(ErtsFiles, BasePltFiles2),
  55. {ok, PltFiles} = plt_files(Plt),
  56. ?assertEqual(ErtsFiles, PltFiles).
  57. update_app_plt(Config) ->
  58. AppDir = ?config(apps, Config),
  59. RebarConfig = ?config(rebar_config, Config),
  60. Plt = ?config(plt, Config),
  61. Name = rebar_test_utils:create_random_name("app1_"),
  62. Vsn = rebar_test_utils:create_random_vsn(),
  63. rebar_test_utils:create_app(AppDir, Name, Vsn, [erts]),
  64. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  65. {ok, [{app, Name}]}),
  66. ErtsFiles = erts_files(),
  67. {ok, PltFiles} = plt_files(Plt),
  68. ?assertEqual(ErtsFiles, PltFiles),
  69. alter_plt(Plt),
  70. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  71. {ok, [{app, Name}]}),
  72. {ok, PltFiles2} = plt_files(Plt),
  73. ?assertEqual(ErtsFiles, PltFiles2),
  74. ok = file:delete(Plt),
  75. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  76. {ok, [{app, Name}]}),
  77. {ok, PltFiles3} = plt_files(Plt),
  78. ?assertEqual(ErtsFiles, PltFiles3).
  79. build_release_plt(Config) ->
  80. AppDir = ?config(apps, Config),
  81. RebarConfig = ?config(rebar_config, Config),
  82. BasePlt = ?config(base_plt, Config),
  83. Plt = ?config(plt, Config),
  84. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  85. Vsn1 = rebar_test_utils:create_random_vsn(),
  86. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1,
  87. [erts]),
  88. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  89. Vsn2 = rebar_test_utils:create_random_vsn(),
  90. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2,
  91. [erts, ec_cnv:to_atom(Name1)]),
  92. rebar_test_utils:run_and_check(Config, RebarConfig, ["dialyzer"],
  93. {ok, [{app, Name1}, {app, Name2}]}),
  94. ErtsFiles = erts_files(),
  95. {ok, BasePltFiles} = plt_files(BasePlt),
  96. ?assertEqual(ErtsFiles, BasePltFiles),
  97. {ok, PltFiles} = plt_files(Plt),
  98. ?assertEqual(ErtsFiles, PltFiles).
  99. plt_apps_option(Config) ->
  100. AppDir = ?config(apps, Config),
  101. RebarConfig = ?config(rebar_config, Config),
  102. Plt = ?config(plt, Config),
  103. State = ?config(state, Config),
  104. %% Create applications
  105. Name1 = rebar_test_utils:create_random_name("app1_"),
  106. Vsn1 = rebar_test_utils:create_random_vsn(),
  107. rebar_test_utils:create_app(filename:join([AppDir,"deps",Name1]), Name1, Vsn1,
  108. []),
  109. App1 = ec_cnv:to_atom(Name1),
  110. Name2 = rebar_test_utils:create_random_name("app2_"),
  111. Vsn2 = rebar_test_utils:create_random_vsn(),
  112. rebar_test_utils:create_app(filename:join([AppDir,"deps",Name2]), Name2, Vsn2,
  113. [App1]), % App2 depends on App1
  114. App2 = ec_cnv:to_atom(Name2),
  115. Name3 = rebar_test_utils:create_random_name("app3_"), % the project application
  116. Vsn3 = rebar_test_utils:create_random_vsn(),
  117. rebar_test_utils:create_app(AppDir, Name3, Vsn3,
  118. [App2]), % App3 depends on App2
  119. %% Dependencies settings
  120. State1 = rebar_state:add_resource(State, {localfs, rebar_localfs_resource}),
  121. Config1 = [{state, State1} | Config],
  122. RebarConfig1 = merge_config(
  123. [{deps,
  124. [
  125. {App1, {localfs, filename:join([AppDir,"deps",Name1])}},
  126. {App2, {localfs, filename:join([AppDir,"deps",Name2])}}
  127. ]}],
  128. RebarConfig),
  129. %% Dialyzer: plt_apps = top_level_deps (default)
  130. rebar_test_utils:run_and_check(Config1, RebarConfig1, ["dialyzer"],
  131. {ok, [{app, Name3}]}),
  132. %% NOTE: `erts` is included in `base_plt_apps`
  133. {ok, PltFiles1} = plt_files(Plt),
  134. ?assertEqual([App2, erts], get_apps_from_beam_files(PltFiles1)),
  135. %% Dialyzer: plt_apps = all_deps
  136. RebarConfig2 = merge_config([{dialyzer, [{plt_apps, all_deps}]}],
  137. RebarConfig1),
  138. rebar_test_utils:run_and_check(Config1, RebarConfig2, ["dialyzer"],
  139. {ok, [{app, Name3}]}),
  140. {ok, PltFiles2} = plt_files(Plt),
  141. ?assertEqual([App1, App2, erts], get_apps_from_beam_files(PltFiles2)).
  142. %% Helpers
  143. erts_files() ->
  144. ErtsDir = code:lib_dir(erts, ebin),
  145. ErtsBeams = filelib:wildcard("*.beam", ErtsDir),
  146. ErtsFiles = lists:map(fun(Beam) -> filename:join(ErtsDir, Beam) end,
  147. ErtsBeams),
  148. lists:sort(ErtsFiles).
  149. plt_files(Plt) ->
  150. case dialyzer:plt_info(Plt) of
  151. {ok, Info} ->
  152. Files = proplists:get_value(files, Info),
  153. {ok, lists:sort(Files)};
  154. Other ->
  155. Other
  156. end.
  157. alter_plt(Plt) ->
  158. {ok, Files} = plt_files(Plt),
  159. _ = dialyzer:run([{analysis_type, plt_remove},
  160. {init_plt, Plt},
  161. {files, [hd(Files)]}]),
  162. _ = dialyzer:run([{analysis_type, plt_add},
  163. {init_plt, Plt},
  164. {files, [code:which(dialyzer)]}]),
  165. ok.
  166. -spec merge_config(Config, Config) -> Config when
  167. Config :: [{term(), term()}].
  168. merge_config(NewConfig, OldConfig) ->
  169. dict:to_list(
  170. rebar_opts:merge_opts(dict:from_list(NewConfig),
  171. dict:from_list(OldConfig))).
  172. -spec get_apps_from_beam_files(string()) -> [atom()].
  173. get_apps_from_beam_files(BeamFiles) ->
  174. lists:usort(
  175. [begin
  176. AppNameVsn = filename:basename(filename:dirname(filename:dirname(File))),
  177. [AppName | _] = string:tokens(AppNameVsn ++ "-", "-"),
  178. ec_cnv:to_atom(AppName)
  179. end || File <- BeamFiles]).