|
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 mozfile |
|
12 import os |
|
13 import unittest |
|
14 |
|
15 |
|
16 class TestNamedTemporaryFile(unittest.TestCase): |
|
17 """test our fix for NamedTemporaryFile""" |
|
18 |
|
19 def test_named_temporary_file(self): |
|
20 """ Ensure the fix for re-opening a NamedTemporaryFile works |
|
21 |
|
22 Refer to https://bugzilla.mozilla.org/show_bug.cgi?id=818777 |
|
23 and https://bugzilla.mozilla.org/show_bug.cgi?id=821362 |
|
24 """ |
|
25 |
|
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() |
|
32 |
|
33 # Test we can open the file again on all platforms |
|
34 self.assertEqual(open(temp.name).read(), test_string) |
|
35 |
|
36 def test_iteration(self): |
|
37 """ensure the line iterator works""" |
|
38 |
|
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() |
|
45 |
|
46 # now read from it |
|
47 tf.seek(0) |
|
48 lines = [line.rstrip('\n') for line in tf.readlines()] |
|
49 self.assertEqual(lines, notes) |
|
50 |
|
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) |
|
61 |
|
62 def test_delete(self): |
|
63 """ensure ``delete=True/False`` works as expected""" |
|
64 |
|
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)) |
|
71 |
|
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)) |
|
79 |
|
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) |
|
89 |
|
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) |
|
100 |
|
101 if __name__ == '__main__': |
|
102 unittest.main() |