1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2010-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19
20-module(rtt).
21-compile([export_all, nowarn_export_all]).
22
23%%  Modules or suites can be shortcuts, for example server expands to reltool_server_SUITE.
24%%
25%%  t(Tests) run reltool testcases.
26%%    Tests can be module, {module, test_case} or [module|{module,test_case}]
27
28t() ->
29    t(read_test_case()).
30t(Test) ->
31    t(Test, []).
32
33t(Mod, TC) when is_atom(Mod), is_atom(TC) ->
34    t({Mod,TC}, []);
35t(all, Config) when is_list(Config) ->
36    Fs = filelib:wildcard("reltool_*_SUITE.erl"),
37    t([list_to_atom(filename:rootname(File)) || File <- Fs], Config);
38t(Test,Config) when is_list(Config) ->
39    Tests = resolve(Test),
40    write_test_case(Test),
41    Res = reltool_test_lib:run_test(Tests, Config),
42    append_test_case_info(Test, Res).
43
44user() ->
45    user(read_test_case()).
46user(Mod) ->
47    t(Mod, [{user,step}]).
48user(Mod,Tc) when is_atom(Tc) ->
49    t({Mod,Tc}, [{user,step}]).
50
51%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52%% Resolves the name of test suites and test cases
53%% according to the alias definitions. Single atoms
54%% are assumed to be the name of a test suite.
55
56resolve(Suite0) when is_atom(Suite0) ->
57    case alias(Suite0) of
58	Suite when is_atom(Suite) ->
59	    {Suite, all};
60	{Suite, Case} ->
61	    {Suite, Case}
62    end;
63resolve({Suite0, Case}) when is_atom(Suite0), is_atom(Case) ->
64    case alias(Suite0) of
65	Suite when is_atom(Suite) ->
66	    {Suite, Case};
67	{Suite, Case2} ->
68	    {Suite, Case2}
69    end;
70resolve(List) when is_list(List) ->
71    [resolve(Case) || Case <- List].
72
73alias(Suite) when is_atom(Suite) ->
74    Str = atom_to_list(Suite),
75    case {Str, lists:reverse(Str)} of
76	{"reltool" ++ _, "ETIUS" ++ _} ->
77	    Suite;
78	_ ->
79	    list_to_atom("reltool_" ++ Str ++ "_SUITE")
80    end.
81
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
84config_fname() ->
85    "reltool_test_case_config".
86
87%% Read default config file
88read_config() ->
89    Fname = config_fname(),
90    reltool_test_lib:log("Consulting file ~s...~n", [Fname]),
91    case file:consult(Fname) of
92        {ok, Config} ->
93	    reltool_test_lib:log("Read config ~w~n", [Config]),
94            Config;
95        _Error ->
96	    Config = reltool_test_lib:default_config(),
97            reltool_test_lib:log("<>WARNING<> Using default config: ~w~n", [Config]),
98            Config
99    end.
100
101%% Write new default config file
102write_config(Config) when is_list(Config) ->
103    Fname = config_fname(),
104    {ok, Fd} = file:open(Fname, write),
105    write_list(Fd, Config),
106    file:close(Fd).
107
108write_list(Fd, [H | T]) ->
109    ok = io:format(Fd, "~p.~n",[H]),
110    write_list(Fd, T);
111write_list(_, []) ->
112    ok.
113
114test_case_fname() ->
115    "reltool_test_case_info".
116
117%% Read name of test case
118read_test_case() ->
119    Fname = test_case_fname(),
120    case file:open(Fname, [read]) of
121	{ok, Fd} ->
122	    Res = io:read(Fd, []),
123	    file:close(Fd),
124	    case Res of
125		{ok, TestCase} ->
126		    reltool_test_lib:log("Using test case ~w from file ~s~n",
127					 [TestCase, Fname]),
128		    TestCase;
129		{error, _} ->
130		    default_test_case(Fname)
131	    end;
132	{error, _} ->
133	    default_test_case(Fname)
134    end.
135
136default_test_case(Fname) ->
137    TestCase = all,
138    reltool_test_lib:log("<>WARNING<> Cannot read file ~s, "
139			 "using default test case: ~w~n",
140			 [Fname, TestCase]),
141    TestCase.
142
143write_test_case(TestCase) ->
144    Fname = test_case_fname(),
145    {ok, Fd} = file:open(Fname, write),
146    ok = io:format(Fd, "~p.~n",[TestCase]),
147    file:close(Fd).
148
149append_test_case_info(TestCase, TestCaseInfo) ->
150    Fname = test_case_fname(),
151    {ok, Fd} = file:open(Fname, [read, write]),
152    ok = io:format(Fd, "~p.~n",[TestCase]),
153    ok = io:format(Fd, "~p.~n",[TestCaseInfo]),
154    file:close(Fd),
155    TestCaseInfo.
156