make t/05testapp.t silent and fix remaining Win32 issues
[catagits/Catalyst-Model-DBIC-Schema.git] / t / 05testapp.t
index bc67a15..5862e67 100644 (file)
@@ -4,12 +4,12 @@ use FindBin;
 use File::Spec::Functions qw/catfile catdir/;
 use File::Find;
 use Config;
+use DBI;
+use IPC::Open3 'open3';
 
 plan skip_all => 'Enable this optional test with $ENV{C_M_DBIC_SCHEMA_TESTAPP}'
     unless $ENV{C_M_DBIC_SCHEMA_TESTAPP};
 
-# XXX this test needs a re-write to fully test the current set of capabilities...
-
 my $test_params = [
     [ 'TestSchema', 'DBIC::Schema', '' ],
     [ 'TestSchemaDSN', 'DBIC::Schema', qw/fakedsn fakeuser fakepass/, '{ AutoCommit => 1 }' ],
@@ -26,11 +26,11 @@ my $catlib_dir = catdir ($cat_dir, 'lib');
 my $schema_dir = catdir ($catlib_dir, 'TestSchemaDSN');
 my $creator    = catfile($cat_dir, 'script', 'testapp_create.pl');
 my $model_dir  = catdir ($catlib_dir, 'TestApp', 'Model');
-my $db         = catdir ($cat_dir, 'testdb.db');
+my $db         = catfile($cat_dir, 'testdb.db');
 
 my $catalyst_pl;
 
-foreach my $bin (split /[$Config{path_sep}:]/, $ENV{PATH}) {
+foreach my $bin (split /(?:$Config{path_sep}|:)/, $ENV{PATH}) {
    my $file = catfile($bin, 'catalyst.pl');
    if (-f $file) {
       $catalyst_pl = $file;
@@ -41,38 +41,42 @@ foreach my $bin (split /[$Config{path_sep}:]/, $ENV{PATH}) {
 plan skip_all => 'catalyst.pl not found' unless $catalyst_pl;
 
 chdir($test_dir);
-system("$^X $catalyst_pl TestApp");
+silent_exec("$^X $catalyst_pl TestApp");
 chdir($cat_dir);
 
 # create test db
-open my $sql, '|-', "sqlite3 $db" or die $!;
-print $sql <<'EOF';
+my $dbh = DBI->connect("dbi:SQLite:$db", '', '', {
+   RaiseError => 1, PrintError => 0
+});
+$dbh->do(<<'EOF');
 CREATE TABLE users (                       
-        id            INTEGER PRIMARY KEY, 
-        username      TEXT,                
-        password      TEXT,                
-        email_address TEXT,                
-        first_name    TEXT,                
-        last_name     TEXT,                
-        active        INTEGER              
+        id            INTEGER PRIMARY KEY,
+        username      TEXT,
+        password      TEXT,
+        email_address TEXT,
+        first_name    TEXT,
+        last_name     TEXT,
+        active        INTEGER
 );
+EOF
+$dbh->do(<<'EOF');
 CREATE TABLE roles (
         id   INTEGER PRIMARY KEY,
         role TEXT
 );
 EOF
-close $sql;
+$dbh->disconnect;
 
 foreach my $tparam (@$test_params) {
    my ($model, $helper, @args) = @$tparam;
 
    cleanup_schema();
 
-   system($^X, "-I$blib_dir", $creator, 'model', $model, $helper, $model, @args);
+   silent_exec($^X, "-I$blib_dir", $creator, 'model', $model, $helper, $model, @args);
 
    my $model_path = catfile($model_dir, $model . '.pm');
    ok( -f $model_path, "$model_path is a file" );
-   my $compile_rv = system("$^X -I$blib_dir -I$catlib_dir -c $model_path");
+   my $compile_rv = silent_exec($^X, "-I$blib_dir", "-I$catlib_dir", "-c", $model_path);
    ok($compile_rv == 0, "perl -c $model_path");
 
    if (grep /create=static/, @args) {
@@ -97,7 +101,7 @@ foreach my $tparam (@$test_params) {
 {
    cleanup_schema();
 
-   system($^X, "-I$blib_dir", $creator, 'model',
+   silent_exec($^X, "-I$blib_dir", $creator, 'model',
       'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
       'create=static', 'use_moose=0', 'dbi:SQLite:testdb.db'
    );
@@ -111,7 +115,7 @@ foreach my $tparam (@$test_params) {
       unlike $code, qr/__PACKAGE__->meta->make_immutable;\n/, 'non use_moose=1 schema';
    }
 
-   system($^X, "-I$blib_dir", $creator, 'model',
+   silent_exec($^X, "-I$blib_dir", $creator, 'model',
       'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
       'create=static', 'dbi:SQLite:testdb.db'
    );
@@ -131,7 +135,7 @@ foreach my $tparam (@$test_params) {
 {
    cleanup_schema();
 
-   system($^X, "-I$blib_dir", $creator, 'model',
+   silent_exec($^X, "-I$blib_dir", $creator, 'model',
       'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
       'create=static', 'dbi:SQLite:testdb.db'
    );
@@ -148,7 +152,7 @@ foreach my $tparam (@$test_params) {
    print $fh "hlagh\n";
    close $fh;
 
-   system($^X, "-I$blib_dir", $creator, 'model',
+   silent_exec($^X, "-I$blib_dir", $creator, 'model',
       'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
       'create=static', 'dbi:SQLite:testdb.db'
    );
@@ -173,7 +177,7 @@ sub rm_rf {
 
 sub cleanup_schema {
    return unless -d $schema_dir;
-   finddepth(\&rm_rf, $schema_dir);
+   finddepth({ wanted => \&rm_rf, no_chdir => 1 }, $schema_dir);
    unlink "${schema_dir}.pm";
 }
 
@@ -206,10 +210,21 @@ sub result_files {
    return @results;
 }
 
+sub silent_exec {
+   local *NULL;
+   open NULL, '+<', File::Spec->devnull;
+
+   my $pid = open3('<&NULL', '>&NULL', '>&NULL', @_);
+
+   waitpid $pid, 0;
+
+   return $?;
+}
+
 END {
     if ($ENV{C_M_DBIC_SCHEMA_TESTAPP}) {
         chdir($test_dir);
-        finddepth(\&rm_rf, $cat_dir);
+        finddepth({ wanted => \&rm_rf, no_chdir => 1 }, $cat_dir);
     }
 }