michael@0: #!/usr/bin/env python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: """ michael@0: tests for mozfile.NamedTemporaryFile michael@0: """ michael@0: michael@0: import mozfile michael@0: import os michael@0: import unittest michael@0: michael@0: michael@0: class TestNamedTemporaryFile(unittest.TestCase): michael@0: """test our fix for NamedTemporaryFile""" michael@0: michael@0: def test_named_temporary_file(self): michael@0: """ Ensure the fix for re-opening a NamedTemporaryFile works michael@0: michael@0: Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777 michael@0: and https://bugzilla.mozilla.org/show_bug.cgi?id=821362 michael@0: """ michael@0: michael@0: test_string = "A simple test" michael@0: with mozfile.NamedTemporaryFile() as temp: michael@0: # Test we can write to file michael@0: temp.write(test_string) michael@0: # Forced flush, so that we can read later michael@0: temp.flush() michael@0: michael@0: # Test we can open the file again on all platforms michael@0: self.assertEqual(open(temp.name).read(), test_string) michael@0: michael@0: def test_iteration(self): michael@0: """ensure the line iterator works""" michael@0: michael@0: # make a file and write to it michael@0: tf = mozfile.NamedTemporaryFile() michael@0: notes = ['doe', 'rae', 'mi'] michael@0: for note in notes: michael@0: tf.write('%s\n' % note) michael@0: tf.flush() michael@0: michael@0: # now read from it michael@0: tf.seek(0) michael@0: lines = [line.rstrip('\n') for line in tf.readlines()] michael@0: self.assertEqual(lines, notes) michael@0: michael@0: # now read from it iteratively michael@0: lines = [] michael@0: for line in tf: michael@0: lines.append(line.strip()) michael@0: self.assertEqual(lines, []) # because we did not seek(0) michael@0: tf.seek(0) michael@0: lines = [] michael@0: for line in tf: michael@0: lines.append(line.strip()) michael@0: self.assertEqual(lines, notes) michael@0: michael@0: def test_delete(self): michael@0: """ensure ``delete=True/False`` works as expected""" michael@0: michael@0: # make a deleteable file; ensure it gets cleaned up michael@0: path = None michael@0: with mozfile.NamedTemporaryFile(delete=True) as tf: michael@0: path = tf.name michael@0: self.assertTrue(isinstance(path, basestring)) michael@0: self.assertFalse(os.path.exists(path)) michael@0: michael@0: # it is also deleted when __del__ is called michael@0: # here we will do so explicitly michael@0: tf = mozfile.NamedTemporaryFile(delete=True) michael@0: path = tf.name michael@0: self.assertTrue(os.path.exists(path)) michael@0: del tf michael@0: self.assertFalse(os.path.exists(path)) michael@0: michael@0: # Now the same thing but we won't delete the file michael@0: path = None michael@0: try: michael@0: with mozfile.NamedTemporaryFile(delete=False) as tf: michael@0: path = tf.name michael@0: self.assertTrue(os.path.exists(path)) michael@0: finally: michael@0: if path and os.path.exists(path): michael@0: os.remove(path) michael@0: michael@0: path = None michael@0: try: michael@0: tf = mozfile.NamedTemporaryFile(delete=False) michael@0: path = tf.name michael@0: self.assertTrue(os.path.exists(path)) michael@0: del tf michael@0: self.assertTrue(os.path.exists(path)) michael@0: finally: michael@0: if path and os.path.exists(path): michael@0: os.remove(path) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()