diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..559501e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +test/value* text eol=lf +"test/dir with spaces/value 3" text eol=lf diff --git a/src/conf_parse.erl b/src/conf_parse.erl index 615eaf1..ab80f17 100644 --- a/src/conf_parse.erl +++ b/src/conf_parse.erl @@ -107,6 +107,13 @@ included_dir_test() -> ], Conf), ok. +escaped_dots_are_removed_test() -> + Conf = conf_parse:parse("#comment\nsetting\\.0 = thing0\n"), + ?assertEqual([ + {["setting.0"],"thing0"} + ], Conf), + ok. + utf8_test() -> Conf = conf_parse:parse("setting = thing" ++ [338] ++ "\n"), ?assertEqual([{["setting"], diff --git a/src/conf_parse.peg b/src/conf_parse.peg index c904d16..d22bb57 100644 --- a/src/conf_parse.peg +++ b/src/conf_parse.peg @@ -202,6 +202,13 @@ included_dir_test() -> ], Conf), ok. +escaped_dots_are_removed_test() -> + Conf = conf_parse:parse("#comment\nsetting\\.0 = thing0\n"), + ?assertEqual([ + {["setting.0"],"thing0"} + ], Conf), + ok. + utf8_test() -> Conf = conf_parse:parse("setting = thing" ++ [338] ++ "\n"), ?assertEqual([{["setting"], diff --git a/src/cuttlefish_conf.erl b/src/cuttlefish_conf.erl index 72b7e48..aa5e328 100644 --- a/src/cuttlefish_conf.erl +++ b/src/cuttlefish_conf.erl @@ -77,7 +77,8 @@ file(Filename) -> {_, Values} = lists:unzip(Conf), case cuttlefish_error:filter(Values) of {errorlist, []} -> - remove_duplicates(Conf); + % expand any non-literal values (ie. included values) + expand_values(Filename, remove_duplicates(Conf)); {errorlist, ErrorList} -> NewErrorList = [ {error, {in_file, {Filename, E}}} || {error, E} <- ErrorList ], {errorlist, NewErrorList} @@ -98,6 +99,31 @@ fold_conf_files(Filename, Conf0) -> [], Conf0). +expand_values(Filename, Conf) -> + lists:map(fun({K, Value0}) -> + case re:split(Value0, "[$(<)]") of + [_, _, _, IncludeFilename0, _] -> + % this is a value of the format "$( + {K, re:replace(Value, "[\n\r]$", "", [{return, list}])}; + {error, Reason} -> + throw({unable_to_open, IncludeFilename, Reason}) + end; + _ -> + % normal value, nothing to do + {K, Value0} + end + end, + Conf). + -spec generate([cuttlefish_mapping:mapping()]) -> [string()]. generate(Mappings) -> lists:foldl( @@ -391,6 +417,17 @@ included_dir_test() -> ]), lists:sort(Conf)), ok. +included_value_test() -> + Conf = file("test/included_value.conf"), + ?assertEqual(lists:sort([ + {["value1"], "42"}, + {["value2"], "43"}, + {["value3"], "42"}, + {["value4"], "multi\nline\nvalue"}, + {["value5"], "12.34"} + ]), lists:sort(Conf)), + ok. + assert_no_output(Setting) -> Mapping = cuttlefish_mapping:parse( {mapping, diff --git a/src/cuttlefish_unit.erl b/src/cuttlefish_unit.erl index 6dbd2a3..e655bbb 100644 --- a/src/cuttlefish_unit.erl +++ b/src/cuttlefish_unit.erl @@ -104,28 +104,6 @@ assert_not_configured(Config, Path) -> assert_error(Config) -> ?assertMatch({error, _, {errorlist, _}}, Config). -%% @doc Asserts that the generated configuration is in error, with the -%% error occurring in a specific phase. -assert_error_in_phase(Config, Phase) when is_atom(Phase) -> - ?assertMatch({error, Phase, {errorlist, _}}, Config). - -%% @doc Asserts that the generated configuration is in error, and the -%% given error message was emitted by the given phase. -assert_error(Config, Phase, Message) -> - assert_error_in_phase(Config, Phase), - assert_error_message(Config, Message). - -%% @doc Asserts that the generated configuration is in error and has -%% the given error messages. -assert_errors(Config, [H|_]=Messages) when is_list(H) -> - [ assert_error_message(Config, Message) || Message <- Messages ]. - -%% @doc Asserts that the generated configuration is in error, with -%% errors occuring in the given phase and containing the given -%% messages. -assert_errors(Config, Phase, [H|_]=Messages) when is_list(H) -> - assert_error_in_phase(Config, Phase), - [ assert_error_message(Config, Message) || Message <- Messages ]. %% @doc Asserts that the generated configuration is in error and %% contains an error tuple that translates to the given error message diff --git a/src/cuttlefish_util.erl b/src/cuttlefish_util.erl index ce174aa..ebb1bf7 100644 --- a/src/cuttlefish_util.erl +++ b/src/cuttlefish_util.erl @@ -188,4 +188,10 @@ ceiling_test() -> ?assertEqual(-2, ceiling(-2.9999999)), ok. +numerify_test() -> + ?assertEqual(42, numerify("42")), + ?assertEqual(42.0, numerify("42.0")), + ?assertEqual(0.5, numerify(".5")), + ok. + -endif. diff --git a/test/dir with spaces/value 3 b/test/dir with spaces/value 3 new file mode 100644 index 0000000..71a7cf9 --- /dev/null +++ b/test/dir with spaces/value 3 @@ -0,0 +1 @@ +12.34 diff --git a/test/included_value.conf b/test/included_value.conf new file mode 100644 index 0000000..afb7e9d --- /dev/null +++ b/test/included_value.conf @@ -0,0 +1,5 @@ +value1 = $(