[Python-Dev] [Python-checkins] cpython: Issue #16799: Switched from getopt to argparse style in regrtest's argument (original) (raw)
Eric Snow ericsnowcurrently at gmail.com
Thu Aug 29 18:43:23 CEST 2013
- Previous message: [Python-Dev] devguide: Issue #18871: make it more explicit that the test suite should be run before
- Next message: [Python-Dev] [Python-checkins] cpython: Issue #16799: Switched from getopt to argparse style in regrtest's argument
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/testregrtest.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/testregrtest.py +++ b/Lib/test/testregrtest.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 oldparseargs(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 parseargs(self, args): - # This is the same logic as that used in regrtest.main() - parser = regrtest.createparser() - ns = parser.parseargs(args=args) - opts = regrtest.convertnamespacetogetopt(ns) - return opts, ns.args + def checkError(self, args, msg): + with support.capturedstderr() as err, self.assertRaises(SystemExit): + regrtest.parseargs(args) + self.assertIn(msg, err.getvalue()) - def checkargs(self, args, expected=None): - """ - The expected parameter is for cases when the behavior of the new - parseargs differs from the old (but deliberately so). - """ - if expected is None: - try: - expected = oldparseargs(args) - except getopt.GetoptError: - # Suppress usage string output when an argparse.ArgumentError - # error is raised. - with support.capturedstderr(): - self.assertRaises(SystemExit, self.parseargs, args) - return - # The new parseargs() sorts by long option string. - expected[0].sort() - actual = self.parseargs(args) - self.assertEqual(actual, expected) + def testhelp(self): + for opt in '-h', '--help': + with self.subTest(opt=opt): _+ with support.capturedstdout() as out, _ + self.assertRaises(SystemExit): + regrtest.parseargs([opt]) + self.assertIn('Run Python regression tests.', out.getvalue()) + + def testtimeout(self): + ns = regrtest.parseargs(['--timeout', '4.2']) + self.assertEqual(ns.timeout, 4.2) + self.checkError(['--timeout'], 'expected one argument') + self.checkError(['--timeout', 'foo'], 'invalid float value') + + def testwait(self): + ns = regrtest.parseargs(['--wait']) + self.assertTrue(ns.wait) + + def testslaveargs(self): + ns = regrtest.parseargs(['--slaveargs', '[[], {}]']) + self.assertEqual(ns.slaveargs, '[[], {}]') + self.checkError(['--slaveargs'], 'expected one argument') + + def teststart(self): + for opt in '-S', '--start': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, 'foo']) + self.assertEqual(ns.start, 'foo') + self.checkError([opt], 'expected one argument') + + def testverbose(self): + ns = regrtest.parseargs(['-v']) + self.assertEqual(ns.verbose, 1) + ns = regrtest.parseargs(['-vvv']) + self.assertEqual(ns.verbose, 3) + ns = regrtest.parseargs(['--verbose']) + self.assertEqual(ns.verbose, 1) + ns = regrtest.parseargs(['--verbose'] * 3) + self.assertEqual(ns.verbose, 3) + ns = regrtest.parseargs([]) + self.assertEqual(ns.verbose, 0) + + def testverbose2(self): + for opt in '-w', '--verbose2': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.verbose2) + + def testverbose3(self): + for opt in '-W', '--verbose3': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.verbose3) + + def testdebug(self): + for opt in '-d', '--debug': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.debug) + + def testquiet(self): + for opt in '-q', '--quiet': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.quiet) + self.assertEqual(ns.verbose, 0) + + def testslow(self): + for opt in '-o', '--slow': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.printslow) + + def testheader(self): + ns = regrtest.parseargs(['--header']) + self.assertTrue(ns.header) + + def testrandomize(self): + for opt in '-r', '--randomize': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.randomize) + + def testrandseed(self): + ns = regrtest.parseargs(['--randseed', '12345']) + self.assertEqual(ns.randomseed, 12345) + self.assertTrue(ns.randomize) + self.checkError(['--randseed'], 'expected one argument') + self.checkError(['--randseed', 'foo'], 'invalid int value') + + def testfromfile(self): + for opt in '-f', '--fromfile': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, 'foo']) + self.assertEqual(ns.fromfile, 'foo') + self.checkError([opt], 'expected one argument') + self.checkError([opt, 'foo', '-s'], "don't go together") + + def testexclude(self): + for opt in '-x', '--exclude': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.exclude) + + def testsingle(self): + for opt in '-s', '--single': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.single) + self.checkError([opt, '-f', 'foo'], "don't go together") + + def testmatch(self): + for opt in '-m', '--match': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, 'pattern']) + self.assertEqual(ns.matchtests, 'pattern') + self.checkError([opt], 'expected one argument') + + def testfailfast(self): + for opt in '-G', '--failfast': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, '-v']) + self.assertTrue(ns.failfast) + ns = regrtest.parseargs([opt, '-W']) + self.assertTrue(ns.failfast) + self.checkError([opt], '-G/--failfast needs either -v or -W') + + def testuse(self): + for opt in '-u', '--use': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, 'gui,network']) + self.assertEqual(ns.useresources, ['gui', 'network']) + ns = regrtest.parseargs([opt, 'gui,none,network']) + self.assertEqual(ns.useresources, ['network']) + expected = list(regrtest.RESOURCENAMES) + expected.remove('gui') + ns = regrtest.parseargs([opt, 'all,-gui']) + self.assertEqual(ns.useresources, expected) + self.checkError([opt], 'expected one argument') + self.checkError([opt, 'foo'], 'invalid resource') + + def testmemlimit(self): + for opt in '-M', '--memlimit': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, '4G']) + self.assertEqual(ns.memlimit, '4G') + self.checkError([opt], 'expected one argument') + + def testtestdir(self): + ns = regrtest.parseargs(['--testdir', 'foo']) + self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo')) + self.checkError(['--testdir'], 'expected one argument') + + def testfindleaks(self): + for opt in '-l', '--findleaks': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.findleaks) + + def testfindleaks(self): + for opt in '-L', '--runleaks': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.runleaks) + + def testfindleaks(self): + for opt in '-R', '--huntrleaks': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, ':']) + self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt')) + ns = regrtest.parseargs([opt, '6:']) + self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt')) + ns = regrtest.parseargs([opt, ':3']) + self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt')) + ns = regrtest.parseargs([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 testmultiprocess(self): + for opt in '-j', '--multiprocess': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, '2']) + self.assertEqual(ns.usemp, 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 testfindleaks(self): + for opt in '-T', '--coverage': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.trace) + + def testcoverdir(self): + for opt in '-D', '--coverdir': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, 'foo']) + self.assertEqual(ns.coverdir, + os.path.join(support.SAVEDCWD, 'foo')) + self.checkError([opt], 'expected one argument') + + def testnocoverdir(self): + for opt in '-N', '--nocoverdir': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertIsNone(ns.coverdir) + + def testthreshold(self): + for opt in '-t', '--threshold': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt, '1000']) + self.assertEqual(ns.threshold, 1000) + self.checkError([opt], 'expected one argument') + self.checkError([opt, 'foo'], 'invalid int value') + + def testnowindows(self): + for opt in '-n', '--nowindows': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.nowindows) + + def testforever(self): + for opt in '-F', '--forever': + with self.subTest(opt=opt): + ns = regrtest.parseargs([opt]) + self.assertTrue(ns.forever) + def testunrecognizedargument(self): - self.checkargs(['--xxx']) - - def testvaluenotprovided(self): - self.checkargs(['--start']) - - def testshortoption(self): - # getopt returns the short option whereas argparse returns the long. - expected = ([('--quiet', '')], []) - self.checkargs(['-q'], expected=expected) - - def testlongoption(self): - self.checkargs(['--quiet']) + self.checkError(['--xxx'], 'usage:') def testlongoption_partial(self): - self.checkargs(['--qui']) + ns = regrtest.parseargs(['--qui']) + self.assertTrue(ns.quiet) + self.assertEqual(ns.verbose, 0) def testtwooptions(self): - self.checkargs(['--quiet', '--exclude']) - - def testoptionwithvalue(self): - self.checkargs(['--start', 'foo']) + ns = regrtest.parseargs(['--quiet', '--exclude']) + self.assertTrue(ns.quiet) + self.assertEqual(ns.verbose, 0) + self.assertTrue(ns.exclude) def testoptionwithemptystringvalue(self): - self.checkargs(['--start', '']) + ns = regrtest.parseargs(['--start', '']) + self.assertEqual(ns.start, '') def testarg(self): - self.checkargs(['foo']) + ns = regrtest.parseargs(['foo']) + self.assertEqual(ns.args, ['foo']) def testoptionandarg(self): - self.checkargs(['--quiet', 'foo']) + ns = regrtest.parseargs(['--quiet', 'foo']) + self.assertTrue(ns.quiet) + self.assertEqual(ns.verbose, 0) + self.assertEqual(ns.args, ['foo']) - def testfromfile(self): - self.checkargs(['--fromfile', 'file']) - - def testmatch(self): - self.checkargs(['--match', 'pattern']) - - def testrandomize(self): - self.checkargs(['--randomize']) - - -def testmain(): - support.rununittest(name) if name == 'main': - testmain() + 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>
- Previous message: [Python-Dev] devguide: Issue #18871: make it more explicit that the test suite should be run before
- Next message: [Python-Dev] [Python-checkins] cpython: Issue #16799: Switched from getopt to argparse style in regrtest's argument
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]