diff --git a/lib/files.cf b/lib/files.cf index 732dd261fa..5f67eaff02 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -401,10 +401,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: @@ -413,7 +421,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")); } ## diff --git a/tests/acceptance/lib/files/CFE-1710.cf b/tests/acceptance/lib/files/CFE-1710.cf index 5b15e113b0..a71ce2ff66 100644 --- a/tests/acceptance/lib/files/CFE-1710.cf +++ b/tests/acceptance/lib/files/CFE-1710.cf @@ -26,15 +26,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 diff --git a/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf b/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf new file mode 100644 index 0000000000..a45209234a --- /dev/null +++ b/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf @@ -0,0 +1,60 @@ +####################################################### +# +# Test that manage_variable_values_ini populates a section it creates +# +####################################################### +body common control +{ + inputs => { "../../default.cf.sub" }; + 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)); +} diff --git a/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf.finish b/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf.finish new file mode 100644 index 0000000000..d1ada43e51 --- /dev/null +++ b/tests/acceptance/lib/files/manage_variable_values_ini_missing_section.cf.finish @@ -0,0 +1,4 @@ +[section2] +key2=value2 +[section1] +key1=value1