From 7f508c30a1ece781f5dee552d3b5ae949947eb2f Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Thu, 5 Mar 2015 17:56:02 -0600 Subject: apply profiles and overrides for an app's opts to the base opts --- src/rebar_app_discover.erl | 4 ++-- src/rebar_core.erl | 9 +++++---- src/rebar_state.erl | 6 ++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index 553947a..c5c0efd 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -34,13 +34,14 @@ format_error({missing_module, Module}) -> io_lib:format("Module defined in app file missing: ~p~n", [Module]). merge_deps(AppInfo, State) -> + Default = rebar_state:default(State), Profiles = rebar_state:current_profiles(State), Name = rebar_app_info:name(AppInfo), C = rebar_config:consult(rebar_app_info:dir(AppInfo)), AppState = rebar_state:apply_overrides( rebar_state:apply_profiles( - rebar_state:new(State, C, rebar_app_info:dir(AppInfo)), Profiles), Name), + rebar_state:new(rebar_state:opts(State, Default), C, rebar_app_info:dir(AppInfo)), Profiles), Name), AppInfo1 = rebar_app_info:state(AppInfo, AppState), State1 = lists:foldl(fun(Profile, StateAcc) -> @@ -50,7 +51,6 @@ merge_deps(AppInfo, State) -> rebar_state:set(StateAcc, {deps, Profile}, ProfDeps2) end, State, lists:reverse(Profiles)), - {AppInfo1, State1}. -spec all_app_dirs(list(file:name())) -> list(file:name()). diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 205258a..f0d9b3d 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -78,12 +78,13 @@ process_command(State, Command) -> Command when Command =:= do; Command =:= as -> do(TargetProviders, State); _ -> + Profiles = providers:profiles(CommandProvider), + State1 = rebar_state:apply_profiles(State, Profiles), Opts = providers:opts(CommandProvider)++rebar3:global_option_spec_list(), - - case getopt:parse(Opts, rebar_state:command_args(State)) of + case getopt:parse(Opts, rebar_state:command_args(State1)) of {ok, Args} -> - State1 = rebar_state:command_parsed_args(State, Args), - do(TargetProviders, State1); + State2 = rebar_state:command_parsed_args(State1, Args), + do(TargetProviders, State2); {error, {invalid_option, Option}} -> {error, io_lib:format("Invalid option ~s on task ~p", [Option, Command])} end diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 8170b8d..6ca3baa 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -3,7 +3,7 @@ -export([new/0, new/1, new/2, new/3, get/2, get/3, set/3, - opts/1, + opts/1, opts/2, default/1, default/2, escript_path/1, escript_path/2, @@ -37,7 +37,6 @@ -record(state_t, {dir :: file:name(), opts = dict:new() :: rebar_dict(), default = dict:new() :: rebar_dict(), - escript_path :: undefined | file:filename_all(), lock = [], @@ -129,6 +128,9 @@ default(State, Opts) -> opts(#state_t{opts=Opts}) -> Opts. +opts(State, Opts) -> + State#state_t{opts=Opts}. + current_profiles(#state_t{current_profiles=Profiles}) -> Profiles. -- cgit v1.1 From ed45ed2d8a7329a9749d98ac5455d853047ab9e0 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Thu, 5 Mar 2015 20:24:08 -0800 Subject: two failing tests and one passing test to attempt to isolate where profiles are failing to be applied relevant to #224 and #233 --- test/rebar_profiles_SUITE.erl | 50 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/test/rebar_profiles_SUITE.erl b/test/rebar_profiles_SUITE.erl index 8a1521e..8f87832 100644 --- a/test/rebar_profiles_SUITE.erl +++ b/test/rebar_profiles_SUITE.erl @@ -10,7 +10,10 @@ profile_merges/1, add_to_profile/1, add_to_existing_profile/1, - profiles_remain_applied_with_config_present/1]). + profiles_remain_applied_with_config_present/1, + test_profile_applied_at_completion/1, + test_profile_applied_before_compile/1, + test_profile_applied_before_eunit/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -19,7 +22,10 @@ all() -> [profile_new_key, profile_merge_keys, profile_merges, add_to_profile, add_to_existing_profile, - profiles_remain_applied_with_config_present]. + profiles_remain_applied_with_config_present, + test_profile_applied_at_completion, + test_profile_applied_before_compile, + test_profile_applied_before_eunit]. init_per_suite(Config) -> application:start(meck), @@ -151,3 +157,43 @@ profiles_remain_applied_with_config_present(Config) -> Mod = list_to_atom("not_a_real_src_" ++ Name), true = lists:member({d, not_ok}, proplists:get_value(options, Mod:module_info(compile), [])). + +test_profile_applied_at_completion(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("test_profile_at_completion_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + + {ok, State} = rebar_test_utils:run_and_check(Config, + [], + ["eunit"], + return), + + Opts = rebar_state:opts(State), + ErlOpts = dict:fetch(erl_opts, Opts), + true = lists:member({d, 'TEST'}, ErlOpts). + +test_profile_applied_before_compile(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("test_profile_before_compile_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + + rebar_test_utils:run_and_check(Config, [], ["eunit"], {ok, [{app, Name}]}), + + S = list_to_atom("not_a_real_src_" ++ Name), + true = lists:member({d, 'TEST'}, proplists:get_value(options, S:module_info(compile), [])). + +test_profile_applied_before_eunit(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("test_profile_before_eunit_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + + rebar_test_utils:run_and_check(Config, [], ["eunit"], {ok, [{app, Name}]}), + + T = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), + true = lists:member({d, 'TEST'}, proplists:get_value(options, T:module_info(compile), [])). -- cgit v1.1 From 67aca881c1ad5f3bb3f10c28bc0bc04c45d09407 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Thu, 5 Mar 2015 20:40:06 -0800 Subject: add test for test profile applied to project apps --- test/rebar_profiles_SUITE.erl | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/rebar_profiles_SUITE.erl b/test/rebar_profiles_SUITE.erl index 8f87832..55ab3d7 100644 --- a/test/rebar_profiles_SUITE.erl +++ b/test/rebar_profiles_SUITE.erl @@ -13,7 +13,8 @@ profiles_remain_applied_with_config_present/1, test_profile_applied_at_completion/1, test_profile_applied_before_compile/1, - test_profile_applied_before_eunit/1]). + test_profile_applied_before_eunit/1, + test_profile_applied_to_apps/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -25,7 +26,8 @@ all() -> profiles_remain_applied_with_config_present, test_profile_applied_at_completion, test_profile_applied_before_compile, - test_profile_applied_before_eunit]. + test_profile_applied_before_eunit, + test_profile_applied_to_apps]. init_per_suite(Config) -> application:start(meck), @@ -197,3 +199,23 @@ test_profile_applied_before_eunit(Config) -> T = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), true = lists:member({d, 'TEST'}, proplists:get_value(options, T:module_info(compile), [])). + +test_profile_applied_to_apps(Config) -> + AppDir = ?config(apps, Config), + + Name = rebar_test_utils:create_random_name("test_profile_applied_to_apps_"), + Vsn = rebar_test_utils:create_random_vsn(), + rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + + {ok, State} = rebar_test_utils:run_and_check(Config, + [], + ["eunit"], + return), + + Apps = rebar_state:project_apps(State), + lists:foreach(fun(App) -> + AppState = rebar_app_info:state(App), + Opts = rebar_state:opts(AppState), + ErlOpts = dict:fetch(erl_opts, Opts), + true = lists:member({d, 'TEST'}, ErlOpts) + end, Apps). -- cgit v1.1 From 0638c85634f3f9f51f17f247a3376f72b2f4d2fa Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 6 Mar 2015 08:59:10 -0600 Subject: store base opts after initialization of providers --- src/rebar3.erl | 9 ++++++--- src/rebar_app_discover.erl | 7 +++---- src/rebar_state.erl | 1 + test/rebar_profiles_SUITE.erl | 20 ++++++++++++++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/rebar3.erl b/src/rebar3.erl index d7597cd..5b7db24 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -115,8 +115,7 @@ run_aux(State, GlobalPluginProviders, RawArgs) -> false -> State; Profile -> - State1 = rebar_state:apply_profiles(State, [list_to_atom(Profile)]), - rebar_state:default(State1, rebar_state:opts(State1)) + rebar_state:apply_profiles(State, [list_to_atom(Profile)]) end, %% Process each command, resetting any state between each one @@ -127,11 +126,15 @@ run_aux(State, GlobalPluginProviders, RawArgs) -> {ok, Providers} = application:get_env(rebar, providers), {ok, PluginProviders, State4} = rebar_plugins:install(State3), rebar_core:update_code_path(State4), + + %% Providers can modify profiles stored in opts, so set default after initializing providers AllProviders = Providers++PluginProviders++GlobalPluginProviders, State5 = rebar_state:create_logic_providers(AllProviders, State4), + State6 = rebar_state:default(State5, rebar_state:opts(State5)), + {Task, Args} = parse_args(RawArgs), - rebar_core:process_command(rebar_state:command_args(State5, Args), Task). + rebar_core:process_command(rebar_state:command_args(State6, Args), Task). init_config() -> %% Initialize logging system diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index c5c0efd..c720f72 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -35,13 +35,12 @@ format_error({missing_module, Module}) -> merge_deps(AppInfo, State) -> Default = rebar_state:default(State), - Profiles = rebar_state:current_profiles(State), + CurrentProfiles = rebar_state:current_profiles(State), Name = rebar_app_info:name(AppInfo), C = rebar_config:consult(rebar_app_info:dir(AppInfo)), - AppState = rebar_state:apply_overrides( rebar_state:apply_profiles( - rebar_state:new(rebar_state:opts(State, Default), C, rebar_app_info:dir(AppInfo)), Profiles), Name), + rebar_state:new(rebar_state:opts(State, Default), C, rebar_app_info:dir(AppInfo)), CurrentProfiles), Name), AppInfo1 = rebar_app_info:state(AppInfo, AppState), State1 = lists:foldl(fun(Profile, StateAcc) -> @@ -49,7 +48,7 @@ merge_deps(AppInfo, State) -> TopLevelProfDeps = rebar_state:get(StateAcc, {deps, Profile}, []), ProfDeps2 = lists:keymerge(1, TopLevelProfDeps, AppProfDeps), rebar_state:set(StateAcc, {deps, Profile}, ProfDeps2) - end, State, lists:reverse(Profiles)), + end, State, lists:reverse(CurrentProfiles)), {AppInfo1, State1}. diff --git a/src/rebar_state.erl b/src/rebar_state.erl index 6ca3baa..e77a259 100644 --- a/src/rebar_state.erl +++ b/src/rebar_state.erl @@ -96,6 +96,7 @@ new(ParentState, Config, Dir) -> D = proplists:get_value(deps, Config, []), dict:from_list([{{deps, default}, D} | Config]) end, + NewOpts = dict:merge(fun(_Key, Value1, _Value2) -> Value1 end, LocalOpts, Opts), diff --git a/test/rebar_profiles_SUITE.erl b/test/rebar_profiles_SUITE.erl index 55ab3d7..a4f926e 100644 --- a/test/rebar_profiles_SUITE.erl +++ b/test/rebar_profiles_SUITE.erl @@ -167,8 +167,11 @@ test_profile_applied_at_completion(Config) -> Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:create_config(AppDir, RebarConfig), + {ok, State} = rebar_test_utils:run_and_check(Config, - [], + RebarConfig, ["eunit"], return), @@ -183,7 +186,10 @@ test_profile_applied_before_compile(Config) -> Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), - rebar_test_utils:run_and_check(Config, [], ["eunit"], {ok, [{app, Name}]}), + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:create_config(AppDir, RebarConfig), + + rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}), S = list_to_atom("not_a_real_src_" ++ Name), true = lists:member({d, 'TEST'}, proplists:get_value(options, S:module_info(compile), [])). @@ -195,7 +201,10 @@ test_profile_applied_before_eunit(Config) -> Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), - rebar_test_utils:run_and_check(Config, [], ["eunit"], {ok, [{app, Name}]}), + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:create_config(AppDir, RebarConfig), + + rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}), T = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"), true = lists:member({d, 'TEST'}, proplists:get_value(options, T:module_info(compile), [])). @@ -207,8 +216,11 @@ test_profile_applied_to_apps(Config) -> Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), + RebarConfig = [{erl_opts, [{d, some_define}]}], + rebar_test_utils:create_config(AppDir, RebarConfig), + {ok, State} = rebar_test_utils:run_and_check(Config, - [], + RebarConfig, ["eunit"], return), -- cgit v1.1 From 0399b0eeb30d531360faa888c7d64b5d0b2f09ce Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 6 Mar 2015 09:12:05 -0600 Subject: add comment about resetting opts --- src/rebar_app_discover.erl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rebar_app_discover.erl b/src/rebar_app_discover.erl index c720f72..63cf703 100644 --- a/src/rebar_app_discover.erl +++ b/src/rebar_app_discover.erl @@ -38,6 +38,8 @@ merge_deps(AppInfo, State) -> CurrentProfiles = rebar_state:current_profiles(State), Name = rebar_app_info:name(AppInfo), C = rebar_config:consult(rebar_app_info:dir(AppInfo)), + + %% We reset the opts here to default so no profiles are applied multiple times AppState = rebar_state:apply_overrides( rebar_state:apply_profiles( rebar_state:new(rebar_state:opts(State, Default), C, rebar_app_info:dir(AppInfo)), CurrentProfiles), Name), -- cgit v1.1