Skip to content

Commit

Permalink
Close group when init_per_group fails or is skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Dec 20, 2022
1 parent edd3580 commit 07d220c
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions big_tests/src/cth_surefire.erl
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ post_end_per_suite(_Suite,Config,Result,State) ->
pre_init_per_group(Suite,Group,Config,Proxy) when is_pid(Proxy) ->
{gen_server:call(Proxy,{?FUNCTION_NAME, [Suite, Group, Config]}),Proxy};
pre_init_per_group(_Suite,Group,Config,State) ->
{Config, init_tc(State#state{ curr_group = [Group|State#state.curr_group]},
Config)}.
{Config, init_tc(set_group(Group, State), Config)}.

post_init_per_group(Suite,Group,Config,Result,Proxy) when is_pid(Proxy) ->
{gen_server:call(Proxy,{?FUNCTION_NAME, [Suite, Group, Config, Result]}),Proxy};
Expand All @@ -199,7 +198,7 @@ post_end_per_group(Suite,Group,Config,Result,Proxy) when is_pid(Proxy) ->
{gen_server:call(Proxy,{?FUNCTION_NAME, [Suite,Group,Config,Result]}),Proxy};
post_end_per_group(_Suite,_Group,Config,Result,State) ->
NewState = end_tc(end_per_group, Config, Result, State),
{Result, NewState#state{ curr_group = tl(NewState#state.curr_group)}}.
{Result, close_group(NewState)}.

pre_init_per_testcase(Suite,TC,Config,Proxy) when is_pid(Proxy) ->
{gen_server:call(Proxy,{?FUNCTION_NAME, [Suite,TC,Config]}),Proxy};
Expand All @@ -219,7 +218,8 @@ on_tc_fail(_Suite, _TestName, _Res, State = #state{test_cases = []}) ->
on_tc_fail(Suite, TestName, Res, State = #state{test_cases = TCs}) ->
TCName = tc_name_to_string(TestName),
{value, TC, RemainingTCs} = lists:keytake(TCName, #testcase.name, TCs),
do_tc_fail(Suite, TC, Res, State#state{test_cases = RemainingTCs}).
State1 = do_tc_fail(Suite, TC, Res, State#state{test_cases = RemainingTCs}),
maybe_close_group(TestName, State1).

do_tc_fail(Suite, TC, Res, State = #state{test_cases = TCs}) ->
Line = case get_line_from_result(Suite, Res) of
Expand All @@ -246,15 +246,11 @@ get_line_from_result(_, _) ->
on_tc_skip(Suite,TC,Result,Proxy) when is_pid(Proxy) ->
_ = gen_server:call(Proxy,{?FUNCTION_NAME, [Suite,TC,Result]}),
Proxy;
on_tc_skip(Suite, TestName, Res, State0 = #state{test_cases = TCs}) ->
on_tc_skip(Suite, TestName, Res, State = #state{test_cases = TCs}) ->
TCName = tc_name_to_string(TestName),
State = State0#state{test_cases = lists:keydelete(TCName, #testcase.name, TCs)},
do_tc_skip(Res, end_tc(TCName, [], Res, init_tc(set_suite(Suite, State), []))).

tc_name_to_string({FuncName, _GroupName}) ->
atom_to_list(FuncName);
tc_name_to_string(FuncName) ->
atom_to_list(FuncName).
State1 = State#state{test_cases = lists:keydelete(TCName, #testcase.name, TCs)},
State2 = do_tc_skip(Res, end_tc(TCName, [], Res, init_tc(set_suite(Suite, State1), []))),
maybe_close_group(TestName, State2).

do_tc_skip(Res, State) ->
TCs = State#state.test_cases,
Expand All @@ -264,6 +260,16 @@ do_tc_skip(Res, State) ->
{skipped,lists:flatten(io_lib:format("~tp",[Res]))} },
State#state{ test_cases = [NewTC | tl(TCs)]}.

tc_name_to_string({FuncName, _GroupName}) ->
atom_to_list(FuncName);
tc_name_to_string(FuncName) ->
atom_to_list(FuncName).

maybe_close_group({init_per_group, _}, State) ->
close_group(State);
maybe_close_group(_, State) ->
State.

init_tc(State, Config) when is_list(Config) == false ->
State#state{ timer = ?now, tc_log = "" };
init_tc(State, Config) ->
Expand Down Expand Up @@ -310,6 +316,12 @@ end_tc(Name, _Config, _Res, State = #state{ curr_suite = Suite,
State#state.test_cases],
tc_log = ""}. % so old tc_log is not set if next is on_tc_skip

set_group(Group, State = #state{curr_group = Groups}) ->
State#state{curr_group = [Group|Groups]}.

close_group(State = #state{curr_group = [_|Groups]}) ->
State#state{curr_group = Groups}.

set_suite(Suite,#state{curr_suite=undefined}=State) ->
State#state{curr_suite=Suite, curr_suite_ts=?now};
set_suite(_,State) ->
Expand Down

0 comments on commit 07d220c

Please sign in to comment.