fixed a few mocking/cleanup issues in packaging test_util for win32

This commit is contained in:
Tarek Ziade 2011-05-21 10:37:58 +02:00
parent 9904b22b78
commit 94449616da
2 changed files with 20 additions and 15 deletions

View File

@ -112,26 +112,28 @@ class TempdirManager:
def setUp(self):
super(TempdirManager, self).setUp()
self._basetempdir = tempfile.mkdtemp()
self._files = []
def tearDown(self):
shutil.rmtree(self._basetempdir, os.name in ('nt', 'cygwin'))
for handle, name in self._files:
handle.close()
if os.path.exists(name):
try:
os.remove(name)
except OSError as exc:
if exc.errno != errno.ENOENT:
raise
super(TempdirManager, self).tearDown()
def mktempfile(self):
"""Create a read-write temporary file and return it."""
def _delete_file(filename):
try:
os.remove(filename)
except OSError as exc:
if exc.errno != errno.ENOENT:
raise
fd, fn = tempfile.mkstemp(dir=self._basetempdir)
os.close(fd)
fp = open(fn, 'w+')
self.addCleanup(fp.close)
self.addCleanup(_delete_file, fn)
self._files.append((fp, fn))
return fp
def mkdtemp(self):

View File

@ -63,7 +63,9 @@ class FakePopen:
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
pass_fds=()):
self.cmd = args.split()[0]
if isinstance(args, str):
args = args.split()
self.cmd = args[0]
exes = self.test_class._exes
if self.cmd not in exes:
# we don't want to call the system, returning an empty
@ -77,6 +79,9 @@ class FakePopen:
def communicate(self, input=None, timeout=None):
return self.stdout.read(), self.stderr.read()
def wait(self, timeout=None):
return 0
class UtilTestCase(support.EnvironRestorer,
support.TempdirManager,
@ -424,10 +429,8 @@ class UtilTestCase(support.EnvironRestorer,
@unittest.skipUnless(os.name in ('nt', 'posix'),
'runs only under posix or nt')
def test_spawn(self):
# Do not patch subprocess on unix because
# packaging.util._spawn_posix uses it
if os.name in 'posix':
subprocess.Popen = self.old_popen
# no patching of Popen here
subprocess.Popen = self.old_popen
tmpdir = self.mkdtemp()
# creating something executable