Skip to content

Commit

Permalink
Merge pull request #89 from inaka/harenson.450.elvis-is-failing-badly…
Browse files Browse the repository at this point in the history
…-when-operator-spaces-and-line-length-checks-don-t-pass

[Fix #450] Fix encoding issue for elvis_file:src/1
  • Loading branch information
Brujo Benavides authored Jun 15, 2017
2 parents e588bda + b248bef commit c2bfc70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/test.pass.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{config,
[#{dirs => ["../../_build/test/lib/elvis/test/examples"],
filter => "**.erl",
rules => [{elvis_style, line_length, #{limit => 800}]
rules => [{elvis_style, line_length, #{limit => 800}}]
}]
},
{output_format, plain}
Expand Down
6 changes: 2 additions & 4 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@
, error_handling
]}
, {plt_apps, top_level_deps}
, {plt_extra_apps, []}
, {plt_location, local}
, {base_plt_apps, [stdlib, kernel]}
, {base_plt_location, global}]}.
, {plt_extra_apps, [kernel, stdlib]}
, {plt_location, local}]}.

%% == Shell ==

Expand Down
19 changes: 13 additions & 6 deletions src/elvis_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
%% @doc Returns a tuple with the contents of the file and the file itself.
-spec src(file()) ->
{binary(), file()} | {error, enoent}.
src(File = #{content := Content}) ->
src(File = #{content := Content, encoding := _}) ->
{Content, File};
src(File = #{content := Content}) ->
{Content, File#{encoding => find_encoding(Content)}};
src(File = #{path := Path}) ->
case file:read_file(Path) of
{ok, Content} ->
Encoding = case epp:read_encoding_from_binary(Content) of
none -> utf8;
Enc -> Enc
end,
Encoding = find_encoding(Content),
src(File#{content => Content,
encoding => Encoding});
Error -> Error
Expand Down Expand Up @@ -127,4 +126,12 @@ escape_all_chars(Glob) -> re:replace(Glob, ".", "[&]", [global]).

replace_stars(Glob) -> re:replace(Glob, "[[][*][]]", ".*", [global]).

replace_questions(Glob) -> re:replace(Glob, "[[][?][]]", ".", [global]).
replace_questions(Glob) -> re:replace(Glob, "[[][?][]]", ".", [global]).

-spec find_encoding(Content::binary()) ->
atom().
find_encoding(Content) ->
case epp:read_encoding_from_binary(Content) of
none -> utf8;
Enc -> Enc
end.

0 comments on commit c2bfc70

Please sign in to comment.