Browse code

skip read-only dir tests on windows and cleanup tmp dirs correctly

Tobias Schultze authored on 21/12/2015 03:25:29 • Fabien Potencier committed on 26/12/2015 16:40:42
Showing 7 changed files
... ...
@@ -9,35 +9,26 @@
9 9
  * file that was distributed with this source code.
10 10
  */
11 11
 
12
+require_once dirname(dirname(__FILE__)).'/FilesystemHelper.php';
13
+
12 14
 class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
13 15
 {
14
-    private $nonce;
15 16
     private $classname;
16 17
     private $directory;
17 18
     private $cache;
18 19
 
19 20
     protected function setUp()
20 21
     {
21
-        $this->nonce = hash('sha256', uniqid(mt_rand(), true));
22
-        $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$this->nonce;
23
-        $this->directory = sys_get_temp_dir().'/twig-test-'.$this->nonce;
22
+        $nonce = hash('sha256', uniqid(mt_rand(), true));
23
+        $this->classname = '__Twig_Tests_Cache_FilesystemTest_Template_'.$nonce;
24
+        $this->directory = sys_get_temp_dir().'/twig-test';
24 25
         $this->cache = new Twig_Cache_Filesystem($this->directory);
25 26
     }
26 27
 
27 28
     protected function tearDown()
28 29
     {
29 30
         if (file_exists($this->directory)) {
30
-            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::CHILD_FIRST);
31
-            foreach ($iterator as $filename => $fileInfo) {
32
-                if (!$iterator->isDot()) {
33
-                    if ($fileInfo->isDir()) {
34
-                        rmdir($filename);
35
-                    } else {
36
-                        unlink($filename);
37
-                    }
38
-                }
39
-            }
40
-            rmdir($this->directory);
31
+            Twig_Tests_FilesystemHelper::removeDir($this->directory);
41 32
         }
42 33
     }
43 34
 
... ...
@@ -86,10 +77,14 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
86 77
 
87 78
     /**
88 79
      * @expectedException RuntimeException
89
-     * @expectedExceptionMessageRegExp #^Unable to create the cache directory #
80
+     * @expectedExceptionMessage Unable to create the cache directory
90 81
      */
91 82
     public function testWriteFailMkdir()
92 83
     {
84
+        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
85
+            $this->markTestSkipped('Read-only directories not possible on Windows.');
86
+        }
87
+
93 88
         $key = $this->directory.'/cache/cachefile.php';
94 89
         $content = $this->generateSource();
95 90
 
... ...
@@ -104,10 +99,14 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
104 99
 
105 100
     /**
106 101
      * @expectedException RuntimeException
107
-     * @expectedExceptionMessageRegExp #^Unable to write in the cache directory #
102
+     * @expectedExceptionMessage Unable to write in the cache directory
108 103
      */
109 104
     public function testWriteFailDirWritable()
110 105
     {
106
+        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
107
+            $this->markTestSkipped('Read-only directories not possible on Windows.');
108
+        }
109
+
111 110
         $key = $this->directory.'/cache/cachefile.php';
112 111
         $content = $this->generateSource();
113 112
 
... ...
@@ -124,7 +123,7 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
124 123
 
125 124
     /**
126 125
      * @expectedException RuntimeException
127
-     * @expectedExceptionMessageRegExp #^Failed to write cache file #
126
+     * @expectedExceptionMessage Failed to write cache file
128 127
      */
129 128
     public function testWriteFailWriteFile()
130 129
     {
... ...
@@ -9,6 +9,8 @@
9 9
  * file that was distributed with this source code.
10 10
  */
11 11
 
12
+require_once dirname(__FILE__).'/FilesystemHelper.php';
13
+
12 14
 class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
13 15
 {
14 16
     private $deprecations = array();
... ...
@@ -154,8 +156,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
154 156
 
155 157
     public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
156 158
     {
157
-        $uid = function_exists('posix_getuid') ? posix_getuid() : '';
158
-        $cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir().'/twig'.$uid);
159
+        $cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir().'/twig');
159 160
         $options = array('cache' => $cache, 'auto_reload' => false, 'debug' => false);
160 161
 
161 162
         // force compilation
... ...
@@ -178,7 +179,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
178 179
         $output = $twig->render('index', array('foo' => 'bar'));
179 180
         $this->assertEquals('bar', $output);
180 181
 
181
-        unlink($key);
182
+        Twig_Tests_FilesystemHelper::removeDir($dir);
182 183
     }
