### ----------------------------------------------------
### -- ake
### -- Licenses: Artistic-2.0
### -- Authors: Jonathan Scott Duff <duff@pobox.com>, Alex Daniel, the Raku Community
### -- File: t/lib/AkeTester.rakumod
### ----------------------------------------------------
unit module AkeTester;

use File::Temp;
use Test;

sub make-ake-directory($contents, :$filename = ‘Akefile’) is export {
    my $dir = tempdir.IO;
    $dir.add($filename).spurt: $contents;
    $dir
}

sub test-run($description, *@args,
             :$out = ‘’, :$err = ‘’,
             :$exitcode = 0,
             :$cwd is copy,
             :$env is copy = %*ENV,
             *%rest) is export {

    $cwd //= CALLERS::(‘$_’);
    # TODO ↓ any better way ?
    $env = %(|$env,
             PATH    => “{$*CWD.add: ‘bin’}:{$env<PATH>    // ‘’}”,
             RAKULIB => “{$*CWD.add: ‘lib’},{$env<RAKULIB> // ‘’}”,
            );

    subtest $description, {
        plan 4;
        my $proc = run @args, :$env, :$cwd, :out, :err, |%rest;
        cmp-ok $proc.out.slurp, &[~~],      $out,   ‘stdout is correct’;
        cmp-ok $proc.err.slurp, &[~~],      $err,   ‘stderr is correct’;
        cmp-ok $proc.exitcode,  &[~~], $exitcode, ‘exitcode is correct’;
        cmp-ok $proc.signal,    &[==],         0,   ‘signal is 0’;
    }
}


