<commit_before>-module(rebar_pkg_alias_SUITE).
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("kernel/include/file.hrl").
-include("rebar.hrl").

all() -> [same_alias, diff_alias, diff_alias_vsn, transitive_alias%% ,
          %% transitive_hash_mismatch
         ].

%% {uuid, {pkg, uuid}} = uuid
%% {uuid, {pkg, alias}} = uuid on disk
%% another run should yield the same lock file without error
init_per_suite(Config) ->
    Config.
    %% mock_config(?MODULE, Config).

end_per_suite(Config) ->
    Config.
    %% unmock_config(Config).

init_per_testcase(same_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"same_alias_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, fakelib}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, goodpkg}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias_vsn, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, "1.0.0", {pkg, goodpkg}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_hash_mismatch, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
    [{rebarconfig, RebarConf} | Config].

end_per_testcase(_, Config) ->
    unmock_config(Config),
    Config.

same_alias(Config) ->
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"}, {dep, "fakelib"}]}
    ).

diff_alias(Config) ->
    %% even though the dep is 'fakelib' aliased as 'goodpkg' all
    %% internal records use 'fakelib' as a value. Just make sure
    %% the lock actually maintains the proper source as 'goodpkg'
    AppDir = ?config(apps, Config),
    Lockfile = filename:join([AppDir, "rebar.lock"]),
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true
                      ;  (_) -> false end, LockData)),
    %% An second run yields the same
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    %% So does an upgrade
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["upgrade"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile).

diff_alias_vsn(Config) -> diff_alias(Config).

transitive_alias(Config) ->
    %% ensure that the apps fetched under transitive aliases are
    %% locked properly, but also that they are stored in the right
    %% directory in the build dir to avoid breaking includes and
    %% static analysis tools that rely on the location to work
    AppDir = ?config(apps, Config),
    Lockfile = filename:join([AppDir, "rebar.lock"]),
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(lists:any(fun({<<"transitive_app">>,{pkg,<<"transitive">>,_},_}) -> true
                      ;  (_) -> false end, LockData)),
    AppDir = ?config(apps, Config),
    AliasedName = filename:join([AppDir, "_build", "default", "lib", "transitive_app"]),
    PkgName = filename:join([AppDir, "_build", "default", "lib", "transitive"]),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    %% An second run yields the same
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    %% So does an upgrade
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["upgrade"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    ok.

transitive_hash_mismatch(Config) ->
    %% ensure that the apps fetched under transitive aliases are<commit_msg>Fix OTP24 complaining about underscored variables<commit_after>-module(rebar_pkg_alias_SUITE).
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("kernel/include/file.hrl").
-include("rebar.hrl").

all() -> [same_alias, diff_alias, diff_alias_vsn, transitive_alias%% ,
          %% transitive_hash_mismatch
         ].

%% {uuid, {pkg, uuid}} = uuid
%% {uuid, {pkg, alias}} = uuid on disk
%% another run should yield the same lock file without error
init_per_suite(Config) ->
    Config.
    %% mock_config(?MODULE, Config).

end_per_suite(Config) ->
    Config.
    %% unmock_config(Config).

init_per_testcase(same_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"same_alias_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, fakelib}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, goodpkg}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias_vsn, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, "1.0.0", {pkg, goodpkg}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_alias, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
    [{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_hash_mismatch, Config0) ->
    mock_config(?MODULE, Config0),
    Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
    AppDir = ?config(apps, Config),
    rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
    RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
    [{rebarconfig, RebarConf} | Config].

end_per_testcase(_, Config) ->
    unmock_config(Config),
    Config.

same_alias(Config) ->
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"}, {dep, "fakelib"}]}
    ).

diff_alias(Config) ->
    %% even though the dep is 'fakelib' aliased as 'goodpkg' all
    %% internal records use 'fakelib' as a value. Just make sure
    %% the lock actually maintains the proper source as 'goodpkg'
    AppDir = ?config(apps, Config),
    Lockfile = filename:join([AppDir, "rebar.lock"]),
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true
                      ;  (_) -> false end, LockData)),
    %% An second run yields the same
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{Vsn, LockData}|_]} = file:consult(Lockfile),
    %% So does an upgrade
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["upgrade"],
        {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
    ),
    {ok, [{Vsn, LockData}|_]} = file:consult(Lockfile).

diff_alias_vsn(Config) -> diff_alias(Config).

transitive_alias(Config) ->
    %% ensure that the apps fetched under transitive aliases are
    %% locked properly, but also that they are stored in the right
    %% directory in the build dir to avoid breaking includes and
    %% static analysis tools that rely on the location to work
    AppDir = ?config(apps, Config),
    Lockfile = filename:join([AppDir, "rebar.lock"]),
    {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(lists:any(fun({<<"transitive_app">>,{pkg,<<"transitive">>,_},_}) -> true
                      ;  (_) -> false end, LockData)),
    AppDir = ?config(apps, Config),
    AliasedName = filename:join([AppDir, "_build", "default", "lib", "transitive_app"]),
    PkgName = filename:join([AppDir, "_build", "default", "lib", "transitive"]),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    %% An second run yields the same
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["lock"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    %% So does an upgrade
    rebar_test_utils:run_and_check(
        Config, RebarConfig, ["upgrade"],
        {ok, [{lock, "topdep"},{dep, "topdep"},
              {lock,"transitive_app"},{dep,"transitive_app"}]}
    ),
    {ok, [{Vsn, LockData}|_]} = file:consult(Lockfile),
    ?assert(filelib:is_dir(AliasedName)),
    ?assertNot(filelib:is_dir(PkgName)),
    ok.

transitive_hash_mismatch(Config) ->
    %% ensure that the apps fetched under transitive aliases are
{'fraction_non_alphanumeric': 0.17173611677403572, 'fraction_numerical': 0.006523689146212183, 'mean_word_length': 4.374233128834356, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416165', 'n_tokens_mistral': 4466, 'n_tokens_neox': 4410, 'n_words': 814}