| 1 | package App::Yath::Plugin::pgTAP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.001102'; | ||
| 7 | |||
| 8 | use parent 'App::Yath::Plugin'; | ||
| 9 | use App::Yath::Options; | ||
| 10 | |||
| 11 | option_group {prefix => 'pgtap', category => "Plugin pgTAP"} => sub { | ||
| 12 | |||
| 13 | option dbname => ( | ||
| 14 | type => 's', | ||
| 15 | long_examples => [' DBNAME' ], | ||
| 16 | description => 'The database to which to connect to run the tests. Defaults to the value of the $PGDATABASE environment variable or, if not set, to the system username.', | ||
| 17 | ); | ||
| 18 | |||
| 19 | option username => ( | ||
| 20 | type => 's', | ||
| 21 | long_examples => [' USERNAME' ], | ||
| 22 | description => 'The PostgreSQL username to use to connect to PostgreSQL. If not specified, no username will be used, in which case psql will fall back on either the $PGUSER environment variable or, if not set, the system username.', | ||
| 23 | ); | ||
| 24 | |||
| 25 | option host => ( | ||
| 26 | type => 's', | ||
| 27 | long_examples => [' HOST' ], | ||
| 28 | description => 'Specifies the host name of the machine to which to connect to the PostgreSQL server. If the value begins with a slash, it is used as the directory for the Unix-domain socket. Defaults to the value of the $PGDATABASE environment variable or, if not set, the local host.', | ||
| 29 | ); | ||
| 30 | |||
| 31 | option port => ( | ||
| 32 | type => 's', | ||
| 33 | long_examples => [' PORT' ], | ||
| 34 | default => 5432, | ||
| 35 | description => 'Specifies the TCP port or the local Unix-domain socket file extension on which the server is listening for connections. Defaults to the value of the $PGPORT environment variable or, if not set, to the port specified at the time psql was compiled, usually 5432.', | ||
| 36 | ); | ||
| 37 | |||
| 38 | option pset => ( | ||
| 39 | type => 'm', | ||
| 40 | long_examples => [' PSET' ], | ||
| 41 | description => 'Specifies a hash of printing options in the style of \pset in the psql program. See the psql documentation for details on the supported options.', | ||
| 42 | ); | ||
| 43 | |||
| 44 | option set => ( | ||
| 45 | type => 'm', | ||
| 46 | long_examples => [' SET' ], | ||
| 47 | description => 'Specifies a hash of options in the style of \set in the psql program. See the psql documentation for details on the supported options.', action => \&_pgtap_action | ||
| 48 | ); | ||
| 49 | |||
| 50 | option search_path => ( | ||
| 51 | type => 's', | ||
| 52 | default => 'tap', | ||
| 53 | long_examples => [' public,tap,pg_catalog' ], | ||
| 54 | description => ['The schema search path to use during the execution of the tests.', | ||
| 55 | 'Useful for overriding the default search path if you have pgTAP', | ||
| 56 | 'installed in a schema not included in that search path'] | ||
| 57 | ); | ||
| 58 | |||
| 59 | option suffix => ( | ||
| 60 | type => 's', | ||
| 61 | default => '.pg', | ||
| 62 | long_examples => [' .pg' ], | ||
| 63 | description => 'File suffix of pgTAP test files', | ||
| 64 | action => sub { | ||
| 65 | #warn | ||
| 66 | my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_; | ||
| 67 | |||
| 68 | die "$raw is not an allowed extention" | ||
| 69 | unless $raw =~ /\.(pg|sql|s)/; | ||
| 70 | |||
| 71 | $handler->($slot,$raw); | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | |||
| 75 | option psql => ( | ||
| 76 | type => 's', | ||
| 77 | default => 'psql', | ||
| 78 | long_examples => [' psql' ], | ||
| 79 | description => 'The path to the psql command. Defaults to simply "psql", which should work well enough if it\'s in your path.', | ||
| 80 | ); | ||
| 81 | |||
| 82 | option option => ( | ||
| 83 | type => 'm', | ||
| 84 | |||
| 85 | long_examples => [ ' KEY=VALUE' ], | ||
| 86 | |||
| 87 | description => ['Support prove options syntax for drop-in compatibility', | ||
| 88 | 'where KEY=VALUE is one of:', | ||
| 89 | ' psql=PSQL',' dbname=DBNAME', ' username=USERNAME', | ||
| 90 | ' host=HOST', ' port=PORT', ' pset=OPTION=VALUE', | ||
| 91 | ' set=VAR=VALUE', ' schema=SCHEMA', ' match=REGEX', | ||
| 92 | ' search_path=PATH.', | ||
| 93 | ' See the description of each in --pgtap-KEY' | ||
| 94 | ], | ||
| 95 | |||
| 96 | action => sub { | ||
| 97 | my ($prefix, $field, $raw, $norm, $slot, $settings, $handler) = @_; | ||
| 98 | |||
| 99 | return if $raw !~ /^(username|host|port|suffix|dbname|pset|set|psql|schema)=.+/; | ||
| 100 | |||
| 101 | my ($option,$value) = split /=/, $raw, 2; | ||
| 102 | |||
| 103 | if ( $option =~ /p?set/ ) { | ||
| 104 | push @{$settings->pgtap->$option}, $value | ||
| 105 | } elsif ( $option =~ /suffix/ ) { | ||
| 106 | $settings->pgtap->suffix = $value | ||
| 107 | if $value =~ /\.(pg|sql|s)/; | ||
| 108 | } else { | ||
| 109 | $settings->pgtap->$option = $value; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | ); | ||
| 113 | }; | ||
| 114 | |||
| 115 | use Test2::Harness::TestFile; | ||
| 116 | |||
| 117 | # Munge the file list found | ||
| 118 | # Trying to run: 'psql $args $tf->file' | ||
| 119 | |||
| 120 | sub munge_files { | ||
| 121 | my ($plugin, $testfiles, $settings) = @_; | ||
| 122 | |||
| 123 | for my $tf (@$testfiles) { | ||
| 124 | if ($tf->file =~ m/[.]pg$/) { | ||
| 125 | my @args = ('--no-psqlrc', '--no-align', '--quiet', | ||
| 126 | '--pset', 'pager=off', '--pset', 'tuples_only=true', | ||
| 127 | '--set', 'ON_ERROR_STOP=1'); | ||
| 128 | #TODO: Schema? match? | ||
| 129 | for (qw(username dbname host port)) { | ||
| 130 | push @args, "--$_", $settings->pgtap->$_ | ||
| 131 | if defined $settings->pgtap->$_; | ||
| 132 | } | ||
| 133 | foreach (@{$settings->pgtap->pset}) { | ||
| 134 | push @args, '--pset', $_; | ||
| 135 | } | ||
| 136 | foreach (@{$settings->pgtap->set}) { | ||
| 137 | push @args, '--set', $_; | ||
| 138 | } | ||
| 139 | $tf = Test2::Harness::TestFile->new( | ||
| 140 | file => $tf->file, | ||
| 141 | comment => '--', | ||
| 142 | job_class => 'Test2::Harness::Runner::Job::pgTAP', | ||
| 143 | relative => $tf->relative, | ||
| 144 | queue_args => [ | ||
| 145 | command => $settings->pgtap->psql, | ||
| 146 | non_perl => 1, | ||
| 147 | +test_args => [@args, ( '--file', $tf->relative )] | ||
| 148 | ] | ||
| 149 | ); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | use File::Basename; | ||
| 155 | |||
| 156 | # Claim our files | ||
| 157 | sub claim_file { | ||
| 158 | my ($plugin, $item, $settings) = @_; | ||
| 159 | my ($filename, $dirs, $suffix0) = fileparse($item); | ||
| 160 | return if -d $item; | ||
| 161 | my $suffix = $settings->pgtap->suffix; | ||
| 162 | return $suffix eq $suffix0 | ||
| 163 | ? Test2::Harness::TestFile->new(file => $item) : undef; | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | __END__ |