From cf5390f01876ff8d9e70cff521740ab0dd805929 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 21 Aug 2015 12:18:30 -0500 Subject: replace use of dict of packages and registry with single ets table --- test/mock_pkg_resource.erl | 55 +++++++++++++++++++++------------------------- test/rebar_pkg_SUITE.erl | 26 ++++++++++++++-------- 2 files changed, 42 insertions(+), 39 deletions(-) (limited to 'test') diff --git a/test/mock_pkg_resource.erl b/test/mock_pkg_resource.erl index 9258f72..4ed8d8e 100644 --- a/test/mock_pkg_resource.erl +++ b/test/mock_pkg_resource.erl @@ -35,7 +35,7 @@ mock(Opts) -> unmock() -> meck:unload(?MOD), - meck:unload(rebar_state). + meck:unload(rebar_packages). %%%%%%%%%%%%%%% %%% Private %%% @@ -111,34 +111,19 @@ mock_pkg_index(Opts) -> Deps = proplists:get_value(pkgdeps, Opts, []), Skip = proplists:get_value(not_in_index, Opts, []), %% Dict: {App, Vsn}: [{<<"link">>, <<>>}, {<<"deps">>, []}] - %% Digraph: all apps and deps in the index + %% Index: all apps and deps in the index + Dict = find_parts(Deps, Skip), - GraphParts = to_graph_parts(Dict), - Digraph = rebar_digraph:restore_graph(GraphParts), - meck:new(rebar_state, [passthrough, no_link]), - meck:expect(rebar_state, registry, - fun(_State) -> {ok, to_registry(Deps)} end), - meck:expect(rebar_state, packages, - fun(_State) -> Dict end), - meck:expect(rebar_state, packages_graph, - fun(_State) -> Digraph end). + meck:new(rebar_packages, [passthrough, no_link]), + meck:expect(rebar_packages, packages, + fun(_State) -> to_index(Deps, Dict) end), + meck:expect(rebar_packages, load_and_verify_version, + fun(_State) -> to_index(Deps, Dict), true end). %%%%%%%%%%%%%%% %%% Helpers %%% %%%%%%%%%%%%%%% -to_registry(Deps) -> - Tid = ets:new(registry, []), - lists:foreach(fun({{Name, Vsn}, _}) -> - case ets:lookup(Tid, Name) of - [{_, [Vsns]}] -> - ets:insert(Tid, {Name, [[Vsn | Vsns]]}); - _ -> - ets:insert(Tid, {Name, [[Vsn]]}) - end - end, Deps), - Tid. - all_files(Dir) -> filelib:wildcard(filename:join([Dir, "**"])). @@ -158,10 +143,20 @@ find_parts([{AppName, Deps}|Rest], Skip, Acc) -> find_parts(Rest, Skip, AccNew) end. -to_graph_parts(Dict) -> - LastUpdated = os:timestamp(), - dict:fold(fun(K,Deps,{Ks,Vs}) -> - {[{K,LastUpdated}|Ks], - [{K,{list_to_binary(atom_to_list(DK)), list_to_binary(DV)}} - || {DK,DV} <- Deps] ++ Vs} - end, {[],[]}, Dict). +to_index(AllDeps, Dict) -> + catch ets:delete(package_index), + ets:new(package_index, [named_table, public]), + dict:fold( + fun(K, Deps, _) -> + DepsList = [{ec_cnv:to_binary(DK), ec_cnv:to_binary(DV)} || {DK, DV} <- Deps], + ets:insert(package_index, {K, DepsList, <<"checksum">>}) + end, ok, Dict), + ets:insert(package_index, {package_index_version, 3}), + lists:foreach(fun({{Name, Vsn}, _}) -> + case ets:lookup(package_index, ec_cnv:to_binary(Name)) of + [{_, Vsns}] -> + ets:insert(package_index, {ec_cnv:to_binary(Name), [ec_cnv:to_binary(Vsn) | Vsns]}); + _ -> + ets:insert(package_index, {ec_cnv:to_binary(Name), [ec_cnv:to_binary(Vsn)]}) + end + end, AllDeps). diff --git a/test/rebar_pkg_SUITE.erl b/test/rebar_pkg_SUITE.erl index e99e787..3cd3a67 100644 --- a/test/rebar_pkg_SUITE.erl +++ b/test/rebar_pkg_SUITE.erl @@ -159,25 +159,33 @@ mock_config(Name, Config) -> Priv = ?config(priv_dir, Config), CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]), TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]), - T = ets:new(fake_registry, [public]), - ets:insert_new(T, [ - {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum]}, - {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum]}, - {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum]} + Tid = ets:new(registry_table, [public]), + ets:insert_new(Tid, [ + {<<"badindexchk">>,[[<<"1.0.0">>]]}, + {<<"goodpkg">>,[[<<"1.0.0">>]]}, + {<<"badpkg">>,[[<<"1.0.0">>]]}, + {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]}, + {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}, + {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]} ]), CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]), filelib:ensure_dir(filename:join([CacheDir, "registry"])), - ok = ets:tab2file(T, filename:join([CacheDir, "registry"])), + ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])), + %% The state returns us a fake registry meck:new(rebar_state, [passthrough]), - meck:expect(rebar_state, registry, - fun(_State) -> {ok, T} end), meck:expect(rebar_state, get, fun(_State, rebar_packages_cdn, _Default) -> "http://test.com/" end), + meck:new(rebar_dir, [passthrough]), meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end), + + meck:new(rebar_packages, [passthrough]), + meck:expect(rebar_packages, package_dir, fun(_) -> CacheDir end), + rebar_prv_update:hex_to_index(rebar_state:new()), + %% Cache fetches are mocked -- we assume the server and clients are %% correctly used. GoodCache = ?config(good_cache, Config), @@ -194,7 +202,7 @@ mock_config(Name, Config) -> [{cache_root, CacheRoot}, {cache_dir, CacheDir}, {tmp_dir, TmpDir}, - {mock_table, T} | Config]. + {mock_table, Tid} | Config]. unmock_config(Config) -> meck:unload(), -- cgit v1.1