| 1 | package App::ape::test; | ||||||||||||||||||||
| 2 | 6 | $App::ape::test::VERSION = '0.001'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: Run a test manually and upload the results to Elasticsearch | ||||||||||||||||||||
| 5 | # PODNAME: App::Ape::test | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 6 | ▶ | use Getopt::Long qw{GetOptionsFromArray}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 6 | ▶ | use App::Prove::Elasticsearch::Utils; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | ▶ | use Pod::Usage; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 6 | ▶ | use File::Temp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 6 | ▶ | use POSIX qw{strftime}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 6 | ▶ | use File::Basename qw{basename dirname}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 18 | 4 | my ($class, @args) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 4 | my (%options, @conf, $help); | |||||||||||||||||||
| 21 | GetOptionsFromArray( | ||||||||||||||||||||
| 22 | \@args, | ||||||||||||||||||||
| 23 | 'defect=s@' => \$options{defects}, | ||||||||||||||||||||
| 24 | 'configure=s@' => \@conf, | ||||||||||||||||||||
| 25 | 'status=s' => \$options{status}, | ||||||||||||||||||||
| 26 | 'elapsed=s' => \$options{elapsed}, | ||||||||||||||||||||
| 27 | 'body=s' => \$options{body}, | ||||||||||||||||||||
| 28 | 4 | 'help' => \$help | |||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | #Deliberately exiting here, as I "unit" test this as the binary | ||||||||||||||||||||
| 32 | 4 | ▶ | pod2usage(0) if $help; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 4 | ▶ | if (!$options{status}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 1 | pod2usage( | |||||||||||||||||||
| 36 | -exitval => "NOEXIT", | ||||||||||||||||||||
| 37 | -msg => "Insufficient arguments. You must pass a status.", | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | 1 | return 1; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | ▶ | if (!scalar(@args)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 1 | pod2usage( | |||||||||||||||||||
| 44 | -exitval => "NOEXIT", | ||||||||||||||||||||
| 45 | -msg => "Insufficient arguments. You must pass at least one test.", | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | 1 | return 2; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | my $conf = App::Prove::Elasticsearch::Utils::process_configuration(@conf); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | ▶ | if ( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | scalar( | ||||||||||||||||||||
| 54 | grep { | ||||||||||||||||||||
| 55 | 2 | my $subj = $_; | |||||||||||||||||||
| 56 | 2 | grep { $subj eq $_ } qw{server.host server.port} | |||||||||||||||||||
| 57 | } keys(%$conf) | ||||||||||||||||||||
| 58 | ) != 2 | ||||||||||||||||||||
| 59 | ) { | ||||||||||||||||||||
| 60 | 1 | pod2usage( | |||||||||||||||||||
| 61 | -exitval => "NOEXIT", | ||||||||||||||||||||
| 62 | -msg => | ||||||||||||||||||||
| 63 | "Insufficient information provided to associate defect with test results to elasticsearch", | ||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||
| 65 | 1 | return 3; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | $0 = "ape test: starting up"; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | my $self = {options => \%options}; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | $self->{indexer} = App::Prove::Elasticsearch::Utils::require_indexer($conf); | |||||||||||||||||||
| 73 | 1 | &{\&{$self->{indexer} . "::check_index"}}($conf); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | my $searcher = App::Prove::Elasticsearch::Utils::require_searcher($conf); | |||||||||||||||||||
| 76 | 1 | $self->{searcher} = $searcher->new($conf, $self->{indexer}); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | $self->{versioner} = | ||||||||||||||||||||
| 79 | 1 | App::Prove::Elasticsearch::Utils::require_versioner($conf); | |||||||||||||||||||
| 80 | 1 | $self->{version} = &{\&{$self->{versioner} . "::get_version"}}(); | |||||||||||||||||||
| 81 | my @cases = map { | ||||||||||||||||||||
| 82 | 1 | { | |||||||||||||||||||
| 83 | name => $_, | ||||||||||||||||||||
| 84 | 1 | version => &{\&{$self->{versioner} . "::get_file_version"}}($_) | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } @args; | ||||||||||||||||||||
| 87 | 1 | $self->{cases} = \@cases; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1 | $self->{blamer} = App::Prove::Elasticsearch::Utils::require_blamer($conf); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | $self->{platformer} = | ||||||||||||||||||||
| 92 | 1 | App::Prove::Elasticsearch::Utils::require_platformer($conf); | |||||||||||||||||||
| 93 | 1 | $self->{platforms} = &{\&{$self->{platformer} . "::get_platforms"}}(); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | return bless($self, $class); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=20.1
Pod
documented
| |||||||||||||||||||||
| 99 | 1 | my ($self) = @_; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 1 | foreach my $case (@{$self->{cases}}) { | |||||||||||||||||||
| 102 | 2 | $0 = "ape test : $case->{name}"; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 2 | ▶ | if (!-f $case->{name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 2 | print "No such case $case->{name} on filesystem, skipping.\n"; | |||||||||||||||||||
| 106 | 2 | next; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | my $executor = | ||||||||||||||||||||
| 109 | 0 | &{\&{$self->{blamer} . "::get_responsible_party"}}($case->{name}); | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | my $occurred = time(); | |||||||||||||||||||
| 112 | 0 | my $output = $self->get_test_commentary(); | |||||||||||||||||||
| 113 | 0 | my $elapsed = time() - $occurred; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | my $upload = { | ||||||||||||||||||||
| 116 | body => $output, | ||||||||||||||||||||
| 117 | elapsed => $elapsed, | ||||||||||||||||||||
| 118 | occurred => strftime("%Y-%m-%d %H:%M:%S", localtime($occurred)), | ||||||||||||||||||||
| 119 | status => $self->{options}{status}, | ||||||||||||||||||||
| 120 | platform => $self->{platforms}, | ||||||||||||||||||||
| 121 | executor => $executor, | ||||||||||||||||||||
| 122 | version => $self->{version}, | ||||||||||||||||||||
| 123 | test_version => $case->{version}, | ||||||||||||||||||||
| 124 | name => basename($case->{name}), | ||||||||||||||||||||
| 125 | 0 | path => dirname($case->{name}), | |||||||||||||||||||
| 126 | }; | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | eval { &{\&{$self->{indexer} . "::index_results"}}($upload) }; | |||||||||||||||||||
| 129 | 0 | ▶ | print "$@\n" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 1 | $0 = "ape test: shutting down"; | |||||||||||||||||||
| 132 | 1 | return 0; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | ▶ | sub get_test_commentary { | ||||||||||||||||||
|
Subroutine
get_test_commentary: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | my ($fh, $filename); | |||||||||||||||||||
| 139 | 0 | ▶ | if ($self->{options}->{body}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 0 | $filename = $self->{options}->{body}; | |||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||
| 142 | 0 | ▶ | my $editor = $ENV{EDITOR} || $ENV{VISUAL}; | ||||||||||||||||||
|
Condition0%0/3
$ENV{'EDITOR'} || $ENV{'VISUAL'}
MC/DC0%0/2
$ENV{'EDITOR'} || $ENV{'VISUAL'}
$ENV{'EDITOR'}
$ENV{'VISUAL'}
Truth table0%0/3
A || B
A= $ENV{'EDITOR'}B= $ENV{'VISUAL'}
| |||||||||||||||||||||
| 143 | 0 | ▶ | die | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | "Either pass --body or have EDITOR or VISUAL set to edit your test result body." | ||||||||||||||||||||
| 145 | unless $editor; | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | (undef, $filename) = File::Temp::tempfile(); | |||||||||||||||||||
| 148 | 0 | my $pid = fork(); | |||||||||||||||||||
| 149 | 0 | ▶ | if (!$pid) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 0 | exec($editor, $filename); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 0 | waitpid($pid, 0); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | ▶ | die "No such file $filename!" unless -f $filename; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | 0 | my $out; | |||||||||||||||||||
| 157 | 0 | open($fh, '<', $filename); | |||||||||||||||||||
| 158 | 0 | ▶ | while (<$fh>) { $out .= $_ } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | close $fh; | |||||||||||||||||||
| 160 | 0 | ▶ | die "Empty test update inside of $filename!" unless $out; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | return $out; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 6 | 1; | |||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||