[Python-Dev] [Python-checkins] cpython: Issue #16799: Switched from getopt to argparse style in regrtest's argument

Eric Snow ericsnowcurrently at gmail.com
Thu Aug 29 18:43:23 CEST 2013


On Thu, Aug 29, 2013 at 3:27 AM, serhiy.storchaka <
python-checkins at python.org> wrote:

> http://hg.python.org/cpython/rev/997de0edc5bd
> changeset:   85444:997de0edc5bd
> parent:      85442:676bbd5b0254
> user:        Serhiy Storchaka <storchaka at gmail.com>
> date:        Thu Aug 29 12:26:23 2013 +0300
> summary:
>   Issue #16799: Switched from getopt to argparse style in regrtest's
> argument
> parsing.  Added more tests for regrtest's argument parsing.
>
> files:
>   Lib/test/regrtest.py      |  529 +++++++++++--------------
>   Lib/test/test_regrtest.py |  328 ++++++++++++---
>   Misc/NEWS                 |    3 +
>   3 files changed, 500 insertions(+), 360 deletions(-)
>
>
> diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
> --- a/Lib/test/regrtest.py
> +++ b/Lib/test/regrtest.py
> ...
>
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
> --- a/Lib/test/test_regrtest.py
> +++ b/Lib/test/test_regrtest.py
> @@ -4,97 +4,281 @@
>
>  import argparse
>  import getopt
>

We aren't using getopt in this module anymore, are we?

-eric


