App-Cme

 view release on metacpan or  search on metacpan

t/cme-command.t  view on Meta::CPAN

    },
    {
        label => "line ".__LINE__.": modification with a script with code",
        script => [
            "app:  popcon",
            '---code',
            q!$root->fetch_element('MY_HOSTID')->store($to_store);!,
            '---',
        ],
        args => [qw/--arg to_store=with_code/],
        test => qr/MY_HOSTID="with_code"/,
        stderr => q(
Changes applied to popcon configuration:
- MY_HOSTID: 'aaaaaaaaaaaaaaaaaaaa' -> 'with_code'
)
    },
    {
        label => "line ".__LINE__.": modification with a script with Perl format",
        script => [
            <<'EOS'
# Format:perl
{
   app => 'popcon',
   sub => sub ($root, $arg ) { $root->fetch_element('MY_HOSTID')->store($arg->{to_store});  },
};
EOS
        ],
        args => [qw/--arg to_store=with_code/],
        test => qr/MY_HOSTID="with_code"/,
        stderr => q(
Changes applied to popcon configuration:
- MY_HOSTID: 'aaaaaaaaaaaaaaaaaaaa' -> 'with_code'
)
    },
    {
        label => "line ".__LINE__.": get doc from script with Perl format",
        script => [
            <<'EOS'
#!/usr/bin/perl

## some documentation
## some more
EOS
        ],
        exec_mode => 1,
        args => [qw/--doc/],
        stdout => q(some documentation
some more
)
    },
);


# test cme run real script with arguments
my $i=0;
foreach my $test ( @script_tests) {
    subtest $test->{label} => sub {
        $conf_file->spew_utf8(@orig);
        my $script = $wr_dir->child('my-script'.$i++.'.cme');
        $script->spew_utf8( map { "$_\n"} @{$test->{script}});
        $script->chmod("0755") if $test->{exec_mode};

        my $cmd = [
            run => $script->stringify,
            '-root-dir' => $wr_dir->stringify,
            @{$test->{args} // []}
        ];
        note("cme command: cme @$cmd");
        my $ok = test_app('App::Cme' => $cmd);
        is( $ok->error, undef, 'threw no exceptions');
        is( $ok->exit_code, 0, "all went well" ) or diag("Failed command: @$cmd");

        if ($test->{test}) {
            file_contents_like $conf_file->stringify, $test->{test},
                "updated MY_HOSTID with script" ,{ encoding => 'UTF-8' };
        }
        is(colorstrip($ok->stderr), $test->{stderr} || '', 'run "'.$test->{label}.'" stderr content' );
        is(colorstrip($ok->stdout.''), $test->{stdout} || '', 'run "'.$test->{label}.'": stdout content' );
    };
}

# test failure case for run script
my @bad_script_tests = (
    {
        label => "line ".__LINE__.": missing app",
        script => [ 'load ! MY_HOSTID=dontcare'],
        args => [],
        error_regexp => qr/Missing 'app' parameter in script/
    },
    {
        label => "line ".__LINE__.": modification with a Perl script run by cme run with missing arg",
        script => [ "app:  popcon", 'load ! MY_HOSTID=\$name$name'],
        args => [],
        error_regexp => qr/use option '-arg name=xxx'/
    },
    {
        label => "line ".__LINE__.": modification with a Perl script run by cme run with 2 missing args",
        script => [ "app:  popcon", 'load ! MY_HOSTID=$name1$name2'],
        args => [],
        error_regexp => qr/use option '-arg name1=xxx -arg name2=xxx'/
    },
    {
        label => "line ".__LINE__.": modification with a Perl script run by cme run with missing args in var line",
        script => [
            "app:  popcon",
            'var: $var{name} = $args{name1}.$args{name2}',
            'load: ! MY_HOSTID=$name'],
        args => [],
        error_regexp => qr/use option '-arg name1=xxx -arg name2=xxx'/
    },
    {
        label => "line ".__LINE__.":  load and code section",
        script => [
            "app:  popcon",
            'load: ! MY_HOSTID=$name',
            q!code: $root->load("MY_HOSTID=$name")!,
        ],
        args => [],
        error_regexp => qr/Cannot mix code and load section/
    },
);



( run in 0.690 second using v1.01-cache-2.11-cpan-d7f47b0818f )