PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/python/php/sdk/google/appengine/runtime/GlobTest.php

https://gitlab.com/gregtyka/frankenserver
PHP | 359 lines | 261 code | 39 blank | 59 comment | 7 complexity | 8eaacfa213735c9daeab0953d87f5021 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace google\appengine\runtime;
  18. use google\appengine\testing\TestUtils;
  19. class GlobTest extends \PHPUnit_Framework_TestCase {
  20. protected function tearDown() {
  21. if (!empty($GLOBALS['opendir'])) {
  22. $this->fail('Expected opendir call not made.');
  23. }
  24. if (!empty($GLOBALS['readdir'])) {
  25. $this->fail('Expected readdir call not made.');
  26. }
  27. if (!empty($GLOBALS['closedir'])) {
  28. $this->fail('Expected listdir call not made.');
  29. }
  30. }
  31. protected function setupBasicRead($files, $path = '.') {
  32. $handle = uniqid();
  33. $this->expectOpenDir($path, $handle);
  34. $this->expectReadDir($handle, $files);
  35. $this->expectCloseDir($handle);
  36. }
  37. public function testBasicGlob() {
  38. $this->setupBasicRead(['foo.txt', 'bar.txt']);
  39. $result = Glob::doGlob('*', GLOB_NOSORT);
  40. $this->assertEquals(['foo.txt', 'bar.txt'], $result);
  41. }
  42. public function testBasicGlobWithPath() {
  43. $this->setupBasicRead(['foo.txt', 'bar.txt'], 'bar/baz/zoo');
  44. $result = Glob::doGlob('bar/baz/zoo/*', GLOB_NOSORT);
  45. $this->assertEquals(
  46. ['bar/baz/zoo/foo.txt', 'bar/baz/zoo/bar.txt'], $result);
  47. }
  48. public function testPatternMatchGlob() {
  49. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  50. $result = Glob::doGlob('*.txt', GLOB_NOSORT);
  51. $this->assertEquals(['foo.txt'], $result);
  52. }
  53. public function testBraceExpansionGlob() {
  54. // Expanding the braces will result in muliple reads
  55. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  56. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  57. $result = Glob::doGlob('{*.txt,*.jpg}', GLOB_BRACE);
  58. $expected = ['foo.txt', 'bar.jpg'];
  59. $this->assertEquals($expected, $result);
  60. // We want to ensure that the items in the result array are also in the
  61. // expected order.
  62. while (!empty($expected)) {
  63. $val1 = array_shift($expected);
  64. $val2 = array_shift($result);
  65. $this->assertEquals($val1, $val2);
  66. }
  67. }
  68. public function testGlobSortOrder() {
  69. // Make sure sorting does not maintain the original index
  70. $this->setupBasicRead(['foo.txt', 'zoo.txt', 'bar.txt']);
  71. $result = Glob::doGlob('*');
  72. // The order is important.
  73. $expected = ['bar.txt', 'foo.txt', 'zoo.txt'];
  74. $this->assertEquals($expected, $result);
  75. // We want to ensure that the items in the result array are also in the
  76. // expected order.
  77. while (!empty($expected)) {
  78. $val1 = array_shift($expected);
  79. $val2 = array_shift($result);
  80. $this->assertEquals($val1, $val2);
  81. }
  82. }
  83. public function testNoBraceExpansionGlob() {
  84. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  85. $result = Glob::doGlob('{*.txt,*.jpg}');
  86. $this->assertEquals([], $result);
  87. }
  88. public function testEscapeGlob() {
  89. $this->setupBasicRead(['[house].txt']);
  90. $result = Glob::doGlob('\[house\].txt');
  91. $this->assertEquals(['[house].txt'], $result);
  92. }
  93. public function testNoEscapeSetGlob() {
  94. $this->setupBasicRead(['[house].txt']);
  95. $result = Glob::doGlob('\[house\].txt', GLOB_NOESCAPE);
  96. $this->assertEquals([], $result);
  97. }
  98. public function testNoCheckGlob() {
  99. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  100. $result = Glob::doGlob('*.png', GLOB_NOCHECK);
  101. $this->assertEquals(['*.png'], $result);
  102. }
  103. public function testNoCheckGlobNotEmpty() {
  104. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  105. $result = Glob::doGlob('*.txt', GLOB_NOCHECK);
  106. $this->assertEquals(['foo.txt'], $result);
  107. }
  108. public function testErrorDoesNotTerminateGlob() {
  109. $this->expectFailedOpenDir('.');
  110. $this->setupBasicRead(['foo.txt', 'bar.jpg']);
  111. $result = Glob::doGlob('{foo.*,*.txt}', GLOB_BRACE);
  112. $this->assertEquals(['foo.txt'], $result);
  113. }
  114. public function testDotDirectoryNamesExcluded() {
  115. $this->setupBasicRead(['.', '..', 'foo.txt', 'bar.txt']);
  116. $result = Glob::doGlob('*', GLOB_NOSORT);
  117. $this->assertEquals(['foo.txt', 'bar.txt'], $result);
  118. }
  119. public function testWildcardPath() {
  120. $this->setupBasicRead(['foo']);
  121. $this->setFileIsDir('./foo');
  122. $this->setupBasicRead(['1.txt', '2.txt'], 'foo');
  123. $result = Glob::doGlob('*/*.txt');
  124. $this->assertEquals(['foo/1.txt', 'foo/2.txt'], $result);
  125. }
  126. public function testErrors() {
  127. // Match the semantics of glob as observed in
  128. // https://github.com/php/php-src/blob/master/ext/standard/tests/file/glob_error.phpt
  129. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  130. Glob::doGlob(); // Not enough arguments
  131. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  132. Glob::doGlob("*", GLOB_ERR, 2); // Too many arguments
  133. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  134. Glob::doGlob('*', '');
  135. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  136. Glob::doGlob('*', 'str');
  137. }
  138. /**
  139. * @dataProvider braceExpansionDataProvider
  140. */
  141. public function testBraceExpansion($input, $output) {
  142. $glob = new Glob();
  143. $result = TestUtils::invokeMethod($glob, "expandFilenameBraces", [$input]);
  144. $this->assertEquals($output, $result);
  145. }
  146. /**
  147. * DataProvider for testBraceExpansion
  148. */
  149. public function braceExpansionDataProvider() {
  150. // Format is $input, $output
  151. return [
  152. ["/foo/bar", ["/foo/bar"]],
  153. ["/foo/*/bar", ["/foo/*/bar"]],
  154. ["{*.jpg}", ["*.jpg"]],
  155. ["{*.jpg,*.png}", ["*.jpg", "*.png"]],
  156. ["{*.jpg, *.png}", ["*.jpg", " *.png"]],
  157. ["/foo/{a,b}/bar", ["/foo/a/bar", "/foo/b/bar"]],
  158. ["/a/{1,2}/b/{3,4}", ["/a/1/b/3", "/a/2/b/3", "/a/1/b/4", "/a/2/b/4"]],
  159. ];
  160. }
  161. /**
  162. * @dataProvider dirNameForPathProvider
  163. */
  164. public function testDirNameForPath($input, $output) {
  165. $glob = new Glob();
  166. $this->assertEquals(
  167. $output, TestUtils::invokeMethod($glob, "getDirNameForPath", [$input]));
  168. }
  169. /**
  170. * DataProvider for testDirNameForPath.
  171. */
  172. public function dirNameForPathProvider() {
  173. // Format is [$input, $output]
  174. return [
  175. ['/foo/bar.txt', '/foo'],
  176. ['', '.'],
  177. ['gs://foo/bar/zoo.txt', 'gs://foo/bar'],
  178. ['./foo/bar/*', './foo/bar'],
  179. ];
  180. }
  181. /**
  182. * @dataProvider fileNamePatternMatchProvider
  183. */
  184. public function testFileNamePatternMatch($input, $pattern, $output, $opts) {
  185. $glob = new Glob();
  186. $result = TestUtils::invokeMethod($glob,
  187. "isFileNamePatternMatch",
  188. [$input, $pattern, $opts]);
  189. $this->assertEquals($output, $result);
  190. }
  191. /**
  192. * DataProvider for testFileNamePatternMatch.
  193. */
  194. public function fileNamePatternMatchProvider() {
  195. // Format input, pattern, output, options
  196. return [
  197. ['foo.txt', '*.txt', true, 0],
  198. ['foo.txt', '*', true, 0],
  199. ['foo.txt', '*.jpg', false, 0],
  200. ['foo.txt', 'f??.txt', true, 0],
  201. ['foo.txt', 'f?.txt', false, 0],
  202. ['f9.txt', 'f[0-9].txt', true, 0],
  203. ['f[].txt', 'f\[\].txt', true, 0],
  204. ['f[].txt]', 'f\[\].txt', false, GLOB_NOESCAPE],
  205. ['\".txt', '\".txt', true, GLOB_NOESCAPE],
  206. ['\".txt', '\".txt', false, 0],
  207. ];
  208. }
  209. /**
  210. * @dataProvider directoryOptionsDataProvider
  211. */
  212. public function testDirectoryOptions($base, $files, $dirs, $opts, $expected) {
  213. foreach($dirs as $dir) {
  214. $this->setFileIsDir(
  215. implode(DIRECTORY_SEPARATOR, [
  216. rtrim($base, DIRECTORY_SEPARATOR),
  217. ltrim($dir, DIRECTORY_SEPARATOR)
  218. ])
  219. );
  220. }
  221. $glob = new Glob();
  222. $result = TestUtils::invokeMethod($glob,
  223. "doDirectoryOptions",
  224. [$base, $files, $opts]);
  225. $this->assertEquals($expected, $result);
  226. }
  227. /**
  228. * DataProvider for testDirectoryOptions.
  229. */
  230. public function directoryOptionsDataProvider() {
  231. // Format basename, array files, array dirs, options, result array
  232. return [
  233. ['/foo', ['bar.txt'], ['bar.txt'], GLOB_ONLYDIR, ['bar.txt']],
  234. ['/foo', ['bar.txt'], [], GLOB_ONLYDIR, []],
  235. ['/foo', ['a', 'b', 'c'], ['a'], GLOB_ONLYDIR, ['a']],
  236. ['/foo', ['a', 'b'], ['a'], GLOB_MARK, ['a/', 'b']],
  237. ['/foo', ['a', 'b', 'c'], ['a'], GLOB_ONLYDIR | GLOB_MARK, ['a/']],
  238. ];
  239. }
  240. /**
  241. * @dataProvider splitPathOnWilcardDataProvider
  242. */
  243. public function testSplitPathOnWildcard($input, $output) {
  244. $glob = new Glob();
  245. $result = TestUtils::invokeMethod($glob,
  246. "splitPathOnWildcard",
  247. [$input]);
  248. $this->assertEquals($output, $result);
  249. }
  250. /**
  251. * DataProvider for testSplitPathOnWildcard.
  252. */
  253. public function splitPathOnWilcardDataProvider() {
  254. return [
  255. ['foo/*/bar/', ['foo/', '*', '/bar/']],
  256. ['*/bar/', ['', '*', '/bar/']],
  257. ['/foo/*', ['/foo/', '*', '']],
  258. ['/foo/*/', ['/foo/', '*', '/']],
  259. ['foo/?/bar/', ['foo/', '?', '/bar/']],
  260. ['?/bar/', ['', '?', '/bar/']],
  261. ['/foo/?', ['/foo/', '?', '']],
  262. ['foo/?baz/bar/', ['foo/', '?baz', '/bar/']],
  263. ['?baz/bar/', ['', '?baz', '/bar/']],
  264. ['/foo/?baz', ['/foo/', '?baz', '']],
  265. ['foo/*/bar/*/baz/', ['foo/', '*', '/bar/*/baz/']],
  266. ['*/bar/*/baz/', ['', '*', '/bar/*/baz/']],
  267. ['/foo/*/bar/*', ['/foo/', '*', '/bar/*']],
  268. ['./foo/*/', ['./foo/', '*', '/']],
  269. ['*/', ['', '*', '/']],
  270. ['*foo', ['', '*foo', '']],
  271. ['*', ['', '*', '']],
  272. ];
  273. }
  274. private function expectOpenDir($dirname, $handle) {
  275. $GLOBALS['opendir'][] = ['name' => $dirname, 'handle' => $handle];
  276. }
  277. private function expectFailedOpenDir($dirname) {
  278. $GLOBALS['opendir'][] = ['name' => $dirname, 'handle' => false];
  279. }
  280. private function expectReadDir($handle, $result) {
  281. foreach ($result as $r) {
  282. $GLOBALS['readdir'][] = ['handle' => $handle, 'result' => $r];
  283. }
  284. $GLOBALS['readdir'][] = ['handle' => $handle, 'result' => false];
  285. }
  286. private function expectCloseDir($handle) {
  287. $GLOBALS['closedir'][] = ['handle' => $handle];
  288. }
  289. private function setFileIsDir($file) {
  290. $GLOBALS['is_dir'][] = $file;
  291. }
  292. }
  293. // Mock functions
  294. function opendir($dirname) {
  295. assert(isset($GLOBALS['opendir']), 'opendir called when not expected.');
  296. $expected = array_shift($GLOBALS['opendir']);
  297. assert($expected['name'] === $dirname,
  298. 'opendir got: ' . $dirname . ' expected: ' . $expected['name']);
  299. return $expected['handle'];
  300. }
  301. function readdir($handle) {
  302. assert(isset($GLOBALS['readdir']), 'readdir called when not expected.');
  303. $expected = array_shift($GLOBALS['readdir']);
  304. assert($expected['handle'] === $handle,
  305. 'readdir got: ' . $handle . ' expected: ' . $expected['handle']);
  306. return $expected['result'];
  307. }
  308. function closedir($handle) {
  309. assert(isset($GLOBALS['closedir']), 'closedir called when not expected.');
  310. $expected = array_shift($GLOBALS['closedir']);
  311. assert($expected['handle'] === $handle,
  312. 'closedir got: ' . $handle . ' expected: ' . $expected['handle']);
  313. }
  314. function is_dir($file) {
  315. if (isset($GLOBALS['is_dir'])) {
  316. return in_array($file, $GLOBALS['is_dir']);
  317. } else {
  318. return false;
  319. }
  320. }