### ----------------------------------------------------
### -- Mi6::Helper
### -- Licenses: Artistic-2.0
### -- Authors: Tom Browder <tbrowder@acm.org>
### -- File: xt/2-mi6-create.rakutest
### ----------------------------------------------------
use Test;

use Mi6::Helper;
use File::Temp;
use App::Mi6;
use File::Directory::Tree;
use Proc::Easier;

my $tdir = tempdir;
my $module-name = "Foo::Baz";
say "tmpdir: $tdir";
say "cur dir: {$*CWD}";

# check the system for known values used for fez and mi6
my $oo          = Mi6::Helper.new: :$module-name, :parent-dir($tdir);
my %fez         = App::Mi6::JSON.decode(slurp "$*HOME/.fez-config.json");
my $auth        = "zef:{%fez<un>}";
my $email       = $oo.git-user-email;
my $author      = $oo.git-user-name;
my $meta-author = "$author <$email>";

my $debug = 0;
if 0 and $debug {
    note qq:to/HERE/;
    DEBUG:
    author: $author
    email:  $email
    auth:   zef:$auth
    HERE
    note "DEBUG early exit";exit;
}

# provide a unique testing directory by test file name
my $debug-base = "debug-test";
my $debug-dir  = $debug-base ~ '/' ~ $?FILE.IO.basename;
# remove the trailing '.t*'
$debug-dir ~~ s/'.t'$//;
rmtree $debug-dir if $debug-dir.IO.d;

my ($res, $gs, $proc);

if not $debug {
    # normal testing
    $tdir = tempdir;
}
else {
    # development testing: preserves the output in dir '$tdir'
    #$tempdir = mkdir $debug-dir;
    mkdir $debug-dir;
    $tdir = $debug-dir;
}

ok $tdir.IO.d, "check tempdir";

{
    # home info for a fez user is in file $HOME/.fez-config.json;
    #   "un" : "SOMEBODY",
    #   "key" : "some-hash-key",

    #temp $*CWD = $tempdir.IO;
    #temp $*CWD = $tdir.IO;
    #temp %*ENV<HOME> = $tempdir;

    # DANGER DO NOT MODIFY THE USER'S ENVIRONMENT

    chdir $tdir;

    my $module-name = "Foo::Bar";
    my $moddir = $module-name;
    $moddir ~~ s:g/'::'/-/;
    run("mi6", 'new', '--zef', $module-name);
    ok $moddir.IO.d;

    # check the meta file for known values
    my %meta = App::Mi6::JSON.decode(slurp "$moddir/META6.json");
    if $debug {
        note "DEBUG:";
        for %meta.kv -> $k, $v {
            note "    key: '$k' => '$v'";
        }
    }
    is %meta<auth>, $auth;
    is @(%meta<authors>)[0], $author;
    #my $doc = slurp "$moddir/lib";
}

done-testing;


