summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-12-28 12:06:49 +0000
committerDries Buytaert <dries@buytaert.net>2009-12-28 12:06:49 +0000
commitb057283bc3057bc2e5f65ef86c875827e6d6ca34 (patch)
treecde556d88545f28a84f8dcb3ce56861528f66590
parent438909ba28b0f033c5ce26730d41f49d37abb23e (diff)
downloadbrdo-b057283bc3057bc2e5f65ef86c875827e6d6ca34.tar.gz
brdo-b057283bc3057bc2e5f65ef86c875827e6d6ca34.tar.bz2
- Patch #667264 by Damien Tournoud: improved generation of tests files to avoid concurrency problems causing the tests to /randomly/ fail.
-rw-r--r--modules/simpletest/drupal_web_test_case.php37
-rw-r--r--modules/simpletest/simpletest.install91
-rw-r--r--modules/simpletest/simpletest.module29
3 files changed, 62 insertions, 95 deletions
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index b9cd243be..19fe544c0 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -691,6 +691,11 @@ class DrupalWebTestCase extends DrupalTestCase {
protected $session_id = NULL;
/**
+ * Whether the files were copied to the test files directory.
+ */
+ protected $generatedTestFiles = FALSE;
+
+ /**
* Constructor for DrupalWebTestCase.
*/
function __construct($test_id = NULL) {
@@ -839,13 +844,36 @@ class DrupalWebTestCase extends DrupalTestCase {
* List of files that match filter.
*/
protected function drupalGetTestFiles($type, $size = NULL) {
- $files = array();
+ if (empty($this->generatedTestFiles)) {
+ // Generate binary test files.
+ $lines = array(64, 1024);
+ $count = 0;
+ foreach ($lines as $line) {
+ simpletest_generate_file('binary-' . $count++, 64, $line, 'binary');
+ }
+
+ // Generate text test files.
+ $lines = array(16, 256, 1024, 2048, 20480);
+ $count = 0;
+ foreach ($lines as $line) {
+ simpletest_generate_file('text-' . $count++, 64, $line);
+ }
+
+ // Copy other test files from simpletest.
+ $original = drupal_get_path('module', 'simpletest') . '/files';
+ $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
+ $destination_path = file_directory_path('public');
+ foreach ($files as $file) {
+ file_unmanaged_copy($file->uri, $destination_path);
+ }
+ $this->generatedTestFiles = TRUE;
+ }
+
+ $files = array();
// Make sure type is valid.
if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
- // Use original file directory instead of one created during setUp().
- $path = $this->originalFileDirectory . '/simpletest';
- $files = file_scan_directory($path, '/' . $type . '\-.*/');
+ $files = file_scan_directory(file_directory_path('public'), '/' . $type . '\-.*/');
// If size is set then remove any files that are not of that size.
if ($size !== NULL) {
@@ -1088,6 +1116,7 @@ class DrupalWebTestCase extends DrupalTestCase {
file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY);
file_prepare_directory($temp_files_directory, FILE_CREATE_DIRECTORY);
+ $this->generatedTestFiles = FALSE;
// Log fatal errors.
ini_set('log_errors', 1);
diff --git a/modules/simpletest/simpletest.install b/modules/simpletest/simpletest.install
index 802303192..68eb57815 100644
--- a/modules/simpletest/simpletest.install
+++ b/modules/simpletest/simpletest.install
@@ -7,97 +7,6 @@
*/
/**
- * Implements hook_install().
- */
-function simpletest_install() {
- // Check for files directory.
- $path = 'public://simpletest';
- if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
- // Generate binary and text test files.
- $generated = FALSE;
- if (simpletest_get_file_count($path, 'binary') == 0) {
- $lines = array(64, 1024);
- foreach ($lines as $line) {
- simpletest_generate_file('binary', 64, $line, 'binary');
- }
- $generated = TRUE;
- }
-
- if (simpletest_get_file_count($path, 'text') == 0) {
- $lines = array(16, 256, 1024, 2048, 20480);
- foreach ($lines as $line) {
- simpletest_generate_file('text', 64, $line);
- }
- $generated = TRUE;
- }
-
- // Copy other test files for consistency.
- $original = drupal_get_path('module', 'simpletest') . '/files';
- $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
-
- // If there are more files in SimpleTest's files directory than the site's
- // files directory, restore all the files. This situation might occur when
- // an errant test deletes one or more files from the site's files
- // directory. It serves a convenience to developers so that they can get
- // the test files back easily.
- if (count($files) > count(file_scan_directory($path, '/(html|image|javascript|php|sql)-.*/'))) {
- foreach ($files as $file) {
- file_unmanaged_copy($file->uri, $path, FILE_EXISTS_REPLACE);
- }
- $generated = TRUE;
- }
-
- if ($generated) {
- drupal_set_message('Extra test files generated/copied.');
- }
- }
-}
-
-/**
- * Generate test file.
- */
-function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
- $size = $width * $lines - $lines;
-
- // Generate random text
- $text = '';
- for ($i = 0; $i < $size; $i++) {
- switch ($type) {
- case 'text':
- $text .= chr(rand(32, 126));
- break;
- case 'binary':
- $text .= chr(rand(0, 31));
- break;
- case 'binary-text':
- default:
- $text .= rand(0, 1);
- break;
- }
- }
- $text = wordwrap($text, $width - 1, "\n", TRUE) . "\n"; // Add \n for symetrical file.
-
- // Create filename.
- $path = file_directory_path() . '/simpletest/';
- $count = simpletest_get_file_count($path, $filename);
- file_put_contents($path . $filename . '-' . ($count + 1) . '.txt', $text);
-}
-
-/**
- * Get the number of files that have the specified filename base.
- */
-function simpletest_get_file_count($directory, $filename) {
- $files = scandir($directory);
- $count = 0;
- foreach ($files as $file) {
- if (preg_match('/' . $filename . '.*?/', $file)) {
- $count++;
- }
- }
- return $count;
-}
-
-/**
* Implements hook_uninstall().
*/
function simpletest_uninstall() {
diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module
index 6b7db15cc..b702dc06f 100644
--- a/modules/simpletest/simpletest.module
+++ b/modules/simpletest/simpletest.module
@@ -400,6 +400,35 @@ function simpletest_registry_files_alter(&$files, $modules) {
}
/**
+ * Generate test file.
+ */
+function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
+ $size = $width * $lines - $lines;
+
+ // Generate random text
+ $text = '';
+ for ($i = 0; $i < $size; $i++) {
+ switch ($type) {
+ case 'text':
+ $text .= chr(rand(32, 126));
+ break;
+ case 'binary':
+ $text .= chr(rand(0, 31));
+ break;
+ case 'binary-text':
+ default:
+ $text .= rand(0, 1);
+ break;
+ }
+ }
+ $text = wordwrap($text, $width - 1, "\n", TRUE) . "\n"; // Add \n for symetrical file.
+
+ // Create filename.
+ file_put_contents(file_directory_path() . '/' . $filename . '.txt', $text);
+ return $filename;
+}
+
+/**
* Remove all temporary database tables and directories.
*/
function simpletest_clean_environment() {