> +import os.path
>  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's parsing code matches the prior getopt
> behavior."""
> +    """Test regrtest's argument parsing."""
>
> -    def _parse_args(self, args):
> -        # This is the same logic as that used in regrtest.main()
> -        parser = regrtest._create_parser()
> -        ns = parser.parse_args(args=args)
> -        opts = regrtest._convert_namespace_to_getopt(ns)
> -        return opts, ns.args
> +    def checkError(self, args, msg):
> +        with support.captured_stderr() as err,
> self.assertRaises(SystemExit):
> +            regrtest._parse_args(args)
> +        self.assertIn(msg, err.getvalue())
>
> -    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_help(self):
> +        for opt in '-h', '--help':
> +            with self.subTest(opt=opt):
> +                with support.captured_stdout() as out, \
> +                     self.assertRaises(SystemExit):
> +                    regrtest._parse_args([opt])
> +                self.assertIn('Run Python regression tests.',
> out.getvalue())
> +
> +    def test_timeout(self):
> +        ns = regrtest._parse_args(['--timeout', '4.2'])
> +        self.assertEqual(ns.timeout, 4.2)
> +        self.checkError(['--timeout'], 'expected one argument')
> +        self.checkError(['--timeout', 'foo'], 'invalid float value')
> +
> +    def test_wait(self):
> +        ns = regrtest._parse_args(['--wait'])
> +        self.assertTrue(ns.wait)
> +
> +    def test_slaveargs(self):
> +        ns = regrtest._parse_args(['--slaveargs', '[[], {}]'])
> +        self.assertEqual(ns.slaveargs, '[[], {}]')
> +        self.checkError(['--slaveargs'], 'expected one argument')
> +
> +    def test_start(self):
> +        for opt in '-S', '--start':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, 'foo'])
> +                self.assertEqual(ns.start, 'foo')
> +                self.checkError([opt], 'expected one argument')
> +
> +    def test_verbose(self):
> +        ns = regrtest._parse_args(['-v'])
> +        self.assertEqual(ns.verbose, 1)
> +        ns = regrtest._parse_args(['-vvv'])
> +        self.assertEqual(ns.verbose, 3)
> +        ns = regrtest._parse_args(['--verbose'])
> +        self.assertEqual(ns.verbose, 1)
> +        ns = regrtest._parse_args(['--verbose'] * 3)
> +        self.assertEqual(ns.verbose, 3)
> +        ns = regrtest._parse_args([])
> +        self.assertEqual(ns.verbose, 0)
> +
> +    def test_verbose2(self):
> +        for opt in '-w', '--verbose2':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.verbose2)
> +
> +    def test_verbose3(self):
> +        for opt in '-W', '--verbose3':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.verbose3)
> +
> +    def test_debug(self):
> +        for opt in '-d', '--debug':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.debug)
> +
> +    def test_quiet(self):
> +        for opt in '-q', '--quiet':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.quiet)
> +                self.assertEqual(ns.verbose, 0)
> +
> +    def test_slow(self):
> +        for opt in '-o', '--slow':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.print_slow)
> +
> +    def test_header(self):
> +        ns = regrtest._parse_args(['--header'])
> +        self.assertTrue(ns.header)
> +
> +    def test_randomize(self):
> +        for opt in '-r', '--randomize':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.randomize)
> +
> +    def test_randseed(self):
> +        ns = regrtest._parse_args(['--randseed', '12345'])
> +        self.assertEqual(ns.random_seed, 12345)
> +        self.assertTrue(ns.randomize)
> +        self.checkError(['--randseed'], 'expected one argument')
> +        self.checkError(['--randseed', 'foo'], 'invalid int value')
> +
> +    def test_fromfile(self):
> +        for opt in '-f', '--fromfile':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, 'foo'])
> +                self.assertEqual(ns.fromfile, 'foo')
> +                self.checkError([opt], 'expected one argument')
> +                self.checkError([opt, 'foo', '-s'], "don't go together")
> +
> +    def test_exclude(self):
> +        for opt in '-x', '--exclude':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.exclude)
> +
> +    def test_single(self):
> +        for opt in '-s', '--single':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.single)
> +                self.checkError([opt, '-f', 'foo'], "don't go together")
> +
> +    def test_match(self):
> +        for opt in '-m', '--match':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, 'pattern'])
> +                self.assertEqual(ns.match_tests, 'pattern')
> +                self.checkError([opt], 'expected one argument')
> +
> +    def test_failfast(self):
> +        for opt in '-G', '--failfast':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, '-v'])
> +                self.assertTrue(ns.failfast)
> +                ns = regrtest._parse_args([opt, '-W'])
> +                self.assertTrue(ns.failfast)
> +                self.checkError([opt], '-G/--failfast needs either -v or
> -W')
> +
> +    def test_use(self):
> +        for opt in '-u', '--use':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, 'gui,network'])
> +                self.assertEqual(ns.use_resources, ['gui', 'network'])
> +                ns = regrtest._parse_args([opt, 'gui,none,network'])
> +                self.assertEqual(ns.use_resources, ['network'])
> +                expected = list(regrtest.RESOURCE_NAMES)
> +                expected.remove('gui')
> +                ns = regrtest._parse_args([opt, 'all,-gui'])
> +                self.assertEqual(ns.use_resources, expected)
> +                self.checkError([opt], 'expected one argument')
> +                self.checkError([opt, 'foo'], 'invalid resource')
> +
> +    def test_memlimit(self):
> +        for opt in '-M', '--memlimit':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, '4G'])
> +                self.assertEqual(ns.memlimit, '4G')
> +                self.checkError([opt], 'expected one argument')
> +
> +    def test_testdir(self):
> +        ns = regrtest._parse_args(['--testdir', 'foo'])
> +        self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD,
> 'foo'))
> +        self.checkError(['--testdir'], 'expected one argument')
> +
> +    def test_findleaks(self):
> +        for opt in '-l', '--findleaks':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.findleaks)
> +
> +    def test_findleaks(self):
> +        for opt in '-L', '--runleaks':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.runleaks)
> +
> +    def test_findleaks(self):
> +        for opt in '-R', '--huntrleaks':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, ':'])
> +                self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt'))
> +                ns = regrtest._parse_args([opt, '6:'])
> +                self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt'))
> +                ns = regrtest._parse_args([opt, ':3'])
> +                self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt'))
> +                ns = regrtest._parse_args([opt, '6:3:leaks.log'])
> +                self.assertEqual(ns.huntrleaks, (6, 3, 'leaks.log'))
> +                self.checkError([opt], 'expected one argument')
> +                self.checkError([opt, '6'],
> +                                'needs 2 or 3 colon-separated arguments')
> +                self.checkError([opt, 'foo:'], 'invalid huntrleaks value')
> +                self.checkError([opt, '6:foo'], 'invalid huntrleaks
> value')
> +
> +    def test_multiprocess(self):
> +        for opt in '-j', '--multiprocess':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, '2'])
> +                self.assertEqual(ns.use_mp, 2)
> +                self.checkError([opt], 'expected one argument')
> +                self.checkError([opt, 'foo'], 'invalid int value')
> +                self.checkError([opt, '2', '-T'], "don't go together")
> +                self.checkError([opt, '2', '-l'], "don't go together")
> +                self.checkError([opt, '2', '-M', '4G'], "don't go
> together")
> +
> +    def test_findleaks(self):
> +        for opt in '-T', '--coverage':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.trace)
> +
> +    def test_coverdir(self):
> +        for opt in '-D', '--coverdir':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, 'foo'])
> +                self.assertEqual(ns.coverdir,
> +                                 os.path.join(support.SAVEDCWD, 'foo'))
> +                self.checkError([opt], 'expected one argument')
> +
> +    def test_nocoverdir(self):
> +        for opt in '-N', '--nocoverdir':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertIsNone(ns.coverdir)
> +
> +    def test_threshold(self):
> +        for opt in '-t', '--threshold':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt, '1000'])
> +                self.assertEqual(ns.threshold, 1000)
> +                self.checkError([opt], 'expected one argument')
> +                self.checkError([opt, 'foo'], 'invalid int value')
> +
> +    def test_nowindows(self):
> +        for opt in '-n', '--nowindows':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.nowindows)
> +
> +    def test_forever(self):
> +        for opt in '-F', '--forever':
> +            with self.subTest(opt=opt):
> +                ns = regrtest._parse_args([opt])
> +                self.assertTrue(ns.forever)
> +
>
>      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'])
> +        self.checkError(['--xxx'], 'usage:')
>
>      def test_long_option__partial(self):
> -        self._check_args(['--qui'])
> +        ns = regrtest._parse_args(['--qui'])
> +        self.assertTrue(ns.quiet)
> +        self.assertEqual(ns.verbose, 0)
>
>      def test_two_options(self):
> -        self._check_args(['--quiet', '--exclude'])
> -
> -    def test_option_with_value(self):
> -        self._check_args(['--start', 'foo'])
> +        ns = regrtest._parse_args(['--quiet', '--exclude'])
> +        self.assertTrue(ns.quiet)
> +        self.assertEqual(ns.verbose, 0)
> +        self.assertTrue(ns.exclude)
>
>      def test_option_with_empty_string_value(self):
> -        self._check_args(['--start', ''])
> +        ns = regrtest._parse_args(['--start', ''])
> +        self.assertEqual(ns.start, '')
>
>      def test_arg(self):
> -        self._check_args(['foo'])
> +        ns = regrtest._parse_args(['foo'])
> +        self.assertEqual(ns.args, ['foo'])
>
>      def test_option_and_arg(self):
> -        self._check_args(['--quiet', 'foo'])
> +        ns = regrtest._parse_args(['--quiet', 'foo'])
> +        self.assertTrue(ns.quiet)
> +        self.assertEqual(ns.verbose, 0)
> +        self.assertEqual(ns.args, ['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()
> +    unittest.main()
> diff --git a/Misc/NEWS b/Misc/NEWS
> --- a/Misc/NEWS
> +++ b/Misc/NEWS
> @@ -153,6 +153,9 @@
>  Tests
>  -----
>
> +- Issue #16799: Switched from getopt to argparse style in regrtest's
> argument
> +  parsing.  Added more tests for regrtest's argument parsing.
> +
>  - Issue #18792: Use "127.0.0.1" or "::1" instead of "localhost" as much as
>    possible, since "localhost" goes through a DNS lookup under recent
> Windows
>    versions.
>
> --
> Repository URL: http://hg.python.org/cpython
>
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130829/5583f893/attachment-0001.html>


More information about the Python-Dev mailing list