mirror of
https://github.com/python/cpython.git
synced 2025-10-21 14:12:27 +00:00
Issue #15302: Switch regrtest from using getopt to using argparse.
This is the first step in refactoring regrtest to use argparse. The regrtest module's main() function still expects a getopt-style return value rather than an argparse.Namespace instance.
This commit is contained in:
parent
f7cd05d7af
commit
d6c18dcd20
3 changed files with 245 additions and 77 deletions
96
Lib/test/test_regrtest.py
Normal file
96
Lib/test/test_regrtest.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
"""
|
||||
Tests of regrtest.py.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import getopt
|
||||
import unittest
|
||||
from test import regrtest, support
|
||||
|
||||
def old_parse_args(args):
|
||||
"""Parse arguments as regrtest did strictly prior to 3.4.
|
||||
|
||||
Raises getopt.GetoptError on bad arguments.
|
||||
"""
|
||||
return getopt.getopt(args, 'hvqxsoS:rf:lu:t:TD:NLR:FdwWM:nj:Gm:',
|
||||
['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
|
||||
'exclude', 'single', 'slow', 'randomize', 'fromfile=', 'findleaks',
|
||||
'use=', 'threshold=', 'coverdir=', 'nocoverdir',
|
||||
'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
|
||||
'multiprocess=', 'coverage', 'slaveargs=', 'forever', 'debug',
|
||||
'start=', 'nowindows', 'header', 'testdir=', 'timeout=', 'wait',
|
||||
'failfast', 'match='])
|
||||
|
||||
class ParseArgsTestCase(unittest.TestCase):
|
||||
|
||||
"""Test that regrtest._parse_args() matches the prior getopt behavior."""
|
||||
|
||||
def _parse_args(self, args):
|
||||
return regrtest._parse_args(args=args)
|
||||
|
||||
def _check_args(self, args, expected=None):
|
||||
"""
|
||||
The expected parameter is for cases when the behavior of the new
|
||||
parse_args differs from the old (but deliberately so).
|
||||
"""
|
||||
if expected is None:
|
||||
try:
|
||||
expected = old_parse_args(args)
|
||||
except getopt.GetoptError:
|
||||
# Suppress usage string output when an argparse.ArgumentError
|
||||
# error is raised.
|
||||
with support.captured_stderr():
|
||||
self.assertRaises(SystemExit, self._parse_args, args)
|
||||
return
|
||||
# The new parse_args() sorts by long option string.
|
||||
expected[0].sort()
|
||||
actual = self._parse_args(args)
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_unrecognized_argument(self):
|
||||
self._check_args(['--xxx'])
|
||||
|
||||
def test_value_not_provided(self):
|
||||
self._check_args(['--start'])
|
||||
|
||||
def test_short_option(self):
|
||||
# getopt returns the short option whereas argparse returns the long.
|
||||
expected = ([('--quiet', '')], [])
|
||||
self._check_args(['-q'], expected=expected)
|
||||
|
||||
def test_long_option(self):
|
||||
self._check_args(['--quiet'])
|
||||
|
||||
def test_long_option__partial(self):
|
||||
self._check_args(['--qui'])
|
||||
|
||||
def test_two_options(self):
|
||||
self._check_args(['--quiet', '--exclude'])
|
||||
|
||||
def test_option_with_value(self):
|
||||
self._check_args(['--start', 'foo'])
|
||||
|
||||
def test_option_with_empty_string_value(self):
|
||||
self._check_args(['--start', ''])
|
||||
|
||||
def test_arg(self):
|
||||
self._check_args(['foo'])
|
||||
|
||||
def test_option_and_arg(self):
|
||||
self._check_args(['--quiet', 'foo'])
|
||||
|
||||
def test_fromfile(self):
|
||||
self._check_args(['--fromfile', 'file'])
|
||||
|
||||
def test_match(self):
|
||||
self._check_args(['--match', 'pattern'])
|
||||
|
||||
def test_randomize(self):
|
||||
self._check_args(['--randomize'])
|
||||
|
||||
|
||||
def test_main():
|
||||
support.run_unittest(__name__)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
Loading…
Add table
Add a link
Reference in a new issue