2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2009-12-31 18:42:53 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
2009-12-04 05:49:52 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
|
|
|
|
%%
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
%%
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
%% -------------------------------------------------------------------
|
2009-12-30 12:13:39 +00:00
|
|
|
%%
|
2009-12-04 05:49:52 +00:00
|
|
|
%% Targets:
|
|
|
|
%% test - runs common test suites in ./test
|
|
|
|
%% int_test - runs suites in ./int_test
|
|
|
|
%% perf_test - runs suites inm ./perf_test
|
|
|
|
%%
|
|
|
|
%% Global options:
|
|
|
|
%% verbose=1 - show output from the common_test run as it goes
|
2012-01-06 17:33:41 +00:00
|
|
|
%% suites="foo,bar" - runs <test>/foo_SUITE and <test>/bar_SUITE
|
2009-12-04 05:49:52 +00:00
|
|
|
%% case="mycase" - runs individual test case foo_SUITE:mycase
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
-module(rebar_ct).
|
|
|
|
|
2010-06-22 21:43:09 +00:00
|
|
|
-export([ct/2]).
|
2009-12-04 05:49:52 +00:00
|
|
|
|
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-06-22 21:43:09 +00:00
|
|
|
ct(Config, File) ->
|
2011-09-18 12:38:31 +00:00
|
|
|
TestDir = rebar_config:get_local(Config, ct_dir, "test"),
|
2012-07-28 21:43:56 +00:00
|
|
|
LogDir = rebar_config:get_local(Config, ct_log_dir, "logs"),
|
|
|
|
run_test_if_present(TestDir, LogDir, Config, File).
|
2009-12-04 05:49:52 +00:00
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
2012-07-28 21:43:56 +00:00
|
|
|
run_test_if_present(TestDir, LogDir, Config, File) ->
|
2009-12-04 05:49:52 +00:00
|
|
|
case filelib:is_dir(TestDir) of
|
|
|
|
false ->
|
2010-06-22 21:43:09 +00:00
|
|
|
?WARN("~s directory not present - skipping\n", [TestDir]),
|
2009-12-04 05:49:52 +00:00
|
|
|
ok;
|
|
|
|
true ->
|
2012-08-19 13:25:13 +00:00
|
|
|
case filelib:wildcard(TestDir ++ "/*_SUITE.{beam,erl}") of
|
2012-07-28 21:43:56 +00:00
|
|
|
[] ->
|
|
|
|
?WARN("~s directory present, but no common_test"
|
|
|
|
++ " SUITES - skipping\n", [TestDir]),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
run_test(TestDir, LogDir, Config, File)
|
|
|
|
end
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
2012-07-28 21:43:56 +00:00
|
|
|
run_test(TestDir, LogDir, Config, _File) ->
|
|
|
|
{Cmd, RawLog} = make_cmd(TestDir, LogDir, Config),
|
|
|
|
clear_log(LogDir, RawLog),
|
2012-07-14 21:44:47 +00:00
|
|
|
case rebar_config:is_verbose(Config) of
|
2012-01-17 10:37:21 +00:00
|
|
|
false ->
|
2010-06-22 21:43:09 +00:00
|
|
|
Output = " >> " ++ RawLog ++ " 2>&1";
|
2012-01-17 10:37:21 +00:00
|
|
|
true ->
|
2010-06-22 21:43:09 +00:00
|
|
|
Output = " 2>&1 | tee -a " ++ RawLog
|
2009-12-04 05:49:52 +00:00
|
|
|
end,
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2010-12-05 00:07:12 +00:00
|
|
|
rebar_utils:sh(Cmd ++ Output, [{env,[{"TESTDIR", TestDir}]}]),
|
2012-07-14 21:44:47 +00:00
|
|
|
check_log(Config, RawLog).
|
2009-12-04 05:49:52 +00:00
|
|
|
|
|
|
|
|
2012-07-28 21:43:56 +00:00
|
|
|
clear_log(LogDir, RawLog) ->
|
|
|
|
case filelib:ensure_dir(filename:join(LogDir, "index.html")) of
|
2011-01-09 11:34:19 +00:00
|
|
|
ok ->
|
|
|
|
NowStr = rebar_utils:now_str(),
|
|
|
|
LogHeader = "--- Test run on " ++ NowStr ++ " ---\n",
|
|
|
|
ok = file:write_file(RawLog, LogHeader);
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR("Could not create log dir - ~p\n", [Reason]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL
|
2009-12-30 12:13:39 +00:00
|
|
|
end.
|
2009-12-04 05:49:52 +00:00
|
|
|
|
|
|
|
%% calling ct with erl does not return non-zero on failure - have to check
|
|
|
|
%% log results
|
2012-07-14 21:44:47 +00:00
|
|
|
check_log(Config, RawLog) ->
|
2010-12-05 00:07:12 +00:00
|
|
|
{ok, Msg} =
|
2011-10-01 11:19:49 +00:00
|
|
|
rebar_utils:sh("grep -e 'TEST COMPLETE' -e '{error,make_failed}' "
|
2010-12-05 00:07:12 +00:00
|
|
|
++ RawLog, [{use_stdout, false}]),
|
2009-12-04 14:58:21 +00:00
|
|
|
MakeFailed = string:str(Msg, "{error,make_failed}") =/= 0,
|
|
|
|
RunFailed = string:str(Msg, ", 0 failed") =:= 0,
|
|
|
|
if
|
|
|
|
MakeFailed ->
|
2012-07-14 21:44:47 +00:00
|
|
|
show_log(Config, RawLog),
|
2011-01-09 11:34:19 +00:00
|
|
|
?ERROR("Building tests failed\n",[]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL;
|
2009-12-04 14:58:21 +00:00
|
|
|
|
|
|
|
RunFailed ->
|
2012-07-14 21:44:47 +00:00
|
|
|
show_log(Config, RawLog),
|
2011-01-09 11:34:19 +00:00
|
|
|
?ERROR("One or more tests failed\n",[]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL;
|
2009-12-04 14:58:21 +00:00
|
|
|
|
2011-01-09 11:34:19 +00:00
|
|
|
true ->
|
2012-01-06 17:33:41 +00:00
|
|
|
?CONSOLE("DONE.\n~s\n", [Msg])
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2009-12-04 05:49:52 +00:00
|
|
|
%% Show the log if it hasn't already been shown because verbose was on
|
2012-07-14 21:44:47 +00:00
|
|
|
show_log(Config, RawLog) ->
|
2009-12-04 05:49:52 +00:00
|
|
|
?CONSOLE("Showing log\n", []),
|
2012-07-14 21:44:47 +00:00
|
|
|
case rebar_config:is_verbose(Config) of
|
2012-01-17 10:37:21 +00:00
|
|
|
false ->
|
2011-01-09 11:34:19 +00:00
|
|
|
{ok, Contents} = file:read_file(RawLog),
|
|
|
|
?CONSOLE("~s", [Contents]);
|
2012-01-17 10:37:21 +00:00
|
|
|
true ->
|
2011-01-09 11:34:19 +00:00
|
|
|
ok
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
2012-07-28 21:43:56 +00:00
|
|
|
make_cmd(TestDir, RawLogDir, Config) ->
|
2010-06-22 21:43:09 +00:00
|
|
|
Cwd = rebar_utils:get_cwd(),
|
2012-07-28 21:43:56 +00:00
|
|
|
LogDir = filename:join(Cwd, RawLogDir),
|
2010-06-22 21:43:09 +00:00
|
|
|
EbinDir = filename:absname(filename:join(Cwd, "ebin")),
|
2009-12-04 05:49:52 +00:00
|
|
|
IncludeDir = filename:join(Cwd, "include"),
|
2011-03-22 10:12:57 +00:00
|
|
|
Include = case filelib:is_dir(IncludeDir) of
|
|
|
|
true ->
|
|
|
|
" -include \"" ++ IncludeDir ++ "\"";
|
|
|
|
false ->
|
|
|
|
""
|
|
|
|
end,
|
2009-12-04 05:49:52 +00:00
|
|
|
|
2010-12-08 18:12:46 +00:00
|
|
|
%% Add the code path of the rebar process to the code path. This
|
|
|
|
%% includes the dependencies in the code path. The directories
|
|
|
|
%% that are part of the root Erlang install are filtered out to
|
|
|
|
%% avoid duplication
|
|
|
|
R = code:root_dir(),
|
2011-01-08 18:09:24 +00:00
|
|
|
NonLibCodeDirs = [P || P <- code:get_path(), not lists:prefix(R, P)],
|
2010-12-08 18:12:46 +00:00
|
|
|
CodeDirs = [io_lib:format("\"~s\"", [Dir]) ||
|
|
|
|
Dir <- [EbinDir|NonLibCodeDirs]],
|
|
|
|
CodePathString = string:join(CodeDirs, " "),
|
2011-01-26 02:00:53 +00:00
|
|
|
Cmd = case get_ct_specs(Cwd) of
|
2011-03-22 10:12:57 +00:00
|
|
|
undefined ->
|
|
|
|
?FMT("erl " % should we expand ERL_PATH?
|
2011-01-26 02:00:53 +00:00
|
|
|
" -noshell -pa ~s ~s"
|
2012-05-22 14:50:26 +00:00
|
|
|
" ~s"
|
2011-01-26 02:00:53 +00:00
|
|
|
" -logdir \"~s\""
|
2011-04-06 15:47:29 +00:00
|
|
|
" -env TEST_DIR \"~s\""
|
|
|
|
" ~s"
|
|
|
|
" -s ct_run script_start -s erlang halt",
|
2011-01-26 02:00:53 +00:00
|
|
|
[CodePathString,
|
|
|
|
Include,
|
2012-05-22 14:50:26 +00:00
|
|
|
build_name(Config),
|
2011-01-26 02:00:53 +00:00
|
|
|
LogDir,
|
2011-04-06 15:47:29 +00:00
|
|
|
filename:join(Cwd, TestDir),
|
|
|
|
get_extra_params(Config)]) ++
|
2011-03-22 10:12:57 +00:00
|
|
|
get_cover_config(Config, Cwd) ++
|
|
|
|
get_ct_config_file(TestDir) ++
|
|
|
|
get_config_file(TestDir) ++
|
2012-07-14 21:44:47 +00:00
|
|
|
get_suites(Config, TestDir) ++
|
|
|
|
get_case(Config);
|
2011-03-22 10:12:57 +00:00
|
|
|
SpecFlags ->
|
|
|
|
?FMT("erl " % should we expand ERL_PATH?
|
2011-01-26 02:00:53 +00:00
|
|
|
" -noshell -pa ~s ~s"
|
2012-05-22 14:50:26 +00:00
|
|
|
" ~s"
|
2011-01-26 02:00:53 +00:00
|
|
|
" -logdir \"~s\""
|
2011-04-06 15:47:29 +00:00
|
|
|
" -env TEST_DIR \"~s\""
|
|
|
|
" ~s"
|
|
|
|
" -s ct_run script_start -s erlang halt",
|
2011-01-26 02:00:53 +00:00
|
|
|
[CodePathString,
|
|
|
|
Include,
|
2012-05-22 14:50:26 +00:00
|
|
|
build_name(Config),
|
2011-01-26 02:00:53 +00:00
|
|
|
LogDir,
|
2011-04-06 15:47:29 +00:00
|
|
|
filename:join(Cwd, TestDir),
|
|
|
|
get_extra_params(Config)]) ++
|
2011-03-22 10:12:57 +00:00
|
|
|
SpecFlags ++ get_cover_config(Config, Cwd)
|
|
|
|
end,
|
2009-12-04 05:49:52 +00:00
|
|
|
RawLog = filename:join(LogDir, "raw.log"),
|
|
|
|
{Cmd, RawLog}.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
2012-05-22 14:50:26 +00:00
|
|
|
build_name(Config) ->
|
|
|
|
case rebar_config:get_local(Config, ct_use_short_names, false) of
|
|
|
|
true -> "-sname test";
|
|
|
|
false -> " -name test@" ++ net_adm:localhost()
|
|
|
|
end.
|
|
|
|
|
2011-04-06 15:47:29 +00:00
|
|
|
get_extra_params(Config) ->
|
|
|
|
rebar_config:get_local(Config, ct_extra_params, "").
|
|
|
|
|
2011-01-26 02:00:53 +00:00
|
|
|
get_ct_specs(Cwd) ->
|
|
|
|
case collect_glob(Cwd, ".*\.test\.spec\$") of
|
|
|
|
[] -> undefined;
|
|
|
|
[Spec] ->
|
|
|
|
" -spec " ++ Spec;
|
|
|
|
Specs ->
|
|
|
|
" -spec " ++
|
|
|
|
lists:flatten([io_lib:format("~s ", [Spec]) || Spec <- Specs])
|
|
|
|
end.
|
|
|
|
|
2010-07-28 13:57:04 +00:00
|
|
|
get_cover_config(Config, Cwd) ->
|
|
|
|
case rebar_config:get_local(Config, cover_enabled, false) of
|
|
|
|
false ->
|
|
|
|
"";
|
|
|
|
true ->
|
2011-01-26 02:00:53 +00:00
|
|
|
case collect_glob(Cwd, ".*cover\.spec\$") of
|
2011-01-28 15:08:27 +00:00
|
|
|
[] ->
|
|
|
|
?DEBUG("No cover spec found: ~s~n", [Cwd]),
|
|
|
|
"";
|
|
|
|
[Spec] ->
|
|
|
|
?DEBUG("Found cover file ~w~n", [Spec]),
|
|
|
|
" -cover " ++ Spec;
|
|
|
|
Specs ->
|
|
|
|
?ABORT("Multiple cover specs found: ~p~n", [Specs])
|
|
|
|
end
|
|
|
|
end.
|
2010-07-28 13:57:04 +00:00
|
|
|
|
2011-01-26 02:00:53 +00:00
|
|
|
collect_glob(Cwd, Glob) ->
|
|
|
|
filelib:fold_files(Cwd, Glob, true, fun collect_files/2, []).
|
|
|
|
|
|
|
|
collect_files(F, Acc) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Ignore any specs under the deps/ directory. Do this pulling
|
|
|
|
%% the dirname off the the F and then splitting it into a list.
|
2010-07-28 13:57:04 +00:00
|
|
|
Parts = filename:split(filename:dirname(F)),
|
|
|
|
case lists:member("deps", Parts) of
|
|
|
|
true ->
|
2011-01-26 02:00:53 +00:00
|
|
|
Acc; % There is a directory named "deps" in path
|
2010-07-28 13:57:04 +00:00
|
|
|
false ->
|
2011-01-26 02:00:53 +00:00
|
|
|
[F | Acc] % No "deps" directory in path
|
2010-07-28 13:57:04 +00:00
|
|
|
end.
|
2009-12-04 05:49:52 +00:00
|
|
|
|
|
|
|
get_ct_config_file(TestDir) ->
|
|
|
|
Config = filename:join(TestDir, "test.config"),
|
|
|
|
case filelib:is_regular(Config) of
|
|
|
|
false ->
|
|
|
|
" ";
|
|
|
|
true ->
|
|
|
|
" -ct_config " ++ Config
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_config_file(TestDir) ->
|
|
|
|
Config = filename:join(TestDir, "app.config"),
|
|
|
|
case filelib:is_regular(Config) of
|
|
|
|
false ->
|
|
|
|
" ";
|
|
|
|
true ->
|
|
|
|
" -config " ++ Config
|
|
|
|
end.
|
|
|
|
|
2012-07-14 21:44:47 +00:00
|
|
|
get_suites(Config, TestDir) ->
|
2012-07-23 14:48:15 +00:00
|
|
|
case rebar_config:get_global(Config, suites, undefined) of
|
2010-06-22 21:43:09 +00:00
|
|
|
undefined ->
|
|
|
|
" -dir " ++ TestDir;
|
2012-01-06 17:33:41 +00:00
|
|
|
Suites ->
|
|
|
|
Suites1 = string:tokens(Suites, ","),
|
|
|
|
Suites2 = [find_suite_path(Suite, TestDir) || Suite <- Suites1],
|
|
|
|
string:join([" -suite"] ++ Suites2, " ")
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_suite_path(Suite, TestDir) ->
|
|
|
|
Path = filename:join(TestDir, Suite ++ "_SUITE.erl"),
|
|
|
|
case filelib:is_regular(Path) of
|
|
|
|
false ->
|
|
|
|
?ERROR("Suite ~s not found\n", [Suite]),
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL;
|
2012-01-06 17:33:41 +00:00
|
|
|
true ->
|
|
|
|
Path
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
2012-07-14 21:44:47 +00:00
|
|
|
get_case(Config) ->
|
|
|
|
case rebar_config:get_global(Config, 'case', undefined) of
|
2010-06-22 21:43:09 +00:00
|
|
|
undefined ->
|
|
|
|
"";
|
|
|
|
Case ->
|
|
|
|
" -case " ++ Case
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|