From 9906fdc25e3769d07b0de78a18501c7a681e6dc9 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Wed, 1 Jul 2015 13:58:40 -0300 Subject: Added support to http and https proxies on bootstrap. Variables are read from environment vars http_proxy and https_proxy. --- bootstrap | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/bootstrap b/bootstrap index 41d9725..ca40489 100755 --- a/bootstrap +++ b/bootstrap @@ -82,6 +82,7 @@ extract(Binary) -> {ok, Contents}. request(Url) -> + setHttpcOptions(), case httpc:request(get, {Url, []}, [{relaxed, true}], [{body_format, binary}]) of @@ -91,6 +92,47 @@ request(Url) -> Error end. +setHttpcOptions() -> + %% Get http_proxy and https_proxy environment variables + case os:getenv("http_proxy") of + false -> + Http = ""; + Http -> + Http + end, + case os:getenv("https_proxy") of + false -> + Https = ""; + Https -> + Https + end, + + %% Parse the variables to extract host and port + Opts = [], + case http_uri:parse(Http) of + {ok,{_, [], Host, Port, _, []}} -> + Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; + {error, _} -> + Opts1 = Opts + end, + + case http_uri:parse(Https) of + {ok,{_, [], Host2, Port2, _, []}} -> + Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; + {error, _} -> + Opts2 = Opts1 + end, + + io:format("Opts: ~p~n", [Opts2]), + + case Opts2 of + [] -> + ok; + _ -> + httpc:set_options(Opts2), + ok + end. + compile(App, FirstFiles) -> Dir = filename:join(filename:absname("_build/default/lib/"), App), filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])), -- cgit v1.1 From 8de84f1af04a716357dbc5473f7daf74cb31f44a Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Wed, 1 Jul 2015 14:13:43 -0300 Subject: Added support for proxy on rebar3 based on environment variables. --- bootstrap | 2 -- src/rebar3.erl | 12 ++---------- src/rebar_utils.erl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/bootstrap b/bootstrap index ca40489..1e56263 100755 --- a/bootstrap +++ b/bootstrap @@ -123,8 +123,6 @@ setHttpcOptions() -> Opts2 = Opts1 end, - io:format("Opts: ~p~n", [Opts2]), - case Opts2 of [] -> ok; diff --git a/src/rebar3.erl b/src/rebar3.erl index c501709..13f6017 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -273,13 +273,5 @@ start_and_load_apps() -> application:start(public_key), application:start(ssl), inets:start(), - inets:start(httpc, [{profile, hex}]), - http_opts(). - -http_opts() -> - Opts = [{max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000}], - httpc:set_options(Opts, hex). + inets:start(httpc), + rebar_utils:set_httpc_options(). diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 0cbc7c2..35964ea 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -56,7 +56,8 @@ wordsize/0, tup_umerge/2, tup_sort/1, - line_count/1]). + line_count/1, + set_httpc_options/0]). %% for internal use only -export([otp_release/0]). @@ -661,3 +662,54 @@ maybe_ends_in_comma(H) -> "," ++ Flag -> lists:reverse(Flag); _ -> false end. + +set_httpc_options() -> + %% Get http_proxy and https_proxy environment variables + case os:getenv("http_proxy") of + false -> + Http = ""; + Http -> + Http + end, + case os:getenv("https_proxy") of + false -> + Https = ""; + Https -> + Https + end, + + %% Parse the variables to extract host and port + Opts = [], + case http_uri:parse(Http) of + {ok,{_, [], Host, Port, _, []}} -> + Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; + {error, _} -> + Opts1 = Opts + end, + + case http_uri:parse(Https) of + {ok,{_, [], Host2, Port2, _, []}} -> + Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; + {error, _} -> + Opts2 = Opts1 + end, + + case Opts2 of + [] -> + Opts3 = [ + {max_sessions, 4}, + {max_keep_alive_length, 4}, + {keep_alive_timeout, 120000}, + {max_pipeline_length, 4}, + {pipeline_timeout, 60000} + ]; + _ -> + Opts3 = Opts2 ++ [ + {max_sessions, 4}, + {max_keep_alive_length, 4}, + {keep_alive_timeout, 120000}, + {max_pipeline_length, 4}, + {pipeline_timeout, 60000} + ] + end, + httpc:set_options(Opts3). -- cgit v1.1 From 60265aba3497aa97a35b706e00a2526e100733bd Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Wed, 1 Jul 2015 18:01:35 -0300 Subject: Get proxy vars from ~/.config/rebar3/rebar.config. Variable format is {http_proxy, http://host:port} or {http_proxy, http://host:port} --- bootstrap | 59 +++++++++++++++++---------------------------- src/rebar_utils.erl | 69 ++++++++++++++++------------------------------------- 2 files changed, 43 insertions(+), 85 deletions(-) diff --git a/bootstrap b/bootstrap index 1e56263..ad94561 100755 --- a/bootstrap +++ b/bootstrap @@ -82,7 +82,7 @@ extract(Binary) -> {ok, Contents}. request(Url) -> - setHttpcOptions(), + set_httpc_options(), case httpc:request(get, {Url, []}, [{relaxed, true}], [{body_format, binary}]) of @@ -92,45 +92,30 @@ request(Url) -> Error end. -setHttpcOptions() -> - %% Get http_proxy and https_proxy environment variables - case os:getenv("http_proxy") of - false -> - Http = ""; - Http -> - Http - end, - case os:getenv("https_proxy") of - false -> - Https = ""; - Https -> - Https - end, - - %% Parse the variables to extract host and port - Opts = [], - case http_uri:parse(Http) of - {ok,{_, [], Host, Port, _, []}} -> - Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; - {error, _} -> - Opts1 = Opts - end, - - case http_uri:parse(Https) of - {ok,{_, [], Host2, Port2, _, []}} -> - Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; - {error, _} -> - Opts2 = Opts1 - end, - - case Opts2 of - [] -> - ok; +get_http_var() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; _ -> - httpc:set_options(Opts2), - ok + [] end. +get_http(Scheme) -> + proplists:get_value(Scheme, get_http_var(), ""). + +set_httpc_options() -> + set_httpc_options(https_proxy, get_http(https_proxy)), + set_httpc_options(proxy, get_http(http_proxy)). + +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}]). + compile(App, FirstFiles) -> Dir = filename:join(filename:absname("_build/default/lib/"), App), filelib:ensure_dir(filename:join([Dir, "ebin", "dummy.beam"])), diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 35964ea..77d5e71 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -663,53 +663,26 @@ maybe_ends_in_comma(H) -> _ -> false end. +get_http_var() -> + {ok, [[Home]]} = init:get_argument(home), + ConfDir = filename:join(Home, ".config/rebar3"), + case file:consult(filename:join(ConfDir, "rebar.config")) of + {ok, Config} -> + Config; + _ -> + [] + end. + +get_http(Scheme) -> + proplists:get_value(Scheme, get_http_var(), ""). + set_httpc_options() -> - %% Get http_proxy and https_proxy environment variables - case os:getenv("http_proxy") of - false -> - Http = ""; - Http -> - Http - end, - case os:getenv("https_proxy") of - false -> - Https = ""; - Https -> - Https - end, - - %% Parse the variables to extract host and port - Opts = [], - case http_uri:parse(Http) of - {ok,{_, [], Host, Port, _, []}} -> - Opts1 = Opts ++ [{proxy, {{Host, Port}, []}}]; - {error, _} -> - Opts1 = Opts - end, + set_httpc_options(https_proxy, get_http(https_proxy)), + set_httpc_options(proxy, get_http(http_proxy)). - case http_uri:parse(Https) of - {ok,{_, [], Host2, Port2, _, []}} -> - Opts2 = Opts1 ++ [{https_proxy, {{Host2, Port2}, []}}]; - {error, _} -> - Opts2 = Opts1 - end, - - case Opts2 of - [] -> - Opts3 = [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ]; - _ -> - Opts3 = Opts2 ++ [ - {max_sessions, 4}, - {max_keep_alive_length, 4}, - {keep_alive_timeout, 120000}, - {max_pipeline_length, 4}, - {pipeline_timeout, 60000} - ] - end, - httpc:set_options(Opts3). +set_httpc_options(_, []) -> + ok; + +set_httpc_options(Scheme, Proxy) -> + {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), + httpc:set_options([{Scheme, {{Host, Port}, []}}]). -- cgit v1.1 From 9a20d3b56a9a0d10d189224d785445c80fa6395f Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 10:05:54 -0300 Subject: No need to use profile hex. Proxy settings are applied globally. --- src/rebar_pkg_resource.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl index 5b37788..59ce0dc 100644 --- a/src/rebar_pkg_resource.erl +++ b/src/rebar_pkg_resource.erl @@ -95,8 +95,7 @@ make_vsn(_) -> request(Url, ETag) -> case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]}, [{relaxed, true}], - [{body_format, binary}], - hex) of + [{body_format, binary}]) of {ok, {{_Version, 200, _Reason}, Headers, Body}} -> ?DEBUG("Successfully downloaded ~s", [Url]), {"etag", ETag1} = lists:keyfind("etag", 1, Headers), -- cgit v1.1 From 46ca2bb6b0e9c99c7a5b4efb7b9efc310c0982b8 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 10:42:46 -0300 Subject: Fix for cases with no proxy configured. --- bootstrap | 2 +- src/rebar_utils.erl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bootstrap b/bootstrap index ad94561..8a0afd6 100755 --- a/bootstrap +++ b/bootstrap @@ -103,7 +103,7 @@ get_http_var() -> end. get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), ""). + proplists:get_value(Scheme, get_http_var(), []). set_httpc_options() -> set_httpc_options(https_proxy, get_http(https_proxy)), diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 77d5e71..70741e4 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -674,7 +674,7 @@ get_http_var() -> end. get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), ""). + proplists:get_value(Scheme, get_http_var(), []). set_httpc_options() -> set_httpc_options(https_proxy, get_http(https_proxy)), -- cgit v1.1 From 38cc32c40afdf8e999d8705501acd7e13918cdc9 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 11:59:00 -0300 Subject: Added rebar profile to httpc initialization and calls. --- bootstrap | 8 +++++--- src/rebar3.erl | 3 ++- src/rebar_pkg_resource.erl | 3 ++- src/rebar_prv_update.erl | 3 ++- src/rebar_utils.erl | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bootstrap b/bootstrap index 8a0afd6..4fed22a 100755 --- a/bootstrap +++ b/bootstrap @@ -9,6 +9,8 @@ main(_Args) -> application:start(public_key), application:start(ssl), inets:start(), + inets:start(httpc, [{profile, rebar}]), + set_httpc_options(), %% Fetch and build deps required to build rebar3 BaseDeps = [{providers, []} @@ -82,10 +84,10 @@ extract(Binary) -> {ok, Contents}. request(Url) -> - set_httpc_options(), case httpc:request(get, {Url, []}, [{relaxed, true}], - [{body_format, binary}]) of + [{body_format, binary}], + rebar) of {ok, {{_Version, 200, _Reason}, _Headers, Body}} -> {ok, Body}; Error -> @@ -114,7 +116,7 @@ set_httpc_options(_, []) -> set_httpc_options(Scheme, Proxy) -> {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), - httpc:set_options([{Scheme, {{Host, Port}, []}}]). + httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar). compile(App, FirstFiles) -> Dir = filename:join(filename:absname("_build/default/lib/"), App), diff --git a/src/rebar3.erl b/src/rebar3.erl index 13f6017..a797c46 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -273,5 +273,6 @@ start_and_load_apps() -> application:start(public_key), application:start(ssl), inets:start(), - inets:start(httpc), + inets:start(httpc, [{profile, rebar}]), rebar_utils:set_httpc_options(). + diff --git a/src/rebar_pkg_resource.erl b/src/rebar_pkg_resource.erl index 59ce0dc..450ff1e 100644 --- a/src/rebar_pkg_resource.erl +++ b/src/rebar_pkg_resource.erl @@ -95,7 +95,8 @@ make_vsn(_) -> request(Url, ETag) -> case httpc:request(get, {Url, [{"if-none-match", ETag} || ETag =/= false]}, [{relaxed, true}], - [{body_format, binary}]) of + [{body_format, binary}], + rebar) of {ok, {{_Version, 200, _Reason}, Headers, Body}} -> ?DEBUG("Successfully downloaded ~s", [Url]), {"etag", ETag1} = lists:keyfind("etag", 1, Headers), diff --git a/src/rebar_prv_update.erl b/src/rebar_prv_update.erl index 6838bab..64fe65e 100644 --- a/src/rebar_prv_update.erl +++ b/src/rebar_prv_update.erl @@ -43,7 +43,8 @@ do(State) -> Url = rebar_state:get(State, rebar_packages_cdn, "https://s3.amazonaws.com/s3.hex.pm/registry.ets.gz"), {ok, _RequestId} = httpc:request(get, {Url, []}, - [], [{stream, TmpFile}, {sync, true}]), + [], [{stream, TmpFile}, {sync, true}], + rebar), {ok, Data} = file:read_file(TmpFile), Unzipped = zlib:gunzip(Data), ok = file:write_file(HexFile, Unzipped), diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 70741e4..97104ce 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -685,4 +685,4 @@ set_httpc_options(_, []) -> set_httpc_options(Scheme, Proxy) -> {ok, {_, _, Host, Port, _, _}} = http_uri:parse(Proxy), - httpc:set_options([{Scheme, {{Host, Port}, []}}]). + httpc:set_options([{Scheme, {{Host, Port}, []}}], rebar). -- cgit v1.1 From 4a25a4d9d5f968fe2445707e8077a6efda596192 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 11:59:39 -0300 Subject: Renamed functions. --- bootstrap | 10 +++++----- src/rebar_utils.erl | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bootstrap b/bootstrap index 4fed22a..71c44da 100755 --- a/bootstrap +++ b/bootstrap @@ -94,7 +94,7 @@ request(Url) -> Error end. -get_http_var() -> +get_rebar_config() -> {ok, [[Home]]} = init:get_argument(home), ConfDir = filename:join(Home, ".config/rebar3"), case file:consult(filename:join(ConfDir, "rebar.config")) of @@ -104,12 +104,12 @@ get_http_var() -> [] end. -get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), []). +get_http_vars(Scheme) -> + proplists:get_value(Scheme, get_rebar_config(), []). set_httpc_options() -> - set_httpc_options(https_proxy, get_http(https_proxy)), - set_httpc_options(proxy, get_http(http_proxy)). + set_httpc_options(https_proxy, get_http_vars(https_proxy)), + set_httpc_options(proxy, get_http_vars(http_proxy)). set_httpc_options(_, []) -> ok; diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 97104ce..2bfe99e 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -677,8 +677,8 @@ get_http(Scheme) -> proplists:get_value(Scheme, get_http_var(), []). set_httpc_options() -> - set_httpc_options(https_proxy, get_http(https_proxy)), - set_httpc_options(proxy, get_http(http_proxy)). + set_httpc_options(https_proxy, get_http_vars(https_proxy)), + set_httpc_options(proxy, get_http_vars(http_proxy)). set_httpc_options(_, []) -> ok; -- cgit v1.1 From eb2d31be267ec0f85fe1bcd1f322c37149e3a9a3 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Thu, 2 Jul 2015 12:00:13 -0300 Subject: Use rebar own method to read global config file. --- src/rebar_utils.erl | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 2bfe99e..1cd6694 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -663,18 +663,10 @@ maybe_ends_in_comma(H) -> _ -> false end. -get_http_var() -> - {ok, [[Home]]} = init:get_argument(home), - ConfDir = filename:join(Home, ".config/rebar3"), - case file:consult(filename:join(ConfDir, "rebar.config")) of - {ok, Config} -> - Config; - _ -> - [] - end. - -get_http(Scheme) -> - proplists:get_value(Scheme, get_http_var(), []). +get_http_vars(Scheme) -> + GlobalConfigFile = rebar_dir:global_config(), + Config = rebar_config:consult_file(GlobalConfigFile), + proplists:get_value(Scheme, Config, []). set_httpc_options() -> set_httpc_options(https_proxy, get_http_vars(https_proxy)), -- cgit v1.1 From 6a6cc6a3eb6dc6f6ce68cbb7ca6323ecdeba8980 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Fri, 3 Jul 2015 16:31:35 -0300 Subject: Added myself to THANKS --- THANKS | 1 + 1 file changed, 1 insertion(+) diff --git a/THANKS b/THANKS index a6562ed..d813970 100644 --- a/THANKS +++ b/THANKS @@ -134,3 +134,4 @@ Martin Karlsson Pierre Fenoll David Kubecka Stefan Grundmann +Carlos Eduardo de Paula -- cgit v1.1 From d260c05d3707f59658b047032098d9c205fffd31 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Fri, 3 Jul 2015 16:32:02 -0300 Subject: Added testcases for http and https proxy use. --- test/rebar_deps_SUITE.erl | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/test/rebar_deps_SUITE.erl b/test/rebar_deps_SUITE.erl index afd487e..ece1738 100644 --- a/test/rebar_deps_SUITE.erl +++ b/test/rebar_deps_SUITE.erl @@ -3,7 +3,7 @@ -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). -all() -> [sub_app_deps, newly_added_dep, {group, git}, {group, pkg}]. +all() -> [sub_app_deps, newly_added_dep, http_proxy_settings, https_proxy_settings, {group, git}, {group, pkg}]. groups() -> [{all, [], [flat, pick_highest_left, pick_highest_right, @@ -33,6 +33,10 @@ init_per_testcase(newly_added_dep, Config) -> rebar_test_utils:init_rebar_state(Config); init_per_testcase(sub_app_deps, Config) -> rebar_test_utils:init_rebar_state(Config); +init_per_testcase(http_proxy_settings, Config) -> + rebar_test_utils:init_rebar_state(Config); +init_per_testcase(https_proxy_settings, Config) -> + rebar_test_utils:init_rebar_state(Config); init_per_testcase(Case, Config) -> {Deps, Warnings, Expect} = deps(Case), Expected = case Expect of @@ -223,6 +227,59 @@ newly_added_dep(Config) -> {ok, [{app, Name}, {dep, "a"}, {dep, "b", "1.0.0"}, {dep, "c", "1.0.0"}]}). +http_proxy_settings(Config) -> + %% Create private rebar.config + Priv = ?config(priv_dir, Config), + GlobalDir = filename:join(Priv, "global"), + GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), + GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), + + meck:new(rebar_dir, [passthrough]), + meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), + meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), + + %% Insert proxy variables into config + rebar_test_utils:create_config(GlobalConfigDir, + [{http_proxy, "http://localhost:1234"} + ]), + + %% Load config + rebar_utils:set_httpc_options(), + rebar3:init_config(), + + %% Assert variable is right + ?assertEqual({ok,{{"localhost", 1234}, []}}, + httpc:get_option(proxy, rebar)), + + meck:unload(rebar_dir). + +https_proxy_settings(Config) -> + %% Create private rebar.config + Priv = ?config(priv_dir, Config), + GlobalDir = filename:join(Priv, "global"), + GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), + GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), + + meck:new(rebar_dir, [passthrough]), + meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), + meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), + + %% Insert proxy variables into config + rebar_test_utils:create_config(GlobalConfigDir, + [{https_proxy, "http://localhost:1234"} + ]), + + %% Load config + rebar_utils:set_httpc_options(), + rebar3:init_config(), + + %% Assert variable is right + ?assertEqual({ok,{{"localhost", 1234}, []}}, + httpc:get_option(https_proxy, rebar)), + + meck:unload(rebar_dir). + + run(Config) -> {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)), rebar_test_utils:run_and_check( -- cgit v1.1 From 4d2873e3119a87990bc236aa45bfc8ef9a88f29c Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Fri, 3 Jul 2015 17:34:32 -0300 Subject: Refactor tests to init/exec/end. Check OTP version to skip https test on OTP =< 15. --- test/rebar_deps_SUITE.erl | 84 +++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/test/rebar_deps_SUITE.erl b/test/rebar_deps_SUITE.erl index ece1738..48ebb7f 100644 --- a/test/rebar_deps_SUITE.erl +++ b/test/rebar_deps_SUITE.erl @@ -34,9 +34,43 @@ init_per_testcase(newly_added_dep, Config) -> init_per_testcase(sub_app_deps, Config) -> rebar_test_utils:init_rebar_state(Config); init_per_testcase(http_proxy_settings, Config) -> + %% Create private rebar.config + Priv = ?config(priv_dir, Config), + GlobalDir = filename:join(Priv, "global"), + GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), + GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), + + meck:new(rebar_dir, [passthrough]), + meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), + meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), + + %% Insert proxy variables into config + rebar_test_utils:create_config(GlobalConfigDir, + [{http_proxy, "http://localhost:1234"} + ]), rebar_test_utils:init_rebar_state(Config); init_per_testcase(https_proxy_settings, Config) -> - rebar_test_utils:init_rebar_state(Config); + {OTPVersion, _} = string:to_integer(erlang:system_info(otp_release)), + case OTPVersion of + Vsn when Vsn =< 15 -> + {skip, https_proxy_unsupported_before_R16}; + _ -> + %% Create private rebar.config + Priv = ?config(priv_dir, Config), + GlobalDir = filename:join(Priv, "global"), + GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), + GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), + + meck:new(rebar_dir, [passthrough]), + meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), + meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), + + %% Insert proxy variables into config + rebar_test_utils:create_config(GlobalConfigDir, + [{https_proxy, "http://localhost:1234"} + ]), + rebar_test_utils:init_rebar_state(Config) + end; init_per_testcase(Case, Config) -> {Deps, Warnings, Expect} = deps(Case), Expected = case Expect of @@ -49,6 +83,12 @@ init_per_testcase(Case, Config) -> {warnings, Warnings} | setup_project(Case, Config, rebar_test_utils:expand_deps(DepsType, Deps))]. +end_per_testcase(https_proxy_settings, Config) -> + meck:unload(rebar_dir), + Config; +end_per_testcase(http_proxy_settings, Config) -> + meck:unload(rebar_dir), + Config; end_per_testcase(_, Config) -> meck:unload(), Config. @@ -227,57 +267,23 @@ newly_added_dep(Config) -> {ok, [{app, Name}, {dep, "a"}, {dep, "b", "1.0.0"}, {dep, "c", "1.0.0"}]}). -http_proxy_settings(Config) -> - %% Create private rebar.config - Priv = ?config(priv_dir, Config), - GlobalDir = filename:join(Priv, "global"), - GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), - GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), - - meck:new(rebar_dir, [passthrough]), - meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), - meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), - - %% Insert proxy variables into config - rebar_test_utils:create_config(GlobalConfigDir, - [{http_proxy, "http://localhost:1234"} - ]), - +http_proxy_settings(_Config) -> %% Load config rebar_utils:set_httpc_options(), rebar3:init_config(), %% Assert variable is right ?assertEqual({ok,{{"localhost", 1234}, []}}, - httpc:get_option(proxy, rebar)), - - meck:unload(rebar_dir). - -https_proxy_settings(Config) -> - %% Create private rebar.config - Priv = ?config(priv_dir, Config), - GlobalDir = filename:join(Priv, "global"), - GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]), - GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]), - - meck:new(rebar_dir, [passthrough]), - meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end), - meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end), - - %% Insert proxy variables into config - rebar_test_utils:create_config(GlobalConfigDir, - [{https_proxy, "http://localhost:1234"} - ]), + httpc:get_option(proxy, rebar)). +https_proxy_settings(_Config) -> %% Load config rebar_utils:set_httpc_options(), rebar3:init_config(), %% Assert variable is right ?assertEqual({ok,{{"localhost", 1234}, []}}, - httpc:get_option(https_proxy, rebar)), - - meck:unload(rebar_dir). + httpc:get_option(https_proxy, rebar)). run(Config) -> -- cgit v1.1 From 9e4bf8a3bad3f46f8094339b38ac880ec1b977d2 Mon Sep 17 00:00:00 2001 From: CarlosEDP Date: Sat, 4 Jul 2015 17:56:38 -0300 Subject: Added version check based on commit https://github.com/ferd/rebar3/commit/74c68478aac948beccc80e68c5ac6a9eb578eceb --- test/rebar_deps_SUITE.erl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/rebar_deps_SUITE.erl b/test/rebar_deps_SUITE.erl index 48ebb7f..73c4980 100644 --- a/test/rebar_deps_SUITE.erl +++ b/test/rebar_deps_SUITE.erl @@ -50,11 +50,14 @@ init_per_testcase(http_proxy_settings, Config) -> ]), rebar_test_utils:init_rebar_state(Config); init_per_testcase(https_proxy_settings, Config) -> - {OTPVersion, _} = string:to_integer(erlang:system_info(otp_release)), - case OTPVersion of - Vsn when Vsn =< 15 -> + SupportsHttpsProxy = case erlang:system_info(otp_release) of + "R16"++_ -> true; + "R"++_ -> false; + _ -> true % 17 and up don't have a "R" in the version + end, + if not SupportsHttpsProxy -> {skip, https_proxy_unsupported_before_R16}; - _ -> + SupportsHttpsProxy -> %% Create private rebar.config Priv = ?config(priv_dir, Config), GlobalDir = filename:join(Priv, "global"), -- cgit v1.1