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

Allow shell expanded .conf values #14

Merged
merged 6 commits into from
Dec 3, 2020
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/value* text eol=lf
"test/dir with spaces/value 3" text eol=lf
7 changes: 7 additions & 0 deletions src/conf_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
7 changes: 7 additions & 0 deletions src/conf_parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
39 changes: 38 additions & 1 deletion src/cuttlefish_conf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 "$(<IncludeFilename), let's read the contents
% of `IncludeFilename` and use that as the new value
%
% strip all space chars from beginning/end and join the relative filename with the
% location of the sourcing .conf file
IncludeFilename = filename:join([filename:dirname(Filename),
re:replace(IncludeFilename0, "(^\\s+)|(\\s+$)", "", [{return, list}])]),
% read the entire file contents and strip newline
case file:read_file(IncludeFilename) of
{ok, Value} ->
{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(
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 0 additions & 22 deletions src/cuttlefish_unit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/cuttlefish_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions test/dir with spaces/value 3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.34
5 changes: 5 additions & 0 deletions test/included_value.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
value1 = $(<value1)
value2 = 43
value3 = $(< value1)
value4 = $(< value2)
value5 = $(< ../test/dir with spaces/value 3)
1 change: 1 addition & 0 deletions test/value1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
3 changes: 3 additions & 0 deletions test/value2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
multi
line
value