From: Peter Rabbitson Date: Sat, 28 Apr 2012 01:12:15 +0000 (+0200) Subject: Switch to sane subcommand launch in tests X-Git-Tag: v0.11011~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ae99f36743b2709504c030f233222b5e0af39cab;p=dbsrgits%2FSQL-Translator.git Switch to sane subcommand launch in tests --- diff --git a/t/31dumper.t b/t/31dumper.t index abf54ce..17004a6 100644 --- a/t/31dumper.t +++ b/t/31dumper.t @@ -4,7 +4,6 @@ use strict; use File::Temp 'tempfile'; -use File::Spec; use FindBin qw/$Bin/; use IPC::Open3; use SQL::Translator; @@ -38,26 +37,24 @@ my $t = SQL::Translator->new( ); my $output = $t->translate( $file ); - ok( $output, 'Got dumper script' ); +like( $output, qr{DBI->connect\(\s*'$dsn',\s*'$db_user',\s*'$db_pass',}, + 'Script contains correct DSN, db user and password' ); + +like( $output, qr/table_name\s*=>\s*'person',/, 'Found "person" table' ); +unlike( $output, qr/table_name\s*=>\s*'pet',/, 'Skipped "pet" table' ); + my ( $fh, $filename ) = tempfile( 'XXXXXXXX' ); print $fh $output; close $fh or die "Can't close file '$filename': $!"; -open( NULL, ">", File::Spec->devnull ); -my $pid = open3( gensym, \*NULL, \*PH, "$^X -cw $filename" ); -my $res; -while( ) { $res .= $_; } +my $out; +my $pid = open3( undef, $out, undef, $^X, '-cw', $filename ); +my $res = do { local $/; <$out> }; waitpid($pid, 0); like( $res, qr/syntax OK/, 'Generated script syntax is OK' ); -like( $output, qr{DBI->connect\(\s*'$dsn',\s*'$db_user',\s*'$db_pass',}, - 'Script contains correct DSN, db user and password' ); - -like( $output, qr/table_name\s*=>\s*'person',/, 'Found "person" table' ); -unlike( $output, qr/table_name\s*=>\s*'pet',/, 'Skipped "pet" table' ); - unlink $filename; diff --git a/t/70sqlt-diff_script.t b/t/70sqlt-diff_script.t index 138d8bc..753fd1b 100644 --- a/t/70sqlt-diff_script.t +++ b/t/70sqlt-diff_script.t @@ -5,6 +5,7 @@ use strict; use File::Spec::Functions qw(catfile updir tmpdir); use FindBin qw($Bin); +use IPC::Open3; use Test::More; use Test::SQL::Translator qw(maybe_plan); @@ -36,8 +37,7 @@ my $mysql_create2 = (-d "t") : catfile($Bin, "t", @mysql_create2); # Test for differences -my @cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL"); -my $out = `@cmd`; +my $out = _run_cmd ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL"); like($out, qr/CHANGE COLUMN person_id/, "Detected altered 'person_id' field"); like($out, qr/CHANGE COLUMN iq/, "Detected altered 'iq' field"); @@ -56,8 +56,7 @@ like($out, qr/ADD CONSTRAINT FK5302D47D93FE702E_diff/, unlike($out, qr/ADD PRIMARY KEY/, "Primary key looks different when it shouldn't"); # Test for quoted output -@cmd = ($^X, $sqlt_diff, '--quote=\`', "$mysql_create1=MySQL", "$mysql_create2=MySQL"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, '--quote=`', "$mysql_create1=MySQL", "$mysql_create2=MySQL"); like($out, qr/ALTER TABLE `person`/, "Quoted table name"); like($out, qr/CHANGE COLUMN `person_id`/, "Quoted 'person_id' field"); @@ -66,7 +65,14 @@ like($out, qr/CHANGE COLUMN `name`/, "Quoted 'name' field"); like($out, qr/CHANGE COLUMN `age`/, "Quoted 'age' field"); # Test for sameness -@cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); like($out, qr/No differences found/, "Properly detected no differences"); + +sub _run_cmd { + my $out; + my $pid = open3( undef, $out, undef, @_ ); + my $res = do { local $/; <$out> }; + waitpid($pid, 0); + $res; +} diff --git a/t/70sqlt-diff_script_old.t b/t/70sqlt-diff_script_old.t index 957f89f..9f144f8 100644 --- a/t/70sqlt-diff_script_old.t +++ b/t/70sqlt-diff_script_old.t @@ -6,6 +6,7 @@ use strict; use File::Spec::Functions qw(catfile updir tmpdir); use FindBin qw($Bin); use Test::More; +use IPC::Open3; use Test::SQL::Translator qw(maybe_plan); my @script = qw(script sqlt-diff-old); @@ -25,16 +26,14 @@ BEGIN { } ok(-e $sqlt_diff, 'Found sqlt-diff script'); -my @cmd = ($^X, $sqlt_diff, "$create1=SQLite", "$create2=SQLite"); -my $out = `@cmd`; +my $out = _run_cmd ($^X, $sqlt_diff, "$create1=SQLite", "$create2=SQLite"); like($out, qr/-- Target database SQLite is untested/, "Detected 'untested' comment"); like($out, qr/ALTER TABLE person CHANGE iq/, "Detected altered 'iq' field"); like($out, qr/ALTER TABLE person ADD is_rock_star/, "Detected missing rock star field"); -@cmd = ($^X, $sqlt_diff, "$create1=SQLite", "$create1=SQLite"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, "$create1=SQLite", "$create1=SQLite"); like($out, qr/There were no differences/, "Properly detected no differences"); @@ -50,8 +49,7 @@ my $mysql_create2 = (-d "t") : catfile($Bin, "t", @mysql_create2); # Test for differences -@cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create2=MySQL"); unlike($out, qr/-- Target database MySQL is untested/, "Did not detect 'untested' comment"); like($out, qr/ALTER TABLE person CHANGE person_id/, "Detected altered 'person_id' field"); @@ -73,8 +71,7 @@ like($out, qr/ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff/, unlike($out, qr/ALTER TABLE employee ADD PRIMARY KEY/, "Primary key looks different when it shouldn't"); # Test for sameness -@cmd = ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, "$mysql_create1=MySQL", "$mysql_create1=MySQL"); like($out, qr/There were no differences/, "Properly detected no differences"); @@ -89,11 +86,18 @@ my $oracle_create2 = (-d "t") ? catfile($Bin, @oracle_create2) : catfile($Bin, "t", @oracle_create2); -@cmd = ($^X, $sqlt_diff, "$oracle_create1=Oracle", "$oracle_create2=Oracle"); -$out = `@cmd`; +$out = _run_cmd ($^X, $sqlt_diff, "$oracle_create1=Oracle", "$oracle_create2=Oracle"); unlike($out, qr/-- Target database Oracle is untested/, "Did not detect 'untested' comment"); like($out, qr/ALTER TABLE TABLE1 DROP FOREIGN KEY/, "Detected drop foreign key"); like($out, qr/ALTER TABLE TABLE1 ADD CONSTRAINT/, "Detected add constraint"); + +sub _run_cmd { + my $out; + my $pid = open3( undef, $out, undef, @_ ); + my $res = do { local $/; <$out> }; + waitpid($pid, 0); + $res; +}