Commit e572ce3f authored by Jason R. Coombs's avatar Jason R. Coombs

#22315: Use technique outlined in test_file_util

parent 4b02e704
...@@ -3,7 +3,7 @@ import unittest ...@@ -3,7 +3,7 @@ import unittest
import os import os
import stat import stat
import sys import sys
import contextlib from unittest.mock import patch
from distutils import dir_util, errors from distutils import dir_util, errors
from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree, from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree,
...@@ -14,19 +14,6 @@ from distutils.tests import support ...@@ -14,19 +14,6 @@ from distutils.tests import support
from test.support import run_unittest from test.support import run_unittest
@contextlib.context_manager
def patch_obj(obj, attr, replacement):
"""
A poor man's mock.patch.object
"""
orig = getattr(obj, attr)
try:
setattr(obj, attr, replacement)
yield
finally:
setattr(obj, attr, orig)
class DirUtilTestCase(support.TempdirManager, unittest.TestCase): class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
def _log(self, msg, *args): def _log(self, msg, *args):
...@@ -135,17 +122,13 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase): ...@@ -135,17 +122,13 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo') self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo')
self.assertEqual(ensure_relative('home\\foo'), 'home\\foo') self.assertEqual(ensure_relative('home\\foo'), 'home\\foo')
def test_copy_tree_exception_in_listdir(self): @patch('os.listdir', side_effect=OSError())
def test_copy_tree_exception_in_listdir(self, listdir):
""" """
An exception in listdir should raise a DistutilsFileError An exception in listdir should raise a DistutilsFileError
""" """
def new_listdir(path): with self.assertRaises(errors.DistutilsFileError):
raise OSError() dir_util.copy_tree('src', None)
# simulate a transient network error or other failure invoking listdir
with patch_obj(os, 'listdir', new_listdir):
args = 'src', None
exc = errors.DistutilsFileError
self.assertRaises(exc, dir_util.copy_tree, *args)
def test_suite(): def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment