tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_tempfile.py (3161B)


      1 #!/usr/bin/env python
      2 
      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/.
      6 
      7 """
      8 tests for mozfile.NamedTemporaryFile
      9 """
     10 
     11 import os
     12 import unittest
     13 
     14 import mozfile
     15 import mozunit
     16 
     17 
     18 class TestNamedTemporaryFile(unittest.TestCase):
     19    """test our fix for NamedTemporaryFile"""
     20 
     21    def test_named_temporary_file(self):
     22        """Ensure the fix for re-opening a NamedTemporaryFile works
     23 
     24        Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777
     25        and https://bugzilla.mozilla.org/show_bug.cgi?id=821362
     26        """
     27 
     28        test_string = b"A simple test"
     29        with mozfile.NamedTemporaryFile() as temp:
     30            # Test we can write to file
     31            temp.write(test_string)
     32            # Forced flush, so that we can read later
     33            temp.flush()
     34 
     35            # Test we can open the file again on all platforms
     36            self.assertEqual(open(temp.name, "rb").read(), test_string)
     37 
     38    def test_iteration(self):
     39        """ensure the line iterator works"""
     40 
     41        # make a file and write to it
     42        tf = mozfile.NamedTemporaryFile()
     43        notes = [b"doe", b"rae", b"mi"]
     44        for note in notes:
     45            tf.write(b"%s\n" % note)
     46        tf.flush()
     47 
     48        # now read from it
     49        tf.seek(0)
     50        lines = [line.rstrip(b"\n") for line in tf.readlines()]
     51        self.assertEqual(lines, notes)
     52 
     53        # now read from it iteratively
     54        lines = []
     55        for line in tf:
     56            lines.append(line.strip())
     57        self.assertEqual(lines, [])  # because we did not seek(0)
     58        tf.seek(0)
     59        lines = []
     60        for line in tf:
     61            lines.append(line.strip())
     62        self.assertEqual(lines, notes)
     63 
     64    def test_delete(self):
     65        """ensure ``delete=True/False`` works as expected"""
     66 
     67        # make a deleteable file; ensure it gets cleaned up
     68        path = None
     69        with mozfile.NamedTemporaryFile(delete=True) as tf:
     70            path = tf.name
     71        self.assertTrue(isinstance(path, str))
     72        self.assertFalse(os.path.exists(path))
     73 
     74        # it is also deleted when __del__ is called
     75        # here we will do so explicitly
     76        tf = mozfile.NamedTemporaryFile(delete=True)
     77        path = tf.name
     78        self.assertTrue(os.path.exists(path))
     79        del tf
     80        self.assertFalse(os.path.exists(path))
     81 
     82        # Now the same thing but we won't delete the file
     83        path = None
     84        try:
     85            with mozfile.NamedTemporaryFile(delete=False) as tf:
     86                path = tf.name
     87            self.assertTrue(os.path.exists(path))
     88        finally:
     89            if path and os.path.exists(path):
     90                os.remove(path)
     91 
     92        path = None
     93        try:
     94            tf = mozfile.NamedTemporaryFile(delete=False)
     95            path = tf.name
     96            self.assertTrue(os.path.exists(path))
     97            del tf
     98            self.assertTrue(os.path.exists(path))
     99        finally:
    100            if path and os.path.exists(path):
    101                os.remove(path)
    102 
    103 
    104 if __name__ == "__main__":
    105    mozunit.main()