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.

476 line
21 KiB

  1. -module(rebar_test_utils).
  2. -include_lib("common_test/include/ct.hrl").
  3. -include_lib("eunit/include/eunit.hrl").
  4. -export([init_rebar_state/1, init_rebar_state/2, run_and_check/4, check_results/3]).
  5. -export([expand_deps/2, flat_deps/1, top_level_deps/1]).
  6. -export([create_app/4, create_plugin/4, create_eunit_app/4, create_empty_app/4,
  7. create_config/2, create_config/3, package_app/3]).
  8. -export([create_random_name/1, create_random_vsn/0, write_src_file/2]).
  9. %%%%%%%%%%%%%%
  10. %%% Public %%%
  11. %%%%%%%%%%%%%%
  12. %% @doc {@see init_rebar_state/2}
  13. init_rebar_state(Config) -> init_rebar_state(Config, "apps_dir1_").
  14. %% @doc Takes a common test config and a name (string) and sets up
  15. %% a basic OTP app directory with a pre-configured rebar state to
  16. %% run tests with.
  17. init_rebar_state(Config, Name) ->
  18. application:load(rebar),
  19. DataDir = ?config(priv_dir, Config),
  20. AppsDir = filename:join([DataDir, create_random_name(Name)]),
  21. CheckoutsDir = filename:join([AppsDir, "_checkouts"]),
  22. ok = ec_file:mkdir_p(AppsDir),
  23. ok = ec_file:mkdir_p(CheckoutsDir),
  24. Verbosity = rebar3:log_level(),
  25. rebar_log:init(command_line, Verbosity),
  26. GlobalDir = filename:join([DataDir, "cache"]),
  27. State = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  28. ,{global_rebar_dir, GlobalDir}
  29. ,{root_dir, AppsDir}]),
  30. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, State} | Config].
  31. %% @doc Takes common test config, a rebar config ([] if empty), a command to
  32. %% run ("install_deps", "compile", etc.), and a list of expected applications
  33. %% and/or dependencies to be present, and verifies whether they are all in
  34. %% place.
  35. %%
  36. %% The expectation list takes elements of the form:
  37. %% - `{app, Name :: string()}': checks that the app is properly built.
  38. %% - `{dep, Name :: string()}': checks that the dependency has been fetched.
  39. %% Ignores the build status of the dependency.
  40. %% - `{dep, Name :: string(), Vsn :: string()}': checks that the dependency
  41. %% has been fetched, and that a given version has been chosen. Useful to
  42. %% test for conflict resolution. Also ignores the build status of the
  43. %% dependency.
  44. %%
  45. %% This function assumes `init_rebar_state/1-2' has run before, in order to
  46. %% fetch the `apps' and `state' values from the CT config.
  47. run_and_check(Config, RebarConfig, Command, Expect) ->
  48. %% Assumes init_rebar_state has run first
  49. AppDir = ?config(apps, Config),
  50. State = ?config(state, Config),
  51. try
  52. Res = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), Command),
  53. case Expect of
  54. {error, Reason} ->
  55. ?assertEqual({error, Reason}, Res);
  56. {ok, Expected} ->
  57. {ok, _} = Res,
  58. check_results(AppDir, Expected, "*"),
  59. Res;
  60. {ok, Expected, ProfileRun} ->
  61. {ok, _} = Res,
  62. check_results(AppDir, Expected, ProfileRun),
  63. Res;
  64. return ->
  65. Res
  66. end
  67. catch
  68. rebar_abort when Expect =:= rebar_abort -> rebar_abort
  69. end.
  70. %% @doc Creates a dummy application including:
  71. %% - src/<file>.erl
  72. %% - src/<file>.app.src
  73. %% And returns a `rebar_app_info' object.
  74. create_app(AppDir, Name, Vsn, Deps) ->
  75. write_src_file(AppDir, Name ++ ".erl"),
  76. write_src_file(AppDir, "not_a_real_src_" ++ Name ++ ".erl"),
  77. write_app_src_file(AppDir, Name, Vsn, Deps),
  78. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  79. %% @doc Creates a dummy plugin including:
  80. %% - src/<file>.erl
  81. %% - src/<file>.app.src
  82. %% And returns a `rebar_app_info' object.
  83. create_plugin(AppDir, Name, Vsn, Deps) ->
  84. write_plugin_file(AppDir, Name ++ ".erl"),
  85. write_src_file(AppDir, "not_a_real_src_" ++ Name ++ ".erl"),
  86. write_app_src_file(AppDir, Name, Vsn, Deps),
  87. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  88. %% @doc Creates a dummy application including:
  89. %% - src/<file>.erl
  90. %% - src/<file>.app.src
  91. %% - test/<file>_tests.erl
  92. %% And returns a `rebar_app_info' object.
  93. create_eunit_app(AppDir, Name, Vsn, Deps) ->
  94. write_eunitized_src_file(AppDir, Name),
  95. write_eunit_suite_file(AppDir, Name),
  96. write_app_src_file(AppDir, Name, Vsn, Deps),
  97. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  98. %% @doc Creates a dummy application including:
  99. %% - ebin/<file>.app
  100. %% And returns a `rebar_app_info' object.
  101. create_empty_app(AppDir, Name, Vsn, Deps) ->
  102. write_app_file(AppDir, Name, Vsn, Deps),
  103. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  104. %% @doc Creates a rebar.config file. The function accepts a list of terms,
  105. %% each of which will be dumped as a consult file. For example, the list
  106. %% `[a, b, c]' will return the consult file `a. b. c.'.
  107. create_config(AppDir, Contents) ->
  108. ConfFilename = filename:join([AppDir, "rebar.config"]),
  109. create_config(AppDir, ConfFilename, Contents).
  110. create_config(_AppDir, ConfFilename, Contents) ->
  111. ok = filelib:ensure_dir(ConfFilename),
  112. Config = lists:flatten([io_lib:fwrite("~p.~n", [Term]) || Term <- Contents]),
  113. ok = ec_file:write(ConfFilename, Config),
  114. ConfFilename.
  115. %% @doc Util to create a random variation of a given name.
  116. create_random_name(Name) ->
  117. random_seed(),
  118. Name ++ erlang:integer_to_list(random:uniform(1000000)).
  119. %% @doc Util to create a random variation of a given version.
  120. create_random_vsn() ->
  121. random_seed(),
  122. lists:flatten([erlang:integer_to_list(random:uniform(100)),
  123. ".", erlang:integer_to_list(random:uniform(100)),
  124. ".", erlang:integer_to_list(random:uniform(100))]).
  125. random_seed() ->
  126. <<A:32, B:32, C:32>> = crypto:rand_bytes(12),
  127. random:seed({A,B,C}).
  128. expand_deps(_, []) -> [];
  129. expand_deps(git, [{Name, Deps} | Rest]) ->
  130. Dep = {Name, ".*", {git, "https://example.org/user/"++Name++".git", "master"}},
  131. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  132. expand_deps(git, [{Name, Vsn, Deps} | Rest]) ->
  133. Dep = {Name, Vsn, {git, "https://example.org/user/"++Name++".git", {tag, Vsn}}},
  134. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  135. expand_deps(pkg, [{Name, Deps} | Rest]) ->
  136. Dep = {pkg, Name, "0.0.0"},
  137. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)];
  138. expand_deps(pkg, [{Name, Vsn, Deps} | Rest]) ->
  139. Dep = {pkg, Name, Vsn},
  140. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)];
  141. expand_deps(mixed, [{Name, Deps} | Rest]) ->
  142. Dep = if hd(Name) >= $a, hd(Name) =< $z ->
  143. {pkg, string:to_upper(Name), "0.0.0"}
  144. ; hd(Name) >= $A, hd(Name) =< $Z ->
  145. {Name, ".*", {git, "https://example.org/user/"++Name++".git", "master"}}
  146. end,
  147. [{Dep, expand_deps(mixed, Deps)} | expand_deps(mixed, Rest)];
  148. expand_deps(mixed, [{Name, Vsn, Deps} | Rest]) ->
  149. Dep = if hd(Name) >= $a, hd(Name) =< $z ->
  150. {pkg, string:to_upper(Name), Vsn}
  151. ; hd(Name) >= $A, hd(Name) =< $Z ->
  152. {Name, Vsn, {git, "https://example.org/user/"++Name++".git", {tag, Vsn}}}
  153. end,
  154. [{Dep, expand_deps(mixed, Deps)} | expand_deps(mixed, Rest)].
  155. %% Source deps can depend on both source and package dependencies;
  156. %% package deps can only depend on package deps.
  157. %% For things to work we have to go down the dep tree and find all
  158. %% lineages of pkg deps and return them, whereas the source deps
  159. %% can be left as is.
  160. flat_deps(Deps) -> flat_deps(Deps, [], []).
  161. flat_deps([], Src, Pkg) -> {Src, Pkg};
  162. flat_deps([{{pkg, Name, Vsn}, PkgDeps} | Rest], Src, Pkg) ->
  163. Current = {{iolist_to_binary(Name), iolist_to_binary(Vsn)},
  164. top_level_deps(PkgDeps)},
  165. {[], FlatPkgDeps} = flat_deps(PkgDeps),
  166. flat_deps(Rest,
  167. Src,
  168. Pkg ++ [Current | FlatPkgDeps]);
  169. flat_deps([{{Name,_Vsn,Ref}, Deps} | Rest], Src, Pkg) ->
  170. Current = {{Name,vsn_from_ref(Ref)}, top_level_deps(Deps)},
  171. {FlatDeps, FlatPkgDeps} = flat_deps(Deps),
  172. flat_deps(Rest,
  173. Src ++ [Current | FlatDeps],
  174. Pkg ++ FlatPkgDeps).
  175. vsn_from_ref({git, _, {_, Vsn}}) -> Vsn;
  176. vsn_from_ref({git, _, Vsn}) -> Vsn.
  177. top_level_deps([]) -> [];
  178. top_level_deps([{{pkg, Name, Vsn}, _} | Deps]) ->
  179. [{list_to_atom(Name), Vsn} | top_level_deps(Deps)];
  180. top_level_deps([{{Name, Vsn, Ref}, _} | Deps]) ->
  181. [{list_to_atom(Name), Vsn, Ref} | top_level_deps(Deps)].
  182. %%%%%%%%%%%%%%%
  183. %%% Helpers %%%
  184. %%%%%%%%%%%%%%%
  185. check_results(AppDir, Expected, ProfileRun) ->
  186. BuildDirs = filelib:wildcard(filename:join([AppDir, "_build", ProfileRun, "lib", "*"])),
  187. PluginDirs = filelib:wildcard(filename:join([AppDir, "_build", ProfileRun, "plugins", "*"])),
  188. GlobalPluginDirs = filelib:wildcard(filename:join([AppDir, "global", "plugins", "*"])),
  189. CheckoutsDir = filename:join([AppDir, "_checkouts", "*"]),
  190. LockFile = filename:join([AppDir, "rebar.lock"]),
  191. Locks = lists:flatten(rebar_config:consult_lock_file(LockFile)),
  192. InvalidApps = rebar_app_discover:find_apps(BuildDirs, invalid),
  193. ValidApps = rebar_app_discover:find_apps(BuildDirs, valid),
  194. InvalidDepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- InvalidApps],
  195. ValidDepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- ValidApps],
  196. Deps = rebar_app_discover:find_apps(BuildDirs, all),
  197. DepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Deps],
  198. Checkouts = rebar_app_discover:find_apps([CheckoutsDir], all),
  199. CheckoutsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Checkouts],
  200. Plugins = rebar_app_discover:find_apps(PluginDirs, all),
  201. PluginsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Plugins],
  202. GlobalPlugins = rebar_app_discover:find_apps(GlobalPluginDirs, all),
  203. GlobalPluginsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- GlobalPlugins],
  204. lists:foreach(
  205. fun({app, Name}) ->
  206. ct:pal("App Name: ~p", [Name]),
  207. case lists:keyfind(Name, 1, DepsNames) of
  208. false ->
  209. error({app_not_found, Name});
  210. {Name, _App} ->
  211. ok
  212. end
  213. ; ({app, Name, invalid}) ->
  214. ct:pal("Invalid Name: ~p", [Name]),
  215. case lists:keyfind(Name, 1, InvalidDepsNames) of
  216. false ->
  217. error({app_not_found, Name});
  218. {Name, _App} ->
  219. ok
  220. end
  221. ; ({app, Name, valid}) ->
  222. ct:pal("Valid Name: ~p", [Name]),
  223. case lists:keyfind(Name, 1, ValidDepsNames) of
  224. false ->
  225. error({app_not_found, Name});
  226. {Name, _App} ->
  227. ok
  228. end
  229. ; ({dep_not_exist, Name}) ->
  230. ct:pal("App Not Exist Name: ~p", [Name]),
  231. case lists:keyfind(Name, 1, DepsNames) of
  232. false ->
  233. ok;
  234. {Name, _App} ->
  235. error({app_found, Name})
  236. end
  237. ; ({checkout, Name}) ->
  238. ct:pal("Checkout Name: ~p", [Name]),
  239. ?assertNotEqual(false, lists:keyfind(Name, 1, CheckoutsNames))
  240. ; ({dep, Name}) ->
  241. ct:pal("Dep Name: ~p", [Name]),
  242. ?assertNotEqual(false, lists:keyfind(Name, 1, DepsNames))
  243. ; ({dep, Name, Vsn}) ->
  244. ct:pal("Dep Name: ~p, Vsn: ~p", [Name, Vsn]),
  245. case lists:keyfind(Name, 1, DepsNames) of
  246. false ->
  247. error({dep_not_found, Name});
  248. {Name, App} ->
  249. ?assertEqual(iolist_to_binary(Vsn),
  250. iolist_to_binary(rebar_app_info:original_vsn(App)))
  251. end
  252. ; ({plugin, Name}) ->
  253. ct:pal("Plugin Name: ~p", [Name]),
  254. ?assertNotEqual(false, lists:keyfind(Name, 1, PluginsNames))
  255. ; ({plugin, Name, Vsn}) ->
  256. ct:pal("Plugin Name: ~p, Vsn: ~p", [Name, Vsn]),
  257. case lists:keyfind(Name, 1, PluginsNames) of
  258. false ->
  259. error({plugin_not_found, Name});
  260. {Name, App} ->
  261. ?assertEqual(iolist_to_binary(Vsn),
  262. iolist_to_binary(rebar_app_info:original_vsn(App)))
  263. end
  264. ; ({global_plugin, Name}) ->
  265. ct:pal("Global Plugin Name: ~p", [Name]),
  266. ?assertNotEqual(false, lists:keyfind(Name, 1, GlobalPluginsNames))
  267. ; ({global_plugin, Name, Vsn}) ->
  268. ct:pal("Global Plugin Name: ~p, Vsn: ~p", [Name, Vsn]),
  269. case lists:keyfind(Name, 1, GlobalPluginsNames) of
  270. false ->
  271. error({global_plugin_not_found, Name});
  272. {Name, App} ->
  273. ?assertEqual(iolist_to_binary(Vsn),
  274. iolist_to_binary(rebar_app_info:original_vsn(App)))
  275. end
  276. ; ({lock, Name}) ->
  277. ct:pal("Lock Name: ~p", [Name]),
  278. ?assertNotEqual(false, lists:keyfind(iolist_to_binary(Name), 1, Locks))
  279. ; ({lock, Name, Vsn}) ->
  280. ct:pal("Lock Name: ~p, Vsn: ~p", [Name, Vsn]),
  281. case lists:keyfind(iolist_to_binary(Name), 1, Locks) of
  282. false ->
  283. error({lock_not_found, Name});
  284. {_LockName, {pkg, _, LockVsn}, _} ->
  285. ?assertEqual(iolist_to_binary(Vsn),
  286. iolist_to_binary(LockVsn));
  287. {_LockName, {_, _, {ref, LockVsn}}, _} ->
  288. ?assertEqual(iolist_to_binary(Vsn),
  289. iolist_to_binary(LockVsn))
  290. end
  291. ; ({lock, pkg, Name, Vsn}) ->
  292. ct:pal("Pkg Lock Name: ~p, Vsn: ~p", [Name, Vsn]),
  293. case lists:keyfind(iolist_to_binary(Name), 1, Locks) of
  294. false ->
  295. error({lock_not_found, Name});
  296. {_LockName, {pkg, _, LockVsn}, _} ->
  297. ?assertEqual(iolist_to_binary(Vsn),
  298. iolist_to_binary(LockVsn));
  299. {_LockName, {_, _, {ref, LockVsn}}, _} ->
  300. error({source_lock, {Name, LockVsn}})
  301. end
  302. ; ({lock, src, Name, Vsn}) ->
  303. ct:pal("Src Lock Name: ~p, Vsn: ~p", [Name, Vsn]),
  304. case lists:keyfind(iolist_to_binary(Name), 1, Locks) of
  305. false ->
  306. error({lock_not_found, Name});
  307. {_LockName, {pkg, _, LockVsn}, _} ->
  308. error({pkg_lock, {Name, LockVsn}});
  309. {_LockName, {_, _, {ref, LockVsn}}, _} ->
  310. ?assertEqual(iolist_to_binary(Vsn),
  311. iolist_to_binary(LockVsn))
  312. end
  313. ; ({release, Name, Vsn, ExpectedDevMode}) ->
  314. ct:pal("Release: ~p-~s", [Name, Vsn]),
  315. {ok, Cwd} = file:get_cwd(),
  316. try
  317. file:set_cwd(AppDir),
  318. [ReleaseDir] = filelib:wildcard(filename:join([AppDir, "_build", "*", "rel"])),
  319. RelxState = rlx_state:new("", [], []),
  320. RelxState1 = rlx_state:base_output_dir(RelxState, ReleaseDir),
  321. {ok, RelxState2} = rlx_prv_app_discover:do(RelxState1),
  322. {ok, RelxState3} = rlx_prv_rel_discover:do(RelxState2),
  323. LibDir = filename:join([ReleaseDir, Name, "lib"]),
  324. {ok, RelLibs} = rebar_utils:list_dir(LibDir),
  325. IsSymLinkFun =
  326. fun(X) ->
  327. ec_file:is_symlink(filename:join(LibDir, X))
  328. end,
  329. DevMode = lists:all(IsSymLinkFun, RelLibs),
  330. ?assertEqual(ExpectedDevMode, DevMode),
  331. %% throws not_found if it doesn't exist
  332. rlx_state:get_realized_release(RelxState3, Name, Vsn)
  333. catch
  334. _ ->
  335. ct:fail(release_not_found)
  336. after
  337. file:set_cwd(Cwd)
  338. end
  339. ; ({tar, Name, Vsn}) ->
  340. ct:pal("Tarball: ~s-~s", [Name, Vsn]),
  341. Tarball = filename:join([AppDir, "_build", "rel", Name, Name++"-"++Vsn++".tar.gz"]),
  342. ?assertNotEqual([], filelib:is_file(Tarball))
  343. ; ({file, Filename}) ->
  344. ct:pal("Filename: ~s", [Filename]),
  345. ?assert(filelib:is_file(Filename))
  346. ; ({dir, Dirname}) ->
  347. ct:pal("Directory: ~s", [Dirname]),
  348. ?assert(filelib:is_dir(Dirname))
  349. end, Expected).
  350. write_plugin_file(Dir, Name) ->
  351. Erl = filename:join([Dir, "src", Name]),
  352. ok = filelib:ensure_dir(Erl),
  353. ok = ec_file:write(Erl, plugin_src_file(Name)).
  354. write_src_file(Dir, Name) ->
  355. Erl = filename:join([Dir, "src", Name]),
  356. ok = filelib:ensure_dir(Erl),
  357. ok = ec_file:write(Erl, erl_src_file(Name)).
  358. write_eunitized_src_file(Dir, Name) ->
  359. Erl = filename:join([Dir, "src", "not_a_real_src_" ++ Name ++ ".erl"]),
  360. ok = filelib:ensure_dir(Erl),
  361. ok = ec_file:write(Erl, erl_eunitized_src_file("not_a_real_src_" ++ Name ++ ".erl")).
  362. write_eunit_suite_file(Dir, Name) ->
  363. Erl = filename:join([Dir, "test", "not_a_real_src_" ++ Name ++ "_tests.erl"]),
  364. ok = filelib:ensure_dir(Erl),
  365. ok = ec_file:write(Erl, erl_eunit_suite_file("not_a_real_src_" ++ Name ++ ".erl")).
  366. write_app_file(Dir, Name, Version, Deps) ->
  367. Filename = filename:join([Dir, "ebin", Name ++ ".app"]),
  368. ok = filelib:ensure_dir(Filename),
  369. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  370. write_app_src_file(Dir, Name, Version, Deps) ->
  371. Filename = filename:join([Dir, "src", Name ++ ".app.src"]),
  372. ok = filelib:ensure_dir(Filename),
  373. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  374. erl_src_file(Name) ->
  375. io_lib:format("-module('~s').\n"
  376. "-export([main/0]).\n"
  377. "main() -> ok.\n", [filename:basename(Name, ".erl")]).
  378. plugin_src_file(Name) ->
  379. io_lib:format("-module('~s').\n"
  380. "-export([init/1]).\n"
  381. "init(State) -> \n"
  382. "Provider = providers:create([\n"
  383. "{name, '~s'},\n"
  384. "{module, '~s'}\n"
  385. "]),\n"
  386. "{ok, rebar_state:add_provider(State, Provider)}.\n", [filename:basename(Name, ".erl"),
  387. filename:basename(Name, ".erl"),
  388. filename:basename(Name, ".erl")]).
  389. erl_eunitized_src_file(Name) ->
  390. io_lib:format("-module('~s').\n"
  391. "-export([main/0]).\n"
  392. "main() -> ok.\n"
  393. "-ifdef(TEST).\n"
  394. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  395. "some_test_() -> ?_assertEqual(ok, main()).\n"
  396. "-endif.\n", [filename:basename(Name, ".erl")]).
  397. erl_eunit_suite_file(Name) ->
  398. BaseName = filename:basename(Name, ".erl"),
  399. io_lib:format("-module('~s_tests').\n"
  400. "-compile(export_all).\n"
  401. "-ifndef(some_define).\n"
  402. "-define(some_define, false).\n"
  403. "-endif.\n"
  404. "-ifdef(TEST).\n"
  405. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  406. "some_test_() -> ?_assertEqual(ok, ~s:main()).\n"
  407. "define_test_() -> ?_assertEqual(true, ?some_define).\n"
  408. "-endif.\n", [BaseName, BaseName]).
  409. get_app_metadata(Name, Vsn, Deps) ->
  410. {application, erlang:list_to_atom(Name),
  411. [{description, ""},
  412. {vsn, Vsn},
  413. {modules, []},
  414. {included_applications, []},
  415. {registered, []},
  416. {applications, Deps}]}.
  417. package_app(AppDir, DestDir, PkgName) ->
  418. Name = PkgName++".tar",
  419. {ok, Fs} = rebar_utils:list_dir(AppDir),
  420. ok = erl_tar:create(filename:join(DestDir, "contents.tar.gz"),
  421. lists:zip(Fs, [filename:join(AppDir,F) || F <- Fs]),
  422. [compressed]),
  423. ok = file:write_file(filename:join(DestDir, "metadata.config"), "who cares"),
  424. ok = file:write_file(filename:join(DestDir, "VERSION"), "3"),
  425. {ok, Contents} = file:read_file(filename:join(DestDir, "contents.tar.gz")),
  426. Blob = <<"3who cares", Contents/binary>>,
  427. <<X:256/big-unsigned>> = crypto:hash(sha256, Blob),
  428. BinChecksum = list_to_binary(string:to_upper(lists:flatten(io_lib:format("~64.16.0b", [X])))),
  429. ok = file:write_file(filename:join(DestDir, "CHECKSUM"), BinChecksum),
  430. PkgFiles = ["contents.tar.gz", "VERSION", "metadata.config", "CHECKSUM"],
  431. Archive = filename:join(DestDir, Name),
  432. ok = erl_tar:create(Archive,
  433. lists:zip(PkgFiles, [filename:join(DestDir,F) || F <- PkgFiles])),
  434. {ok, BinFull} = file:read_file(Archive),
  435. <<E:128/big-unsigned-integer>> = crypto:hash(md5, BinFull),
  436. Etag = string:to_lower(lists:flatten(io_lib:format("~32.16.0b", [E]))),
  437. {BinChecksum, Etag}.