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