#######################################################
#
# Test insert_ini_section
#
#######################################################

body common control
{
      inputs => { '../../default.sub.cf' };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}
#######################################################

bundle agent init
{
  files:
      # The tested file "actual" is copied from our seeded starting position.
      "$(G.testfile).actual"
      copy_from => local_cp("$(this.promise_filename).start");

     # 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-2721" }
        string => "Test that insert_ini_section works with more than one section";

    cfengine_3_7::
      "test_soft_fail"
        string => "any",
        meta => { "CFE-2721" };

  vars:
      "options[test][test_option_one]" string => "test_option_one_value";
      "options[fake][fake_option_one]" string => "fake_option_one_value";

      # TODO: refactor after 3.7.x is EOL
      "sections_indices" slist => getindices(options);
      "sections_sorted" slist => sort("sections_indices", lex);
      "sections" slist => reverse("sections_sorted");

  files:
      "$(G.testfile).actual"
      edit_line => insert_ini_section( $(sections), "$(this.bundle).options[$(sections)]" );
}

#######################################################

bundle agent check
{
  methods:
      "check"
      usebundle => dcs_if_diff( "$(G.testfile).actual", "$(G.testfile).expected",
                                "pass", "_fail");
      # Fail the test if any of the files fail.

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

    pass::
      "pass"
        usebundle => dcs_pass( $(this.promise_filename) );
}
