Created
July 13, 2015 21:51
-
-
Save Ovid/295df2e46d66cb06f4a9 to your computer and use it in GitHub Desktop.
Testing Your Sqitch Changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use autodie ':all'; | |
use Test::Most; | |
use FindBin; | |
use Veure::Config 'config'; | |
use File::Spec::Functions 'catfile'; | |
use Capture::Tiny 'capture'; | |
bail_on_fail; | |
# if you need to, you can override your user for this test in veure_local.conf | |
my $PSQL_USER = config(qw/database tsqitcht user/) | |
// config(qw/database test user/); | |
my $PSQL_ROOT_USER = config(qw/database tsqitcht root_user/) | |
// config(qw/database test root_user/); | |
my $TEST_DATABASE = 'veure_sqitch_test'; | |
END { | |
system( | |
"dropdb -U $PSQL_ROOT_USER $TEST_DATABASE #2>/dev/null" | |
); | |
} | |
use aliased 'App::Sqitch'; | |
use aliased 'App::Sqitch::Config'; | |
use aliased 'App::Sqitch::Target'; | |
my $config = Config->new; | |
my $sqitch = Sqitch->new( options => {}, config => $config ); | |
my $target = Target->new( sqitch => $sqitch, name => 'veure_test' ); | |
my $plan = $target->plan; | |
my ( @deploy, @verify, @revert ); | |
my @sqitch_dir = ( $FindBin::Bin, '..', 'sqitch' ); | |
while ( my $change = $plan->next ) { | |
my @change = $change->path_segments; | |
push @deploy => catfile( @sqitch_dir, 'deploy', @change ); | |
push @verify => catfile( @sqitch_dir, 'verify', @change ); | |
unshift @revert => catfile( @sqitch_dir, 'revert', @change ); | |
} | |
system( | |
"createdb -U $PSQL_ROOT_USER -O $PSQL_USER $TEST_DATABASE #2>/dev/null" | |
); | |
for my $i ( 0 .. $#deploy ) { | |
my $change = $deploy[$i]; | |
my $verify = $verify[$i]; | |
psql_ok("psql -q -U $PSQL_USER $TEST_DATABASE < $change"); | |
psql_ok("psql -q -U $PSQL_USER $TEST_DATABASE < $verify"); | |
} | |
pop @verify; | |
for my $revert (@revert) { | |
my $verify = pop @verify; | |
psql_ok("psql -q -U $PSQL_USER $TEST_DATABASE < $revert"); | |
if ($verify) { | |
psql_ok("psql -q -U $PSQL_USER $TEST_DATABASE < $verify"); | |
} | |
} | |
sub psql_ok { | |
my $command = shift; | |
local $Test::Builder::Level = $Test::Builder::Level + 1; | |
my ( $stdout, $stderr, $exit ) = capture { system($command) }; | |
if ( $stderr =~ /ERROR:/ ) { | |
fail "Error running ($command): $stderr"; | |
} | |
else { | |
pass "Successfully ran ($command)"; | |
} | |
} | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment