Changeset 121

Show
Ignore:
Timestamp:
03/01/08 20:31:50 (10 months ago)
Author:
iteman
Message:

- Added support for autotest by -a option. At this time only Windows is supported. (Ticket #10)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/Stagehand/TestRunner.php

    r120 r121  
    9494        $argv = Console_Getopt::readPHPArgv(); 
    9595        array_shift($argv); 
    96         $allOptions = Console_Getopt::getopt2($argv, 'hVRcp:'); 
     96        $allOptions = Console_Getopt::getopt2($argv, 'hVRcp:a'); 
    9797        if (PEAR::isError($allOptions)) { 
    9898            echo 'ERROR: ' . preg_replace('/^Console_Getopt: /', '', $allOptions->getMessage()) . "\n"; 
     
    104104        $isRecursive = false; 
    105105        $color = false; 
     106        $enableAutotest = false; 
     107        $preload = false; 
     108        $preloadFile = null; 
    106109        foreach ($allOptions as $options) { 
    107110            if (!count($options)) { 
     
    126129                        } 
    127130                        break; 
     131                    case 'p': 
     132                        $preload = true; 
     133                        $preloadFile = $option[1]; 
     134                        break; 
     135                    case 'a': 
     136                        $enableAutotest = true; 
     137                        break; 
    128138                    } 
    129139                } else { 
     
    133143        } 
    134144 
    135         include_once "Stagehand/TestRunner/Collector/$testRunnerName.php"; 
    136         $className = "Stagehand_TestRunner_Collector_$testRunnerName"; 
    137         $collector = new $className($directory, $isRecursive); 
    138  
    139         try { 
    140             $suite = $collector->collect(); 
    141         } catch (Stagehand_TestRunner_Exception $e) { 
    142             echo 'ERROR: ' . $e->getMessage() . "\n"; 
    143             self::_displayUsage(); 
    144             return 1; 
     145        if (!$enableAutotest) { 
     146            include_once "Stagehand/TestRunner/Collector/$testRunnerName.php"; 
     147            $className = "Stagehand_TestRunner_Collector_$testRunnerName"; 
     148            $collector = new $className($directory, $isRecursive); 
     149 
     150            try { 
     151                $suite = $collector->collect(); 
     152            } catch (Stagehand_TestRunner_Exception $e) { 
     153                echo 'ERROR: ' . $e->getMessage() . "\n"; 
     154                self::_displayUsage(); 
     155                return 1; 
     156            } 
     157 
     158            include_once "Stagehand/TestRunner/Runner/$testRunnerName.php"; 
     159            $className = "Stagehand_TestRunner_Runner_$testRunnerName"; 
     160            $runner = new $className(); 
     161            $runner->run($suite, $color); 
     162        } else { 
     163            if (php_uname('s') !== 'Windows NT') { 
     164                echo "ERROR: -a option is not supported for your platform.\n"; 
     165                self::_displayUsage(); 
     166                return 1; 
     167            } 
     168 
     169            $directory = realpath($directory); 
     170            if ($directory === false || !is_dir($directory)) { 
     171                echo "ERROR: The specified path [ $directory ] is not found or not a directory.\n"; 
     172                self::_displayUsage(); 
     173                return 1; 
     174            } 
     175 
     176            if (array_key_exists('_', $_SERVER)) { 
     177                $command = $_SERVER['_']; 
     178            } else { 
     179                $command = $_SERVER['argv'][0]; 
     180            } 
     181 
     182            $options = array(); 
     183            if (preg_match('!^/cygdrive/([a-z])/(.+)!', $command, $matches)) { 
     184                $command = "{$matches[1]}:\\" . str_replace('/', '\\', $matches[2]); 
     185            } 
     186 
     187            if (preg_match('/\.bat$/', $command)) { 
     188                $command = str_replace('/', '\\', $command); 
     189            } 
     190 
     191            if (!preg_match('/(?:test|spec)runner(?:\.bat)?$/', $command)) { 
     192                $configFile = get_cfg_var('cfg_file_path'); 
     193                if ($configFile !== false) { 
     194                    $options[] = '-c'; 
     195                    $options[] = dirname($configFile); 
     196                } 
     197 
     198                $options[] = $_SERVER['argv'][0]; 
     199            } 
     200 
     201            $options[] = '-R'; 
     202 
     203            if ($preload) { 
     204                $options[] = "-p $preloadFile"; 
     205            } 
     206 
     207            if ($color) { 
     208                $options[] = '-c'; 
     209            } 
     210 
     211            $options[] = $directory; 
     212 
     213            define('FILE_NOTIFY_CHANGE_FILE_NAME',    0x00000001); 
     214            define('FILE_NOTIFY_CHANGE_DIR_NAME',     0x00000002); 
     215            define('FILE_NOTIFY_CHANGE_ATTRIBUTES',   0x00000004); 
     216            define('FILE_NOTIFY_CHANGE_SIZE',         0x00000008); 
     217            define('FILE_NOTIFY_CHANGE_LAST_WRITE',   0x00000010); 
     218            define('FILE_NOTIFY_CHANGE_LAST_ACCESS',  0x00000020); 
     219            define('FILE_NOTIFY_CHANGE_CREATION',     0x00000040); 
     220            define('FILE_NOTIFY_CHANGE_EA',           0x00000080); 
     221            define('FILE_NOTIFY_CHANGE_SECURITY',     0x00000100); 
     222            define('FILE_NOTIFY_CHANGE_STREAM_NAME',  0x00000200); 
     223            define('FILE_NOTIFY_CHANGE_STREAM_SIZE',  0x00000400); 
     224            define('FILE_NOTIFY_CHANGE_STREAM_WRITE', 0x00000800); 
     225            define('INFINITE', 0xffffffff); 
     226 
     227            $kernel32 = wb_load_library('kernel32.dll'); 
     228            while (true) { 
     229                $h1 = wb_call_function(wb_get_function_address('FindFirstChangeNotification', $kernel32), 
     230                                       array($directory, 
     231                                             1, 
     232                                             FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_EA | FILE_NOTIFY_CHANGE_SECURITY) 
     233                                       ); 
     234 
     235                print "Waiting for changes in the directory [ $directory ] ...\n"; 
     236                $h2 = wb_call_function(wb_get_function_address('WaitForSingleObject', $kernel32), 
     237                                       array($h1, INFINITE) 
     238                                       ); 
     239                passthru("$command " . implode(' ', $options), $result); 
     240 
     241                $h3 = wb_call_function(wb_get_function_address('FindCloseChangeNotification', $kernel32), 
     242                                       array($h1) 
     243                                       ); 
     244 
     245                if ($result !== 0) { 
     246                    return 1; 
     247                } 
     248            } 
    145249        } 
    146  
    147         include_once "Stagehand/TestRunner/Runner/$testRunnerName.php"; 
    148         $className = "Stagehand_TestRunner_Runner_$testRunnerName"; 
    149         $runner = new $className(); 
    150         $runner->run($suite, $color); 
    151250 
    152251        return 0; 
     
    182281  -c        color the result of a test runner run 
    183282  -p <file> preload <file> as a PHP script 
     283  -a        watch for changes in a specified directory and run all tests in 
     284            the directory recursively when changes are detected (autotest) 
    184285 
    185286With no [directory or file], run all tests in the current directory.