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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1998-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(error_SUITE).
-include("test_server.hrl").
-export([all/1,
head_mismatch_line/1,warnings_as_errors/1, bif_clashes/1]).
all(suite) ->
test_lib:recompile(?MODULE),
[head_mismatch_line,warnings_as_errors,bif_clashes].
bif_clashes(Config) when is_list(Config) ->
Ts = [{bif_clashes1,
<<"
-export([t/0]).
t() ->
length([a,b,c]).
length(X) ->
erlang:length(X).
">>,
[return_warnings],
{error,
[{4, erl_lint,{call_to_redefined_old_bif,{length,1}}}], []} }],
?line [] = run(Config, Ts),
Ts1 = [{bif_clashes2,
<<"
-export([t/0]).
-import(x,[length/1]).
t() ->
length([a,b,c]).
">>,
[return_warnings],
{error,
[{3, erl_lint,{redefine_old_bif_import,{length,1}}}], []} }],
?line [] = run(Config, Ts1),
Ts00 = [{bif_clashes3,
<<"
-export([t/0]).
-compile({no_auto_import,[length/1]}).
t() ->
length([a,b,c]).
length(X) ->
erlang:length(X).
">>,
[return_warnings],
[]}],
?line [] = run(Config, Ts00),
Ts11 = [{bif_clashes4,
<<"
-export([t/0]).
-compile({no_auto_import,[length/1]}).
-import(x,[length/1]).
t() ->
length([a,b,c]).
">>,
[return_warnings],
[]}],
?line [] = run(Config, Ts11),
Ts000 = [{bif_clashes5,
<<"
-export([t/0]).
t() ->
binary_part(<<1,2,3,4>>,1,2).
binary_part(X,Y,Z) ->
erlang:binary_part(X,Y,Z).
">>,
[return_warnings],
{warning,
[{4, erl_lint,{call_to_redefined_bif,{binary_part,3}}}]} }],
?line [] = run(Config, Ts000),
Ts111 = [{bif_clashes6,
<<"
-export([t/0]).
-import(x,[binary_part/3]).
t() ->
binary_part(<<1,2,3,4>>,1,2).
">>,
[return_warnings],
{warning,
[{3, erl_lint,{redefine_bif_import,{binary_part,3}}}]} }],
?line [] = run(Config, Ts111),
Ts2 = [{bif_clashes7,
<<"
-export([t/0]).
-compile({no_auto_import,[length/1]}).
-import(x,[length/1]).
t() ->
length([a,b,c]).
length(X) ->
erlang:length(X).
">>,
[],
{error,
[{7,erl_lint,{define_import,{length,1}}}],
[]} }],
?line [] = run2(Config, Ts2),
Ts3 = [{bif_clashes8,
<<"
-export([t/1]).
-compile({no_auto_import,[length/1]}).
t(X) when length(X) > 3 ->
length([a,b,c]).
length(X) ->
erlang:length(X).
">>,
[],
{error,
[{4,erl_lint,{illegal_guard_local_call,{length,1}}}],
[]} }],
?line [] = run2(Config, Ts3),
Ts4 = [{bif_clashes9,
<<"
-export([t/1]).
-compile({no_auto_import,[length/1]}).
-import(x,[length/1]).
t(X) when length(X) > 3 ->
length([a,b,c]).
">>,
[],
{error,
[{5,erl_lint,{illegal_guard_local_call,{length,1}}}],
[]} }],
?line [] = run2(Config, Ts4),
ok.
%% Tests that a head mismatch is reported on the correct line (OTP-2125).
head_mismatch_line(Config) when is_list(Config) ->
?line [E|_] = get_compilation_errors(Config, "head_mismatch_line"),
?line {26, Mod, Reason} = E,
?line Mod:format_error(Reason),
ok.
%% Compiles a test file and returns the list of errors.
get_compilation_errors(Config, Filename) ->
?line DataDir = ?config(data_dir, Config),
?line File = filename:join(DataDir, Filename),
?line {error, [{_Name, E}|_], []} = compile:file(File, [return_errors]),
E.
warnings_as_errors(Config) when is_list(Config) ->
Ts = [{warnings_as_errors,
<<"
t() ->
A = unused,
ok.
">>,
[export_all,warnings_as_errors],
{error,
[],
[{3,erl_lint,{unused_var,'A'}}]} }],
?line [] = run(Config, Ts),
ok.
run(Config, Tests) ->
F = fun({N,P,Ws,E}, BadL) ->
case catch run_test(Config, P, Ws) of
E ->
BadL;
Bad ->
?t:format("~nTest ~p failed. Expected~n ~p~n"
"but got~n ~p~n", [N, E, Bad]),
fail()
end
end,
lists:foldl(F, [], Tests).
run2(Config, Tests) ->
F = fun({N,P,Ws,E}, BadL) ->
case catch filter(run_test(Config, P, Ws)) of
E ->
BadL;
Bad ->
?t:format("~nTest ~p failed. Expected~n ~p~n"
"but got~n ~p~n", [N, E, Bad]),
fail()
end
end,
lists:foldl(F, [], Tests).
filter({error,Es,_Ws}) ->
{error,Es,[]};
filter(X) ->
X.
%% Compiles a test module and returns the list of errors and warnings.
run_test(Conf, Test0, Warnings) ->
Filename = 'errors_test.erl',
?line DataDir = ?config(priv_dir, Conf),
?line Test = ["-module(errors_test). ", Test0],
?line File = filename:join(DataDir, Filename),
?line Opts = [binary,return_errors|Warnings],
?line ok = file:write_file(File, Test),
%% Compile once just to print all errors and warnings.
?line compile:file(File, [binary,report|Warnings]),
%% Test result of compilation.
?line Res = case compile:file(File, Opts) of
{ok,errors_test,_,[{_File,Ws}]} ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,Ws]),
{warning,Ws};
{ok,errors_test,_,[]} ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,Ws]),
[];
{error,[{XFile,Es}],Ws} = _ZZ when is_list(XFile) ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,_ZZ]),
{error,Es,Ws};
{error,Es,[{_File,Ws}]} = _ZZ->
%io:format("compile:file(~s,~p) ->~n~p~n",
% [File,Opts,_ZZ]),
{error,Es,Ws}
end,
file:delete(File),
Res.
fail() ->
io:format("failed~n"),
?t:fail().
|