This file is indexed.

/usr/share/perl5/MooX/Cmd/Tester.pm is in libmoox-cmd-perl 0.015-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package MooX::Cmd::Tester;

use strict;
use warnings;

our $VERSION = "0.015";

require Exporter;
use Test::More import => ['!pass'];
use Package::Stash;
use Capture::Tiny qw(:all);

use parent qw(Test::Builder::Module Exporter);

our @EXPORT    = qw(test_cmd test_cmd_ok);
our @EXPORT_OK = qw(test_cmd test_cmd_ok);

our $TEST_IN_PROGRESS;
my $CLASS = __PACKAGE__;

BEGIN
{
    *CORE::GLOBAL::exit = sub {
        return CORE::exit(@_) unless $TEST_IN_PROGRESS;
        MooX::Cmd::Tester::Exited->throw( $_[0] );
    };
}

sub result_class { 'MooX::Cmd::Tester::Result' }

sub test_cmd
{
    my ( $app, $argv ) = @_;

    my $result = _run_with_capture( $app, $argv );
    my $exit_code = defined $result->{error} ? ( ( 0 + $! ) || -1 ) : 0;

    $result->{error}
      and eval { $result->{error}->isa('MooX::Cmd::Tester::Exited') }
      and $exit_code = ${ $result->{error} };

    result_class->new(
        {
            exit_code => $exit_code,
            %$result,
        }
    );
}

sub test_cmd_ok
{
    my $rv = test_cmd(@_);

    my $test_ident = $rv->app . " => [ " . join( " ", @{ $_[1] } ) . " ]";
    ok( !$rv->error, "Everythink ok running cmd $test_ident" ) or diag( $rv->error );
    # no error and cmd means, we're reasonable successful so far
    $rv
      and !$rv->error
      and $rv->cmd
      and $rv->cmd->command_name
      and ok( $rv->cmd->command_commands->{ $rv->cmd->command_name }, "found command at $test_ident" );

    $rv;
}

sub _capture_merged(&)
{
    my $code = shift;
    my ( $stdout, $stderr, $merged, $ok );
    if ( $^O eq 'MSWin32' )
    {
        ( $stdout, $stderr, $ok ) = tee { $code->(); };
        $merged = $stdout . $stderr;
    }
    else
    {
        ($merged) = tee_merged
        {
            ( $stdout, $stderr, $ok ) = tee { $code->() };
        };
    }
    ( $stdout, $stderr, $merged, $ok );
}

sub _run_with_capture
{
    my ( $app, $argv ) = @_;

    my ( $execute_rv, $cmd, $cmd_name );

    my ( $stdout, $stderr, $merged, $ok ) = _capture_merged
    {
        eval {
            local $TEST_IN_PROGRESS = 1;
            local @ARGV             = @$argv;

            my $tb = $CLASS->builder();

            $cmd = ref $app ? $app : $app->new_with_cmd;
            ref $app and $app = ref $app;
            my $test_ident = "$app => [ " . join( " ", @$argv ) . " ]";
            ok( $cmd->isa($app), "got a '$app' from new_with_cmd" );
            @$argv
              and defined( $cmd_name = $cmd->command_name )
              and ok( ( grep { $_ =~ m/$cmd_name/ } @$argv ), "proper cmd name from $test_ident" );
            ok( scalar @{ $cmd->command_chain } <= 1 + scalar @$argv, "\$#argv vs. command chain length testing $test_ident" );
            @$argv and ok( $cmd->command_chain_end == $cmd->command_chain->[-1], "command_chain_end ok" );

            unless ( $execute_rv = $cmd->execute_return )
            {
                my ( $command_execute_from_new, $command_execute_method_name );
                my $cce = $cmd->can("command_chain_end");
                $cce                      and $cce                      = $cce->($cmd);
                $cce                      and $command_execute_from_new = $cce->can("command_execute_from_new");
                $command_execute_from_new and $command_execute_from_new = $command_execute_from_new->($cce);
                $command_execute_from_new or $command_execute_method_name = $cce->can('command_execute_method_name');
                $command_execute_method_name
                  and $execute_rv = [ $cce->can( $command_execute_method_name->($cce) )->($cce) ];
            }
            1;
        };
    };

    my $error = $ok ? undef : $@;

    return {
        app        => $app,
        cmd        => $cmd,
        stdout     => $stdout,
        stderr     => $stderr,
        output     => $merged,
        error      => $error,
        execute_rv => $execute_rv,
    };
}

{
    package    # no-index
      MooX::Cmd::Tester::Result;

    sub new
    {
        my ( $class, $arg ) = @_;
        bless $arg => $class;
    }
}

my $res = Package::Stash->new("MooX::Cmd::Tester::Result");
for my $attr (qw(app cmd stdout stderr output error execute_rv exit_code))
{
    $res->add_symbol( '&' . $attr, sub { $_[0]->{$attr} } );
}

{
    package    # no-index
      MooX::Cmd::Tester::Exited;

    sub throw
    {
        my ( $class, $code ) = @_;
        defined $code or $code = 0;
        my $self = ( bless \$code => $class );
        die $self;
    }
}

=head1 NAME

MooX::Cmd::Tester - MooX cli app commands tester

=head1 SYNOPSIS

  use MooX::Cmd::Tester;
  use Test::More;

  use MyFoo;

  # basic tests as instance check, initialization check etc. is done there
  my $rv = test_cmd( MyFoo => [ command(s) option(s) ] );

  like( $rv->stdout, qr/operation successful/, "Command performed" );
  like( $rv->stderr, qr/patient dead/, "Deal with expected command error" );

  is_deeply( $rv->execute_rv, \@expected_return_values, "got what I deserve?" );

  cmp_ok( $rv->exit_code, "==", 0, "Command successful" );

=head1 DESCRIPTION

The test coverage of most CLI apps is somewhere between poor and wretched.
With the same approach as L<App::Cmd::Tester> comes MooX::Cmd::Tester to
ease writing tests for CLI apps.

=head1 FUNCTIONS

=head2 test_cmd

  my $rv = test_cmd( MyApp => \@argv );

test_cmd invokes the app with given argv as if would be invoked from
command line and captures the output, the return values and exit code.

Some minor tests are done to prove whether class matches, execute succeeds,
command_name and command_chain are not totally scrambled.

It returns an object with following attributes/accessors:

=head3 app

Name of package of App

=head3 cmd

Name of executed (1st level) command

=head3 stdout

Content of stdout

=head3 stderr

Content of stderr

=head3 output

Content of merged stdout and stderr

=head3 error

the exception thrown by running the application (if any)

=head3 execute_rv

return values from execute

=head3 exit_code

0 on success, $! when error occurred and $! available, -1 otherwise

=head2 test_cmd_ok

  my $rv = test_cmd_ok( MyApp => \@argv );

Runs C<test_cmd> and expects it being successful - command_name must be in
command_commands, etc.

Returns the same object C<test_cmd> returns.

If an error occurred, no additional test is done (behavior as C<test_cmd>).

=head2 result_class

Builder for result class to use. Returns C<MooX::Cmd::Tester::Result> by
default.

=head1 ACKNOWLEDGEMENTS

MooX::Cmd::Tester is I<inspired> by L<App::Cmd::Tester> from Ricardo Signes.
In fact, I reused the entire design and adopt it to the requirements of
MooX::Cmd.

=head1 LICENSE AND COPYRIGHT

Copyright 2013-2015 Jens Rehsack.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See L<http://dev.perl.org/licenses/> for more information.

=cut

1;