testing/mozbase/mozfile/tests/test_tempfile.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #!/usr/bin/env python
     3 # This Source Code Form is subject to the terms of the Mozilla Public
     4 # License, v. 2.0. If a copy of the MPL was not distributed with this
     5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
     7 """
     8 tests for mozfile.NamedTemporaryFile
     9 """
    11 import mozfile
    12 import os
    13 import unittest
    16 class TestNamedTemporaryFile(unittest.TestCase):
    17     """test our fix for NamedTemporaryFile"""
    19     def test_named_temporary_file(self):
    20         """ Ensure the fix for re-opening a NamedTemporaryFile works
    22             Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777
    23             and https://bugzilla.mozilla.org/show_bug.cgi?id=821362
    24         """
    26         test_string = "A simple test"
    27         with mozfile.NamedTemporaryFile() as temp:
    28             # Test we can write to file
    29             temp.write(test_string)
    30             # Forced flush, so that we can read later
    31             temp.flush()
    33             # Test we can open the file again on all platforms
    34             self.assertEqual(open(temp.name).read(), test_string)
    36     def test_iteration(self):
    37         """ensure the line iterator works"""
    39         # make a file and write to it
    40         tf = mozfile.NamedTemporaryFile()
    41         notes = ['doe', 'rae', 'mi']
    42         for note in notes:
    43             tf.write('%s\n' % note)
    44         tf.flush()
    46         # now read from it
    47         tf.seek(0)
    48         lines = [line.rstrip('\n') for line in tf.readlines()]
    49         self.assertEqual(lines, notes)
    51         # now read from it iteratively
    52         lines = []
    53         for line in tf:
    54             lines.append(line.strip())
    55         self.assertEqual(lines, [])  # because we did not seek(0)
    56         tf.seek(0)
    57         lines = []
    58         for line in tf:
    59             lines.append(line.strip())
    60         self.assertEqual(lines, notes)
    62     def test_delete(self):
    63         """ensure ``delete=True/False`` works as expected"""
    65         # make a deleteable file; ensure it gets cleaned up
    66         path = None
    67         with mozfile.NamedTemporaryFile(delete=True) as tf:
    68             path = tf.name
    69         self.assertTrue(isinstance(path, basestring))
    70         self.assertFalse(os.path.exists(path))
    72         # it is also deleted when __del__ is called
    73         # here we will do so explicitly
    74         tf = mozfile.NamedTemporaryFile(delete=True)
    75         path = tf.name
    76         self.assertTrue(os.path.exists(path))
    77         del tf
    78         self.assertFalse(os.path.exists(path))
    80         # Now the same thing but we won't delete the file
    81         path = None
    82         try:
    83             with mozfile.NamedTemporaryFile(delete=False) as tf:
    84                 path = tf.name
    85             self.assertTrue(os.path.exists(path))
    86         finally:
    87             if path and os.path.exists(path):
    88                 os.remove(path)
    90         path = None
    91         try:
    92             tf = mozfile.NamedTemporaryFile(delete=False)
    93             path = tf.name
    94             self.assertTrue(os.path.exists(path))
    95             del tf
    96             self.assertTrue(os.path.exists(path))
    97         finally:
    98             if path and os.path.exists(path):
    99                 os.remove(path)
   101 if __name__ == '__main__':
   102     unittest.main()

mercurial