Skip to content

Commit 05e1d55

Browse files
TheGeorgefacebook-github-bot
authored andcommitted
fix dialyzer warnings in prelude
Summary: Running dialyzer on prelude//erlang/... reveals a lot of issues we are fixing in this commit. Reviewed By: Balajiganapathi Differential Revision: D74003025 fbshipit-source-id: d785de05c60950585c1870df96059165abdb0455
1 parent 201beb8 commit 05e1d55

20 files changed

+146
-259
lines changed

prelude/erlang/common_test/common/include/buck_ct_records.hrl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
suite_path :: file:filename_all(),
2929
output_dir :: file:filename_all(),
3030
dependencies :: [file:filename_all()],
31-
test_spec_file :: file:filename_all(),
31+
test_spec_file :: file:filename_all() | undefined,
3232
output_format :: xml | json,
3333
config_files :: [file:filename_all()],
3434
providers :: [{module(), [term()]}],
@@ -37,7 +37,7 @@
3737
erl_cmd :: [binary()],
3838
extra_flags :: [string()],
3939
artifact_annotation_mfa :: artifact_annotations:annotation_function(),
40-
raw_target :: binary()
40+
raw_target :: binary() | undefined
4141
}).
4242

4343
-record(run_specs, {

prelude/erlang/common_test/common/include/tpx_records.hrl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77

88
-record(test_spec_test_info, {name :: string(), filter :: string()}).
99

10-
-record(test_spec_test_case, {suite :: string(), testcases :: [#test_spec_test_info{}]}).
10+
-record(test_spec_test_case, {suite :: binary(), testcases :: [#test_spec_test_info{}]}).
11+
12+
-type optional(Type) :: undefined | Type.
1113

1214
-record(test, {
1315
name :: string(),
1416
suite :: string(),
1517
type :: string(),
16-
message :: junit_interfacer:optional(string()),
17-
stacktrace :: junit_interfacer:optional(string()),
18-
stdout :: junit_interfacer:optional(string()),
19-
stderr :: junit_interfacer:optional(string()),
20-
time :: junit_interfacer:optional(integer())
18+
message :: optional(string()),
19+
stacktrace :: optional(string()),
20+
stdout :: optional(string()),
21+
stderr :: optional(string()),
22+
time :: optional(integer())
2123
}).
2224

2325
-record(test_case, {

prelude/erlang/common_test/common/src/ct_error_printer.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ format_error(ErrType, Reason, FormatStackTrace) ->
5050
end
5151
end.
5252

53-
-spec format_reason(term()) -> {ok, unicode:chardata()}.
53+
-spec format_reason(term()) -> {ok, unicode:chardata()} | unrecognized_error.
5454
format_reason(Reason) ->
5555
blame_reason(Reason),
5656
lists:foldl(

prelude/erlang/common_test/common/src/io_buffer.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ multi_request([], {LatestReply, State}) ->
181181
{LatestReply, State};
182182
multi_request([_ | _], {{error, Error}, State}) ->
183183
{{error, Error}, State};
184-
multi_request([R | _Rs], {_Reply, State}) ->
185-
multi_request(R, request(R, State)).
184+
multi_request([R | Rs], {_Reply, State}) ->
185+
multi_request(Rs, request(R, State)).
186186

187187
terminate(#state{process = Process, group_leader = GroupLeader} = _State) ->
188188
group_leader(GroupLeader, Process).

prelude/erlang/common_test/cth_hooks/src/cth_tpx.erl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
%% % @format
99
-module(cth_tpx).
10+
11+
-export([is_running_in_sandcastle/0]).
1012
-eqwalizer(ignore).
1113

1214
%% Callbacks
@@ -33,6 +35,9 @@
3335

3436
-export([terminate/1]).
3537

38+
%% please dialyzer
39+
-export([ok_group/1, fail_group/1]).
40+
3641
%% For tests purposes
3742

3843
-include("method_ids.hrl").
@@ -53,21 +58,21 @@
5358

5459
-record(state, {
5560
io_buffer :: pid() | undefined,
56-
suite :: string(),
61+
suite :: string() | undefined,
5762
groups :: list(string()),
5863
starting_times :: starting_times(),
5964
tree_results :: tree_node(),
60-
previous_group_failed :: string(),
65+
previous_group_failed :: boolean() | undefined,
6166
output :: {file, string()} | stdout
6267
}).
6368

64-
-type hook_opts() :: #{role := top, result_json => string()} | #{role := bot}.
69+
-type hook_opts() :: #{role := cth_tpx_role:role(), result_json => string()}.
6570

6671
-type shared_state() :: #state{}.
6772
-type hook_state() :: #{
6873
id := term(),
69-
role := ct_tpx_role:role(),
70-
server := shared_state()
74+
role := cth_tpx_role:role(),
75+
server := 'undefined' | pid() | port()
7176
}.
7277
-type starting_times() :: #{method_id() => float()}.
7378

@@ -117,13 +122,13 @@ fmt_stack(_Suite, _CasePat, _CaseArgs, Reason, _Label) ->
117122
%% -----------------------------------------------------------------------------
118123

119124
%% @doc Return a unique id for this CTH.
120-
-spec id(hook_opts()) -> term().
125+
-spec id(hook_opts()) -> {?MODULE, cth_tpx_role:role()}.
121126
id(#{role := Role}) ->
122127
{?MODULE, Role}.
123128

124129
%% @doc Always called before any other callback function. Use this to initiate
125130
%% any common state.
126-
-spec init(_Id :: term(), Opts :: hook_opts()) -> {ok, hook_state()}.
131+
-spec init(_Id :: term(), Opts :: hook_opts()) -> {ok, hook_state(), integer()}.
127132
init(Id, Opts = #{role := Role}) ->
128133
ServerName = '$cth_tpx$server$',
129134
case Role of
@@ -138,7 +143,8 @@ init(Id, Opts = #{role := Role}) ->
138143
init_role_bot(Id, ServerName)
139144
end.
140145

141-
-spec init_role_top(Id :: term(), ServerName :: atom(), Output :: stdout | {file, string()}) -> {ok, hook_state()}.
146+
-spec init_role_top(Id :: term(), ServerName :: atom(), Output :: stdout | {file, string()}) ->
147+
{ok, hook_state(), integer()}.
142148
init_role_top(Id, ServerName, Output) ->
143149
% IoBuffer that will catpures all the output produced by ct
144150
IoBuffer = whereis(cth_tpx_io_buffer),
@@ -167,7 +173,7 @@ init_role_top(Id, ServerName, Output) ->
167173
},
168174
{ok, HookState, cth_tpx_role:role_priority(top)}.
169175

170-
-spec init_role_bot(Id :: term(), ServerName :: atom()) -> {ok, hook_state()}.
176+
-spec init_role_bot(Id :: term(), ServerName :: atom()) -> {ok, hook_state(), integer()}.
171177
init_role_bot(Id, ServerName) ->
172178
% Put there by init_role_top
173179
Handle = whereis(ServerName),
@@ -180,7 +186,7 @@ init_role_bot(Id, ServerName) ->
180186
{ok, HookState, cth_tpx_role:role_priority(bot)}.
181187

182188
%% @doc Called before init_per_suite is called.
183-
-spec pre_init_per_suite(string(), any(), hook_state()) -> hook_state().
189+
-spec pre_init_per_suite(string(), any(), hook_state()) -> {any(), hook_state()}.
184190
pre_init_per_suite(Suite, Config, HookState) ->
185191
on_shared_state(HookState, ?FUNCTION_NAME, Config, fun(State) ->
186192
initialize_stdout_capture(State),
@@ -480,11 +486,6 @@ add_result(
480486
output = {file, OutputFile}
481487
}
482488
) ->
483-
NameMethod =
484-
case Method of
485-
{TestCase, Phase} -> io_lib:format("~s.~s", [TestCase, atom_to_list(Phase)]);
486-
NameMethod0 -> NameMethod0
487-
end,
488489
StdOut =
489490
case IoBuffer of
490491
undefined ->
@@ -515,7 +516,8 @@ add_result(
515516
Io
516517
end
517518
end,
518-
QualifiedName = cth_tpx_test_tree:qualified_name(Groups, NameMethod),
519+
520+
QualifiedName = method_name(Method, Groups),
519521
TS = second_timestamp(),
520522
Result0 = #{
521523
name => QualifiedName,
@@ -539,6 +541,15 @@ add_result(
539541
NewTreeResults = cth_tpx_test_tree:register_result(TreeResults, Result, Groups, Method),
540542
State#state{starting_times = ST1, tree_results = NewTreeResults}.
541543

544+
-spec method_name(method_id(), [string()]) -> string().
545+
method_name(Method, Groups) ->
546+
MethodName =
547+
case Method of
548+
{TestCase, Phase} -> io_lib:format("~s.~s", [atom_to_list(TestCase), atom_to_list(Phase)]);
549+
MethodName0 -> atom_to_list(MethodName0)
550+
end,
551+
cth_tpx_test_tree:qualified_name(Groups, MethodName).
552+
542553
pre_end_per_testcase(_SuiteName, TC, Config, HookState) ->
543554
on_shared_state(HookState, ?FUNCTION_NAME, Config, fun(State) ->
544555
{Config, capture_starting_time(State, {TC, ?END_PER_TESTCASE})}
@@ -632,7 +643,7 @@ terminate(#{role := top, server := Handle}) ->
632643
terminate(#{role := bot}) ->
633644
ok.
634645

635-
-spec write_output({file, string()} | stdout, string()) -> ok.
646+
-spec write_output({file, string()} | stdout, binary()) -> ok.
636647
write_output({file, FN}, JSON) ->
637648
io:format("Writing result file ~p", [FN]),
638649
ok = filelib:ensure_dir(FN),

prelude/erlang/common_test/cth_hooks/src/cth_tpx_test_tree.erl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@
4646
type := node,
4747
init_method := option(method_result()),
4848
end_method := option(method_result()),
49-
test_cases := #{string() => test_leaf()},
49+
test_cases := #{name() => test_leaf()},
5050
sub_groups := #{name() => tree_node()}
5151
}.
5252

5353
-type test_leaf() :: #{
54-
name := string(),
54+
name := name(),
5555
type := leaf,
5656
init_method := option(method_result()),
5757
end_method := option(method_result()),
5858
main_method := option(method_result())
5959
}.
6060

6161
-type method_result() :: #{
62-
name := string(),
62+
name := name(),
6363
startedTime => float(),
6464
endedTime => float(),
6565
outcome := outcome(),
@@ -87,10 +87,9 @@ qualified_name(Groups, TestCase) ->
8787
JoinedGroups = string:join(lists:reverse(StringGroups), ":"),
8888
Raw = io_lib:format("~s.~s", [JoinedGroups, TestCase]),
8989
case unicode:characters_to_list(Raw, latin1) of
90-
Res when is_list(Res) -> Res
90+
Res when not is_tuple(Res) -> Res
9191
end.
9292

93-
9493
%% Tree creation and update
9594

9695
%% @doc Creates a new node
@@ -106,7 +105,7 @@ new_node(Name) ->
106105
}.
107106

108107
%% @doc Creates a new leaf
109-
-spec new_leaf(Name :: string()) -> test_leaf().
108+
-spec new_leaf(Name :: name()) -> test_leaf().
110109
new_leaf(Name) ->
111110
#{
112111
name => Name,
@@ -198,15 +197,19 @@ report_end_failure(
198197
) ->
199198
MergedOutcome = merge_outcome(EndOutcome, ResultOutcome),
200199
EndFailedDetails =
201-
unicode_characters_to_string([io_lib:format("~p ~p because ~p failed with ~n", [TestName, MergedOutcome, EndName]), EndDetails]),
200+
unicode_characters_to_string([
201+
io_lib:format("~p ~p because ~p failed with ~n", [TestName, MergedOutcome, EndName]), EndDetails
202+
]),
202203
MergedDetails =
203204
case ResultOutcome of
204205
passed ->
205206
EndFailedDetails;
206207
_ ->
207-
unicode_characters_to_string(lists:flatten(
208-
io_lib:format("~s~n~n~s", [ResultDetails, EndFailedDetails])
209-
))
208+
unicode_characters_to_string(
209+
lists:flatten(
210+
io_lib:format("~s~n~n~s", [ResultDetails, EndFailedDetails])
211+
)
212+
)
210213
end,
211214
report_end_failure(Rest, ResultAcc#{outcome => MergedOutcome, details => MergedDetails}).
212215

prelude/erlang/common_test/cth_hooks/src/method_ids.hrl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
?END_PER_TESTCASE |
2424
?END_PER_GROUP |
2525
?END_PER_SUITE |
26-
?MAIN_TESTCASE.
26+
?MAIN_TESTCASE |
27+
{atom(), ?INIT_PER_TESTCASE | ?END_PER_TESTCASE | ?MAIN_TESTCASE}.
2728

2829
-endif. % CTH_TPX_METHOD_IDS_HRL

prelude/erlang/common_test/test_binary/src/junit_interfacer.erl

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)