summaryrefslogtreecommitdiff
path: root/src/rebar_ct.erl
blob: 350e944ab494d621e4890655f44d0f6674199ca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% 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.
%% -------------------------------------------------------------------
%%
%% 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).

-export([ct/2]).

-include("rebar.hrl").

%% ===================================================================
%% Public API
%% ===================================================================

ct(Config, File) ->
    run_test_if_present("test", Config, File).


%% ===================================================================
%% Internal functions
%% ===================================================================
run_test_if_present(TestDir, Config, File) ->
    case filelib:is_dir(TestDir) of
        false ->
            ?WARN("~s directory not present - skipping\n", [TestDir]),
            ok;
        true ->
            run_test(TestDir, Config, File)
    end.

run_test(TestDir, Config, _File) ->
    {Cmd, RawLog} = make_cmd(TestDir, Config),
    clear_log(RawLog),
    case rebar_config:get_global(verbose, "0") of
        "0" ->
            Output = " >> " ++ RawLog ++ " 2>&1";
        _ ->
            Output = " 2>&1 | tee -a " ++ RawLog
    end,

    case rebar_utils:sh(Cmd ++ Output, [{"TESTDIR", TestDir}]) of
        ok ->
            check_log(RawLog);
        {error, _Rc} ->
            show_log(RawLog),
            ?ERROR("Executing tests failed.\n", []),
            ?FAIL
    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
    end.

%% calling ct with erl does not return non-zero on failure - have to check
%% log results
check_log(RawLog) ->
    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 ->
	    show_log(RawLog),
	    ?ERROR("One or more tests failed\n",[]),
	    ?FAIL;

	true ->
	    ?CONSOLE("DONE. ~s\n", [Msg])
    end.

%% Show the log if it hasn't already been shown because verbose was on
show_log(RawLog) ->
    ?CONSOLE("Showing log\n", []),
    case rebar_config:get_global(verbose, "0") of
	"0" ->
	    {ok, Contents} = file:read_file(RawLog),
	    ?CONSOLE("~s", [Contents]);
	_ ->
	    ok
    end.

make_cmd(TestDir, Config) ->
    Cwd = rebar_utils:get_cwd(),
    LogDir = filename:join(Cwd, "logs"),
    EbinDir = filename:absname(filename:join(Cwd, "ebin")),
    IncludeDir = filename:join(Cwd, "include"),
    case filelib:is_dir(IncludeDir) of
        true ->
            Include = " -I \"" ++ IncludeDir ++ "\"";
        false ->
            Include = ""
    end,

    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_cover_config(Config, Cwd) ++
        get_ct_config_file(TestDir) ++
        get_config_file(TestDir) ++
        get_suite(TestDir) ++
        get_case(),
    RawLog = filename:join(LogDir, "raw.log"),
    {Cmd, RawLog}.

get_cover_config(Config, Cwd) ->
    case rebar_config:get_local(Config, cover_enabled, false) of
        false ->
            "";
        true ->
            case filelib:fold_files(Cwd, ".*cover\.spec\$", true, fun collect_ct_specs/2, []) of
				[] ->
					?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.

collect_ct_specs(F, Acc) ->
    %% Ignore any specs under the deps/ directory. Do this pulling the dirname off the
    %% the F and then splitting it into a list.
    Parts = filename:split(filename:dirname(F)),
    case lists:member("deps", Parts) of
        true ->
            Acc;                     % There is a directory named "deps" in path
        false ->
            [F | Acc]                % No "deps" directory in path
    end.

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
        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
    end.

get_case() ->
    case rebar_config:get_global('case', undefined) of
        undefined ->
            "";
        Case ->
            " -case " ++ Case
    end.