Skip to content

Commit 3b9a4df

Browse files
committed
Finish stdio input & add tests
1 parent 00865e1 commit 3b9a4df

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

‎src/erlfmt.erl‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,17 @@ init(State) ->
6363

6464
%% API entry point
6565
-spec format_file(file:name_all() | stdin, config()) ->
66-
{ok, [error_info()]} | {error, error_info()}.
66+
{ok, [error_info()]} | skip | {error, error_info()}.
6767
format_file(FileName, {Pragma, Out}) ->
6868
try
69-
%% TODO: fix for stdin
70-
ShouldFormat = (Pragma == ignore) orelse contains_pragma_file(FileName),
71-
case ShouldFormat of
72-
true ->
73-
{ok, Nodes, Warnings} = file_read_nodes(FileName),
69+
case file_read_nodes(FileName, Pragma) of
70+
{ok, Nodes, Warnings} ->
7471
[$\n | Formatted] = format_nodes(Nodes),
7572
verify_nodes(FileName, Nodes, Formatted),
7673
write_formatted(FileName, Formatted, Out),
7774
{ok, Warnings};
78-
false ->
79-
{ok, []}
75+
skip ->
76+
skip
8077
end
8178
catch
8279
{error, Error} -> {error, Error}
@@ -119,7 +116,7 @@ contains_pragma_comment(_) ->
119116
{options, [{erlfmt_scan:location(), erlfmt_scan:location()}]}.
120117
format_range(FileName, StartLocation, EndLocation) ->
121118
try
122-
{ok, Nodes, Warnings} = file_read_nodes(FileName),
119+
{ok, Nodes, Warnings} = file_read_nodes(FileName, ignore),
123120
case verify_ranges(Nodes, StartLocation, EndLocation) of
124121
{ok, NodesInRange} ->
125122
[$\n | Result] = format_nodes(NodesInRange),
@@ -136,14 +133,14 @@ format_range(FileName, StartLocation, EndLocation) ->
136133
-spec read_nodes(file:name_all()) ->
137134
{ok, [erlfmt_parse:abstract_form()], [error_info()]} | {error, error_info()}.
138135
read_nodes(FileName) ->
139-
try file_read_nodes(FileName)
136+
try file_read_nodes(FileName, ignore)
140137
catch
141138
{error, Error} -> {error, Error}
142139
end.
143140

144-
file_read_nodes(FileName) ->
141+
file_read_nodes(FileName, Pragma) ->
145142
read_file(FileName, fun (File) ->
146-
read_nodes(erlfmt_scan:io_node(File), FileName, [], [])
143+
read_nodes(erlfmt_scan:io_node(File), FileName, Pragma)
147144
end).
148145

149146
read_file(stdin, Action) ->
@@ -162,17 +159,28 @@ read_file(FileName, Action) ->
162159
-spec read_nodes_string(file:name_all(), string()) ->
163160
{ok, [erlfmt_parse:abstract_form()], [error_info()]} | {error, error_info()}.
164161
read_nodes_string(FileName, String) ->
165-
try read_nodes(erlfmt_scan:string_node(String), FileName, [], [])
162+
try read_nodes(erlfmt_scan:string_node(String), FileName, ignore)
166163
catch
167164
{error, Error} -> {error, Error}
168165
end.
169166

170-
read_nodes({ok, Tokens, Comments, Cont}, FileName, Acc, Warnings0) ->
167+
read_nodes({ok, Tokens, Comments, Cont}, FileName, Pragma) ->
168+
{Node, Warnings} = parse_nodes(Tokens, Comments, FileName, Cont, []),
169+
case contains_pragma_node(Node) of
170+
false when Pragma =:= require ->
171+
skip;
172+
_ ->
173+
read_nodes_loop(erlfmt_scan:continue(Cont), FileName, [Node], Warnings)
174+
end;
175+
read_nodes(Other, FileName, _Pragma) ->
176+
read_nodes_loop(Other, FileName, [], []).
177+
178+
read_nodes_loop({ok, Tokens, Comments, Cont}, FileName, Acc, Warnings0) ->
171179
{Node, Warnings} = parse_nodes(Tokens, Comments, FileName, Cont, Warnings0),
172-
read_nodes(erlfmt_scan:continue(Cont), FileName, [Node | Acc], Warnings);
173-
read_nodes({eof, _Loc}, _FileName, Acc, Warnings) ->
180+
read_nodes_loop(erlfmt_scan:continue(Cont), FileName, [Node | Acc], Warnings);
181+
read_nodes_loop({eof, _Loc}, _FileName, Acc, Warnings) ->
174182
{ok, lists:reverse(Acc), lists:reverse(Warnings)};
175-
read_nodes({error, {ErrLoc, Mod, Reason}, _Loc}, FileName, _Acc, _Warnings) ->
183+
read_nodes_loop({error, {ErrLoc, Mod, Reason}, _Loc}, FileName, _Acc, _Warnings) ->
176184
throw({error, {FileName, ErrLoc, Mod, Reason}}).
177185

