diff options
author | Morris Jobke <hey@morrisjobke.de> | 2014-11-22 10:05:47 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2014-11-22 10:05:47 +0100 |
commit | 2cc3edb10d0a37dce9954f1a402c9c31c7c3dab9 (patch) | |
tree | 4d867d035ed4691baf882d5858313d52fa4aa00b /tests | |
parent | fa3f7ad9e9bf92974b1115a2bae03075b97c1367 (diff) | |
parent | b8420592853717d3d637e3ec6c3ab3a1cfb850a5 (diff) | |
download | nextcloud-server-2cc3edb10d0a37dce9954f1a402c9c31c7c3dab9.tar.gz nextcloud-server-2cc3edb10d0a37dce9954f1a402c9c31c7c3dab9.zip |
Merge pull request #12328 from owncloud/remove-testcleanuplistener
Remove testcleanuplistener
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/testcase.php | 116 | ||||
-rw-r--r-- | tests/phpunit-autotest.xml | 5 | ||||
-rw-r--r-- | tests/testcleanuplistener.php | 176 |
3 files changed, 111 insertions, 186 deletions
diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 315cd6858ed..e6f5ca71dac 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -23,21 +23,127 @@ namespace Test; abstract class TestCase extends \PHPUnit_Framework_TestCase { + /** + * Returns a unique identifier as uniqid() is not reliable sometimes + * + * @param string $prefix + * @param int $length + * @return string + */ protected function getUniqueID($prefix = '', $length = 13) { - // Do not use dots and slashes as we use the value for file names return $prefix . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate( $length, + // Do not use dots and slashes as we use the value for file names '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ); } public static function tearDownAfterClass() { + $dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest'); + + self::tearDownAfterClassCleanFileMapper($dataDir); + self::tearDownAfterClassCleanStorages(); + self::tearDownAfterClassCleanFileCache(); + self::tearDownAfterClassCleanStrayDataFiles($dataDir); + self::tearDownAfterClassCleanStrayHooks(); + self::tearDownAfterClassCleanProxies(); + + parent::tearDownAfterClass(); + } + + /** + * Remove all entries from the files map table + * @param string $dataDir + */ + static protected function tearDownAfterClassCleanFileMapper($dataDir) { if (\OC_Util::runningOnWindows()) { - $rootDirectory = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest'); - $mapper = new \OC\Files\Mapper($rootDirectory); - $mapper->removePath($rootDirectory, true, true); + $mapper = new \OC\Files\Mapper($dataDir); + $mapper->removePath($dataDir, true, true); } + } - parent::tearDownAfterClass(); + /** + * Remove all entries from the storages table + * @throws \DatabaseException + */ + static protected function tearDownAfterClassCleanStorages() { + $sql = 'DELETE FROM `*PREFIX*storages`'; + $query = \OC_DB::prepare($sql); + $query->execute(); + } + + /** + * Remove all entries from the filecache table + * @throws \DatabaseException + */ + static protected function tearDownAfterClassCleanFileCache() { + $sql = 'DELETE FROM `*PREFIX*filecache`'; + $query = \OC_DB::prepare($sql); + $query->execute(); + } + + /** + * Remove all unused files from the data dir + * + * @param string $dataDir + */ + static protected function tearDownAfterClassCleanStrayDataFiles($dataDir) { + $knownEntries = array( + 'owncloud.log' => true, + 'owncloud.db' => true, + '.ocdata' => true, + '..' => true, + '.' => true, + ); + + if ($dh = opendir($dataDir)) { + while (($file = readdir($dh)) !== false) { + if (!isset($knownEntries[$file])) { + self::tearDownAfterClassCleanStrayDataUnlinkDir($dataDir . '/' . $file); + } + } + closedir($dh); + } + } + + /** + * Recursive delete files and folders from a given directory + * + * @param string $dir + */ + static protected function tearDownAfterClassCleanStrayDataUnlinkDir($dir) { + if ($dh = @opendir($dir)) { + while (($file = readdir($dh)) !== false) { + if ($file === '..' || $file === '.') { + continue; + } + $path = $dir . '/' . $file; + if (is_dir($path)) { + self::tearDownAfterClassCleanStrayDataUnlinkDir($path); + } + else { + @unlink($path); + } + } + closedir($dh); + } + @rmdir($dir); + } + + /** + * Clean up the list of hooks + */ + static protected function tearDownAfterClassCleanStrayHooks() { + \OC_Hook::clear(); + } + + /** + * Clean up the list of file proxies + * + * Also reenables file proxies, in case a test disabled them + */ + static protected function tearDownAfterClassCleanProxies() { + \OC_FileProxy::$enabled = true; + \OC_FileProxy::clearProxies(); } } diff --git a/tests/phpunit-autotest.xml b/tests/phpunit-autotest.xml index 282f5477c30..9fba824bc7d 100644 --- a/tests/phpunit-autotest.xml +++ b/tests/phpunit-autotest.xml @@ -39,11 +39,6 @@ </filter> <listeners> <listener class="StartSessionListener" file="startsessionlistener.php" /> - <listener class="TestCleanupListener" file="testcleanuplistener.php"> - <arguments> - <string>detail</string> - </arguments> - </listener> </listeners> </phpunit> diff --git a/tests/testcleanuplistener.php b/tests/testcleanuplistener.php deleted file mode 100644 index 7b442bbd4f7..00000000000 --- a/tests/testcleanuplistener.php +++ /dev/null @@ -1,176 +0,0 @@ -<?php -/** - * Copyright (c) 2013 Vincent Petry <pvince81@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -/** - * Detects tests that didn't clean up properly, show a warning, then clean up after them. - */ -class TestCleanupListener implements PHPUnit_Framework_TestListener { - private $verbosity; - - public function __construct($verbosity = 'verbose') { - $this->verbosity = $verbosity; - } - - public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) { - } - - public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) { - } - - public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) { - } - - public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) { - } - - public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) { - } - - public function startTest(PHPUnit_Framework_Test $test) { - } - - public function endTest(PHPUnit_Framework_Test $test, $time) { - } - - public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { - } - - public function endTestSuite(PHPUnit_Framework_TestSuite $suite) { - // don't clean up the test environment if a data provider finished - if (!($suite instanceof PHPUnit_Framework_TestSuite_DataProvider)) { - if ($this->cleanStorages() && $this->isShowSuiteWarning()) { - printf("TestSuite '%s': Did not clean up storages\n", $suite->getName()); - } - if ($this->cleanFileCache() && $this->isShowSuiteWarning()) { - printf("TestSuite '%s': Did not clean up file cache\n", $suite->getName()); - } - if ($this->cleanStrayDataFiles() && $this->isShowSuiteWarning()) { - printf("TestSuite '%s': Did not clean up data dir\n", $suite->getName()); - } - if ($this->cleanStrayHooks() && $this->isShowSuiteWarning()) { - printf("TestSuite '%s': Did not clean up hooks\n", $suite->getName()); - } - if ($this->cleanProxies() && $this->isShowSuiteWarning()) { - printf("TestSuite '%s': Did not clean up proxies\n", $suite->getName()); - } - } - } - - private function isShowSuiteWarning() { - return $this->verbosity === 'suite' || $this->verbosity === 'detail'; - } - - private function isShowDetail() { - return $this->verbosity === 'detail'; - } - - /** - * @param string $dir - */ - private function unlinkDir($dir) { - if ($dh = @opendir($dir)) { - while (($file = readdir($dh)) !== false) { - if ($file === '..' || $file === '.') { - continue; - } - $path = $dir . '/' . $file; - if (is_dir($path)) { - $this->unlinkDir($path); - } - else { - @unlink($path); - } - } - closedir($dh); - } - @rmdir($dir); - } - - private function cleanStrayDataFiles() { - $knownEntries = array( - 'owncloud.log' => true, - 'owncloud.db' => true, - '.ocdata' => true, - '..' => true, - '.' => true - ); - $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data'); - $entries = array(); - if ($dh = opendir($datadir)) { - while (($file = readdir($dh)) !== false) { - if (!isset($knownEntries[$file])) { - $entries[] = $file; - } - } - closedir($dh); - } - - if (count($entries) > 0) { - foreach ($entries as $entry) { - $this->unlinkDir($datadir . '/' . $entry); - if ($this->isShowDetail()) { - printf("Stray datadir entry: %s\n", $entry); - } - } - return true; - } - - return false; - } - - private function cleanStorages() { - $sql = 'DELETE FROM `*PREFIX*storages`'; - $query = \OC_DB::prepare( $sql ); - $result = $query->execute(); - if ($result > 0) { - return true; - } - return false; - } - - private function cleanFileCache() { - $sql = 'DELETE FROM `*PREFIX*filecache`'; - $query = \OC_DB::prepare( $sql ); - $result = $query->execute(); - if ($result > 0) { - return true; - } - return false; - } - - private function cleanStrayHooks() { - $hasHooks = false; - $hooks = OC_Hook::getHooks(); - if (!$hooks || sizeof($hooks) === 0) { - return false; - } - - foreach ($hooks as $signalClass => $signals) { - if (sizeof($signals)) { - foreach ($signals as $signalName => $handlers ) { - if (sizeof($handlers) > 0) { - $hasHooks = true; - OC_Hook::clear($signalClass, $signalName); - if ($this->isShowDetail()) { - printf("Stray hook: \"%s\" \"%s\"\n", $signalClass, $signalName); - } - } - } - } - } - return $hasHooks; - } - - private function cleanProxies() { - $proxies = OC_FileProxy::getProxies(); - OC_FileProxy::clearProxies(); - // reenable in case some test failed to reenable them - OC_FileProxy::$enabled = true; - return count($proxies) > 0; - } -} |