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