### Eclipse Workspace Patch 1.0
#P simpletest
Index: simpletest.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simpletest/simpletest.module,v
retrieving revision 1.46
diff -u -r1.46 simpletest.module
--- simpletest.module	8 Apr 2008 19:31:59 -0000	1.46
+++ simpletest.module	14 Apr 2008 21:55:32 -0000
@@ -194,13 +194,13 @@
     '#type' => 'fieldset',
     '#collapsible' => FALSE,
     '#collapsed' => FALSE,
-    '#title' => 'Clean Database',
-    '#description' => 'Remove tables with the prefix "simpletest" that are left over from tests that crashed.'
+    '#title' => 'Clean Environment',
+    '#description' => 'Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed.'
   );
   $output['reset']['op'] = array(
     '#type' => 'submit',
-    '#value' => t('Clean Database'),
-    '#submit' => array('simpletest_clean_database')
+    '#value' => t('Clean Environment'),
+    '#submit' => array('simpletest_clean_environment')
   );
   return $output;
 }
@@ -302,6 +302,14 @@
 }
 
 /**
+ * Remove all temporary database tables and directories.
+ */
+function simpletest_clean_environment() {
+  simpletest_clean_database();
+  simpletest_clean_temporary_directories();
+}
+
+/**
  * Removed prefixed talbes from the database that are left over from crashed tests.
  */
 function simpletest_clean_database() {
@@ -313,10 +321,10 @@
   }
 
   if (count($ret) > 0) {
-    drupal_set_message(t('Removed '. count($ret) .' left over tables.'));
+    drupal_set_message(t('Removed @count left over tables.', array('@count' => count($ret))));
   }
   else {
-    drupal_set_message(t('No left over tables removed.'));
+    drupal_set_message(t('No left over tables to remove.'));
   }
 }
 
@@ -333,7 +341,7 @@
   $database = substr($url['path'], 1);
   $select = $count ? 'COUNT(table_name)' : 'table_name';
   $result = db_query("SELECT $select FROM information_schema.tables WHERE table_schema = '$database' AND table_name LIKE '$db_prefix$base_table%'");
-  
+
   if ($count) {
     return db_result($result);
   }
@@ -345,6 +353,49 @@
 }
 
 /**
+ * Find all left over temporary directories and remove them.
+ */
+function simpletest_clean_temporary_directories() {
+  $files = scandir(file_directory_path());
+  $count = 0;
+  foreach ($files as $file) {
+    $path = file_directory_path() .'/'. $file;
+    if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) {
+      simpletest_clean_temporary_directory($path);
+      $count++;
+    }
+  }
+
+  if ($count > 0) {
+    drupal_set_message(t('Removed @count temporary directories.', array('@count' => $count)));
+  }
+  else {
+    drupal_set_message(t('No temporary directories to remove.'));
+  }
+}
+
+/**
+ * Remove all files from specified firectory and then remove directory.
+ *
+ * @param string $path Directory path.
+ */
+function simpletest_clean_temporary_directory($path) {
+  $files = scandir($path);
+  foreach ($files as $file) {
+    if ($file != '.' && $file != '..') {
+      $file_path = "$path/$file";
+      if (is_dir($file_path)) {
+        simpletest_clean_temporary_directory($file_path);
+      }
+      else {
+        file_delete($file_path);
+      }
+    }
+  }
+  rmdir($path);
+}
+
+/**
  * Actually runs tests
  * @param array $testlist list of tests to run or DEFAULT NULL run all tests
  * @param boolean $html_reporter true if you want results in simple html, FALSE for full drupal page
Index: drupal_test_case.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simpletest/drupal_test_case.php,v
retrieving revision 1.86
diff -u -r1.86 drupal_test_case.php
--- drupal_test_case.php	14 Apr 2008 21:03:29 -0000	1.86
+++ drupal_test_case.php	14 Apr 2008 21:55:32 -0000
@@ -16,6 +16,7 @@
   // Overwrite this any time to supply cURL options as necessary,
   // DrupalTestCase itself never sets this but always obeys whats set.
   protected $curl_options         = array();
+  protected $original_file_directory;
 
   /**
    * Retrieve the test information from getInfo().
@@ -133,7 +134,8 @@
 
     // Make sure type is valid.
     if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
-      $path = file_directory_path() .'/simpletest';
+     // Use original file directory instead of one created during setUp().
+      $path = $this->original_file_directory .'/simpletest';
       $files = file_scan_directory($path, $type .'\-.*');
 
       // If size is set then remove any files that are not of that size.
@@ -332,8 +334,9 @@
   /**
    * Generates a random database prefix and runs the install scripts on the prefixed database.
    * After installation many caches are flushed and the internal browser is setup so that the page
-   * requests will run on the new prefix.
-   * 
+   * requests will run on the new prefix. A temporary files directory is created with the same name
+   * as the database prefix.
+   *
    * @param ... List modules to enable.
    */
   function setUp() {
@@ -356,16 +359,26 @@
       variable_set('install_profile', 'default');
       variable_set('install_task', 'profile-finished');
       variable_set('clean_url', $clean_url_original);
+
+      // Use temporary files directory with the same prefix as database.
+      $this->original_file_directory = file_directory_path();
+      variable_set('file_directory_path', file_directory_path() .'/'. $db_prefix);
+      file_check_directory(file_directory_path(), TRUE); // Create the files directory.
     }
     parent::setUp();
   }
 
   /**
-   * Delete the tables created by setUp() and reset the database prefix.
+   * Delete created files and temporary files directory, delete the tables created by setUp(),
+   * and reset the database prefix.
    */
   function tearDown() {
     global $db_prefix;
     if (preg_match('/simpletest\d+/', $db_prefix)) {
+      // Delete temporary files directory and reset files directory path.
+      simpletest_clean_temporary_directory(file_directory_path());
+      variable_set('file_directory_path', $this->original_file_directory);
+
       $schema = drupal_get_schema(NULL, TRUE);
       $ret = array();
       foreach ($schema as $name => $table) {