183 184
 
184 185
     public function testAutoReloadCacheMiss()
... ...
@@ -13,7 +13,7 @@ class Twig_Tests_Extension_SandboxTest extends PHPUnit_Framework_TestCase
13 13
 {
14 14
     protected static $params, $templates;
15 15
 
16
-    public function setUp()
16
+    protected function setUp()
17 17
     {
18 18
         self::$params = array(
19 19
             'name' => 'Fabien',
... ...
@@ -9,13 +9,14 @@
9 9
  * file that was distributed with this source code.
10 10
  */
11 11
 
12
+require_once dirname(__FILE__).'/FilesystemHelper.php';
13
+
12 14
 class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
13 15
 {
14
-    protected $fileName;
15
-    protected $env;
16
-    protected $tmpDir;
16
+    private $env;
17
+    private $tmpDir;
17 18
 
18
-    public function setUp()
19
+    protected function setUp()
19 20
     {
20 21
         $this->tmpDir = sys_get_temp_dir().'/TwigTests';
21 22
         if (!file_exists($this->tmpDir)) {
... ...
@@ -29,13 +30,9 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
29 30
         $this->env = new Twig_Environment(new Twig_Loader_Array(array('index' => 'index', 'index2' => 'index2')), array('cache' => $this->tmpDir));
30 31
     }
31 32
 
32
-    public function tearDown()
33
+    protected function tearDown()
33 34
     {
34
-        if ($this->fileName) {
35
-            unlink($this->fileName);
36
-        }
37
-
38
-        $this->removeDir($this->tmpDir);
35
+        Twig_Tests_FilesystemHelper::removeDir($this->tmpDir);
39 36
     }
40 37
 
41 38
     /**
... ...
@@ -48,7 +45,6 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
48 45
         $cacheFileName = $this->env->getCacheFilename($name);
49 46
 
50 47
         $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
51
-        $this->fileName = $cacheFileName;
52 48
     }
53 49
 
54 50
     /**
... ...
@@ -64,22 +60,4 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
64 60
         $this->env->clearCacheFiles();
65 61
         $this->assertFalse(file_exists($cacheFileName), 'Cache file was not cleared.');
66 62
     }
67
-
68
-    private function removeDir($target)
69
-    {
70
-        $fp = opendir($target);
71
-        while (false !== $file = readdir($fp)) {
72
-            if (in_array($file, array('.', '..'))) {
73
-                continue;
74
-            }
75
-
76
-            if (is_dir($target.'/'.$file)) {
77
-                self::removeDir($target.'/'.$file);
78
-            } else {
79
-                unlink($target.'/'.$file);
80
-            }
81
-        }
82
-        closedir($fp);
83
-        rmdir($target);
84
-    }
85 63
 }
86 64
new file mode 100644
... ...
@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+/*
4
+ * This file is part of Twig.
5
+ *
6
+ * (c) Fabien Potencier
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+class Twig_Tests_FilesystemHelper
13
+{
14
+    public static function removeDir($dir)
15
+    {
16
+        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, PHP_VERSION_ID < 50300 ? 0 : FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
17
+        foreach ($iterator as $filename => $fileInfo) {
18
+            if ($iterator->isDot()) {
19
+                continue;
20
+            }
21
+
22
+            if ($fileInfo->isDir()) {
23
+                rmdir($filename);
24
+            } else {
25
+                unlink($filename);
26
+            }
27
+        }
28
+        rmdir($dir);
29
+    }
30
+}
... ...
@@ -13,7 +13,7 @@ class Twig_Tests_TokenStreamTest extends PHPUnit_Framework_TestCase
13 13
 {
14 14
     protected static $tokens;
15 15
 
16
-    public function setUp()
16
+    protected function setUp()
17 17
     {
18 18
         self::$tokens = array(
19 19
             new Twig_Token(Twig_Token::TEXT_TYPE, 1, 1),
... ...
@@ -144,7 +144,7 @@ class Twig_Test_EscapingTest extends PHPUnit_Framework_TestCase
144 144
 
145 145
     protected $env;
146 146
 
147
-    public function setUp()
147
+    protected function setUp()
148 148
     {
149 149
         $this->env = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
150 150
     }