|
3 | 3 | import os |
4 | 4 | import stat |
5 | 5 | import sys |
6 | | -import contextlib |
| 6 | +from unittest.mock import patch |
7 | 7 |
|
8 | 8 | from distutils import dir_util, errors |
9 | 9 | from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree, |
|
14 | 14 | from test.support import run_unittest |
15 | 15 |
|
16 | 16 |
|
17 | | -@contextlib.context_manager |
18 | | -def patch_obj(obj, attr, replacement): |
19 | | - """ |
20 | | - A poor man's mock.patch.object |
21 | | - """ |
22 | | - orig = getattr(obj, attr) |
23 | | - try: |
24 | | - setattr(obj, attr, replacement) |
25 | | - yield |
26 | | - finally: |
27 | | - setattr(obj, attr, orig) |
28 | | - |
29 | | - |
30 | 17 | class DirUtilTestCase(support.TempdirManager, unittest.TestCase): |
31 | 18 |
|
32 | 19 | def _log(self, msg, *args): |
@@ -135,17 +122,13 @@ def test_ensure_relative(self): |
135 | 122 | self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo') |
136 | 123 | self.assertEqual(ensure_relative('home\\foo'), 'home\\foo') |
137 | 124 |
|
138 | | - def test_copy_tree_exception_in_listdir(self): |
| 125 | + @patch('os.listdir', side_effect=OSError()) |
| 126 | + def test_copy_tree_exception_in_listdir(self, listdir): |
139 | 127 | """ |
140 | 128 | An exception in listdir should raise a DistutilsFileError |
141 | 129 | """ |
142 | | - def new_listdir(path): |
143 | | - raise OSError() |
144 | | - # simulate a transient network error or other failure invoking listdir |
145 | | - with patch_obj(os, 'listdir', new_listdir): |
146 | | - args = 'src', None |
147 | | - exc = errors.DistutilsFileError |
148 | | - self.assertRaises(exc, dir_util.copy_tree, *args) |
| 130 | + with self.assertRaises(errors.DistutilsFileError): |
| 131 | + dir_util.copy_tree('src', None) |
149 | 132 |
|
150 | 133 |
|
151 | 134 | def test_suite(): |
|
0 commit comments