#######################################################
#
# Test rmdirs => true never deletes non empty directories
# but returns promise kept/repaired, even if dir not empty.
# Ref: https://dev.cfengine.com/issues/6331
#
#######################################################

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

bundle agent init
{
  vars:
    "dir"      string => "$(G.testdir)/dir/.";
    "subdir1"  string => "$(dir)/subdir1/.";
    "subdir2"  string => "$(dir)/subdir2/.";

  files:
      "$(dir)"          create => "true";
      "$(subdir1)"      create => "true";
      "$(subdir2)"      create => "true";
      "$(subdir2)/blah" create => "true";
}

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

bundle agent test
{
  # When trying to recursively delete "dir", it will be unable to delete
  # "dir/subdir2" because it's not empty, but it should be skipped
  # silently, and the whole promise will be success.

  files:
    "$(init.dir)"
      depth_search => recurse_include_basedir("inf"),
      file_select  => not_blah,
      delete       => tidy,
      # success is being set despite skipping non-empty dir
      classes      => if_ok("success");
}

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

bundle agent check
{
  classes:
    "dir_exists"     expression => isdir($(init.dir));
    "subdir1_deleted"       not => isdir($(init.subdir1));
    "subdir2_exists" expression => isdir($(init.subdir2));

    "ok" expression => "success.dir_exists.subdir1_deleted.subdir2_exists";

  reports:
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}



body depth_search recurse_include_basedir(d)
{
  depth           => "$(d)";
  include_basedir => "true";
}

body file_select not_blah
{
  leaf_name => { "blah" };
  file_result => "!leaf_name";
}


### PROJECT_ID: core
### CATEGORY_ID: 27
