| 1 | package Mojolicious::Command::coverage; | ||
| 2 | use strict; | ||
| 3 | use warnings FATAL => 'all'; | ||
| 4 | use Mojo::Base 'Mojolicious::Command', -signatures; | ||
| 5 | use Mojo::Util qw(getopt); | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | our $VERSION = "0.0.3"; # Do not update manually | ||
| 10 | |||
| 11 | # Short description | ||
| 12 | has description => 'Start you Mojo app in coverage mode'; | ||
| 13 | |||
| 14 | # Usage message from SYNOPSIS | ||
| 15 | has usage => sub($self) {$self->extract_usage}; | ||
| 16 | |||
| 17 | |||
| 18 | sub run($self, @args) { | ||
| 19 | my $deanonConfig = ""; | ||
| 20 | my $coverageConfig = ""; | ||
| 21 | my $appName = ref $self->app; | ||
| 22 | my $n_args = 0; | ||
| 23 | my @orig_args = @args; | ||
| 24 | |||
| 25 | getopt \@args, [ 'pass_through' ], | ||
| 26 | 'd|deanon-args=s' => sub { | ||
| 27 | $n_args += 2; | ||
| 28 | $deanonConfig = $_[1] | ||
| 29 | }, | ||
| 30 | 'c|cover-args=s' => sub { | ||
| 31 | $n_args += 2; | ||
| 32 | $coverageConfig = $_[1] | ||
| 33 | }; | ||
| 34 | |||
| 35 | if ($deanonConfig eq "" && $self->app->can('deanonymizeConfig')) { | ||
| 36 | $deanonConfig = $self->app->deanonymizeConfig; | ||
| 37 | print "Command::coverage: Taking config from `$appName->deanonymizeConfig` for Devel::Deanonymize \n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | if ($coverageConfig eq "" && $self->app->can('coverageConfig')) { | ||
| 41 | $coverageConfig = $self->app->coverageConfig; | ||
| 42 | print "Command::coverage: Taking config from `$appName->coverageConfig` for Devel::Cover \n"; | ||
| 43 | } | ||
| 44 | # We also have to forward @INC which may have been set-up with Findbin | ||
| 45 | my @Is = ('-I') x scalar @INC; | ||
| 46 | my @includes = map {($Is[$_], $INC[$_])} (0 .. $#INC); # this is basically zip(arr1,arr2) in other languages | ||
| 47 | |||
| 48 | # if there is no custom config, we fallback to default | ||
| 49 | $deanonConfig = $appName if $deanonConfig eq ""; | ||
| 50 | $coverageConfig = "-ignore,t/,-coverage,statement,branch,condition,path,subroutine" if $coverageConfig eq ""; | ||
| 51 | |||
| 52 | my @commandline_inject = (); | ||
| 53 | if ($deanonConfig ne "0") { | ||
| 54 | @commandline_inject = ('perl', @includes, "-MDevel::Cover=$coverageConfig", "-MDevel::Deanonymize=$deanonConfig", $0); | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | @commandline_inject = ('perl', @includes, "-MDevel::Cover=$coverageConfig", $0); | ||
| 58 | } | ||
| 59 | # Merge with application arguments | ||
| 60 | my @full_commandline = (@commandline_inject, @orig_args[$n_args .. $#orig_args]); | ||
| 61 | my $full_commandline_str = join " ", @full_commandline; | ||
| 62 | print "Command::coverage: Starting application with: `$full_commandline_str` \n"; | ||
| 63 | |||
| 64 | exec @full_commandline; | ||
| 65 | } | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | Mojolicious::Command::coverage - Run your application with coverage statistics | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | Usage: APPLICATION coverage [OPTIONS] [APPLICATION_OPTIONS] | ||
| 76 | |||
| 77 | ./myapp.pl coverage [application arguments] | ||
| 78 | |||
| 79 | Options: | ||
| 80 | -h, --help Shows this help | ||
| 81 | -c, --cover-args <args> Options for Devel::Cover | ||
| 82 | -d, --deanon-args <args> Options for Devel::Deanonymize (set to 0 to disable Devel::Deanonymize) | ||
| 83 | |||
| 84 | Application Options | ||
| 85 | - All parameters not matching [OPTIONS] are forwarded to the mojo application | ||
| 86 | |||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | =head1 DESCRIPTION | ||
| 91 | |||
| 92 | Starts your mojo application with L<Devel::Cover> and optionally L<Devel::Deanonymize>. In short this command does the following: | ||
| 93 | |||
| 94 | ./myapp.pl coverage [application arguments] | ||
| 95 | # Is translated to | ||
| 96 | perl -I $INC[0] ... - I $INC[N] -MDevel::Cover=$coverageConfig -MDevel::Deanonymize=$deanonConfig myapp.pl [application arguments] | ||
| 97 | |||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 USAGE | ||
| 102 | |||
| 103 | Runtime configuration for both modules are either passed through the command-line arguments or by specifying | ||
| 104 | C<has> sections in your application: | ||
| 105 | |||
| 106 | has coverageConfig => sub{ | ||
| 107 | return "-ignore,t/,+ignore,prove,+ignore,thirdparty/,-coverage,statement,branch,condition,path,subroutine"; | ||
| 108 | }; | ||
| 109 | |||
| 110 | has deanonymizeConfig => sub { | ||
| 111 | return "<Include-pattern>" | ||
| 112 | }; | ||
| 113 | |||
| 114 | If both are present, command-line args are preferred. If none of them are present, we fall-back to these default values: | ||
| 115 | |||
| 116 | =over 1 | ||
| 117 | |||
| 118 | =item Devel::Cover | ||
| 119 | |||
| 120 | C<-ignore,t/,-coverage,statement,branch,condition,path,subroutine> | ||
| 121 | |||
| 122 | =item Devel::Deanonymize | ||
| 123 | |||
| 124 | C<ref $self->app> ( -> Resolves to your application name) | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | =head1 EXAMPLES | ||
| 129 | |||
| 130 | # (with `has` sections or default config) | ||
| 131 | ./myapp.pl coverage daemon --listen "http://*:8888" | ||
| 132 | |||
| 133 | # (with args) | ||
| 134 | ./myapp.pl coverage -d MyPattern* daemon --listen "http://*:8888" | ||
| 135 | |||
| 136 | |||
| 137 | =head1 AUTHORS | ||
| 138 | |||
| 139 | Since there is a lot of spam flooding my mailbox, I had to put spam filtering in place. If you want to make sure | ||
| 140 | that your email gets delivered into my mailbox, include C<#im_not_a_bot#> in the B<subject!> | ||
| 141 | S<Tobias Bossert E<lt>tobib at cpan.orgE<gt>> | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | =head1 COPYRIGHT AND LICENSE | ||
| 146 | |||
| 147 | MIT License | ||
| 148 | Copyright (c) 2022 Tobias Bossert | ||
| 149 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 150 | of this software and associated documentation files (the "Software"), to deal | ||
| 151 | in the Software without restriction, including without limitation the rights | ||
| 152 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 153 | copies of the Software, and to permit persons to whom the Software is | ||
| 154 | furnished to do so, subject to the following conditions: | ||
| 155 | The above copyright notice and this permission notice shall be included in all | ||
| 156 | copies or substantial portions of the Software. | ||
| 157 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 158 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 159 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 160 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 161 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 162 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 163 | SOFTWARE. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | 1; |