Skip to content
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
15 changes: 14 additions & 1 deletion lib/files.cf
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,18 @@ bundle edit_line manage_variable_values_ini(tab, sectionName)
{
vars:
"index" slist => getindices("$(tab)[$(sectionName)]");
"_escaped_section" string => escape("$(sectionName)");

classes:
"section_$(sectionName)_exists"
expression => regline("^\[$(_escaped_section)\]", "$(edit.filename)"),
if => fileexists("$(edit.filename)"),
comment => "Check if the section header already exists in the file";

delete_lines:
".*"
select_region => INI_section(escape("$(sectionName)")),
if => canonify("section_$(sectionName)_exists"),
comment => "Remove all entries in the region so there are no extra entries";

insert_lines:
Expand All @@ -362,7 +370,12 @@ bundle edit_line manage_variable_values_ini(tab, sectionName)
comment => "Insert lines";

"$(index)=$($(tab)[$(sectionName)][$(index)])"
select_region => INI_section(escape("$(sectionName)"));
select_region => INI_section(escape("$(sectionName)")),
if => canonify("section_$(sectionName)_exists");

"$(index)=$($(tab)[$(sectionName)][$(index)])"
location => after("^\[$(_escaped_section)\]"),
if => not(canonify("section_$(sectionName)_exists"));
}

##
Expand Down
7 changes: 0 additions & 7 deletions tests/acceptance/lib/files/CFE-1710.cf
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ bundle agent test
"description" -> { "CFE-1710" }
string => "Test that the last section can be managed";

# The test fails to insert proper lines into LAST section (even though
# select_end_match_eof="true" is defined for versions greater than 3.10.0)
# Additionally the test fails to insert key values in to the newly inserted section at the top of the file.
"test_soft_fail"
string => "any",
meta => { "CFE-1710" };

# At test start:
#
# section exists with correct keys and bad values
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#######################################################
#
# Test that manage_variable_values_ini populates a section it creates
#
#######################################################
body common control
{
inputs => { "../../default.sub.cf" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

#######################################################
bundle agent init
{
files:
# The tested file must not exist, the test creates it.
"$(G.testfile).actual" delete => tidy;

# Next we place the file which we will compare the final result with.
"$(G.testfile).expected"
copy_from => local_cp("$(this.promise_filename).finish");
}

#######################################################
bundle agent test
{
meta:
"description" -> { "CFE-3898" }
string => "Test that manage_variable_values_ini populates a section it creates";

vars:
"config[section1][key1]" string => "value1";
"config[section2][key2]" string => "value2";

files:
"$(G.testfile).actual"
create => "true",
edit_line => manage_variable_values_ini("test.config", "section1");

"$(G.testfile).actual"
edit_line => manage_variable_values_ini("test.config", "section2");
}

#######################################################
bundle agent check
{
methods:
"check"
usebundle => dcs_if_diff(
"$(G.testfile).actual", "$(G.testfile).expected", "pass", "_fail"
);

"fail"
usebundle => dcs_fail($(this.promise_filename)),
if => "_fail";

pass::
"pass" usebundle => dcs_pass($(this.promise_filename));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[section2]
key2=value2
[section1]
key1=value1
Loading