### ----------------------------------------------------
### -- Collection-Plugin-Development
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth, aka finanalyst
### -- File: xt/10-modify-plugin-config.rakutest
### ----------------------------------------------------
use v6.d;
use Test;
use Test::Run :runs_ok;
use File::Directory::Tree;
use RakuConfig;
use Collection::Entities;

plan 12;

constant DIR = 'xt/config-test';
if DIR.IO ~~ :e & :d {
    rmtree DIR
}

runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} >>,
    :err(/ 'is not a valid path' /)
    :exitcode(1),
    'directory does not exist'
);

mktree DIR;

runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} >>,
    :exitcode(1),
    :err(/ 'Something is Wrong' /),
    'No min conditions'
);
runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='all' -defaults>>,
    :exitcode(1),
    :err(/ 'There are no potential plugin' /),
    'No plugins'
);
runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='Camelia' -defaults>>,
    :exitcode(1),
    :err(/ 'plugin sub-directory' /),
    'No named plugin'
);
mktree DIR ~ '/Camelia';
runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='Camelia' -version='v0.2.0'>>,
    :exitcode(1),
    :err(/ 'wrong with version' /),
    'Bad version'
);
runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='Camelia' -v='0.2.0' -quiet>>,
    :exitcode(1),
    :err(/ 'contains neither ｢config.raku｣ nor ｢configs/｣' /),
    'No configuration'
);
my %plug-conf = %(:render, :custom-raku<custom.raku>, :template-raku(), :css<my.css>);
(DIR ~ "/Camelia/config.raku").IO.spurt(format-config(%plug-conf));

runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='Camelia' -defaults -quiet>>,
    :exitcode(0),
    'Accepts config.raku'
);
my %new-conf;
lives-ok {%new-conf = EVALFILE (DIR ~ "/Camelia/config.raku")}, 'new config compiles';

cmp-ok %new-conf.keys, '(==)', (%plug-conf.keys, 'name', %collection-defaults.keys).flat, 'config round trips';
runs_ok(
    :args<<raku -I. bin/modify-collection-plugin-config -path={DIR} -plugin='Camelia' -bump -quiet >>,
    :exitcode(0),
    'Bumps version in config.raku'
);
lives-ok {%new-conf = EVALFILE (DIR ~ "/Camelia/config.raku")}, 'config with new version compiles';
is %new-conf<version>, '0.1.1', 'version is bumped';

done-testing;


