Changeset 126

Show
Ignore:
Timestamp:
03/02/08 22:48:47 (10 months ago)
Author:
iteman
Message:

- Refactored.

Files:
1 modified

Legend:

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

    r125 r126  
    3838require_once 'Console/Getopt.php'; 
    3939require_once 'Stagehand/TestRunner/AlterationMonitor.php'; 
     40require_once 'Stagehand/TestRunner/Exception.php'; 
    4041 
    4142// {{{ Stagehand_TestRunner 
     
    9394        } 
    9495 
     96        try { 
     97            $config = self::_parseOptions(); 
     98            if (is_null($config)) { 
     99                return 1; 
     100            } 
     101 
     102            if (!$config->enableAutotest) { 
     103                self::_runTests($testRunnerName, $config); 
     104            } else { 
     105                self::_monitorAlteration($config); 
     106            } 
     107        } catch (Stagehand_TestRunner_Exception $e) { 
     108            echo 'ERROR: ' . $e->getMessage() . "\n"; 
     109            self::_displayUsage(); 
     110            return 1; 
     111        } 
     112 
     113        return 0; 
     114    } 
     115 
     116    /**#@-*/ 
     117 
     118    /**#@+ 
     119     * @access protected 
     120     */ 
     121 
     122    /**#@-*/ 
     123 
     124    /**#@+ 
     125     * @access private 
     126     * @static 
     127     */ 
     128 
     129    // }}} 
     130    // {{{ _displayUsage() 
     131 
     132    /** 
     133     * Displays the usage. 
     134     */ 
     135    private static function _displayUsage() 
     136    { 
     137        echo "Usage: {$_SERVER['SCRIPT_NAME']} [options] [directory or file] 
     138 
     139Options: 
     140  -h        display this help and exit 
     141  -V        display version information and exit 
     142  -R        run tests recursively 
     143  -c        color the result of a test runner run 
     144  -p <file> preload <file> as a PHP script 
     145  -a        watch for changes in a specified directory and run tests in the directory recursively when changes are detected (autotest) 
     146 
     147With no [directory or file], run all tests in the current directory. 
     148"; 
     149    } 
     150 
     151    // }}} 
     152    // {{{ _displayVersion() 
     153 
     154    /** 
     155     * Displays the version. 
     156     */ 
     157    private static function _displayVersion() 
     158    { 
     159        echo "Stagehand_TestRunner @package_version@ 
     160 
     161Copyright (c) 2005-2008 KUBO Atsuhiro <iteman@users.sourceforge.net>, 
     162              2007 Masahiko Sakamoto <msakamoto-sf@users.sourceforge.net>, 
     163All rights reserved. 
     164"; 
     165    } 
     166 
     167    // }}} 
     168    // {{{ _monitorAlteration() 
     169 
     170    /** 
     171     * Watches for changes in the directory and runs tests in the directory 
     172     * recursively when changes are detected. 
     173     * 
     174     * @param stdClass $config 
     175     * @throws Stagehand_TestRunner_Exception 
     176     */ 
     177    private static function _monitorAlteration($config) 
     178    { 
     179        if (!is_dir($config->directory)) { 
     180            throw new Stagehand_TestRunner_Exception("ERROR: The specified path [ {$config->directory} ] is not found or not a directory."); 
     181        } 
     182 
     183        $config->directory = realpath($config->directory); 
     184        if ($config->directory === false) { 
     185            throw new Stagehand_TestRunner_Exception("ERROR: Cannnot get the absolute path of the specified directory [ {$config->directory} ]. Make sure all elements of the absolute path have valid permissions."); 
     186        } 
     187 
     188        if (array_key_exists('_', $_SERVER)) { 
     189            $command = $_SERVER['_']; 
     190        } else { 
     191            $command = $_SERVER['argv'][0]; 
     192        } 
     193 
     194        $options = array(); 
     195        if (preg_match('!^/cygdrive/([a-z])/(.+)!', $command, $matches)) { 
     196            $command = "{$matches[1]}:\\" . str_replace('/', '\\', $matches[2]); 
     197        } 
     198 
     199        if (preg_match('/\.bat$/', $command)) { 
     200            $command = str_replace('/', '\\', $command); 
     201        } 
     202 
     203        if (!preg_match('/(?:test|spec)runner(?:\.bat)?$/', $command)) { 
     204            $configFile = get_cfg_var('cfg_file_path'); 
     205            if ($configFile !== false) { 
     206                $options[] = '-c'; 
     207                $options[] = dirname($configFile); 
     208            } 
     209 
     210            $options[] = $_SERVER['argv'][0]; 
     211        } 
     212 
     213        $options[] = '-R'; 
     214 
     215        if ($config->preload) { 
     216            $options[] = "-p {$config->preloadFile}"; 
     217        } 
     218 
     219        if ($config->color) { 
     220            $options[] = '-c'; 
     221        } 
     222 
     223        $options[] = $config->directory; 
     224 
     225        $monitor = new Stagehand_TestRunner_AlterationMonitor($config->directory, 
     226                                                              "$command " . implode(' ', $options) 
     227                                                              ); 
     228        $monitor->monitor(); 
     229    } 
     230 
     231    // }}} 
     232    // {{{ _parseOptions() 
     233 
     234    /** 
     235     * Parses the command line options and creates a configuration object. 
     236     * 
     237     * @return stdClass 
     238     * @throws Stagehand_TestRunner_Exception 
     239     */ 
     240    private static function _parseOptions() 
     241    { 
    95242        $argv = Console_Getopt::readPHPArgv(); 
    96243        array_shift($argv); 
    97244        $allOptions = Console_Getopt::getopt2($argv, 'hVRcp:a'); 
    98245        if (PEAR::isError($allOptions)) { 
    99             echo 'ERROR: ' . preg_replace('/^Console_Getopt: /', '', $allOptions->getMessage()) . "\n"; 
    100             self::_displayUsage(); 
    101             return 1; 
     246            throw new Stagehand_TestRunner_Exception('ERROR: ' . preg_replace('/^Console_Getopt: /', '', $allOptions->getMessage())); 
    102247        } 
    103248 
     
    118263                    case 'h': 
    119264                        self::_displayUsage(); 
    120                         return 1; 
     265                        return; 
    121266                    case 'V': 
    122267                        self::_displayVersion(); 
    123                         return 1; 
     268                        return; 
    124269                    case 'R': 
    125270                        $isRecursive = true; 
     
    144289        } 
    145290 
    146         if (!$enableAutotest) { 
    147             include_once "Stagehand/TestRunner/Collector/$testRunnerName.php"; 
    148             $className = "Stagehand_TestRunner_Collector_$testRunnerName"; 
    149             $collector = new $className($directory, $isRecursive); 
    150  
    151             try { 
    152                 $suite = $collector->collect(); 
    153             } catch (Stagehand_TestRunner_Exception $e) { 
    154                 echo 'ERROR: ' . $e->getMessage() . "\n"; 
    155                 self::_displayUsage(); 
    156                 return 1; 
    157             } 
    158  
    159             include_once "Stagehand/TestRunner/Runner/$testRunnerName.php"; 
    160             $className = "Stagehand_TestRunner_Runner_$testRunnerName"; 
    161             $runner = new $className(); 
    162             $runner->run($suite, $color); 
    163         } else { 
    164             if (!is_dir($directory)) { 
    165                 echo "ERROR: The specified path [ $directory ] is not found or not a directory.\n"; 
    166                 self::_displayUsage(); 
    167                 return 1; 
    168             } 
    169  
    170             $directory = realpath($directory); 
    171             if ($directory === false) { 
    172                 echo "ERROR: Cannnot get the absolute path of the specified directory [ $directory ]. Make sure all elements of the absolute path have valid permissions."; 
    173                 self::_displayUsage(); 
    174                 return 1; 
    175             } 
    176  
    177             if (array_key_exists('_', $_SERVER)) { 
    178                 $command = $_SERVER['_']; 
    179             } else { 
    180                 $command = $_SERVER['argv'][0]; 
    181             } 
    182  
    183             $options = array(); 
    184             if (preg_match('!^/cygdrive/([a-z])/(.+)!', $command, $matches)) { 
    185                 $command = "{$matches[1]}:\\" . str_replace('/', '\\', $matches[2]); 
    186             } 
    187  
    188             if (preg_match('/\.bat$/', $command)) { 
    189                 $command = str_replace('/', '\\', $command); 
    190             } 
    191  
    192             if (!preg_match('/(?:test|spec)runner(?:\.bat)?$/', $command)) { 
    193                 $configFile = get_cfg_var('cfg_file_path'); 
    194                 if ($configFile !== false) { 
    195                     $options[] = '-c'; 
    196                     $options[] = dirname($configFile); 
    197                 } 
    198  
    199                 $options[] = $_SERVER['argv'][0]; 
    200             } 
    201  
    202             $options[] = '-R'; 
    203  
    204             if ($preload) { 
    205                 $options[] = "-p $preloadFile"; 
    206             } 
    207  
    208             if ($color) { 
    209                 $options[] = '-c'; 
    210             } 
    211  
    212             $options[] = $directory; 
    213  
    214             $monitor = new Stagehand_TestRunner_AlterationMonitor($directory, 
    215                                                                   "$command " . implode(' ', $options) 
    216                                                                   ); 
    217             $monitor->monitor(); 
    218         } 
    219  
    220         return 0; 
    221     } 
    222  
    223     /**#@-*/ 
    224  
    225     /**#@+ 
    226      * @access protected 
    227      */ 
    228  
    229     /**#@-*/ 
    230  
    231     /**#@+ 
    232      * @access private 
    233      * @static 
    234      */ 
    235  
    236     // }}} 
    237     // {{{ _displayUsage() 
    238  
    239     /** 
    240      * Displays the usage. 
    241      */ 
    242     private static function _displayUsage() 
    243     { 
    244         echo "Usage: {$_SERVER['SCRIPT_NAME']} [options] [directory or file] 
    245  
    246 Options: 
    247   -h        display this help and exit 
    248   -V        display version information and exit 
    249   -R        run tests recursively 
    250   -c        color the result of a test runner run 
    251   -p <file> preload <file> as a PHP script 
    252   -a        watch for changes in a specified directory and run tests in 
    253             the directory recursively when changes are detected (autotest) 
    254  
    255 With no [directory or file], run all tests in the current directory. 
    256 "; 
    257     } 
    258  
    259     // }}} 
    260     // {{{ _displayVersion() 
    261  
    262     /** 
    263      * Displays the version. 
    264      */ 
    265     private static function _displayVersion() 
    266     { 
    267         echo "Stagehand_TestRunner @package_version@ 
    268  
    269 Copyright (c) 2005-2008 KUBO Atsuhiro <iteman@users.sourceforge.net>, 
    270               2007 Masahiko Sakamoto <msakamoto-sf@users.sourceforge.net>, 
    271 All rights reserved. 
    272 "; 
     291        return (object)array('directory' => $directory, 
     292                             'isRecursive' => $isRecursive, 
     293                             'color' => $color, 
     294                             'enableAutotest' => $enableAutotest, 
     295                             'preload' => $preload, 
     296                             'preloadFile' => $preloadFile 
     297                             ); 
     298    } 
     299 
     300    // }}} 
     301    // {{{ _runTests() 
     302 
     303    /** 
     304     * Runs tests. 
     305     * 
     306     * @param string $testRunnerName 
     307     * @param stdClass $config 
     308     */ 
     309    private static function _runTests($testRunnerName, $config) 
     310    { 
     311        include_once "Stagehand/TestRunner/Collector/$testRunnerName.php"; 
     312        $className = "Stagehand_TestRunner_Collector_$testRunnerName"; 
     313        $collector = new $className($config->directory, $config->isRecursive); 
     314        $suite = $collector->collect(); 
     315 
     316        include_once "Stagehand/TestRunner/Runner/$testRunnerName.php"; 
     317        $className = "Stagehand_TestRunner_Runner_$testRunnerName"; 
     318        $runner = new $className(); 
     319        $runner->run($suite, $config->color); 
    273320    } 
    274321