File Coverage

blib/lib/App/Prove/Plugin/TestArgs.pm
Criterion Covered Total %
statement 34 34 100.0
branch 8 8 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 49 50 98.0


line stmt bran cond sub pod time code
1 1     1   449415 use strict;
  1         3  
  1         48  
2 1     1   8 use warnings;
  1         1  
  1         110  
3              
4             package App::Prove::Plugin::TestArgs;
5              
6 1     1   767 use Class::Method::Modifiers qw( install_modifier );
  1         2334  
  1         104  
7 1     1   750 use String::Format qw( stringf );
  1         1081  
  1         95  
8 1     1   812 use YAML::PP qw( LoadFile );
  1         99030  
  1         97  
9              
10             # keeping the following $VERSION declaration on a single line is important
11             #<<<
12 1     1   12 use version 0.9915; our $VERSION = version->declare( '2.1.1' );
  1         23  
  1         11  
13             #>>>
14              
15             my $command_line_test_args;
16             # script means test script
17             my %script_has_alias;
18              
19             sub load {
20 9     9 0 74912 my $plugin_name = shift;
21 9         23 my ( $app_prove, $plugin_args ) = @{ +shift }{ qw( app_prove args ) };
  9         72  
22              
23 6 100       23 $command_line_test_args = defined $app_prove->test_args ? $app_prove->test_args : [];
24             # initialize (overwrite) test args
25 6         84 $app_prove->test_args( {} );
26              
27 6         58 my $config = LoadFile( $plugin_args->[ 0 ] );
28              
29 6 100       106575 my $scripts = exists $config->{ scripts } ? $config->{ scripts } : $config;
30 6         42 for my $script ( keys %$scripts ) {
31 6         12 for ( @{ $scripts->{ $script } } ) {
  6         23  
32 18         39 my ( $alias, $script_args ) = @{ $_ }{ qw( alias args ) };
  18         62  
33 18 100       86 $alias = stringf( $config->{ name }, { a => $alias, s => $script } ) if exists $config->{ name };
34             # update test args ("args" is optional)
35 18 100       670 $app_prove->test_args->{ $alias } = defined $script_args ? $script_args : $command_line_test_args;
36 18         153 push @{ $script_has_alias{ $script } }, [ $script, $alias ];
  18         118  
37             }
38             }
39             }
40              
41             install_modifier 'App::Prove', 'around', '_get_tests' => sub {
42             my $_get_tests_orig = shift;
43             my $app_prove = shift;
44              
45             my @tests;
46             for ( $app_prove->$_get_tests_orig( @_ ) ) {
47             if ( exists $script_has_alias{ $_ } ) {
48             push @tests, @{ $script_has_alias{ $_ } };
49             } else {
50             my $alias = $_;
51             push @tests, [ $_, $alias ];
52             # register remaining test scripts to avoid the excetion
53             # TAP::Harness Can't find test_args for ... at ...
54             $app_prove->test_args->{ $alias } = $command_line_test_args;
55             }
56             }
57             undef $command_line_test_args;
58             undef %script_has_alias;
59              
60             return @tests;
61             };
62              
63             1;