1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozfile/tests/test_tempdir.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,42 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +# This Source Code Form is subject to the terms of the Mozilla Public 1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.9 + 1.10 +""" 1.11 +tests for mozfile.TemporaryDirectory 1.12 +""" 1.13 + 1.14 +from mozfile import TemporaryDirectory 1.15 +import os 1.16 +import unittest 1.17 + 1.18 + 1.19 +class TestTemporaryDirectory(unittest.TestCase): 1.20 + 1.21 + def test_removed(self): 1.22 + """ensure that a TemporaryDirectory gets removed""" 1.23 + path = None 1.24 + with TemporaryDirectory() as tmp: 1.25 + path = tmp 1.26 + self.assertTrue(os.path.isdir(tmp)) 1.27 + tmpfile = os.path.join(tmp, "a_temp_file") 1.28 + open(tmpfile, "w").write("data") 1.29 + self.assertTrue(os.path.isfile(tmpfile)) 1.30 + self.assertFalse(os.path.isdir(path)) 1.31 + self.assertFalse(os.path.exists(path)) 1.32 + 1.33 + def test_exception(self): 1.34 + """ensure that TemporaryDirectory handles exceptions""" 1.35 + path = None 1.36 + with self.assertRaises(Exception): 1.37 + with TemporaryDirectory() as tmp: 1.38 + path = tmp 1.39 + self.assertTrue(os.path.isdir(tmp)) 1.40 + raise Exception("oops") 1.41 + self.assertFalse(os.path.isdir(path)) 1.42 + self.assertFalse(os.path.exists(path)) 1.43 + 1.44 +if __name__ == '__main__': 1.45 + unittest.main()