You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testcleanuplistener.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * Detects tests that didn't clean up properly, show a warning, then clean up after them.
  10. */
  11. class TestCleanupListener implements PHPUnit_Framework_TestListener {
  12. private $verbosity;
  13. public function __construct($verbosity = 'verbose') {
  14. $this->verbosity = $verbosity;
  15. }
  16. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
  17. }
  18. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
  19. }
  20. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  21. }
  22. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  23. }
  24. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  25. }
  26. public function startTest(PHPUnit_Framework_Test $test) {
  27. }
  28. public function endTest(PHPUnit_Framework_Test $test, $time) {
  29. }
  30. public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
  31. }
  32. public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
  33. if ($this->cleanStrayDataFiles() && $this->isShowSuiteWarning()) {
  34. printf("TestSuite '%s': Did not clean up data dir\n", $suite->getName());
  35. }
  36. if ($this->cleanStrayHooks() && $this->isShowSuiteWarning()) {
  37. printf("TestSuite '%s': Did not clean up hooks\n", $suite->getName());
  38. }
  39. if ($this->cleanProxies() && $this->isShowSuiteWarning()) {
  40. printf("TestSuite '%s': Did not clean up proxies\n", $suite->getName());
  41. }
  42. }
  43. private function isShowSuiteWarning() {
  44. return $this->verbosity === 'suite' || $this->verbosity === 'detail';
  45. }
  46. private function isShowDetail() {
  47. return $this->verbosity === 'detail';
  48. }
  49. /**
  50. * @param string $dir
  51. */
  52. private function unlinkDir($dir) {
  53. if ($dh = @opendir($dir)) {
  54. while (($file = readdir($dh)) !== false) {
  55. if ($file === '..' || $file === '.') {
  56. continue;
  57. }
  58. $path = $dir . '/' . $file;
  59. if (is_dir($path)) {
  60. $this->unlinkDir($path);
  61. }
  62. else {
  63. @unlink($path);
  64. }
  65. }
  66. closedir($dh);
  67. }
  68. @rmdir($dir);
  69. }
  70. private function cleanStrayDataFiles() {
  71. $knownEntries = array(
  72. 'owncloud.log' => true,
  73. 'owncloud.db' => true,
  74. '.ocdata' => true,
  75. '..' => true,
  76. '.' => true
  77. );
  78. $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data');
  79. $entries = array();
  80. if ($dh = opendir($datadir)) {
  81. while (($file = readdir($dh)) !== false) {
  82. if (!isset($knownEntries[$file])) {
  83. $entries[] = $file;
  84. }
  85. }
  86. closedir($dh);
  87. }
  88. if (count($entries) > 0) {
  89. foreach ($entries as $entry) {
  90. $this->unlinkDir($datadir . '/' . $entry);
  91. if ($this->isShowDetail()) {
  92. printf("Stray datadir entry: %s\n", $entry);
  93. }
  94. }
  95. return true;
  96. }
  97. return false;
  98. }
  99. private function cleanStrayHooks() {
  100. $hasHooks = false;
  101. $hooks = OC_Hook::getHooks();
  102. if (!$hooks || sizeof($hooks) === 0) {
  103. return false;
  104. }
  105. foreach ($hooks as $signalClass => $signals) {
  106. if (sizeof($signals)) {
  107. foreach ($signals as $signalName => $handlers ) {
  108. if (sizeof($handlers) > 0) {
  109. $hasHooks = true;
  110. OC_Hook::clear($signalClass, $signalName);
  111. if ($this->isShowDetail()) {
  112. printf("Stray hook: \"%s\" \"%s\"\n", $signalClass, $signalName);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return $hasHooks;
  119. }
  120. private function cleanProxies() {
  121. $proxies = OC_FileProxy::getProxies();
  122. OC_FileProxy::clearProxies();
  123. return count($proxies) > 0;
  124. }
  125. }