#######################################################
#
# Ensure that the transformer runs only once for every file in a recursive tree
#
#######################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
  cache_system_functions => "off";

  # for execresult in check bundle
}

#######################################################
bundle agent init
{
  vars:
    "files" slist => { "1", "2", "3" };

  files:
    "$(G.testdir)/files/." create => "true";
    "$(G.testdir)/files/$(files)" copy_from => init_copy;
}

body copy_from init_copy
{
  source => "$(G.etc_group)";
}

body classes init_set_class(class)
{
  promise_kept => { "$(class)" };
  promise_repaired => { "$(class)" };
}

#######################################################
bundle agent test
{
  vars:
    "counter" string => "$(G.testdir)/counter";

  files:
    windows::
      "$(G.testdir)/files"
        transformer => "$(G.echo) $(this.promiser) >> $(counter)",
        file_select => test_plain,
        depth_search => test_recurse;

    !windows::
      "$(G.testdir)/files"
        transformer => "/bin/sh -c 'echo $(this.promiser) >> $(counter)'",
        file_select => test_plain,
        depth_search => test_recurse;
}

body file_select test_plain
{
  file_types => { "plain" };
  file_result => "file_types";
}

body depth_search test_recurse
{
  depth => "inf";
}

#######################################################
bundle agent check
{
  vars:
    "count" string => execresult("$(G.wc) -l $(test.counter)", "noshell");

    DEBUG::
      "list" string => execresult("$(G.ls) -1 $(G.testdir)/files", "noshell");

  classes:
    "ok" expression => regcmp("^\s*3\s.*", "$(count)");

  reports:
    DEBUG.!ok::
      "3 transformations expected, saw (wc): '$(count)'";
      "3 copies of $(G.etc_group) expected, saw (ls): '$(list)'";

    ok::
      "$(this.promise_filename) Pass";

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

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