Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testcleanuplistener.php « tests - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a969ece6dd579e3bd8079853459e95865992cb44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?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 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) {
		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';
	}

	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,
			'..' => 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 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();
		return count($proxies) > 0;
	}
}
?>