Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / config / tests / unit-mozunit.py
blob0c18ec7227ae01d3f2a0308e30b9f4e5d6444a95
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/.
5 from __future__ import absolute_import
6 import os
7 from mozunit import main, MockedOpen
8 import unittest
9 from tempfile import mkstemp
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")
19 self.assertFalse(os.path.exists("file1"))
20 self.assertFalse(os.path.exists("file2"))
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"))
27 # Check the contents of the files given at MockedOpen creation.
28 self.assertEqual(open("file1", "r").read(), "content1")
29 self.assertEqual(open("file2", "r").read(), "content2")
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", "r").read(), "foo")
37 # ... but not until the file is closed.
38 file = open("file2", "w")
39 file.write("bar")
40 self.assertEqual(open("file2", "r").read(), "content2")
41 file.close()
42 self.assertEqual(open("file2", "r").read(), "bar")
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", "r").read(), "foobar")
49 self.assertFalse(os.path.exists("file3"))
51 # Opening a non-existing file ought to fail.
52 self.assertRaises(IOError, open, "file3", "r")
53 self.assertFalse(os.path.exists("file3"))
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", "r").read(), "baz")
59 self.assertTrue(os.path.exists("file3"))
61 # Check the content of the file created outside MockedOpen.
62 self.assertEqual(open(path, "r").read(), "foobar")
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, "r").read(), "bazqux")
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, "r").read(), "foobarbazqux")
77 # Check that the file was not actually modified on the file system.
78 self.assertEqual(open(path, "r").read(), "foobar")
79 os.remove(path)
81 # Check that the file created inside MockedOpen wasn't actually
82 # created.
83 self.assertRaises(IOError, open, "file3", "r")
86 if __name__ == "__main__":
87 main()