File Coverage

lib/App/Duppy.pm
Criterion Covered Total %
statement 33 90 36.6
branch 0 20 0.0
condition 0 3 0.0
subroutine 11 18 61.1
pod 0 3 0.0
total 44 134 32.8


line stmt bran cond sub pod time code
1             package App::Duppy;
2              
3             # ABSTRACT: a wrapper around casperjs to pass test configurations as json files
4 3     3   89383 use strict;
  3         9  
  3         103  
5 3     3   19 use warnings;
  3         7  
  3         101  
6 3     3   17093 use Moo;
  3         80798  
  3         19  
7 3     3   10711 use MooX::Options;
  3         6359  
  3         20  
8 3     3   1760984 use IPC::Run qw/run new_chunker/;
  3         136854  
  3         235  
9 3     3   9948 use File::Which;
  3         3709  
  3         203  
10 3     3   3043 use IO::All;
  3         42893  
  3         38  
11 3     3   257 use JSON;
  3         7  
  3         32  
12 3     3   3457 use DDP;
  3         141752  
  3         31  
13 3     3   144 use Carp;
  3         5  
  3         220  
14 3     3   19 use Try::Tiny;
  3         5  
  3         8146  
15              
16             option 'test' => (
17             is => 'rw',
18             required => 1,
19             format => 's@',
20             doc =>
21             'Test option: one ore more json file(s) containing the casperjs tests to perform'
22             );
23              
24             option 'casper_path' => (
25             is => 'rw',
26             format => 's',
27             doc => 'Path to casperjs, if not standard',
28             predicate => 'has_casper_path',
29             );
30              
31             has 'tests' => ( is => 'lazy', );
32              
33             has 'buffer_return' => ( is => 'rw', default => sub {''});
34              
35             has 'silent_run' => (is => 'rw', default => sub { 0 });
36              
37             sub _build_tests {
38 0     0     my $self = shift;
39 0           my $ret = {};
40 0           foreach my $file ( @{ $self->test } ) {
  0            
41 0 0         if ( io($file)->exists ) {
42 0           my $content = io($file)->slurp;
43             try {
44 0     0     $ret->{$file} = decode_json($content);
45             }
46             catch {
47 0     0     carp "'$file' is not valid: $_";
48 0           };
49             }
50             else {
51 0           carp "'$file' does not exist";
52             }
53             }
54 0           return $ret;
55             }
56              
57             sub run_casper {
58 0     0 0   my $self = shift;
59 0           my $full_path;
60 0 0         $self->buffer_return('') if ($self->buffer_return);
61 0 0         if ( $self->has_casper_path ) {
62 0 0 0       if ( -f $self->casper_path and -x $self->casper_path ) {
63 0           $full_path = $self->casper_path;
64             }
65             else {
66 0           croak sprintf(
67             q{'%s' is not an executable file},
68             $self->casper_path
69             );
70             }
71             }
72             else {
73 0           $full_path = which('casperjs');
74             }
75 0           $self->silent_run (shift @_);
76 0           foreach my $test ( keys %{ $self->tests } ) {
  0            
77 0           my $param_spec = $self->transform_arg_spec( $self->tests->{$test} );
78 0           unshift @{ $param_spec->{cmd} }, $full_path;
  0            
79 0           push @{ $param_spec->{cmd} }, "test", @{ $param_spec->{paths} };
  0            
  0            
80 0           print "\n\n\n";
81 0           print "="x10;
82 0           print "> Running test from file $test... \n\n\n";
83 0           run $param_spec->{cmd}, '>', new_chunker("\n"),$self->lines_handler;
84             }
85              
86 0 0         if ($self->silent_run) {
87 0           return $self->buffer_return;
88             }
89             else {
90 0           return;
91             }
92             }
93              
94             sub lines_handler {
95 0     0 0   my ($self,$in_ref,$out_ref) = @_;
96             return sub {
97 0     0     my ($out) = @_;
98 0 0         if ($out) {
99 0 0         if ($self->silent_run) {
100 0           $self->buffer_return($self->buffer_return.$out);
101             }
102             else {
103 0           print $out;
104             }
105             }
106             }
107 0           }
108              
109             sub transform_arg_spec {
110 0     0 0   my $self = shift;
111 0           my $ref_params = shift;
112 0           my $ret = {};
113 0           my %params = %{$ref_params};
  0            
114 0           $ret->{paths} = delete $params{paths};
115 0           while ( my ( $k, $v ) = each %params ) {
116 0 0         if ( ref($v) eq 'ARRAY' ) {
117 0           $v = join( ',', @{$v} );
  0            
118             }
119             else {
120 0 0         $v = "true" if ( $v eq '1' );
121 0 0         $v = "false" if ( $v eq '0' );
122             }
123 0           push @{ $ret->{cmd} }, "--$k=$v";
  0            
124             }
125 0           return $ret;
126             }
127              
128              
129              
130             1;
131              
132             __END__