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