aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_utils/py_utils/tempfile_ext_unittest.py
diff options
context:
space:
mode:
Diffstat (limited to 'catapult/common/py_utils/py_utils/tempfile_ext_unittest.py')
-rw-r--r--catapult/common/py_utils/py_utils/tempfile_ext_unittest.py37
1 files changed, 36 insertions, 1 deletions
diff --git a/catapult/common/py_utils/py_utils/tempfile_ext_unittest.py b/catapult/common/py_utils/py_utils/tempfile_ext_unittest.py
index 68446235..76a0efd9 100644
--- a/catapult/common/py_utils/py_utils/tempfile_ext_unittest.py
+++ b/catapult/common/py_utils/py_utils/tempfile_ext_unittest.py
@@ -2,14 +2,15 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import filecmp
import os
+import shutil
from py_utils import tempfile_ext
from pyfakefs import fake_filesystem_unittest
class NamedTemporaryDirectoryTest(fake_filesystem_unittest.TestCase):
-
def setUp(self):
self.setUpPyfakefs()
@@ -37,3 +38,37 @@ class NamedTemporaryDirectoryTest(fake_filesystem_unittest.TestCase):
self.fs.CreateDirectory(test_dir)
with tempfile_ext.NamedTemporaryDirectory(dir=test_dir) as d:
self.assertEquals(test_dir, os.path.dirname(d))
+
+
+class TemporaryFilesTest(fake_filesystem_unittest.TestCase):
+ def setUp(self):
+ self.setUpPyfakefs()
+
+ def tearDown(self):
+ self.tearDownPyfakefs()
+
+ def testNamedTemporaryFile(self):
+ with tempfile_ext.NamedTemporaryFile() as f:
+ self.assertTrue(os.path.isfile(f.name))
+ f.write('<data>')
+ f.close()
+ self.assertTrue(os.path.exists(f.name))
+ with open(f.name) as f2:
+ self.assertEqual(f2.read(), '<data>')
+
+ self.assertFalse(os.path.exists(f.name))
+
+ def testTemporaryFileName(self):
+ with tempfile_ext.TemporaryFileName('foo') as filepath:
+ self.assertTrue(os.path.basename(filepath), 'foo')
+ self.assertFalse(os.path.exists(filepath))
+
+ with open(filepath, 'w') as f:
+ f.write('<data>')
+ self.assertTrue(os.path.exists(filepath))
+
+ shutil.copyfile(filepath, filepath + '.bak')
+ self.assertTrue(filecmp.cmp(filepath, filepath + '.bak'))
+
+ self.assertFalse(os.path.exists(filepath))
+ self.assertFalse(os.path.exists(os.path.dirname(filepath)))