### ----------------------------------------------------
### -- App::Mi6
### -- Licenses: Artistic-2.0
### -- Authors: Shoichi Kaji
### -- File: xt/01-actual.rakutest
### ----------------------------------------------------
use Test;
use lib "xt";
use Util;
use File::Temp;
use App::Mi6::JSON;

my $r;
$r = mi6 "new";
isnt $r.exit, 0;

$r = mi6 "unknown";
isnt $r.exit, 0;

my $tempdir = tempdir;
{
    temp $*CWD = $tempdir.IO;
    $r = mi6 "new", "Foo::Bar";
    is $r.exit, 0;
    ok "Foo-Bar".IO.d;
    chdir "Foo-Bar";
    ok $_.IO.e for <.git  .gitignore  LICENSE  META6.json  README.md  bin  lib  t>;
    ok !"xt".IO.d;
    ok "lib/Foo/Bar.rakumod".IO.e;
    $r = mi6 "test";
    is $r.exit, 0;
    like $r.out, rx/All \s+ tests \s+ successful/;

    mkdir "xt";
    "xt/01-fail.rakutest".IO.spurt: q:to/EOF/;
    use Test;
    plan 1;
    ok False;
    EOF
    $r = mi6 "test";
    isnt $r.exit, 0;
    like $r.out, rx/Failed/;

    # Check .t6 extension
    "xt/01-fail.rakutest".IO.rename("xt/01-fail.t6");
    $r = mi6 "test";
    isnt $r.exit, 0;
    like $r.out, rx/Failed/;
}
{
    temp $*CWD = $tempdir.IO;
    my $fez-config-json = "$tempdir/fez-config.json";
    $fez-config-json.IO.spurt: '{"un":"test-user"}';
    temp %*ENV<FEZ_CONFIG> = $fez-config-json;
    LEAVE %*ENV<FEZ_CONFIG>:delete; # XXX WHY IS THIS NEEDED?

    $r = mi6 "new", "Hello";
    chdir "Hello";
    my $meta = App::Mi6::JSON.decode( "META6.json".IO.slurp );
    is $meta<description>, "blah blah blah";
    is $meta<auth>, "zef:test-user";
    "lib/Hello.rakumod".IO.spurt: q:to/EOF/;
    use v6;
    unit module Hello;

    =begin pod

    =head1 NAME

    Hello - This is hello module.

    =head1 DESC

    =end pod
    EOF
    $r = mi6 "build";
    $meta = App::Mi6::JSON.decode( "META6.json".IO.slurp );
    is $meta<description>, "This is hello module.";
}
{
    temp $*CWD = $tempdir.IO;
    mi6 "new", "Hello::World";
    chdir "Hello-World";
    "lib/Hello/World1.rakumod".IO.spurt("");
    "lib/Hello/World2.pm6".IO.spurt("");
    "lib/Hello/World3.pm6".IO.spurt("");
    "lib/Hello/World4.pm6".IO.spurt("");
    "lib/Hello/World5.pm6".IO.spurt("");
    "dist.ini".IO.spurt: q:to/EOF/;
    name = Hello::World
    source-url = https://github.com/foo/bar.git
    [PruneFiles]
    filename = lib/Hello/World5.pm6
    [MetaNoIndex]
    filename = lib/Hello/World3.pm6
    filename = lib/Hello/World4.pm6
    EOF
    run "git", "add", ".";

    mi6 "build";
    my $meta = App::Mi6::JSON.decode( "META6.json".IO.slurp );
    is $meta<provides>, {
        "Hello::World" => "lib/Hello/World.rakumod",
        "Hello::World1" => "lib/Hello/World1.rakumod",
        "Hello::World2" => "lib/Hello/World2.pm6",
    };
    is $meta<source-url>, "https://github.com/foo/bar.git";
}

{
    temp $*CWD = $tempdir.IO;
    mi6 "new", "Build::Test";
    chdir "Build-Test";
    "Build.rakumod".IO.spurt: q:to/EOF/;
    use Build::Test;
    class Build {
        method build($pwd) {
            say "pwd=$pwd";
            True;
        }
    }
    EOF
    my $r = mi6 "build";
    like $r.out, rx/ 'pwd=' /;
    like $r.err, rx/ '==> Execute Build.rakumod' /;
    is $r.exit, 0;
}

{
    temp $*CWD = $tempdir.IO;
    mi6 "new", "Build::Test2";
    chdir "Build-Test2";
    my $meta = App::Mi6::JSON.decode( "META6.json".IO.slurp );
    $meta<builder> = "MakeFromJSON";
    "META6.json".IO.spurt: App::Mi6::JSON.encode($meta);
    "Makefile.in".IO.spurt: qq:to/EOF/;
    default:
    \t@echo %SO%
    EOF
    my $r = mi6 "build";
    is $r.exit, 0 or diag $r.err;
}

{
    temp $*CWD = $tempdir.IO;
    mi6 "new", "Build::Test3";
    chdir "Build-Test3";
    "dist.ini".IO.spurt: q:to/EOF/, :append;
    [RunBeforeBuild]
    cmd = %x -e 'print 1'
    cmd = %x -e 'print 2'
    [RunAfterBuild]
    cmd = %x -e 'print 3'
    cmd = %x -e 'print 4'
    EOF
    my $r = mi6 "build";
    is $r.exit, 0 or diag $r.err;
    like $r.out, rx/1234/;
}

done-testing;


