testcleanuplistener.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  23. }
  24. public function startTest(PHPUnit_Framework_Test $test) {
  25. }
  26. public function endTest(PHPUnit_Framework_Test $test, $time) {
  27. }
  28. public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
  29. }
  30. public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
  31. if ($this->cleanStrayDataFiles() && $this->isShowSuiteWarning()) {
  32. printf("TestSuite '%s': Did not clean up data dir\n", $suite->getName());
  33. }
  34. if ($this->cleanStrayHooks() && $this->isShowSuiteWarning()) {
  35. printf("TestSuite '%s': Did not clean up hooks\n", $suite->getName());
  36. }
  37. if ($this->cleanProxies() && $this->isShowSuiteWarning()) {
  38. printf("TestSuite '%s': Did not clean up proxies\n", $suite->getName());
  39. }
  40. }
  41. private function isShowSuiteWarning() {
  42. return $this->verbosity === 'suite' || $this->verbosity === 'detail';
  43. }
  44. private function isShowDetail() {
  45. return $this->verbosity === 'detail';
  46. }
  47. /**
  48. * @param string $dir
  49. */
  50. private function unlinkDir($dir) {
  51. if ($dh = @opendir($dir)) {
  52. while (($file = readdir($dh)) !== false) {
  53. if ($file === '..' || $file === '.') {
  54. continue;
  55. }
  56. $path = $dir . '/' . $file;
  57. if (is_dir($path)) {
  58. $this->unlinkDir($path);
  59. }
  60. else {
  61. @unlink($path);
  62. }
  63. }
  64. closedir($dh);
  65. }
  66. @rmdir($dir);
  67. }
  68. private function cleanStrayDataFiles() {
  69. $knownEntries = array(
  70. 'owncloud.log' => true,
  71. 'owncloud.db' => true,
  72. '..' => true,
  73. '.' => true
  74. );
  75. $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data');
  76. $entries = array();
  77. if ($dh = opendir($datadir)) {
  78. while (($file = readdir($dh)) !== false) {
  79. if (!isset($knownEntries[$file])) {
  80. $entries[] = $file;
  81. }
  82. }
  83. closedir($dh);
  84. }
  85. if (count($entries) > 0) {
  86. foreach ($entries as $entry) {
  87. $this->unlinkDir($datadir . '/' . $entry);
  88. if ($this->isShowDetail()) {
  89. printf("Stray datadir entry: %s\n", $entry);
  90. }
  91. }
  92. return true;
  93. }
  94. return false;
  95. }
  96. private function cleanStrayHooks() {
  97. $hasHooks = false;
  98. $hooks = OC_Hook::getHooks();
  99. if (!$hooks || sizeof($hooks) === 0) {
  100. return false;
  101. }
  102. foreach ($hooks as $signalClass => $signals) {
  103. if (sizeof($signals)) {
  104. foreach ($signals as $signalName => $handlers ) {
  105. if (sizeof($handlers) > 0) {
  106. $hasHooks = true;
  107. OC_Hook::clear($signalClass, $signalName);
  108. if ($this->isShowDetail()) {
  109. printf("Stray hook: \"%s\" \"%s\"\n", $signalClass, $signalName);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. return $hasHooks;
  116. }
  117. private function cleanProxies() {
  118. $proxies = OC_FileProxy::getProxies();
  119. OC_FileProxy::clearProxies();
  120. return count($proxies) > 0;
  121. }
  122. }
  123. ?>