Commit 579f7355 authored by Tim Peters's avatar Tim Peters

Be more careful about reverting mutuations to system-wide (sys) variables.

This fixes 15 spurious test failures on Windows (probably all due to
the test leaving a wrong path in sys.argv[0], which then prevented
regrtest.py from finding the expected-output files for tests running
after test_optparse).
parent 9c8fe1a9
......@@ -125,19 +125,25 @@ and kwargs %(kwargs)r
def assertParseFail(self, cmdline_args, expected_output):
"""Assert the parser fails with the expected message."""
save_stderr = sys.stderr
try:
sys.stderr = StringIO()
self.assertRaises(self.parser.parse_args, (cmdline_args,), None,
SystemExit, expected_output,
self.redirected_stderr)
sys.stderr = sys.__stderr__
finally:
sys.stderr = save_stderr
def assertStdoutEquals(self, cmdline_args, expected_output):
"""Assert the parser prints the expected output on stdout."""
save_stdout = sys.stdout
try:
sys.stdout = StringIO()
self.assertRaises(self.parser.parse_args, (cmdline_args,), None,
SystemExit, expected_output,
self.redirected_stdout)
sys.stdout = sys.__stdout__
finally:
sys.stdout = save_stdout
def assertTypeError(self, func, expected_output, *args):
"""Assert a TypeError is raised when executing func."""
......@@ -426,6 +432,9 @@ class TestProgName(BaseTest):
def test_default_progname(self):
# Make sure that program name taken from sys.argv[0] by default.
save_argv = sys.argv[:]
try:
# XXX Should the path be hard-coding forward-slashes?
sys.argv[0] = "/foo/bar/baz.py"
parser = OptionParser("usage: %prog ...", version="%prog 1.2")
expected_usage = "usage: baz.py ...\n"
......@@ -436,6 +445,8 @@ class TestProgName(BaseTest):
"options:\n"
" --version show program's version number and exit\n"
" -h, --help show this help message and exit\n")
finally:
sys.argv[:] = save_argv
def test_custom_progname(self):
parser = OptionParser(prog="thingy",
......
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