From ef9039067db6286710fd634ecce829e923f05aaf Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Fri, 2 Aug 2019 18:37:31 -0700 Subject: [PATCH 1/4] Add failing test for gh-1 --- src/conf_parse.erl | 18 +++++++++++------- src/conf_parse.peg | 9 +++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/conf_parse.erl b/src/conf_parse.erl index ad6f6c39..22317965 100644 --- a/src/conf_parse.erl +++ b/src/conf_parse.erl @@ -91,20 +91,24 @@ file_test() -> ok. utf8_test() -> - Conf = conf_parse:parse("setting = thingÅ’\n"), + Conf = conf_parse:parse("setting = thing" ++ [338] ++ "\n"), ?assertEqual([{["setting"], {error, {conf_to_latin1, 1}} }], Conf), ok. + +gh_1_only_whitespace_test() -> + Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"), + ?assertEqual([ + {["setting0"],"thing0"}, + {["setting1"],"thing1"} + ], Conf), + ok. + -endif. -spec file(file:name()) -> any(). -file(Filename) -> - AbsFilename = filename:absname(Filename), - case erl_prim_loader:get_file(AbsFilename) of - {ok, Bin, _} -> parse(Bin); - error -> {error, undefined} - end. +file(Filename) -> case file:read_file(Filename) of {ok,Bin} -> parse(Bin); Err -> Err end. -spec parse(binary() | list()) -> any(). parse(List) when is_list(List) -> parse(unicode:characters_to_binary(List)); diff --git a/src/conf_parse.peg b/src/conf_parse.peg index 090ffdd9..9f32e530 100644 --- a/src/conf_parse.peg +++ b/src/conf_parse.peg @@ -161,5 +161,14 @@ utf8_test() -> {error, {conf_to_latin1, 1}} }], Conf), ok. + +gh_1_only_whitespace_test() -> + Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"), + ?assertEqual([ + {["setting0"],"thing0"}, + {["setting1"],"thing1"} + ], Conf), + ok. + -endif. %} From 143dcad6c5a389b2f984f4be88b491192ba80675 Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Fri, 2 Aug 2019 19:02:55 -0700 Subject: [PATCH 2/4] Fix PEG grammar to consider whitespace as one or more whitespace characters, fix test in cuttlefish_conf --- src/conf_parse.erl | 13 +++++++++++-- src/conf_parse.peg | 13 +++++++++++-- src/cuttlefish_conf.erl | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/conf_parse.erl b/src/conf_parse.erl index 22317965..074c99d8 100644 --- a/src/conf_parse.erl +++ b/src/conf_parse.erl @@ -66,6 +66,7 @@ %% @doc Only let through lines that are not comments or whitespace. is_setting(ws) -> false; +is_setting([ws]) -> false; is_setting(comment) -> false; is_setting(_) -> true. @@ -97,7 +98,7 @@ utf8_test() -> }], Conf), ok. -gh_1_only_whitespace_test() -> +gh_1_two_tab_test() -> Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"), ?assertEqual([ {["setting0"],"thing0"}, @@ -105,6 +106,14 @@ gh_1_only_whitespace_test() -> ], Conf), ok. +gh_1_three_tab_test() -> + Conf = conf_parse:parse("setting0 = thing0\n\t\t\t\nsetting1 = thing1\n"), + ?assertEqual([ + {["setting0"],"thing0"}, + {["setting1"],"thing1"} + ], Conf), + ok. + -endif. -spec file(file:name()) -> any(). @@ -180,7 +189,7 @@ parse(Input) when is_binary(Input) -> -spec 'ws'(input(), index()) -> parse_result(). 'ws'(Input, Index) -> - p(Input, Index, 'ws', fun(I,D) -> (p_charclass(<<"[\s\t]">>))(I,D) end, fun(_Node, _Idx) ->ws end). + p(Input, Index, 'ws', fun(I,D) -> (p_one_or_more(p_charclass(<<"[\s\t]">>)))(I,D) end, fun(_Node, _Idx) ->ws end). diff --git a/src/conf_parse.peg b/src/conf_parse.peg index 9f32e530..faef76e3 100644 --- a/src/conf_parse.peg +++ b/src/conf_parse.peg @@ -75,7 +75,7 @@ crlf <- "\r"? "\n" `ws`; eof <- !. `ws`; %% Whitespace is either spaces or tabs. -ws <- [ \t] `ws`; +ws <- [ \t]+ `ws`; % Erlang code %{ @@ -131,6 +131,7 @@ ws <- [ \t] `ws`; %% @doc Only let through lines that are not comments or whitespace. is_setting(ws) -> false; +is_setting([ws]) -> false; is_setting(comment) -> false; is_setting(_) -> true. @@ -162,7 +163,7 @@ utf8_test() -> }], Conf), ok. -gh_1_only_whitespace_test() -> +gh_1_two_tab_test() -> Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"), ?assertEqual([ {["setting0"],"thing0"}, @@ -170,5 +171,13 @@ gh_1_only_whitespace_test() -> ], Conf), ok. +gh_1_three_tab_test() -> + Conf = conf_parse:parse("setting0 = thing0\n\t\t\t\nsetting1 = thing1\n"), + ?assertEqual([ + {["setting0"],"thing0"}, + {["setting1"],"thing1"} + ], Conf), + ok. + -endif. %} diff --git a/src/cuttlefish_conf.erl b/src/cuttlefish_conf.erl index 52f11913..8aa4167b 100644 --- a/src/cuttlefish_conf.erl +++ b/src/cuttlefish_conf.erl @@ -312,7 +312,7 @@ duplicates_multi_test() -> files_one_nonent_test() -> Conf = files(["test/multi1.conf", "test/nonent.conf"]), - ?assertEqual({errorlist,[{error, {file_open, {"test/nonent.conf", undefined}}}]}, Conf), + ?assertEqual({errorlist,[{error, {file_open, {"test/nonent.conf", enoent}}}]}, Conf), ok. files_incomplete_parse_test() -> From 7a154b5745af9d278f4e050c39b3b1a00bcaac0d Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Sat, 3 Aug 2019 09:22:37 +0300 Subject: [PATCH 3/4] Restore schema file reading from .ez archives Introduced in 19c44629236eec4661177276930f7fb13bb10c6e. --- src/conf_parse.erl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/conf_parse.erl b/src/conf_parse.erl index 074c99d8..2de179c2 100644 --- a/src/conf_parse.erl +++ b/src/conf_parse.erl @@ -117,7 +117,12 @@ gh_1_three_tab_test() -> -endif. -spec file(file:name()) -> any(). -file(Filename) -> case file:read_file(Filename) of {ok,Bin} -> parse(Bin); Err -> Err end. +file(Filename) -> + AbsFilename = filename:absname(Filename), + case erl_prim_loader:get_file(AbsFilename) of + {ok, Bin, _} -> parse(Bin); + error -> {error, undefined} + end. -spec parse(binary() | list()) -> any(). parse(List) when is_list(List) -> parse(unicode:characters_to_binary(List)); From 2caf4524bfd2b50b5acaa02a65b77ddd9d987d57 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Sat, 3 Aug 2019 09:56:42 +0300 Subject: [PATCH 4/4] Update an assertion --- src/cuttlefish_conf.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cuttlefish_conf.erl b/src/cuttlefish_conf.erl index 8aa4167b..250d4d5a 100644 --- a/src/cuttlefish_conf.erl +++ b/src/cuttlefish_conf.erl @@ -312,7 +312,7 @@ duplicates_multi_test() -> files_one_nonent_test() -> Conf = files(["test/multi1.conf", "test/nonent.conf"]), - ?assertEqual({errorlist,[{error, {file_open, {"test/nonent.conf", enoent}}}]}, Conf), + ?assertMatch({errorlist,[{error, {file_open, {"test/nonent.conf", _}}}]}, Conf), ok. files_incomplete_parse_test() ->