File Coverage

blib/lib/Pg/CLI/Role/HasVersion.pm
Criterion Covered Total %
statement 20 25 80.0
branch 1 6 16.6
condition n/a
subroutine 7 8 87.5
pod n/a
total 28 39 71.7


line stmt bran cond sub pod time code
1             package Pg::CLI::Role::HasVersion;
2              
3 5     5   3423 use strict;
  5         13  
  5         160  
4 5     5   45 use warnings;
  5         9  
  5         134  
5 5     5   26 use namespace::autoclean;
  5         9  
  5         35  
6              
7             our $VERSION = '0.14';
8              
9 5     5   429 use IPC::Run3 qw( run3 );
  5         19  
  5         288  
10 5     5   33 use MooseX::Types::Moose qw( Str );
  5         13  
  5         38  
11              
12 5     5   24936 use Moose::Role;
  5         13  
  5         34  
13              
14             has version => (
15             is => 'ro',
16             isa => Str,
17             init_arg => '_version', # for testing
18             lazy => 1,
19             builder => '_build_version',
20             );
21              
22             has two_part_version => (
23             is => 'ro',
24             isa => Str,
25             init_arg => undef,
26             lazy => 1,
27             builder => '_build_two_part_version',
28             );
29              
30             sub _build_version {
31 0     0   0 my $self = shift;
32              
33 0         0 my ( $output, $error );
34              
35 0         0 run3(
36             [ $self->executable(), '--version' ],
37             \undef,
38             \$output,
39             \$error,
40             );
41              
42 0 0       0 die $error if $error;
43              
44             ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
45             #
46             # https://github.com/Perl-Critic/Perl-Critic/issues/533
47 0 0       0 return $1
48             if $output =~ /\Qpsql (PostgreSQL) \E([0-9]+\.[0-9]+(?:\.[0-9]+)?)/;
49             }
50              
51             sub _build_two_part_version {
52 14     14   32 my $self = shift;
53              
54             ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
55 14 50       362 return $1 if $self->version() =~ /^([0-9]+.[0-9]+)/;
56             }
57              
58             1;