summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/couchdb/couch_config_writer.erl14
-rw-r--r--test/couch_config_writer_test.erl30
2 files changed, 38 insertions, 6 deletions
diff --git a/src/couchdb/couch_config_writer.erl b/src/couchdb/couch_config_writer.erl
index 270e615f..fd4a0215 100644
--- a/src/couchdb/couch_config_writer.erl
+++ b/src/couchdb/couch_config_writer.erl
@@ -42,11 +42,15 @@ save_to_file({{Section, Option}, Value}, File) ->
% produce the contents for the config file
NewFileContents =
- case NewFileContents2 = save_loop({{SectionName, OptionList}, Value}, Lines, "", "", []) of
+ case {NewFileContents2, DoneOptions} = save_loop({{SectionName, OptionList}, Value}, Lines, "", "", []) of
% we didn't change anything, that means we couldn't find a matching
% [ini section] in which case we just append a new one.
- OldFileContents ->
- append_new_ini_section({{SectionName, OptionList}, Value}, OldFileContents);
+ {OldFileContents, DoneOptions} ->
+ % but only if we haven't actually written anything.
+ case lists:member(OptionList, DoneOptions) of
+ true -> OldFileContents;
+ _ -> append_new_ini_section({{SectionName, OptionList}, Value}, OldFileContents)
+ end;
_ ->
NewFileContents2
end,
@@ -110,9 +114,9 @@ save_loop({{Section, Option}, Value}, [], OldSection, NewFileContents, DoneOptio
case lists:member(Option, DoneOptions) of
% append Deferred Option
false when Section == OldSection ->
- NewFileContents ++ "\n" ++ Option ++ " = " ++ Value ++ "\n";
+ {NewFileContents ++ "\n" ++ Option ++ " = " ++ Value ++ "\n", DoneOptions};
% we're out of new lines, just return the new file's contents
- _ -> NewFileContents
+ _ -> {NewFileContents, DoneOptions}
end.
append_new_ini_section({{SectionName, Option}, Value}, OldFileContents) ->
diff --git a/test/couch_config_writer_test.erl b/test/couch_config_writer_test.erl
index 39666f5d..aa88abeb 100644
--- a/test/couch_config_writer_test.erl
+++ b/test/couch_config_writer_test.erl
@@ -11,7 +11,8 @@ couch_config_writer_test() ->
fun() -> replace_existing_variable3() end,
fun() -> append_new_variable() end,
fun() -> append_new_module() end,
- fun() -> overwrite_variable_further_down() end
+ fun() -> overwrite_variable_further_down() end,
+ fun() -> double_append_new_section_bug() end
].
@@ -149,6 +150,33 @@ option2 = value2
",
run_operation_and_compare_results(Contents, Expect, [{{"erlang", "option"}, "value"}, {{"erlang", "option2"}, "value2"}]).
+double_append_new_section_bug() ->
+ % create file
+ Contents = "[section]
+variable = value
+
+[another_section]
+another_var = another_value
+
+[erlang]
+option = value
+
+option2 = value2
+",
+
+ Expect = "[section]
+variable = value
+
+[another_section]
+another_var = another_value
+
+[erlang]
+option = value
+
+option2 = value2
+",
+ run_operation_and_compare_results(Contents, Expect, [{{"another_section", "another_var"}, "another_value"}]).
+
run_operation_and_compare_results(Contents, Expect, Config) when not is_list(Config) ->
run_operation_and_compare_results(Contents, Expect, [Config]);