Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to the default error format when a file is empty #2931

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions apps/rebar/src/rebar_compiler_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ format(Source, {Line, Column}, Extra, Desc, Config) ->
end.

find_line(Nth, Source) ->
try
{ok, Bin} = file:read_file(Source),
Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
{ok, lists:nth(Nth, Splits)}
catch
error:X -> {error, X}
end.
try do_find_line(Nth, Source)
catch
error:X -> {error, X}
end.

do_find_line(Nth, Source) ->
case file:read_file(Source) of
{ok, <<>>} ->
{error, empty_file};
{ok, Bin} ->
Splits = re:split(Bin, "(?:\n|\r\n|\r)", [{newline, anycrlf}]),
{ok, lists:nth(Nth, Splits)};
{error, Reason} ->
{error, Reason}
end.

indent(0, _) -> "";
indent(N, <<"\t", Rest/binary>>) -> [$\t | indent(N-1, Rest)];
Expand Down
8 changes: 7 additions & 1 deletion apps/rebar/test/rebar_compiler_format_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ init_per_testcase(_, Config) ->
application:set_env(cf, colour_term, cf_term:has_color("dumb")),
FileName = filename:join(?config(priv_dir, Config), "oracle.erl"),
ok = file:write_file(FileName, oracle()),
EmptyFileName = filename:join(?config(priv_dir, Config), "empty.erl"),
ok = file:write_file(EmptyFileName, ""),
Conf = dict:from_list([{compiler_error_format, rich}]),
[{conf, Conf}, {file, FileName}, {term, OriginalTerm} | Config].
[{conf, Conf}, {file, FileName}, {empty_file, EmptyFileName}, {term, OriginalTerm} | Config].

end_per_testcase(_, Config) ->
case ?config(term, Config) of
Expand Down Expand Up @@ -65,6 +67,7 @@ nocolor() ->
[{doc, "testing all sorts of planned output"}].
nocolor(Config) ->
Path = ?config(file, Config),
EmptyPath = ?config(empty_file, Config),
Conf = ?config(conf, Config),
?assertEqual(" ┌─ "++Path++":"++?EOL++
" │"++?EOL++
Expand All @@ -90,5 +93,8 @@ nocolor(Config) ->
rebar_compiler_format:format(Path, {855,1}, "", "invalid ranges.", Conf)),
?assertEqual("/very/fake/path.oof:1:1: unknown file."++?EOL,
rebar_compiler_format:format("/very/fake/path.oof", {1,1}, "", "unknown file.", Conf)),
%% empty file
?assertEqual(EmptyPath++":1:1: should fallback to the minimal output"++?EOL,
rebar_compiler_format:format(EmptyPath, {1,1}, "", "should fallback to the minimal output", Conf)),
ok.

Loading