1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozfile/tests/test_tempfile.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +""" 1.11 +tests for mozfile.NamedTemporaryFile 1.12 +""" 1.13 + 1.14 +import mozfile 1.15 +import os 1.16 +import unittest 1.17 + 1.18 + 1.19 +class TestNamedTemporaryFile(unittest.TestCase): 1.20 + """test our fix for NamedTemporaryFile""" 1.21 + 1.22 + def test_named_temporary_file(self): 1.23 + """ Ensure the fix for re-opening a NamedTemporaryFile works 1.24 + 1.25 + Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777 1.26 + and https://bugzilla.mozilla.org/show_bug.cgi?id=821362 1.27 + """ 1.28 + 1.29 + test_string = "A simple test" 1.30 + with mozfile.NamedTemporaryFile() as temp: 1.31 + # Test we can write to file 1.32 + temp.write(test_string) 1.33 + # Forced flush, so that we can read later 1.34 + temp.flush() 1.35 + 1.36 + # Test we can open the file again on all platforms 1.37 + self.assertEqual(open(temp.name).read(), test_string) 1.38 + 1.39 + def test_iteration(self): 1.40 + """ensure the line iterator works""" 1.41 + 1.42 + # make a file and write to it 1.43 + tf = mozfile.NamedTemporaryFile() 1.44 + notes = ['doe', 'rae', 'mi'] 1.45 + for note in notes: 1.46 + tf.write('%s\n' % note) 1.47 + tf.flush() 1.48 + 1.49 + # now read from it 1.50 + tf.seek(0) 1.51 + lines = [line.rstrip('\n') for line in tf.readlines()] 1.52 + self.assertEqual(lines, notes) 1.53 + 1.54 + # now read from it iteratively 1.55 + lines = [] 1.56 + for line in tf: 1.57 + lines.append(line.strip()) 1.58 + self.assertEqual(lines, []) # because we did not seek(0) 1.59 + tf.seek(0) 1.60 + lines = [] 1.61 + for line in tf: 1.62 + lines.append(line.strip()) 1.63 + self.assertEqual(lines, notes) 1.64 + 1.65 + def test_delete(self): 1.66 + """ensure ``delete=True/False`` works as expected""" 1.67 + 1.68 + # make a deleteable file; ensure it gets cleaned up 1.69 + path = None 1.70 + with mozfile.NamedTemporaryFile(delete=True) as tf: 1.71 + path = tf.name 1.72 + self.assertTrue(isinstance(path, basestring)) 1.73 + self.assertFalse(os.path.exists(path)) 1.74 + 1.75 + # it is also deleted when __del__ is called 1.76 + # here we will do so explicitly 1.77 + tf = mozfile.NamedTemporaryFile(delete=True) 1.78 + path = tf.name 1.79 + self.assertTrue(os.path.exists(path)) 1.80 + del tf 1.81 + self.assertFalse(os.path.exists(path)) 1.82 + 1.83 + # Now the same thing but we won't delete the file 1.84 + path = None 1.85 + try: 1.86 + with mozfile.NamedTemporaryFile(delete=False) as tf: 1.87 + path = tf.name 1.88 + self.assertTrue(os.path.exists(path)) 1.89 + finally: 1.90 + if path and os.path.exists(path): 1.91 + os.remove(path) 1.92 + 1.93 + path = None 1.94 + try: 1.95 + tf = mozfile.NamedTemporaryFile(delete=False) 1.96 + path = tf.name 1.97 + self.assertTrue(os.path.exists(path)) 1.98 + del tf 1.99 + self.assertTrue(os.path.exists(path)) 1.100 + finally: 1.101 + if path and os.path.exists(path): 1.102 + os.remove(path) 1.103 + 1.104 +if __name__ == '__main__': 1.105 + unittest.main()