Index: TestCase.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpunit/lib/DrupalTest/TestCase.php,v
retrieving revision 1.4
diff -u -r1.4 TestCase.php
--- TestCase.php	12 Mar 2009 06:37:52 -0000	1.4
+++ TestCase.php	23 Mar 2009 01:25:20 -0000
@@ -1,6 +1,14 @@
 <?php
 // $Id: TestCase.php,v 1.4 2009/03/12 06:37:52 chop Exp $
 /**
+* @file
+* Defines a Test Case for all Drupal Tests. 
+*
+* @package DrupalTest
+* @author Christopher Hopper <christopher.jf.hopper@gmail.com>
+*/
+
+/**
 * DrupalTest TestCase
 * 
 * Defines the fixtures that are required or assist when running 
@@ -11,6 +19,14 @@
 */
 class DrupalTest_TestCase extends PHPUnit_Framework_TestCase {
   /**
+  * A location where we can store the working directory we started in 
+  * before we put ourselves in the root path of the drupal install. 
+  * 
+  * @var string
+  */
+  protected $currentWorkingDir;
+  
+  /**
   * Disable backup and restore operations for the global and super-
   * global variables. This prevents breaking references to MySQL-Link 
   * resources such as in the Drupal variable $GLOBALS['active_db']
@@ -22,24 +38,31 @@
   protected $backupGlobals = FALSE;
   
   /**
-  * A location where we can store the working directory we started in 
-  * before we put ourselves in the root path of the drupal install. 
-  * 
-  * @var string
-  */
-  protected $currentWorkingDir;
-  
-  /**
    * Set up environment for a unit test case
    *
    * @return void
    */
   protected function setUp() {
     parent::setUp();
-    // Save the current working directory. 
-    $this->currentWorkingDir = getcwd();
-    // Put us in the root path of the drupal install. 
-    chdir(DrupalTest_Bootstrap::getInstance()->drupalRoot);
+
+    // When using the command line test runner, put ourselves in the 
+    // correct working directory for Drupal. 
+    if (PHP_SAPI == 'cli' && !isset($this->currentWorkingDir)) {
+      $working_dir = getcwd();
+      $drupal_root = DrupalTest_Bootstrap::getInstance()->drupalRoot;
+      if ($working_dir != $drupal_root) {
+        // Save the current working directory. 
+        $this->currentWorkingDir = $working_dir;
+        // Put us in the root path of the drupal install. 
+        chdir($drupal_root);
+      }
+      else {
+        // Save an empty string to the current working directory 
+        // property to indicate that we were already in the correct 
+        // Drupal path and did not need to change directories. 
+        $this->currentWorkingDir = '';
+      }
+    }
 
     /** 
      * Supress devel footer output.
@@ -57,7 +80,12 @@
    */
   protected function tearDown() {
     parent::tearDown();
-    // Return us to our starting working directory. 
-    chdir($this->currentWorkingDir);
+
+    // When using the command line test runner, we find a working 
+    // directory has been saved, return us to that working directory 
+    // that we started in. 
+    if (PHP_SAPI == 'cli' && !empty($this->currentWorkingDir)) {
+      chdir($this->currentWorkingDir);
+    }
   }
 }
\ No newline at end of file
Index: TestSuite.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phpunit/lib/DrupalTest/TestSuite.php,v
retrieving revision 1.2
diff -u -r1.2 TestSuite.php
--- TestSuite.php	24 Feb 2009 07:46:49 -0000	1.2
+++ TestSuite.php	23 Mar 2009 01:24:57 -0000
@@ -1,6 +1,14 @@
 <?php
 // $Id: TestSuite.php,v 1.2 2009/02/24 07:46:49 chop Exp $
 /**
+* @file
+* Defines a Test Suite for all Drupal Tests. 
+*
+* @package DrupalTest
+* @author Christopher Hopper <christopher.jf.hopper@gmail.com>
+*/
+
+/**
 * DrupalTest TestSuite
 * 
 * Prepare and run a collection of Drupal Test Cases as a Suite. 
@@ -8,8 +16,15 @@
 * @package DrupalTest
 * @author  Christopher Hopper <christopher.jf.hopper@gmail.com>
 */
-class DrupalTest_TestSuite extends PHPUnit_Framework_TestSuite
-{
+class DrupalTest_TestSuite extends PHPUnit_Framework_TestSuite {
+  /**
+  * A location where we can store the working directory we started in 
+  * before we put ourselves in the root path of the drupal install. 
+  * 
+  * @var string
+  */
+  protected $currentWorkingDir;
+  
   /**
    * @return DrupalTest_TestSuite
    *    Instance of the DrupalTest TestSuite.
@@ -24,9 +39,30 @@
    * @return void
    */
   protected function setUp() {
+    parent::setUp();
+
     // Back-up the Drupal Database prior to running Test Suite.
 //    DrupalTest_Bootstrap::getInstance()->backupDatabase();
-    
+
+    // When using the command line test runner, put ourselves in the 
+    // correct working directory for Drupal. 
+    if (PHP_SAPI == 'cli' && !isset($this->currentWorkingDir)) {
+      $working_dir = getcwd();
+      $drupal_root = DrupalTest_Bootstrap::getInstance()->drupalRoot;
+      if ($working_dir != $drupal_root) {
+        // Save the current working directory. 
+        $this->currentWorkingDir = $working_dir;
+        // Put us in the root path of the drupal install. 
+        chdir($drupal_root);
+      }
+      else {
+        // Save an empty string to the current working directory 
+        // property to indicate that we were already in the correct 
+        // Drupal path and did not need to change directories. 
+        $this->currentWorkingDir = '';
+      }
+    }
+
     // Create a shared test_user fixture that will exist for all tests in 
     // the test suite.
     $test_user = array(
@@ -48,14 +84,23 @@
    * @return void
    */
   protected function tearDown() {
+    parent::tearDown();
     
     // Remove the test user created for our shared fixture in setUp. 
     if (isset($this->test_user->uid)) {
       user_delete(array(), $this->test_user->uid);
     }
     
+    // When using the command line test runner, we find a working 
+    // directory has been saved, return us to that working directory we 
+    // started in. 
+    if (PHP_SAPI == 'cli' && !empty($this->currentWorkingDir)) {
+      chdir($this->currentWorkingDir);
+    }
+    
     // Restore the Drupal Database back to the state saved prior to 
     // running our Test Suite.
 //    DrupalTest_Bootstrap::getInstance()->restoreDatabase();
   }
 }
+ 
\ No newline at end of file

