Parser test improvements:
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 23 Sep 2004 00:33:07 +0000 (00:33 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 23 Sep 2004 00:33:07 +0000 (00:33 +0000)
* Show a diff of the expected and actual output rather than dumping large swaths of HTML
* Set exit code depending on success
* Standardize some global settings such as the URL paths which affect output
* Added copyright/GPL notice to test script code

maintenance/parserTests.php

index 5e70ca0..8028c83 100644 (file)
@@ -1,5 +1,24 @@
 <?php
+# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
+# http://www.mediawiki.org/
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or 
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# http://www.gnu.org/copyleft/gpl.html
+
 /**
+ * @todo Make this more independent of the configuration (and if possible the database)
  * @todo document
  * @package MediaWiki
  * @subpackage Maintenance
@@ -64,6 +83,9 @@ class ParserTest {
                if( $total > 0 ) {
                        $ratio = IntVal( 100.0 * $success / $total );
                        print "\nPassed $success of $total tests ($ratio%)\n";
+                       return ($success == $total);
+               } else {
+                       die( "No tests found.\n" );
                }
        }
 
@@ -88,6 +110,8 @@ class ParserTest {
        function runTest( $desc, $input, $result ) {
                print "Running test $desc...";
 
+               $this->setupGlobals();
+               
                $user =& new User();
                $options =& ParserOptions::newFromUser( $user );
                $parser =& new Parser();
@@ -111,6 +135,8 @@ class ParserTest {
                        $result = Parser::tidy($result);
                }
                
+               $this->teardownGlobals();
+               
                if( rtrim($result) === rtrim($html) ) {
                        return $this->showSuccess( $desc );
                } else {
@@ -118,6 +144,26 @@ class ParserTest {
                }
        }
        
+       function setupGlobals() {
+               static $settings = array(
+                       'wgServer' => 'http://localhost',
+                       'wgScript' => '/index.php',
+                       'wgScriptPath' => '/',
+                       'wgArticlePath' => '/wiki/$1',
+                       );
+               $this->savedGlobals = array();
+               foreach( $settings as $var => $val ) {
+                       $this->savedGlobals[$var] = $GLOBALS[$var];
+                       $GLOBALS[$var] = $val;
+               }
+       }
+       
+       function teardownGlobals() {
+               foreach( $this->savedGlobals as $var => $val ) {
+                       $GLOBALS[$var] = $val;
+               }
+       }
+       
        function showSuccess( $desc ) {
                print "ok\n";
                return true;
@@ -125,13 +171,41 @@ class ParserTest {
        
        function showFailure( $desc, $result, $html ) {
                print "FAILED\n";
-               print "!! Expected:\n$result\n";
-               print "!! Received:\n$html\n!!\n";
+               #print "!! Expected:\n$result\n";
+               #print "!! Received:\n$html\n!!\n";
+               print $this->quickDiff( $result, $html );
                return false;
        }
+       
+       function quickDiff( $input, $output ) {
+               $prefix = "/tmp/mwParser-" . mt_rand();
+               
+               $infile = "$prefix-in";
+               $this->dumpToFile( $input, $infile );
+               
+               $outfile = "$prefix-out";
+               $this->dumpToFile( $output, $outfile );
+               
+               $diff = `diff -u $infile $outfile`;
+               unlink( $infile );
+               unlink( $outfile );
+               
+               return $diff;
+       }
+       
+       function dumpToFile( $data, $filename ) {
+               $file = fopen( $filename, "wt" );
+               fwrite( $file, $data );
+               fclose( $file );
+       }
 }
 
 $tester =& new ParserTest();
-$tester->runTestsFromFile( 'maintenance/parserTests.txt' );
+
+# Note: the command line setup changes the current working directory
+# to the parent, which is why we have to put the subdir here:
+$ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
+
+exit ($ok ? 0 : -1);
 
 ?>