| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import os |
| 6 |
import unittest |
| 7 |
from tempfile import mkstemp |
| 8 |
|
| 9 |
from mozunit import MockedOpen, main |
| 10 |
|
| 11 |
|
| 12 |
class TestMozUnit(unittest.TestCase): |
| 13 |
def test_mocked_open(self): |
| 14 |
# Create a temporary file on the file system. |
| 15 |
(fd, path) = mkstemp() |
| 16 |
with os.fdopen(fd, "w") as file: |
| 17 |
file.write("foobar") |
| 18 |
|
| 19 |
self.assertFalse(os.path.exists("file1")) |
| 20 |
self.assertFalse(os.path.exists("file2")) |
| 21 |
|
| 22 |
with MockedOpen({"file1": "content1", "file2": "content2"}): |
| 23 |
self.assertTrue(os.path.exists("file1")) |
| 24 |
self.assertTrue(os.path.exists("file2")) |
| 25 |
self.assertFalse(os.path.exists("foo/file1")) |
| 26 |
|
| 27 |
# Check the contents of the files given at MockedOpen creation. |
| 28 |
self.assertEqual(open("file1").read(), "content1") |
| 29 |
self.assertEqual(open("file2").read(), "content2") |
| 30 |
|
| 31 |
# Check that overwriting these files alters their content. |
| 32 |
with open("file1", "w") as file: |
| 33 |
file.write("foo") |
| 34 |
self.assertTrue(os.path.exists("file1")) |
| 35 |
self.assertEqual(open("file1").read(), "foo") |
| 36 |
|
| 37 |
# ... but not until the file is closed. |
| 38 |
file = open("file2", "w") |
| 39 |
file.write("bar") |
| 40 |
self.assertEqual(open("file2").read(), "content2") |
| 41 |
file.close() |
| 42 |
self.assertEqual(open("file2").read(), "bar") |
| 43 |
|
| 44 |
# Check that appending to a file does append |
| 45 |
with open("file1", "a") as file: |
| 46 |
file.write("bar") |
| 47 |
self.assertEqual(open("file1").read(), "foobar") |
| 48 |
|
| 49 |
self.assertFalse(os.path.exists("file3")) |
| 50 |
|
| 51 |
# Opening a non-existing file ought to fail. |
| 52 |
self.assertRaises(IOError, open, "file3", "r") |
| 53 |
self.assertFalse(os.path.exists("file3")) |
| 54 |
|
| 55 |
# Check that writing a new file does create the file. |
| 56 |
with open("file3", "w") as file: |
| 57 |
file.write("baz") |
| 58 |
self.assertEqual(open("file3").read(), "baz") |
| 59 |
self.assertTrue(os.path.exists("file3")) |
| 60 |
|
| 61 |
# Check the content of the file created outside MockedOpen. |
| 62 |
self.assertEqual(open(path).read(), "foobar") |
| 63 |
|
| 64 |
# Check that overwriting a file existing on the file system |
| 65 |
# does modify its content. |
| 66 |
with open(path, "w") as file: |
| 67 |
file.write("bazqux") |
| 68 |
self.assertEqual(open(path).read(), "bazqux") |
| 69 |
|
| 70 |
with MockedOpen(): |
| 71 |
# Check that appending to a file existing on the file system |
| 72 |
# does modify its content. |
| 73 |
with open(path, "a") as file: |
| 74 |
file.write("bazqux") |
| 75 |
self.assertEqual(open(path).read(), "foobarbazqux") |
| 76 |
|
| 77 |
# Check that the file was not actually modified on the file system. |
| 78 |
self.assertEqual(open(path).read(), "foobar") |
| 79 |
os.remove(path) |
| 80 |
|
| 81 |
# Check that the file created inside MockedOpen wasn't actually |
| 82 |
# created. |
| 83 |
self.assertRaises(IOError, open, "file3", "r") |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__": |
| 87 |
main() |