Debian Codegraph @

testing/mozbase/mozfile/tests/test_tempdir.py

raw
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.TemporaryDirectory
9 """
10
11 import os
12 import unittest
13
14 import mozunit
15 from mozfile import TemporaryDirectory
16
17
18 class TestTemporaryDirectory(unittest.TestCase):
19 def test_removed(self):
20 """ensure that a TemporaryDirectory gets removed"""
21 path = None
22 with TemporaryDirectory() as tmp:
23 path = tmp
24 self.assertTrue(os.path.isdir(tmp))
25 tmpfile = os.path.join(tmp, "a_temp_file")
26 open(tmpfile, "w").write("data")
27 self.assertTrue(os.path.isfile(tmpfile))
28 self.assertFalse(os.path.isdir(path))
29 self.assertFalse(os.path.exists(path))
30
31 def test_exception(self):
32 """ensure that TemporaryDirectory handles exceptions"""
33 path = None
34 with self.assertRaises(Exception):
35 with TemporaryDirectory() as tmp:
36 path = tmp
37 self.assertTrue(os.path.isdir(tmp))
38 raise Exception("oops")
39 self.assertFalse(os.path.isdir(path))
40 self.assertFalse(os.path.exists(path))
41
42
43 if __name__ == "__main__":
44 mozunit.main()