178186
parse_nodes([], _Comments, _FileName, Cont, Warnings) ->

‎src/erlfmt_cli.erl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ format_files([FileName | FileNames], Config, HadErrors) ->
6464
{ok, Warnings} ->
6565
[print_error_info(Warning) || Warning <- Warnings],
6666
format_files(FileNames, Config, HadErrors);
67+
skip when Config#config.verbose ->
68+
io:format(standard_error, "Skipping ~s because of missing @format pragma\n", [FileName]),
69+
format_files(FileNames, Config, HadErrors);
70+
skip ->
71+
format_files(FileNames, Config, HadErrors);
6772
{error, Error} ->
6873
print_error_info(Error),
6974
format_files(FileNames, Config, true)
@@ -88,6 +93,8 @@ parse_opts([require_pragma | Rest], Name, Files, Config) ->
8893
parse_opts(Rest, Name, Files, Config#config{require_pragma = true});
8994
parse_opts([{files, NewFiles} | Rest], Name, Files0, Config) ->
9095
parse_opts(Rest, Name, expand_files(NewFiles, Files0), Config);
96+
parse_opts([], _Name, [stdin], #config{out = Out}) when Out =/= standard_out ->
97+
{error, "stdin mode can't be combined with out or write options"};
9198
parse_opts([], _Name, [stdin], Config) ->
9299
{format, [stdin], Config};
93100
parse_opts([], _Name, [], _Config) ->

‎test/erlfmt_SUITE.erl‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
annos/1,
4949
shebang/1,
5050
smoke_test_cli/1,
51+
smoke_test_stdio_escript/1,
52+
smoke_test_stdio_regular/1,
53+
smoke_test_stdio_without_pragma/1,
54+
smoke_test_stdio_with_pragma/1,
5155
snapshot_simple_comments/1,
5256
snapshot_big_binary/1,
5357
snapshot_attributes/1,
@@ -104,7 +108,11 @@ groups() ->
104108
]},
105109
{smoke_tests, [parallel], [
106110
{group, snapshot_tests},
107-
smoke_test_cli
111+
smoke_test_cli,
112+
smoke_test_stdio_escript,
113+
smoke_test_stdio_regular,
114+
smoke_test_stdio_without_pragma,
115+
smoke_test_stdio_with_pragma
108116
]},
109117
{snapshot_tests, [parallel], [
110118
snapshot_simple_comments,
@@ -918,9 +926,38 @@ parse_forms(String) ->
918926
end.
919927

920928
smoke_test_cli(Config) when is_list(Config) ->
929+
?assertMatch("Usage: erlfmt " ++ _, os:cmd(escript() ++ " -h")).
930+
931+
smoke_test_stdio_escript(Config) when is_list(Config) ->
932+
DataDir = ?config(data_dir, Config),
933+
Path = filename:join(DataDir, "escript.erl"),
934+
Formatted = os:cmd("cat " ++ Path ++ " | " ++ escript() ++ " -"),
935+
{ok, Expected} = file:read_file(Path),
936+
?assertEqual(Expected, unicode:characters_to_binary(Formatted)).
937+
938+
smoke_test_stdio_regular(Config) when is_list(Config) ->
939+
DataDir = ?config(data_dir, Config),
940+
Path = filename:join(DataDir, "attributes.erl"),
941+
Formatted = os:cmd("cat " ++ Path ++ " | " ++ escript() ++ " -"),
942+
{ok, Expected} = file:read_file(Path),
943+
?assertEqual(Expected, unicode:characters_to_binary(Formatted)).
944+
945+
smoke_test_stdio_without_pragma(Config) when is_list(Config) ->
946+
DataDir = ?config(data_dir, Config),
947+
Path = filename:join(DataDir, "big_binary.erl"),
948+
Formatted = os:cmd("cat " ++ Path ++ " | " ++ escript() ++ " - --require-pragma"),
949+
?assertEqual("", Formatted).
950+
951+
smoke_test_stdio_with_pragma(Config) when is_list(Config) ->
952+
DataDir = ?config(data_dir, Config),
953+
Path = filename:join(DataDir, "pragma.erl"),
954+
Formatted= os:cmd("cat " ++ Path ++ " | " ++ escript() ++ " - --require-pragma"),
955+
{ok, Expected} = file:read_file(Path),
956+
?assertEqual(Expected, unicode:characters_to_binary(Formatted)).
957+
958+
escript() ->
921959
%% this relies on the _build structure rebar3 uses
922-
Escript = filename:join(code:lib_dir(erlfmt), "../../bin/erlfmt"),
923-
?assertMatch("Usage: erlfmt " ++ _, os:cmd(Escript ++ " -h")).
960+
filename:join(code:lib_dir(erlfmt), "../../bin/erlfmt").
924961

925962
snapshot_simple_comments(Config) -> snapshot_same("simple_comments.erl", Config).
926963

‎test/erlfmt_SUITE_data/pragma.erl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%% @format
2+
3+
-module(pragma).

0 commit comments

Comments
 (0)