Skip to content

Commit

Permalink
Merge pull request #445 from alanz/440-global-config
Browse files Browse the repository at this point in the history
[#440] global config
  • Loading branch information
alanz authored Feb 21, 2020
2 parents 2de099a + 472e53c commit e10c894
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
2 changes: 2 additions & 0 deletions priv/rebar3_release/erlang_ls.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
apps_dirs:
- "apps/*"
macros:
- name: DEFINED_FOR_RELATIVE_TEST
13 changes: 11 additions & 2 deletions src/els_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ handle_cast(_Msg, State) -> {noreply, State}.
%%==============================================================================

-spec config_paths(path(), map()) -> [path()].
config_paths(RootPath, #{<<"erlang">> := #{<<"config_path">> := ConfigPath}}) ->
[filename:join([RootPath, ConfigPath]) | default_config_paths(RootPath)];
config_paths( RootPath
, #{<<"erlang">> := #{<<"config_path">> := ConfigPath0}}) ->
ConfigPath = binary_to_list(ConfigPath0),
lists:append([ possible_config_paths(ConfigPath)
, possible_config_paths(filename:join([RootPath, ConfigPath]))
, default_config_paths(RootPath)]);
config_paths(RootPath, _Config) ->
default_config_paths(RootPath).

Expand All @@ -161,6 +165,11 @@ default_config_paths(RootPath) ->
, filename:join([GlobalConfigDir, ?DEFAULT_CONFIG_FILE])
].

%% @doc Bare `Path' as well as with default config file name suffix.
-spec possible_config_paths(path()) -> [path()].
possible_config_paths(Path) ->
[ Path, filename:join([Path, ?DEFAULT_CONFIG_FILE]) ].

-spec consult_config([path()]) -> map().
consult_config([]) -> #{};
consult_config([Path | Paths]) ->
Expand Down
103 changes: 103 additions & 0 deletions test/els_initialization_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-module(els_initialization_SUITE).

-include("erlang_ls.hrl").

%% CT Callbacks
-export([ suite/0
, init_per_suite/1
, end_per_suite/1
, init_per_testcase/2
, end_per_testcase/2
, groups/0
, all/0
]).

%% Test cases
-export([ initialize_default/1
, initialize_custom_relative/1
, initialize_custom_absolute/1
]).

%%==============================================================================
%% Includes
%%==============================================================================
-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").

%%==============================================================================
%% Types
%%==============================================================================
-type config() :: [{atom(), any()}].

%%==============================================================================
%% CT Callbacks
%%==============================================================================
-spec suite() -> [tuple()].
suite() ->
[{timetrap, {seconds, 30}}].

-spec all() -> [{group, stdio | tcp}].
all() ->
[{group, tcp}, {group, stdio}].

-spec groups() -> [atom()].
groups() ->
els_test_utils:groups(?MODULE).

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
els_test_utils:init_per_suite(Config).

-spec end_per_suite(config()) -> ok.
end_per_suite(Config) ->
els_test_utils:end_per_suite(Config).

-spec init_per_testcase(atom(), config()) -> config().
init_per_testcase(_TestCase, Config) ->
Transport = els_test_utils:get_group(Config),
Started = els_test_utils:start(Transport),

[{started, Started} | Config].

-spec end_per_testcase(atom(), config()) -> ok.
end_per_testcase(TestCase, Config) ->
els_test_utils:end_per_testcase(TestCase, Config).

%%==============================================================================
%% Testcases
%%==============================================================================

-spec initialize_default(config()) -> ok.
initialize_default(Config) ->
RootUri = ?config(root_uri, Config),
els_client:initialize(RootUri, []),
Result = els_config:get(macros),
Expected = [#{"name" => "DEFINED_WITHOUT_VALUE"},
#{"name" => "DEFINED_WITH_VALUE","value" => 1}],
?assertEqual(Expected, Result),
ok.

-spec initialize_custom_relative(config()) -> ok.
initialize_custom_relative(Config) ->
RootUri = ?config(root_uri, Config),
ConfigPath = <<"../rebar3_release/erlang_ls.config">>,
InitOpts = #{ <<"erlang">>
=> #{ <<"config_path">> => ConfigPath }},
els_client:initialize(RootUri, InitOpts),
Result = els_config:get(macros),
Expected = [#{"name" => "DEFINED_FOR_RELATIVE_TEST"}],
?assertEqual(Expected, Result),
ok.

-spec initialize_custom_absolute(config()) -> ok.
initialize_custom_absolute(Config) ->
RootUri = ?config(root_uri, Config),
ConfigPath = filename:join( els_uri:path(RootUri)
, "../rebar3_release/erlang_ls.config"),
InitOpts = #{ <<"erlang">>
=> #{ <<"config_path">> => ConfigPath }},
els_client:initialize(RootUri, InitOpts),
Result = els_config:get(macros),
Expected = [#{"name" => "DEFINED_FOR_RELATIVE_TEST"}],
?assertEqual(Expected, Result),
ok.

0 comments on commit e10c894

Please sign in to comment.