### ----------------------------------------------------
### -- Collection-Plugin-Development
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth, aka finanalyst
### -- File: xt/30-add-plugin.rakutest
### ----------------------------------------------------
use v6.d;
use Test;
use Test::Run :runs_ok;
use File::Directory::Tree;
use RakuConfig;
plan 12;

my $dir = 'xt/make-test';
if $dir.IO ~~ :e {
    rmtree $dir
}

mktree $dir;

my @args;

@args = <raku -I. bin/add-collection-plugin -test >;
@args.append: "-format=$dir";
# operating plugin name rules prohibit _
@args.append: 'old-plug_v1_auth_collection';
runs_ok(
    :@args,
    :err(/ 'plugin naming rules' /)
    :exitcode(1),
    'plugin naming rules broken'
);
@args.pop;
@args.append: 'old-plug';
runs_ok(
    :@args,
    :exitcode(0),
    'plugin naming rules kept'
);
runs_ok(
    :@args,
    :err(/ 'already exists' /)
    :exitcode(1),
    'psuedo old-plugin exists'
);
@args.pop;
@args.append: 'new-plug';
runs_ok(
    :@args,
    :exitcode(0),
    'new-plugin created'
);
ok "$dir/new-plug/config.raku".IO ~~ :e & :f, 'new-plugin config file exists';
@args.pop;
@args.append: <-mile=setup setup-plug >;
runs_ok(
    :@args,
    :exitcode(0),
    'setup-plugin created for milestone setup'
);
ok "$dir/setup-plug/setup-callable.raku".IO ~~ :e & :f, 'setup-plugin callable file exists';
my @miles = <setup compilation report>;
@args.pop;
@args.pop;
@args.append: "-mile={ @miles }";
@args.append: 'complex-plug';
runs_ok(
    :@args,
    :exitcode(0),
    'multiple milestone callables created'
);
my %config = get-config(:path($dir ~ '/complex-plug/config.raku'));
cmp-ok @miles, '(<)', %config.keys, 'config has all callable keys';
for @miles {
    ok "$dir/complex-plug/$_\-callable.raku".IO ~~ :e & :f, "complex-plug $_ callable file exists";
}
done-testing;


