| 1 | # -*- coding: utf-8 -*- |
| 2 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | # See https://llvm.org/LICENSE.txt for license information. |
| 4 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | |
| 6 | import libear as sut |
| 7 | import unittest |
| 8 | import os.path |
| 9 | |
| 10 | |
| 11 | class TemporaryDirectoryTest(unittest.TestCase): |
| 12 | def test_creates_directory(self): |
| 13 | dirname = None |
| 14 | with sut.TemporaryDirectory() as tmpdir: |
| 15 | self.assertTrue(os.path.isdir(tmpdir)) |
| 16 | dirname = tmpdir |
| 17 | self.assertIsNotNone(dirname) |
| 18 | self.assertFalse(os.path.exists(dirname)) |
| 19 | |
| 20 | def test_removes_directory_when_exception(self): |
| 21 | dirname = None |
| 22 | try: |
| 23 | with sut.TemporaryDirectory() as tmpdir: |
| 24 | self.assertTrue(os.path.isdir(tmpdir)) |
| 25 | dirname = tmpdir |
| 26 | raise RuntimeError('message') |
| 27 | except: |
| 28 | self.assertIsNotNone(dirname) |
| 29 | self.assertFalse(os.path.exists(dirname)) |
| 30 | |