#######################################################
#
# Check the example outputs
#
#######################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
}

#######################################################
bundle agent init
{
  vars:
    "basedir" string => "$(G.cwd)/../../examples";
    "checker" string => "$(basedir)/remake_outputs.pl";
    "all_examples" slist => findfiles("$(basedir)/*.cf");

  reports:
    DEBUG_LOTS::
      "$(this.bundle): will consider example $(all_examples)";
}

#######################################################
bundle agent test
{
  meta:
    # It's unrealistic to keep this test perfectly stable on all non-Linux platforms.
    # It would require examples that quickly get very cluttered.
    "test_skip_unsupported" string => "!linux";

  vars:
    "examples" slist => { @(init.all_examples) };
    "canon[$(examples)]" string => canonify($(examples));

  classes:
    "has_output_block_$(canon[$(examples)])"
      expression => regline(".*example_output.*", $(examples));

  methods:
    "$(examples)"
      usebundle => test_example($(examples)),
      if => "has_output_block_$(canon[$(examples)])";

  reports:
    DEBUG::
      "$(this.bundle): found example with output $(examples)"
        if => "has_output_block_$(canon[$(examples)])";
}

bundle agent test_example(file)
{
  vars:
    "cfile" string => canonify($(file));

    "checker"
      string => "$(G.perl) $(init.checker) --cfagent=$(sys.cf_agent) --workdir=$(G.testdir) -c";

  classes:
    "failure_$(cfile)"
      not => returnszero("$(checker) $(file)", "noshell"),
      scope => "namespace";

  methods:
    "with verbose"
      usebundle => verbose_output("$(checker) -v $(file)"),
      if => "failure_$(cfile)";

  reports:
    "$(this.bundle): checker '$(checker) $(file)' returned error"
      if => "failure_$(cfile)";

    DEBUG::
      "$(this.bundle): ran checker '$(checker) $(file)'";
}

bundle agent verbose_output(runme)
{
  commands:
    "$(runme)";
}

#######################################################
bundle agent check
{
  classes:
    "ok" not => classmatch("failure.*");

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

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