2009-12-31 18:42:53 +00:00
|
|
|
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
|
|
|
%% 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
|
|
|
|
%% suite="foo"" - runs <test>/foo_SUITE
|
|
|
|
%% 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) ->
|
2009-12-04 05:49:52 +00:00
|
|
|
run_test_if_present("test", Config, File).
|
|
|
|
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
run_test_if_present(TestDir, Config, File) ->
|
|
|
|
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 ->
|
2010-06-22 21:43:09 +00:00
|
|
|
run_test(TestDir, Config, File)
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
run_test(TestDir, Config, _File) ->
|
|
|
|
{Cmd, RawLog} = make_cmd(TestDir, Config),
|
|
|
|
clear_log(RawLog),
|
2009-12-31 23:08:00 +00:00
|
|
|
case rebar_config:get_global(verbose, "0") of
|
2010-06-22 21:43:09 +00:00
|
|
|
"0" ->
|
|
|
|
Output = " >> " ++ RawLog ++ " 2>&1";
|
|
|
|
_ ->
|
|
|
|
Output = " 2>&1 | tee -a " ++ RawLog
|
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
|
|
|
case rebar_utils:sh(Cmd ++ Output, [{"TESTDIR", TestDir}]) of
|
2010-06-22 21:43:09 +00:00
|
|
|
ok ->
|
|
|
|
check_log(RawLog);
|
|
|
|
{error, _Rc} ->
|
|
|
|
show_log(RawLog),
|
|
|
|
?ERROR("Executing tests failed.\n", []),
|
|
|
|
?FAIL
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
clear_log(RawLog) ->
|
|
|
|
case filelib:ensure_dir("logs/index.html") of
|
|
|
|
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]),
|
|
|
|
?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
|
|
|
|
check_log(RawLog) ->
|
2009-12-04 14:58:21 +00:00
|
|
|
Msg = os:cmd("grep -e 'TEST COMPLETE' -e '{error,make_failed}' " ++ RawLog),
|
|
|
|
MakeFailed = string:str(Msg, "{error,make_failed}") =/= 0,
|
|
|
|
RunFailed = string:str(Msg, ", 0 failed") =:= 0,
|
|
|
|
if
|
|
|
|
MakeFailed ->
|
|
|
|
show_log(RawLog),
|
|
|
|
?ERROR("Building tests failed\n",[]),
|
|
|
|
?FAIL;
|
|
|
|
|
|
|
|
RunFailed ->
|
2009-12-04 05:49:52 +00:00
|
|
|
show_log(RawLog),
|
|
|
|
?ERROR("One or more tests failed\n",[]),
|
|
|
|
?FAIL;
|
2009-12-04 14:58:21 +00:00
|
|
|
|
|
|
|
true ->
|
2009-12-04 05:49:52 +00:00
|
|
|
?CONSOLE("DONE. ~s\n", [Msg])
|
|
|
|
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
|
|
|
|
show_log(RawLog) ->
|
|
|
|
?CONSOLE("Showing log\n", []),
|
2009-12-31 23:08:00 +00:00
|
|
|
case rebar_config:get_global(verbose, "0") of
|
|
|
|
"0" ->
|
2009-12-04 05:49:52 +00:00
|
|
|
{ok, Contents} = file:read_file(RawLog),
|
|
|
|
?CONSOLE("~s", [Contents]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2009-12-14 13:58:22 +00:00
|
|
|
make_cmd(TestDir, _Config) ->
|
2010-06-22 21:43:09 +00:00
|
|
|
Cwd = rebar_utils:get_cwd(),
|
2009-12-04 05:49:52 +00:00
|
|
|
LogDir = filename:join(Cwd, "logs"),
|
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"),
|
|
|
|
case filelib:is_dir(IncludeDir) of
|
2010-06-22 21:43:09 +00:00
|
|
|
true ->
|
|
|
|
Include = " -I \"" ++ IncludeDir ++ "\"";
|
|
|
|
false ->
|
|
|
|
Include = ""
|
2009-12-04 05:49:52 +00:00
|
|
|
end,
|
|
|
|
|
2010-06-22 21:43:09 +00:00
|
|
|
Cmd = ?FMT("erl " % should we expand ERL_PATH?
|
|
|
|
" -noshell -pa \"~s\" ~s"
|
|
|
|
" -s ct_run script_start -s erlang halt"
|
|
|
|
" -name test@~s"
|
|
|
|
" -logdir \"~s\""
|
|
|
|
" -env TEST_DIR \"~s\"",
|
|
|
|
[EbinDir,
|
|
|
|
Include,
|
|
|
|
net_adm:localhost(),
|
|
|
|
LogDir,
|
|
|
|
filename:join(Cwd, TestDir)]) ++
|
|
|
|
get_ct_config_file(TestDir) ++
|
|
|
|
get_config_file(TestDir) ++
|
|
|
|
get_suite(TestDir) ++
|
|
|
|
get_case(),
|
2009-12-04 05:49:52 +00:00
|
|
|
RawLog = filename:join(LogDir, "raw.log"),
|
|
|
|
{Cmd, RawLog}.
|
2009-12-30 12:13:39 +00:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
get_suite(TestDir) ->
|
|
|
|
case rebar_config:get_global(suite, undefined) of
|
2010-06-22 21:43:09 +00:00
|
|
|
undefined ->
|
|
|
|
" -dir " ++ TestDir;
|
|
|
|
Suite ->
|
|
|
|
Filename = filename:join(TestDir, Suite ++ "_SUITE.erl"),
|
|
|
|
case filelib:is_regular(Filename) of
|
|
|
|
false ->
|
|
|
|
?ERROR("Suite ~s not found\n", [Suite]),
|
|
|
|
?FAIL;
|
|
|
|
true ->
|
|
|
|
" -suite " ++ Filename
|
|
|
|
end
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_case() ->
|
|
|
|
case rebar_config:get_global('case', undefined) of
|
2010-06-22 21:43:09 +00:00
|
|
|
undefined ->
|
|
|
|
"";
|
|
|
|
Case ->
|
|
|
|
" -case " ++ Case
|
2009-12-04 05:49:52 +00:00
|
|
|
end.
|