Commit 70bbc5c7 authored by Jason R. Coombs's avatar Jason R. Coombs

Use a context manager instead of calling _test.run in multiple places....

Use a context manager instead of calling _test.run in multiple places. Alleviates need to warn about Exceptions
parent d68b3904
...@@ -4,6 +4,7 @@ import io ...@@ -4,6 +4,7 @@ import io
import os import os
import sys import sys
import textwrap import textwrap
import contextlib
# Allow to run setup.py from another directory. # Allow to run setup.py from another directory.
os.chdir(os.path.dirname(os.path.abspath(__file__))) os.chdir(os.path.dirname(os.path.abspath(__file__)))
...@@ -63,19 +64,24 @@ class build_py(_build_py): ...@@ -63,19 +64,24 @@ class build_py(_build_py):
class test(_test): class test(_test):
"""Specific test class to avoid rewriting the entry_points.txt""" """Specific test class to avoid rewriting the entry_points.txt"""
def run(self): def run(self):
with self._save_entry_points():
_test.run(self)
@contextlib.contextmanager
def _save_entry_points(self):
entry_points = os.path.join('setuptools.egg-info', 'entry_points.txt') entry_points = os.path.join('setuptools.egg-info', 'entry_points.txt')
if not os.path.exists(entry_points): if not os.path.exists(entry_points):
_test.run(self) yield
return # even though _test.run will raise SystemExit return
# save the content # save the content
with open(entry_points, 'rb') as f: with open(entry_points, 'rb') as f:
ep_content = f.read() ep_content = f.read()
# run the test # run the tests
try: try:
_test.run(self) yield
finally: finally:
# restore the file # restore the file
with open(entry_points, 'wb') as f: with open(entry_points, 'wb') as f:
......
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