| 1 | n/a | # |
|---|
| 2 | n/a | # Test suite for Optik. Supplied by Johannes Gijsbers |
|---|
| 3 | n/a | # (taradino@softhome.net) -- translated from the original Optik |
|---|
| 4 | n/a | # test suite to this PyUnit-based version. |
|---|
| 5 | n/a | # |
|---|
| 6 | n/a | # $Id$ |
|---|
| 7 | n/a | # |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | import sys |
|---|
| 10 | n/a | import os |
|---|
| 11 | n/a | import re |
|---|
| 12 | n/a | import copy |
|---|
| 13 | n/a | import unittest |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from io import StringIO |
|---|
| 16 | n/a | from test import support |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | import optparse |
|---|
| 20 | n/a | from optparse import make_option, Option, \ |
|---|
| 21 | n/a | TitledHelpFormatter, OptionParser, OptionGroup, \ |
|---|
| 22 | n/a | SUPPRESS_USAGE, OptionError, OptionConflictError, \ |
|---|
| 23 | n/a | BadOptionError, OptionValueError, Values |
|---|
| 24 | n/a | from optparse import _match_abbrev |
|---|
| 25 | n/a | from optparse import _parse_num |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | retype = type(re.compile('')) |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | class InterceptedError(Exception): |
|---|
| 30 | n/a | def __init__(self, |
|---|
| 31 | n/a | error_message=None, |
|---|
| 32 | n/a | exit_status=None, |
|---|
| 33 | n/a | exit_message=None): |
|---|
| 34 | n/a | self.error_message = error_message |
|---|
| 35 | n/a | self.exit_status = exit_status |
|---|
| 36 | n/a | self.exit_message = exit_message |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def __str__(self): |
|---|
| 39 | n/a | return self.error_message or self.exit_message or "intercepted error" |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | class InterceptingOptionParser(OptionParser): |
|---|
| 42 | n/a | def exit(self, status=0, msg=None): |
|---|
| 43 | n/a | raise InterceptedError(exit_status=status, exit_message=msg) |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def error(self, msg): |
|---|
| 46 | n/a | raise InterceptedError(error_message=msg) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | class BaseTest(unittest.TestCase): |
|---|
| 50 | n/a | def assertParseOK(self, args, expected_opts, expected_positional_args): |
|---|
| 51 | n/a | """Assert the options are what we expected when parsing arguments. |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | Otherwise, fail with a nicely formatted message. |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | Keyword arguments: |
|---|
| 56 | n/a | args -- A list of arguments to parse with OptionParser. |
|---|
| 57 | n/a | expected_opts -- The options expected. |
|---|
| 58 | n/a | expected_positional_args -- The positional arguments expected. |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | Returns the options and positional args for further testing. |
|---|
| 61 | n/a | """ |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | (options, positional_args) = self.parser.parse_args(args) |
|---|
| 64 | n/a | optdict = vars(options) |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | self.assertEqual(optdict, expected_opts, |
|---|
| 67 | n/a | """ |
|---|
| 68 | n/a | Options are %(optdict)s. |
|---|
| 69 | n/a | Should be %(expected_opts)s. |
|---|
| 70 | n/a | Args were %(args)s.""" % locals()) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | self.assertEqual(positional_args, expected_positional_args, |
|---|
| 73 | n/a | """ |
|---|
| 74 | n/a | Positional arguments are %(positional_args)s. |
|---|
| 75 | n/a | Should be %(expected_positional_args)s. |
|---|
| 76 | n/a | Args were %(args)s.""" % locals ()) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | return (options, positional_args) |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def assertRaises(self, |
|---|
| 81 | n/a | func, |
|---|
| 82 | n/a | args, |
|---|
| 83 | n/a | kwargs, |
|---|
| 84 | n/a | expected_exception, |
|---|
| 85 | n/a | expected_message): |
|---|
| 86 | n/a | """ |
|---|
| 87 | n/a | Assert that the expected exception is raised when calling a |
|---|
| 88 | n/a | function, and that the right error message is included with |
|---|
| 89 | n/a | that exception. |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | Arguments: |
|---|
| 92 | n/a | func -- the function to call |
|---|
| 93 | n/a | args -- positional arguments to `func` |
|---|
| 94 | n/a | kwargs -- keyword arguments to `func` |
|---|
| 95 | n/a | expected_exception -- exception that should be raised |
|---|
| 96 | n/a | expected_message -- expected exception message (or pattern |
|---|
| 97 | n/a | if a compiled regex object) |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | Returns the exception raised for further testing. |
|---|
| 100 | n/a | """ |
|---|
| 101 | n/a | if args is None: |
|---|
| 102 | n/a | args = () |
|---|
| 103 | n/a | if kwargs is None: |
|---|
| 104 | n/a | kwargs = {} |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | try: |
|---|
| 107 | n/a | func(*args, **kwargs) |
|---|
| 108 | n/a | except expected_exception as err: |
|---|
| 109 | n/a | actual_message = str(err) |
|---|
| 110 | n/a | if isinstance(expected_message, retype): |
|---|
| 111 | n/a | self.assertTrue(expected_message.search(actual_message), |
|---|
| 112 | n/a | """\ |
|---|
| 113 | n/a | expected exception message pattern: |
|---|
| 114 | n/a | /%s/ |
|---|
| 115 | n/a | actual exception message: |
|---|
| 116 | n/a | '''%s''' |
|---|
| 117 | n/a | """ % (expected_message.pattern, actual_message)) |
|---|
| 118 | n/a | else: |
|---|
| 119 | n/a | self.assertEqual(actual_message, |
|---|
| 120 | n/a | expected_message, |
|---|
| 121 | n/a | """\ |
|---|
| 122 | n/a | expected exception message: |
|---|
| 123 | n/a | '''%s''' |
|---|
| 124 | n/a | actual exception message: |
|---|
| 125 | n/a | '''%s''' |
|---|
| 126 | n/a | """ % (expected_message, actual_message)) |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | return err |
|---|
| 129 | n/a | else: |
|---|
| 130 | n/a | self.fail("""expected exception %(expected_exception)s not raised |
|---|
| 131 | n/a | called %(func)r |
|---|
| 132 | n/a | with args %(args)r |
|---|
| 133 | n/a | and kwargs %(kwargs)r |
|---|
| 134 | n/a | """ % locals ()) |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | # -- Assertions used in more than one class -------------------- |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def assertParseFail(self, cmdline_args, expected_output): |
|---|
| 140 | n/a | """ |
|---|
| 141 | n/a | Assert the parser fails with the expected message. Caller |
|---|
| 142 | n/a | must ensure that self.parser is an InterceptingOptionParser. |
|---|
| 143 | n/a | """ |
|---|
| 144 | n/a | try: |
|---|
| 145 | n/a | self.parser.parse_args(cmdline_args) |
|---|
| 146 | n/a | except InterceptedError as err: |
|---|
| 147 | n/a | self.assertEqual(err.error_message, expected_output) |
|---|
| 148 | n/a | else: |
|---|
| 149 | n/a | self.assertFalse("expected parse failure") |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | def assertOutput(self, |
|---|
| 152 | n/a | cmdline_args, |
|---|
| 153 | n/a | expected_output, |
|---|
| 154 | n/a | expected_status=0, |
|---|
| 155 | n/a | expected_error=None): |
|---|
| 156 | n/a | """Assert the parser prints the expected output on stdout.""" |
|---|
| 157 | n/a | save_stdout = sys.stdout |
|---|
| 158 | n/a | try: |
|---|
| 159 | n/a | try: |
|---|
| 160 | n/a | sys.stdout = StringIO() |
|---|
| 161 | n/a | self.parser.parse_args(cmdline_args) |
|---|
| 162 | n/a | finally: |
|---|
| 163 | n/a | output = sys.stdout.getvalue() |
|---|
| 164 | n/a | sys.stdout = save_stdout |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | except InterceptedError as err: |
|---|
| 167 | n/a | self.assertTrue( |
|---|
| 168 | n/a | isinstance(output, str), |
|---|
| 169 | n/a | "expected output to be an ordinary string, not %r" |
|---|
| 170 | n/a | % type(output)) |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | if output != expected_output: |
|---|
| 173 | n/a | self.fail("expected: \n'''\n" + expected_output + |
|---|
| 174 | n/a | "'''\nbut got \n'''\n" + output + "'''") |
|---|
| 175 | n/a | self.assertEqual(err.exit_status, expected_status) |
|---|
| 176 | n/a | self.assertEqual(err.exit_message, expected_error) |
|---|
| 177 | n/a | else: |
|---|
| 178 | n/a | self.assertFalse("expected parser.exit()") |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def assertTypeError(self, func, expected_message, *args): |
|---|
| 181 | n/a | """Assert that TypeError is raised when executing func.""" |
|---|
| 182 | n/a | self.assertRaises(func, args, None, TypeError, expected_message) |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def assertHelp(self, parser, expected_help): |
|---|
| 185 | n/a | actual_help = parser.format_help() |
|---|
| 186 | n/a | if actual_help != expected_help: |
|---|
| 187 | n/a | raise self.failureException( |
|---|
| 188 | n/a | 'help text failure; expected:\n"' + |
|---|
| 189 | n/a | expected_help + '"; got:\n"' + |
|---|
| 190 | n/a | actual_help + '"\n') |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | # -- Test make_option() aka Option ------------------------------------- |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | # It's not necessary to test correct options here. All the tests in the |
|---|
| 195 | n/a | # parser.parse_args() section deal with those, because they're needed |
|---|
| 196 | n/a | # there. |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | class TestOptionChecks(BaseTest): |
|---|
| 199 | n/a | def setUp(self): |
|---|
| 200 | n/a | self.parser = OptionParser(usage=SUPPRESS_USAGE) |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | def assertOptionError(self, expected_message, args=[], kwargs={}): |
|---|
| 203 | n/a | self.assertRaises(make_option, args, kwargs, |
|---|
| 204 | n/a | OptionError, expected_message) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | def test_opt_string_empty(self): |
|---|
| 207 | n/a | self.assertTypeError(make_option, |
|---|
| 208 | n/a | "at least one option string must be supplied") |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | def test_opt_string_too_short(self): |
|---|
| 211 | n/a | self.assertOptionError( |
|---|
| 212 | n/a | "invalid option string 'b': must be at least two characters long", |
|---|
| 213 | n/a | ["b"]) |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | def test_opt_string_short_invalid(self): |
|---|
| 216 | n/a | self.assertOptionError( |
|---|
| 217 | n/a | "invalid short option string '--': must be " |
|---|
| 218 | n/a | "of the form -x, (x any non-dash char)", |
|---|
| 219 | n/a | ["--"]) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def test_opt_string_long_invalid(self): |
|---|
| 222 | n/a | self.assertOptionError( |
|---|
| 223 | n/a | "invalid long option string '---': " |
|---|
| 224 | n/a | "must start with --, followed by non-dash", |
|---|
| 225 | n/a | ["---"]) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def test_attr_invalid(self): |
|---|
| 228 | n/a | self.assertOptionError( |
|---|
| 229 | n/a | "option -b: invalid keyword arguments: bar, foo", |
|---|
| 230 | n/a | ["-b"], {'foo': None, 'bar': None}) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def test_action_invalid(self): |
|---|
| 233 | n/a | self.assertOptionError( |
|---|
| 234 | n/a | "option -b: invalid action: 'foo'", |
|---|
| 235 | n/a | ["-b"], {'action': 'foo'}) |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | def test_type_invalid(self): |
|---|
| 238 | n/a | self.assertOptionError( |
|---|
| 239 | n/a | "option -b: invalid option type: 'foo'", |
|---|
| 240 | n/a | ["-b"], {'type': 'foo'}) |
|---|
| 241 | n/a | self.assertOptionError( |
|---|
| 242 | n/a | "option -b: invalid option type: 'tuple'", |
|---|
| 243 | n/a | ["-b"], {'type': tuple}) |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def test_no_type_for_action(self): |
|---|
| 246 | n/a | self.assertOptionError( |
|---|
| 247 | n/a | "option -b: must not supply a type for action 'count'", |
|---|
| 248 | n/a | ["-b"], {'action': 'count', 'type': 'int'}) |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def test_no_choices_list(self): |
|---|
| 251 | n/a | self.assertOptionError( |
|---|
| 252 | n/a | "option -b/--bad: must supply a list of " |
|---|
| 253 | n/a | "choices for type 'choice'", |
|---|
| 254 | n/a | ["-b", "--bad"], {'type': "choice"}) |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | def test_bad_choices_list(self): |
|---|
| 257 | n/a | typename = type('').__name__ |
|---|
| 258 | n/a | self.assertOptionError( |
|---|
| 259 | n/a | "option -b/--bad: choices must be a list of " |
|---|
| 260 | n/a | "strings ('%s' supplied)" % typename, |
|---|
| 261 | n/a | ["-b", "--bad"], |
|---|
| 262 | n/a | {'type': "choice", 'choices':"bad choices"}) |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | def test_no_choices_for_type(self): |
|---|
| 265 | n/a | self.assertOptionError( |
|---|
| 266 | n/a | "option -b: must not supply choices for type 'int'", |
|---|
| 267 | n/a | ["-b"], {'type': 'int', 'choices':"bad"}) |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def test_no_const_for_action(self): |
|---|
| 270 | n/a | self.assertOptionError( |
|---|
| 271 | n/a | "option -b: 'const' must not be supplied for action 'store'", |
|---|
| 272 | n/a | ["-b"], {'action': 'store', 'const': 1}) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | def test_no_nargs_for_action(self): |
|---|
| 275 | n/a | self.assertOptionError( |
|---|
| 276 | n/a | "option -b: 'nargs' must not be supplied for action 'count'", |
|---|
| 277 | n/a | ["-b"], {'action': 'count', 'nargs': 2}) |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | def test_callback_not_callable(self): |
|---|
| 280 | n/a | self.assertOptionError( |
|---|
| 281 | n/a | "option -b: callback not callable: 'foo'", |
|---|
| 282 | n/a | ["-b"], {'action': 'callback', |
|---|
| 283 | n/a | 'callback': 'foo'}) |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def dummy(self): |
|---|
| 286 | n/a | pass |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | def test_callback_args_no_tuple(self): |
|---|
| 289 | n/a | self.assertOptionError( |
|---|
| 290 | n/a | "option -b: callback_args, if supplied, " |
|---|
| 291 | n/a | "must be a tuple: not 'foo'", |
|---|
| 292 | n/a | ["-b"], {'action': 'callback', |
|---|
| 293 | n/a | 'callback': self.dummy, |
|---|
| 294 | n/a | 'callback_args': 'foo'}) |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | def test_callback_kwargs_no_dict(self): |
|---|
| 297 | n/a | self.assertOptionError( |
|---|
| 298 | n/a | "option -b: callback_kwargs, if supplied, " |
|---|
| 299 | n/a | "must be a dict: not 'foo'", |
|---|
| 300 | n/a | ["-b"], {'action': 'callback', |
|---|
| 301 | n/a | 'callback': self.dummy, |
|---|
| 302 | n/a | 'callback_kwargs': 'foo'}) |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | def test_no_callback_for_action(self): |
|---|
| 305 | n/a | self.assertOptionError( |
|---|
| 306 | n/a | "option -b: callback supplied ('foo') for non-callback option", |
|---|
| 307 | n/a | ["-b"], {'action': 'store', |
|---|
| 308 | n/a | 'callback': 'foo'}) |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | def test_no_callback_args_for_action(self): |
|---|
| 311 | n/a | self.assertOptionError( |
|---|
| 312 | n/a | "option -b: callback_args supplied for non-callback option", |
|---|
| 313 | n/a | ["-b"], {'action': 'store', |
|---|
| 314 | n/a | 'callback_args': 'foo'}) |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | def test_no_callback_kwargs_for_action(self): |
|---|
| 317 | n/a | self.assertOptionError( |
|---|
| 318 | n/a | "option -b: callback_kwargs supplied for non-callback option", |
|---|
| 319 | n/a | ["-b"], {'action': 'store', |
|---|
| 320 | n/a | 'callback_kwargs': 'foo'}) |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | def test_no_single_dash(self): |
|---|
| 323 | n/a | self.assertOptionError( |
|---|
| 324 | n/a | "invalid long option string '-debug': " |
|---|
| 325 | n/a | "must start with --, followed by non-dash", |
|---|
| 326 | n/a | ["-debug"]) |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | self.assertOptionError( |
|---|
| 329 | n/a | "option -d: invalid long option string '-debug': must start with" |
|---|
| 330 | n/a | " --, followed by non-dash", |
|---|
| 331 | n/a | ["-d", "-debug"]) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | self.assertOptionError( |
|---|
| 334 | n/a | "invalid long option string '-debug': " |
|---|
| 335 | n/a | "must start with --, followed by non-dash", |
|---|
| 336 | n/a | ["-debug", "--debug"]) |
|---|
| 337 | n/a | |
|---|
| 338 | n/a | class TestOptionParser(BaseTest): |
|---|
| 339 | n/a | def setUp(self): |
|---|
| 340 | n/a | self.parser = OptionParser() |
|---|
| 341 | n/a | self.parser.add_option("-v", "--verbose", "-n", "--noisy", |
|---|
| 342 | n/a | action="store_true", dest="verbose") |
|---|
| 343 | n/a | self.parser.add_option("-q", "--quiet", "--silent", |
|---|
| 344 | n/a | action="store_false", dest="verbose") |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def test_add_option_no_Option(self): |
|---|
| 347 | n/a | self.assertTypeError(self.parser.add_option, |
|---|
| 348 | n/a | "not an Option instance: None", None) |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | def test_add_option_invalid_arguments(self): |
|---|
| 351 | n/a | self.assertTypeError(self.parser.add_option, |
|---|
| 352 | n/a | "invalid arguments", None, None) |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | def test_get_option(self): |
|---|
| 355 | n/a | opt1 = self.parser.get_option("-v") |
|---|
| 356 | n/a | self.assertIsInstance(opt1, Option) |
|---|
| 357 | n/a | self.assertEqual(opt1._short_opts, ["-v", "-n"]) |
|---|
| 358 | n/a | self.assertEqual(opt1._long_opts, ["--verbose", "--noisy"]) |
|---|
| 359 | n/a | self.assertEqual(opt1.action, "store_true") |
|---|
| 360 | n/a | self.assertEqual(opt1.dest, "verbose") |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | def test_get_option_equals(self): |
|---|
| 363 | n/a | opt1 = self.parser.get_option("-v") |
|---|
| 364 | n/a | opt2 = self.parser.get_option("--verbose") |
|---|
| 365 | n/a | opt3 = self.parser.get_option("-n") |
|---|
| 366 | n/a | opt4 = self.parser.get_option("--noisy") |
|---|
| 367 | n/a | self.assertTrue(opt1 is opt2 is opt3 is opt4) |
|---|
| 368 | n/a | |
|---|
| 369 | n/a | def test_has_option(self): |
|---|
| 370 | n/a | self.assertTrue(self.parser.has_option("-v")) |
|---|
| 371 | n/a | self.assertTrue(self.parser.has_option("--verbose")) |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | def assertTrueremoved(self): |
|---|
| 374 | n/a | self.assertTrue(self.parser.get_option("-v") is None) |
|---|
| 375 | n/a | self.assertTrue(self.parser.get_option("--verbose") is None) |
|---|
| 376 | n/a | self.assertTrue(self.parser.get_option("-n") is None) |
|---|
| 377 | n/a | self.assertTrue(self.parser.get_option("--noisy") is None) |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | self.assertFalse(self.parser.has_option("-v")) |
|---|
| 380 | n/a | self.assertFalse(self.parser.has_option("--verbose")) |
|---|
| 381 | n/a | self.assertFalse(self.parser.has_option("-n")) |
|---|
| 382 | n/a | self.assertFalse(self.parser.has_option("--noisy")) |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | self.assertTrue(self.parser.has_option("-q")) |
|---|
| 385 | n/a | self.assertTrue(self.parser.has_option("--silent")) |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | def test_remove_short_opt(self): |
|---|
| 388 | n/a | self.parser.remove_option("-n") |
|---|
| 389 | n/a | self.assertTrueremoved() |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | def test_remove_long_opt(self): |
|---|
| 392 | n/a | self.parser.remove_option("--verbose") |
|---|
| 393 | n/a | self.assertTrueremoved() |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | def test_remove_nonexistent(self): |
|---|
| 396 | n/a | self.assertRaises(self.parser.remove_option, ('foo',), None, |
|---|
| 397 | n/a | ValueError, "no such option 'foo'") |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | @support.impl_detail('Relies on sys.getrefcount', cpython=True) |
|---|
| 400 | n/a | def test_refleak(self): |
|---|
| 401 | n/a | # If an OptionParser is carrying around a reference to a large |
|---|
| 402 | n/a | # object, various cycles can prevent it from being GC'd in |
|---|
| 403 | n/a | # a timely fashion. destroy() breaks the cycles to ensure stuff |
|---|
| 404 | n/a | # can be cleaned up. |
|---|
| 405 | n/a | big_thing = [42] |
|---|
| 406 | n/a | refcount = sys.getrefcount(big_thing) |
|---|
| 407 | n/a | parser = OptionParser() |
|---|
| 408 | n/a | parser.add_option("-a", "--aaarggh") |
|---|
| 409 | n/a | parser.big_thing = big_thing |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | parser.destroy() |
|---|
| 412 | n/a | #self.assertEqual(refcount, sys.getrefcount(big_thing)) |
|---|
| 413 | n/a | del parser |
|---|
| 414 | n/a | self.assertEqual(refcount, sys.getrefcount(big_thing)) |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | class TestOptionValues(BaseTest): |
|---|
| 418 | n/a | def setUp(self): |
|---|
| 419 | n/a | pass |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | def test_basics(self): |
|---|
| 422 | n/a | values = Values() |
|---|
| 423 | n/a | self.assertEqual(vars(values), {}) |
|---|
| 424 | n/a | self.assertEqual(values, {}) |
|---|
| 425 | n/a | self.assertNotEqual(values, {"foo": "bar"}) |
|---|
| 426 | n/a | self.assertNotEqual(values, "") |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | dict = {"foo": "bar", "baz": 42} |
|---|
| 429 | n/a | values = Values(defaults=dict) |
|---|
| 430 | n/a | self.assertEqual(vars(values), dict) |
|---|
| 431 | n/a | self.assertEqual(values, dict) |
|---|
| 432 | n/a | self.assertNotEqual(values, {"foo": "bar"}) |
|---|
| 433 | n/a | self.assertNotEqual(values, {}) |
|---|
| 434 | n/a | self.assertNotEqual(values, "") |
|---|
| 435 | n/a | self.assertNotEqual(values, []) |
|---|
| 436 | n/a | |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | class TestTypeAliases(BaseTest): |
|---|
| 439 | n/a | def setUp(self): |
|---|
| 440 | n/a | self.parser = OptionParser() |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | def test_str_aliases_string(self): |
|---|
| 443 | n/a | self.parser.add_option("-s", type="str") |
|---|
| 444 | n/a | self.assertEqual(self.parser.get_option("-s").type, "string") |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | def test_type_object(self): |
|---|
| 447 | n/a | self.parser.add_option("-s", type=str) |
|---|
| 448 | n/a | self.assertEqual(self.parser.get_option("-s").type, "string") |
|---|
| 449 | n/a | self.parser.add_option("-x", type=int) |
|---|
| 450 | n/a | self.assertEqual(self.parser.get_option("-x").type, "int") |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | # Custom type for testing processing of default values. |
|---|
| 454 | n/a | _time_units = { 's' : 1, 'm' : 60, 'h' : 60*60, 'd' : 60*60*24 } |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | def _check_duration(option, opt, value): |
|---|
| 457 | n/a | try: |
|---|
| 458 | n/a | if value[-1].isdigit(): |
|---|
| 459 | n/a | return int(value) |
|---|
| 460 | n/a | else: |
|---|
| 461 | n/a | return int(value[:-1]) * _time_units[value[-1]] |
|---|
| 462 | n/a | except (ValueError, IndexError): |
|---|
| 463 | n/a | raise OptionValueError( |
|---|
| 464 | n/a | 'option %s: invalid duration: %r' % (opt, value)) |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | class DurationOption(Option): |
|---|
| 467 | n/a | TYPES = Option.TYPES + ('duration',) |
|---|
| 468 | n/a | TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER) |
|---|
| 469 | n/a | TYPE_CHECKER['duration'] = _check_duration |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | class TestDefaultValues(BaseTest): |
|---|
| 472 | n/a | def setUp(self): |
|---|
| 473 | n/a | self.parser = OptionParser() |
|---|
| 474 | n/a | self.parser.add_option("-v", "--verbose", default=True) |
|---|
| 475 | n/a | self.parser.add_option("-q", "--quiet", dest='verbose') |
|---|
| 476 | n/a | self.parser.add_option("-n", type="int", default=37) |
|---|
| 477 | n/a | self.parser.add_option("-m", type="int") |
|---|
| 478 | n/a | self.parser.add_option("-s", default="foo") |
|---|
| 479 | n/a | self.parser.add_option("-t") |
|---|
| 480 | n/a | self.parser.add_option("-u", default=None) |
|---|
| 481 | n/a | self.expected = { 'verbose': True, |
|---|
| 482 | n/a | 'n': 37, |
|---|
| 483 | n/a | 'm': None, |
|---|
| 484 | n/a | 's': "foo", |
|---|
| 485 | n/a | 't': None, |
|---|
| 486 | n/a | 'u': None } |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | def test_basic_defaults(self): |
|---|
| 489 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | def test_mixed_defaults_post(self): |
|---|
| 492 | n/a | self.parser.set_defaults(n=42, m=-100) |
|---|
| 493 | n/a | self.expected.update({'n': 42, 'm': -100}) |
|---|
| 494 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | def test_mixed_defaults_pre(self): |
|---|
| 497 | n/a | self.parser.set_defaults(x="barf", y="blah") |
|---|
| 498 | n/a | self.parser.add_option("-x", default="frob") |
|---|
| 499 | n/a | self.parser.add_option("-y") |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | self.expected.update({'x': "frob", 'y': "blah"}) |
|---|
| 502 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | self.parser.remove_option("-y") |
|---|
| 505 | n/a | self.parser.add_option("-y", default=None) |
|---|
| 506 | n/a | self.expected.update({'y': None}) |
|---|
| 507 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | def test_process_default(self): |
|---|
| 510 | n/a | self.parser.option_class = DurationOption |
|---|
| 511 | n/a | self.parser.add_option("-d", type="duration", default=300) |
|---|
| 512 | n/a | self.parser.add_option("-e", type="duration", default="6m") |
|---|
| 513 | n/a | self.parser.set_defaults(n="42") |
|---|
| 514 | n/a | self.expected.update({'d': 300, 'e': 360, 'n': 42}) |
|---|
| 515 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 516 | n/a | |
|---|
| 517 | n/a | self.parser.set_process_default_values(False) |
|---|
| 518 | n/a | self.expected.update({'d': 300, 'e': "6m", 'n': "42"}) |
|---|
| 519 | n/a | self.assertEqual(self.parser.get_default_values(), self.expected) |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | |
|---|
| 522 | n/a | class TestProgName(BaseTest): |
|---|
| 523 | n/a | """ |
|---|
| 524 | n/a | Test that %prog expands to the right thing in usage, version, |
|---|
| 525 | n/a | and help strings. |
|---|
| 526 | n/a | """ |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | def assertUsage(self, parser, expected_usage): |
|---|
| 529 | n/a | self.assertEqual(parser.get_usage(), expected_usage) |
|---|
| 530 | n/a | |
|---|
| 531 | n/a | def assertVersion(self, parser, expected_version): |
|---|
| 532 | n/a | self.assertEqual(parser.get_version(), expected_version) |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | def test_default_progname(self): |
|---|
| 536 | n/a | # Make sure that program name taken from sys.argv[0] by default. |
|---|
| 537 | n/a | save_argv = sys.argv[:] |
|---|
| 538 | n/a | try: |
|---|
| 539 | n/a | sys.argv[0] = os.path.join("foo", "bar", "baz.py") |
|---|
| 540 | n/a | parser = OptionParser("%prog ...", version="%prog 1.2") |
|---|
| 541 | n/a | expected_usage = "Usage: baz.py ...\n" |
|---|
| 542 | n/a | self.assertUsage(parser, expected_usage) |
|---|
| 543 | n/a | self.assertVersion(parser, "baz.py 1.2") |
|---|
| 544 | n/a | self.assertHelp(parser, |
|---|
| 545 | n/a | expected_usage + "\n" + |
|---|
| 546 | n/a | "Options:\n" |
|---|
| 547 | n/a | " --version show program's version number and exit\n" |
|---|
| 548 | n/a | " -h, --help show this help message and exit\n") |
|---|
| 549 | n/a | finally: |
|---|
| 550 | n/a | sys.argv[:] = save_argv |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | def test_custom_progname(self): |
|---|
| 553 | n/a | parser = OptionParser(prog="thingy", |
|---|
| 554 | n/a | version="%prog 0.1", |
|---|
| 555 | n/a | usage="%prog arg arg") |
|---|
| 556 | n/a | parser.remove_option("-h") |
|---|
| 557 | n/a | parser.remove_option("--version") |
|---|
| 558 | n/a | expected_usage = "Usage: thingy arg arg\n" |
|---|
| 559 | n/a | self.assertUsage(parser, expected_usage) |
|---|
| 560 | n/a | self.assertVersion(parser, "thingy 0.1") |
|---|
| 561 | n/a | self.assertHelp(parser, expected_usage + "\n") |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | |
|---|
| 564 | n/a | class TestExpandDefaults(BaseTest): |
|---|
| 565 | n/a | def setUp(self): |
|---|
| 566 | n/a | self.parser = OptionParser(prog="test") |
|---|
| 567 | n/a | self.help_prefix = """\ |
|---|
| 568 | n/a | Usage: test [options] |
|---|
| 569 | n/a | |
|---|
| 570 | n/a | Options: |
|---|
| 571 | n/a | -h, --help show this help message and exit |
|---|
| 572 | n/a | """ |
|---|
| 573 | n/a | self.file_help = "read from FILE [default: %default]" |
|---|
| 574 | n/a | self.expected_help_file = self.help_prefix + \ |
|---|
| 575 | n/a | " -f FILE, --file=FILE read from FILE [default: foo.txt]\n" |
|---|
| 576 | n/a | self.expected_help_none = self.help_prefix + \ |
|---|
| 577 | n/a | " -f FILE, --file=FILE read from FILE [default: none]\n" |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | def test_option_default(self): |
|---|
| 580 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 581 | n/a | default="foo.txt", |
|---|
| 582 | n/a | help=self.file_help) |
|---|
| 583 | n/a | self.assertHelp(self.parser, self.expected_help_file) |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | def test_parser_default_1(self): |
|---|
| 586 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 587 | n/a | help=self.file_help) |
|---|
| 588 | n/a | self.parser.set_default('file', "foo.txt") |
|---|
| 589 | n/a | self.assertHelp(self.parser, self.expected_help_file) |
|---|
| 590 | n/a | |
|---|
| 591 | n/a | def test_parser_default_2(self): |
|---|
| 592 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 593 | n/a | help=self.file_help) |
|---|
| 594 | n/a | self.parser.set_defaults(file="foo.txt") |
|---|
| 595 | n/a | self.assertHelp(self.parser, self.expected_help_file) |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | def test_no_default(self): |
|---|
| 598 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 599 | n/a | help=self.file_help) |
|---|
| 600 | n/a | self.assertHelp(self.parser, self.expected_help_none) |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | def test_default_none_1(self): |
|---|
| 603 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 604 | n/a | default=None, |
|---|
| 605 | n/a | help=self.file_help) |
|---|
| 606 | n/a | self.assertHelp(self.parser, self.expected_help_none) |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | def test_default_none_2(self): |
|---|
| 609 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 610 | n/a | help=self.file_help) |
|---|
| 611 | n/a | self.parser.set_defaults(file=None) |
|---|
| 612 | n/a | self.assertHelp(self.parser, self.expected_help_none) |
|---|
| 613 | n/a | |
|---|
| 614 | n/a | def test_float_default(self): |
|---|
| 615 | n/a | self.parser.add_option( |
|---|
| 616 | n/a | "-p", "--prob", |
|---|
| 617 | n/a | help="blow up with probability PROB [default: %default]") |
|---|
| 618 | n/a | self.parser.set_defaults(prob=0.43) |
|---|
| 619 | n/a | expected_help = self.help_prefix + \ |
|---|
| 620 | n/a | " -p PROB, --prob=PROB blow up with probability PROB [default: 0.43]\n" |
|---|
| 621 | n/a | self.assertHelp(self.parser, expected_help) |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | def test_alt_expand(self): |
|---|
| 624 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 625 | n/a | default="foo.txt", |
|---|
| 626 | n/a | help="read from FILE [default: *DEFAULT*]") |
|---|
| 627 | n/a | self.parser.formatter.default_tag = "*DEFAULT*" |
|---|
| 628 | n/a | self.assertHelp(self.parser, self.expected_help_file) |
|---|
| 629 | n/a | |
|---|
| 630 | n/a | def test_no_expand(self): |
|---|
| 631 | n/a | self.parser.add_option("-f", "--file", |
|---|
| 632 | n/a | default="foo.txt", |
|---|
| 633 | n/a | help="read from %default file") |
|---|
| 634 | n/a | self.parser.formatter.default_tag = None |
|---|
| 635 | n/a | expected_help = self.help_prefix + \ |
|---|
| 636 | n/a | " -f FILE, --file=FILE read from %default file\n" |
|---|
| 637 | n/a | self.assertHelp(self.parser, expected_help) |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | # -- Test parser.parse_args() ------------------------------------------ |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | class TestStandard(BaseTest): |
|---|
| 643 | n/a | def setUp(self): |
|---|
| 644 | n/a | options = [make_option("-a", type="string"), |
|---|
| 645 | n/a | make_option("-b", "--boo", type="int", dest='boo'), |
|---|
| 646 | n/a | make_option("--foo", action="append")] |
|---|
| 647 | n/a | |
|---|
| 648 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 649 | n/a | option_list=options) |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | def test_required_value(self): |
|---|
| 652 | n/a | self.assertParseFail(["-a"], "-a option requires 1 argument") |
|---|
| 653 | n/a | |
|---|
| 654 | n/a | def test_invalid_integer(self): |
|---|
| 655 | n/a | self.assertParseFail(["-b", "5x"], |
|---|
| 656 | n/a | "option -b: invalid integer value: '5x'") |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | def test_no_such_option(self): |
|---|
| 659 | n/a | self.assertParseFail(["--boo13"], "no such option: --boo13") |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | def test_long_invalid_integer(self): |
|---|
| 662 | n/a | self.assertParseFail(["--boo=x5"], |
|---|
| 663 | n/a | "option --boo: invalid integer value: 'x5'") |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | def test_empty(self): |
|---|
| 666 | n/a | self.assertParseOK([], {'a': None, 'boo': None, 'foo': None}, []) |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | def test_shortopt_empty_longopt_append(self): |
|---|
| 669 | n/a | self.assertParseOK(["-a", "", "--foo=blah", "--foo="], |
|---|
| 670 | n/a | {'a': "", 'boo': None, 'foo': ["blah", ""]}, |
|---|
| 671 | n/a | []) |
|---|
| 672 | n/a | |
|---|
| 673 | n/a | def test_long_option_append(self): |
|---|
| 674 | n/a | self.assertParseOK(["--foo", "bar", "--foo", "", "--foo=x"], |
|---|
| 675 | n/a | {'a': None, |
|---|
| 676 | n/a | 'boo': None, |
|---|
| 677 | n/a | 'foo': ["bar", "", "x"]}, |
|---|
| 678 | n/a | []) |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | def test_option_argument_joined(self): |
|---|
| 681 | n/a | self.assertParseOK(["-abc"], |
|---|
| 682 | n/a | {'a': "bc", 'boo': None, 'foo': None}, |
|---|
| 683 | n/a | []) |
|---|
| 684 | n/a | |
|---|
| 685 | n/a | def test_option_argument_split(self): |
|---|
| 686 | n/a | self.assertParseOK(["-a", "34"], |
|---|
| 687 | n/a | {'a': "34", 'boo': None, 'foo': None}, |
|---|
| 688 | n/a | []) |
|---|
| 689 | n/a | |
|---|
| 690 | n/a | def test_option_argument_joined_integer(self): |
|---|
| 691 | n/a | self.assertParseOK(["-b34"], |
|---|
| 692 | n/a | {'a': None, 'boo': 34, 'foo': None}, |
|---|
| 693 | n/a | []) |
|---|
| 694 | n/a | |
|---|
| 695 | n/a | def test_option_argument_split_negative_integer(self): |
|---|
| 696 | n/a | self.assertParseOK(["-b", "-5"], |
|---|
| 697 | n/a | {'a': None, 'boo': -5, 'foo': None}, |
|---|
| 698 | n/a | []) |
|---|
| 699 | n/a | |
|---|
| 700 | n/a | def test_long_option_argument_joined(self): |
|---|
| 701 | n/a | self.assertParseOK(["--boo=13"], |
|---|
| 702 | n/a | {'a': None, 'boo': 13, 'foo': None}, |
|---|
| 703 | n/a | []) |
|---|
| 704 | n/a | |
|---|
| 705 | n/a | def test_long_option_argument_split(self): |
|---|
| 706 | n/a | self.assertParseOK(["--boo", "111"], |
|---|
| 707 | n/a | {'a': None, 'boo': 111, 'foo': None}, |
|---|
| 708 | n/a | []) |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | def test_long_option_short_option(self): |
|---|
| 711 | n/a | self.assertParseOK(["--foo=bar", "-axyz"], |
|---|
| 712 | n/a | {'a': 'xyz', 'boo': None, 'foo': ["bar"]}, |
|---|
| 713 | n/a | []) |
|---|
| 714 | n/a | |
|---|
| 715 | n/a | def test_abbrev_long_option(self): |
|---|
| 716 | n/a | self.assertParseOK(["--f=bar", "-axyz"], |
|---|
| 717 | n/a | {'a': 'xyz', 'boo': None, 'foo': ["bar"]}, |
|---|
| 718 | n/a | []) |
|---|
| 719 | n/a | |
|---|
| 720 | n/a | def test_defaults(self): |
|---|
| 721 | n/a | (options, args) = self.parser.parse_args([]) |
|---|
| 722 | n/a | defaults = self.parser.get_default_values() |
|---|
| 723 | n/a | self.assertEqual(vars(defaults), vars(options)) |
|---|
| 724 | n/a | |
|---|
| 725 | n/a | def test_ambiguous_option(self): |
|---|
| 726 | n/a | self.parser.add_option("--foz", action="store", |
|---|
| 727 | n/a | type="string", dest="foo") |
|---|
| 728 | n/a | self.assertParseFail(["--f=bar"], |
|---|
| 729 | n/a | "ambiguous option: --f (--foo, --foz?)") |
|---|
| 730 | n/a | |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | def test_short_and_long_option_split(self): |
|---|
| 733 | n/a | self.assertParseOK(["-a", "xyz", "--foo", "bar"], |
|---|
| 734 | n/a | {'a': 'xyz', 'boo': None, 'foo': ["bar"]}, |
|---|
| 735 | n/a | []) |
|---|
| 736 | n/a | |
|---|
| 737 | n/a | def test_short_option_split_long_option_append(self): |
|---|
| 738 | n/a | self.assertParseOK(["--foo=bar", "-b", "123", "--foo", "baz"], |
|---|
| 739 | n/a | {'a': None, 'boo': 123, 'foo': ["bar", "baz"]}, |
|---|
| 740 | n/a | []) |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | def test_short_option_split_one_positional_arg(self): |
|---|
| 743 | n/a | self.assertParseOK(["-a", "foo", "bar"], |
|---|
| 744 | n/a | {'a': "foo", 'boo': None, 'foo': None}, |
|---|
| 745 | n/a | ["bar"]) |
|---|
| 746 | n/a | |
|---|
| 747 | n/a | def test_short_option_consumes_separator(self): |
|---|
| 748 | n/a | self.assertParseOK(["-a", "--", "foo", "bar"], |
|---|
| 749 | n/a | {'a': "--", 'boo': None, 'foo': None}, |
|---|
| 750 | n/a | ["foo", "bar"]) |
|---|
| 751 | n/a | self.assertParseOK(["-a", "--", "--foo", "bar"], |
|---|
| 752 | n/a | {'a': "--", 'boo': None, 'foo': ["bar"]}, |
|---|
| 753 | n/a | []) |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | def test_short_option_joined_and_separator(self): |
|---|
| 756 | n/a | self.assertParseOK(["-ab", "--", "--foo", "bar"], |
|---|
| 757 | n/a | {'a': "b", 'boo': None, 'foo': None}, |
|---|
| 758 | n/a | ["--foo", "bar"]), |
|---|
| 759 | n/a | |
|---|
| 760 | n/a | def test_hyphen_becomes_positional_arg(self): |
|---|
| 761 | n/a | self.assertParseOK(["-ab", "-", "--foo", "bar"], |
|---|
| 762 | n/a | {'a': "b", 'boo': None, 'foo': ["bar"]}, |
|---|
| 763 | n/a | ["-"]) |
|---|
| 764 | n/a | |
|---|
| 765 | n/a | def test_no_append_versus_append(self): |
|---|
| 766 | n/a | self.assertParseOK(["-b3", "-b", "5", "--foo=bar", "--foo", "baz"], |
|---|
| 767 | n/a | {'a': None, 'boo': 5, 'foo': ["bar", "baz"]}, |
|---|
| 768 | n/a | []) |
|---|
| 769 | n/a | |
|---|
| 770 | n/a | def test_option_consumes_optionlike_string(self): |
|---|
| 771 | n/a | self.assertParseOK(["-a", "-b3"], |
|---|
| 772 | n/a | {'a': "-b3", 'boo': None, 'foo': None}, |
|---|
| 773 | n/a | []) |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | def test_combined_single_invalid_option(self): |
|---|
| 776 | n/a | self.parser.add_option("-t", action="store_true") |
|---|
| 777 | n/a | self.assertParseFail(["-test"], |
|---|
| 778 | n/a | "no such option: -e") |
|---|
| 779 | n/a | |
|---|
| 780 | n/a | class TestBool(BaseTest): |
|---|
| 781 | n/a | def setUp(self): |
|---|
| 782 | n/a | options = [make_option("-v", |
|---|
| 783 | n/a | "--verbose", |
|---|
| 784 | n/a | action="store_true", |
|---|
| 785 | n/a | dest="verbose", |
|---|
| 786 | n/a | default=''), |
|---|
| 787 | n/a | make_option("-q", |
|---|
| 788 | n/a | "--quiet", |
|---|
| 789 | n/a | action="store_false", |
|---|
| 790 | n/a | dest="verbose")] |
|---|
| 791 | n/a | self.parser = OptionParser(option_list = options) |
|---|
| 792 | n/a | |
|---|
| 793 | n/a | def test_bool_default(self): |
|---|
| 794 | n/a | self.assertParseOK([], |
|---|
| 795 | n/a | {'verbose': ''}, |
|---|
| 796 | n/a | []) |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | def test_bool_false(self): |
|---|
| 799 | n/a | (options, args) = self.assertParseOK(["-q"], |
|---|
| 800 | n/a | {'verbose': 0}, |
|---|
| 801 | n/a | []) |
|---|
| 802 | n/a | self.assertTrue(options.verbose is False) |
|---|
| 803 | n/a | |
|---|
| 804 | n/a | def test_bool_true(self): |
|---|
| 805 | n/a | (options, args) = self.assertParseOK(["-v"], |
|---|
| 806 | n/a | {'verbose': 1}, |
|---|
| 807 | n/a | []) |
|---|
| 808 | n/a | self.assertTrue(options.verbose is True) |
|---|
| 809 | n/a | |
|---|
| 810 | n/a | def test_bool_flicker_on_and_off(self): |
|---|
| 811 | n/a | self.assertParseOK(["-qvq", "-q", "-v"], |
|---|
| 812 | n/a | {'verbose': 1}, |
|---|
| 813 | n/a | []) |
|---|
| 814 | n/a | |
|---|
| 815 | n/a | class TestChoice(BaseTest): |
|---|
| 816 | n/a | def setUp(self): |
|---|
| 817 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 818 | n/a | self.parser.add_option("-c", action="store", type="choice", |
|---|
| 819 | n/a | dest="choice", choices=["one", "two", "three"]) |
|---|
| 820 | n/a | |
|---|
| 821 | n/a | def test_valid_choice(self): |
|---|
| 822 | n/a | self.assertParseOK(["-c", "one", "xyz"], |
|---|
| 823 | n/a | {'choice': 'one'}, |
|---|
| 824 | n/a | ["xyz"]) |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | def test_invalid_choice(self): |
|---|
| 827 | n/a | self.assertParseFail(["-c", "four", "abc"], |
|---|
| 828 | n/a | "option -c: invalid choice: 'four' " |
|---|
| 829 | n/a | "(choose from 'one', 'two', 'three')") |
|---|
| 830 | n/a | |
|---|
| 831 | n/a | def test_add_choice_option(self): |
|---|
| 832 | n/a | self.parser.add_option("-d", "--default", |
|---|
| 833 | n/a | choices=["four", "five", "six"]) |
|---|
| 834 | n/a | opt = self.parser.get_option("-d") |
|---|
| 835 | n/a | self.assertEqual(opt.type, "choice") |
|---|
| 836 | n/a | self.assertEqual(opt.action, "store") |
|---|
| 837 | n/a | |
|---|
| 838 | n/a | class TestCount(BaseTest): |
|---|
| 839 | n/a | def setUp(self): |
|---|
| 840 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 841 | n/a | self.v_opt = make_option("-v", action="count", dest="verbose") |
|---|
| 842 | n/a | self.parser.add_option(self.v_opt) |
|---|
| 843 | n/a | self.parser.add_option("--verbose", type="int", dest="verbose") |
|---|
| 844 | n/a | self.parser.add_option("-q", "--quiet", |
|---|
| 845 | n/a | action="store_const", dest="verbose", const=0) |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | def test_empty(self): |
|---|
| 848 | n/a | self.assertParseOK([], {'verbose': None}, []) |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | def test_count_one(self): |
|---|
| 851 | n/a | self.assertParseOK(["-v"], {'verbose': 1}, []) |
|---|
| 852 | n/a | |
|---|
| 853 | n/a | def test_count_three(self): |
|---|
| 854 | n/a | self.assertParseOK(["-vvv"], {'verbose': 3}, []) |
|---|
| 855 | n/a | |
|---|
| 856 | n/a | def test_count_three_apart(self): |
|---|
| 857 | n/a | self.assertParseOK(["-v", "-v", "-v"], {'verbose': 3}, []) |
|---|
| 858 | n/a | |
|---|
| 859 | n/a | def test_count_override_amount(self): |
|---|
| 860 | n/a | self.assertParseOK(["-vvv", "--verbose=2"], {'verbose': 2}, []) |
|---|
| 861 | n/a | |
|---|
| 862 | n/a | def test_count_override_quiet(self): |
|---|
| 863 | n/a | self.assertParseOK(["-vvv", "--verbose=2", "-q"], {'verbose': 0}, []) |
|---|
| 864 | n/a | |
|---|
| 865 | n/a | def test_count_overriding(self): |
|---|
| 866 | n/a | self.assertParseOK(["-vvv", "--verbose=2", "-q", "-v"], |
|---|
| 867 | n/a | {'verbose': 1}, []) |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | def test_count_interspersed_args(self): |
|---|
| 870 | n/a | self.assertParseOK(["--quiet", "3", "-v"], |
|---|
| 871 | n/a | {'verbose': 1}, |
|---|
| 872 | n/a | ["3"]) |
|---|
| 873 | n/a | |
|---|
| 874 | n/a | def test_count_no_interspersed_args(self): |
|---|
| 875 | n/a | self.parser.disable_interspersed_args() |
|---|
| 876 | n/a | self.assertParseOK(["--quiet", "3", "-v"], |
|---|
| 877 | n/a | {'verbose': 0}, |
|---|
| 878 | n/a | ["3", "-v"]) |
|---|
| 879 | n/a | |
|---|
| 880 | n/a | def test_count_no_such_option(self): |
|---|
| 881 | n/a | self.assertParseFail(["-q3", "-v"], "no such option: -3") |
|---|
| 882 | n/a | |
|---|
| 883 | n/a | def test_count_option_no_value(self): |
|---|
| 884 | n/a | self.assertParseFail(["--quiet=3", "-v"], |
|---|
| 885 | n/a | "--quiet option does not take a value") |
|---|
| 886 | n/a | |
|---|
| 887 | n/a | def test_count_with_default(self): |
|---|
| 888 | n/a | self.parser.set_default('verbose', 0) |
|---|
| 889 | n/a | self.assertParseOK([], {'verbose':0}, []) |
|---|
| 890 | n/a | |
|---|
| 891 | n/a | def test_count_overriding_default(self): |
|---|
| 892 | n/a | self.parser.set_default('verbose', 0) |
|---|
| 893 | n/a | self.assertParseOK(["-vvv", "--verbose=2", "-q", "-v"], |
|---|
| 894 | n/a | {'verbose': 1}, []) |
|---|
| 895 | n/a | |
|---|
| 896 | n/a | class TestMultipleArgs(BaseTest): |
|---|
| 897 | n/a | def setUp(self): |
|---|
| 898 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 899 | n/a | self.parser.add_option("-p", "--point", |
|---|
| 900 | n/a | action="store", nargs=3, type="float", dest="point") |
|---|
| 901 | n/a | |
|---|
| 902 | n/a | def test_nargs_with_positional_args(self): |
|---|
| 903 | n/a | self.assertParseOK(["foo", "-p", "1", "2.5", "-4.3", "xyz"], |
|---|
| 904 | n/a | {'point': (1.0, 2.5, -4.3)}, |
|---|
| 905 | n/a | ["foo", "xyz"]) |
|---|
| 906 | n/a | |
|---|
| 907 | n/a | def test_nargs_long_opt(self): |
|---|
| 908 | n/a | self.assertParseOK(["--point", "-1", "2.5", "-0", "xyz"], |
|---|
| 909 | n/a | {'point': (-1.0, 2.5, -0.0)}, |
|---|
| 910 | n/a | ["xyz"]) |
|---|
| 911 | n/a | |
|---|
| 912 | n/a | def test_nargs_invalid_float_value(self): |
|---|
| 913 | n/a | self.assertParseFail(["-p", "1.0", "2x", "3.5"], |
|---|
| 914 | n/a | "option -p: " |
|---|
| 915 | n/a | "invalid floating-point value: '2x'") |
|---|
| 916 | n/a | |
|---|
| 917 | n/a | def test_nargs_required_values(self): |
|---|
| 918 | n/a | self.assertParseFail(["--point", "1.0", "3.5"], |
|---|
| 919 | n/a | "--point option requires 3 arguments") |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | class TestMultipleArgsAppend(BaseTest): |
|---|
| 922 | n/a | def setUp(self): |
|---|
| 923 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 924 | n/a | self.parser.add_option("-p", "--point", action="store", nargs=3, |
|---|
| 925 | n/a | type="float", dest="point") |
|---|
| 926 | n/a | self.parser.add_option("-f", "--foo", action="append", nargs=2, |
|---|
| 927 | n/a | type="int", dest="foo") |
|---|
| 928 | n/a | self.parser.add_option("-z", "--zero", action="append_const", |
|---|
| 929 | n/a | dest="foo", const=(0, 0)) |
|---|
| 930 | n/a | |
|---|
| 931 | n/a | def test_nargs_append(self): |
|---|
| 932 | n/a | self.assertParseOK(["-f", "4", "-3", "blah", "--foo", "1", "666"], |
|---|
| 933 | n/a | {'point': None, 'foo': [(4, -3), (1, 666)]}, |
|---|
| 934 | n/a | ["blah"]) |
|---|
| 935 | n/a | |
|---|
| 936 | n/a | def test_nargs_append_required_values(self): |
|---|
| 937 | n/a | self.assertParseFail(["-f4,3"], |
|---|
| 938 | n/a | "-f option requires 2 arguments") |
|---|
| 939 | n/a | |
|---|
| 940 | n/a | def test_nargs_append_simple(self): |
|---|
| 941 | n/a | self.assertParseOK(["--foo=3", "4"], |
|---|
| 942 | n/a | {'point': None, 'foo':[(3, 4)]}, |
|---|
| 943 | n/a | []) |
|---|
| 944 | n/a | |
|---|
| 945 | n/a | def test_nargs_append_const(self): |
|---|
| 946 | n/a | self.assertParseOK(["--zero", "--foo", "3", "4", "-z"], |
|---|
| 947 | n/a | {'point': None, 'foo':[(0, 0), (3, 4), (0, 0)]}, |
|---|
| 948 | n/a | []) |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | class TestVersion(BaseTest): |
|---|
| 951 | n/a | def test_version(self): |
|---|
| 952 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 953 | n/a | version="%prog 0.1") |
|---|
| 954 | n/a | save_argv = sys.argv[:] |
|---|
| 955 | n/a | try: |
|---|
| 956 | n/a | sys.argv[0] = os.path.join(os.curdir, "foo", "bar") |
|---|
| 957 | n/a | self.assertOutput(["--version"], "bar 0.1\n") |
|---|
| 958 | n/a | finally: |
|---|
| 959 | n/a | sys.argv[:] = save_argv |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | def test_no_version(self): |
|---|
| 962 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 963 | n/a | self.assertParseFail(["--version"], |
|---|
| 964 | n/a | "no such option: --version") |
|---|
| 965 | n/a | |
|---|
| 966 | n/a | # -- Test conflicting default values and parser.parse_args() ----------- |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | class TestConflictingDefaults(BaseTest): |
|---|
| 969 | n/a | """Conflicting default values: the last one should win.""" |
|---|
| 970 | n/a | def setUp(self): |
|---|
| 971 | n/a | self.parser = OptionParser(option_list=[ |
|---|
| 972 | n/a | make_option("-v", action="store_true", dest="verbose", default=1)]) |
|---|
| 973 | n/a | |
|---|
| 974 | n/a | def test_conflict_default(self): |
|---|
| 975 | n/a | self.parser.add_option("-q", action="store_false", dest="verbose", |
|---|
| 976 | n/a | default=0) |
|---|
| 977 | n/a | self.assertParseOK([], {'verbose': 0}, []) |
|---|
| 978 | n/a | |
|---|
| 979 | n/a | def test_conflict_default_none(self): |
|---|
| 980 | n/a | self.parser.add_option("-q", action="store_false", dest="verbose", |
|---|
| 981 | n/a | default=None) |
|---|
| 982 | n/a | self.assertParseOK([], {'verbose': None}, []) |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | class TestOptionGroup(BaseTest): |
|---|
| 985 | n/a | def setUp(self): |
|---|
| 986 | n/a | self.parser = OptionParser(usage=SUPPRESS_USAGE) |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | def test_option_group_create_instance(self): |
|---|
| 989 | n/a | group = OptionGroup(self.parser, "Spam") |
|---|
| 990 | n/a | self.parser.add_option_group(group) |
|---|
| 991 | n/a | group.add_option("--spam", action="store_true", |
|---|
| 992 | n/a | help="spam spam spam spam") |
|---|
| 993 | n/a | self.assertParseOK(["--spam"], {'spam': 1}, []) |
|---|
| 994 | n/a | |
|---|
| 995 | n/a | def test_add_group_no_group(self): |
|---|
| 996 | n/a | self.assertTypeError(self.parser.add_option_group, |
|---|
| 997 | n/a | "not an OptionGroup instance: None", None) |
|---|
| 998 | n/a | |
|---|
| 999 | n/a | def test_add_group_invalid_arguments(self): |
|---|
| 1000 | n/a | self.assertTypeError(self.parser.add_option_group, |
|---|
| 1001 | n/a | "invalid arguments", None, None) |
|---|
| 1002 | n/a | |
|---|
| 1003 | n/a | def test_add_group_wrong_parser(self): |
|---|
| 1004 | n/a | group = OptionGroup(self.parser, "Spam") |
|---|
| 1005 | n/a | group.parser = OptionParser() |
|---|
| 1006 | n/a | self.assertRaises(self.parser.add_option_group, (group,), None, |
|---|
| 1007 | n/a | ValueError, "invalid OptionGroup (wrong parser)") |
|---|
| 1008 | n/a | |
|---|
| 1009 | n/a | def test_group_manipulate(self): |
|---|
| 1010 | n/a | group = self.parser.add_option_group("Group 2", |
|---|
| 1011 | n/a | description="Some more options") |
|---|
| 1012 | n/a | group.set_title("Bacon") |
|---|
| 1013 | n/a | group.add_option("--bacon", type="int") |
|---|
| 1014 | n/a | self.assertTrue(self.parser.get_option_group("--bacon"), group) |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | # -- Test extending and parser.parse_args() ---------------------------- |
|---|
| 1017 | n/a | |
|---|
| 1018 | n/a | class TestExtendAddTypes(BaseTest): |
|---|
| 1019 | n/a | def setUp(self): |
|---|
| 1020 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 1021 | n/a | option_class=self.MyOption) |
|---|
| 1022 | n/a | self.parser.add_option("-a", None, type="string", dest="a") |
|---|
| 1023 | n/a | self.parser.add_option("-f", "--file", type="file", dest="file") |
|---|
| 1024 | n/a | |
|---|
| 1025 | n/a | def tearDown(self): |
|---|
| 1026 | n/a | if os.path.isdir(support.TESTFN): |
|---|
| 1027 | n/a | os.rmdir(support.TESTFN) |
|---|
| 1028 | n/a | elif os.path.isfile(support.TESTFN): |
|---|
| 1029 | n/a | os.unlink(support.TESTFN) |
|---|
| 1030 | n/a | |
|---|
| 1031 | n/a | class MyOption (Option): |
|---|
| 1032 | n/a | def check_file(option, opt, value): |
|---|
| 1033 | n/a | if not os.path.exists(value): |
|---|
| 1034 | n/a | raise OptionValueError("%s: file does not exist" % value) |
|---|
| 1035 | n/a | elif not os.path.isfile(value): |
|---|
| 1036 | n/a | raise OptionValueError("%s: not a regular file" % value) |
|---|
| 1037 | n/a | return value |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | TYPES = Option.TYPES + ("file",) |
|---|
| 1040 | n/a | TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER) |
|---|
| 1041 | n/a | TYPE_CHECKER["file"] = check_file |
|---|
| 1042 | n/a | |
|---|
| 1043 | n/a | def test_filetype_ok(self): |
|---|
| 1044 | n/a | support.create_empty_file(support.TESTFN) |
|---|
| 1045 | n/a | self.assertParseOK(["--file", support.TESTFN, "-afoo"], |
|---|
| 1046 | n/a | {'file': support.TESTFN, 'a': 'foo'}, |
|---|
| 1047 | n/a | []) |
|---|
| 1048 | n/a | |
|---|
| 1049 | n/a | def test_filetype_noexist(self): |
|---|
| 1050 | n/a | self.assertParseFail(["--file", support.TESTFN, "-afoo"], |
|---|
| 1051 | n/a | "%s: file does not exist" % |
|---|
| 1052 | n/a | support.TESTFN) |
|---|
| 1053 | n/a | |
|---|
| 1054 | n/a | def test_filetype_notfile(self): |
|---|
| 1055 | n/a | os.mkdir(support.TESTFN) |
|---|
| 1056 | n/a | self.assertParseFail(["--file", support.TESTFN, "-afoo"], |
|---|
| 1057 | n/a | "%s: not a regular file" % |
|---|
| 1058 | n/a | support.TESTFN) |
|---|
| 1059 | n/a | |
|---|
| 1060 | n/a | |
|---|
| 1061 | n/a | class TestExtendAddActions(BaseTest): |
|---|
| 1062 | n/a | def setUp(self): |
|---|
| 1063 | n/a | options = [self.MyOption("-a", "--apple", action="extend", |
|---|
| 1064 | n/a | type="string", dest="apple")] |
|---|
| 1065 | n/a | self.parser = OptionParser(option_list=options) |
|---|
| 1066 | n/a | |
|---|
| 1067 | n/a | class MyOption (Option): |
|---|
| 1068 | n/a | ACTIONS = Option.ACTIONS + ("extend",) |
|---|
| 1069 | n/a | STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) |
|---|
| 1070 | n/a | TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) |
|---|
| 1071 | n/a | |
|---|
| 1072 | n/a | def take_action(self, action, dest, opt, value, values, parser): |
|---|
| 1073 | n/a | if action == "extend": |
|---|
| 1074 | n/a | lvalue = value.split(",") |
|---|
| 1075 | n/a | values.ensure_value(dest, []).extend(lvalue) |
|---|
| 1076 | n/a | else: |
|---|
| 1077 | n/a | Option.take_action(self, action, dest, opt, parser, value, |
|---|
| 1078 | n/a | values) |
|---|
| 1079 | n/a | |
|---|
| 1080 | n/a | def test_extend_add_action(self): |
|---|
| 1081 | n/a | self.assertParseOK(["-afoo,bar", "--apple=blah"], |
|---|
| 1082 | n/a | {'apple': ["foo", "bar", "blah"]}, |
|---|
| 1083 | n/a | []) |
|---|
| 1084 | n/a | |
|---|
| 1085 | n/a | def test_extend_add_action_normal(self): |
|---|
| 1086 | n/a | self.assertParseOK(["-a", "foo", "-abar", "--apple=x,y"], |
|---|
| 1087 | n/a | {'apple': ["foo", "bar", "x", "y"]}, |
|---|
| 1088 | n/a | []) |
|---|
| 1089 | n/a | |
|---|
| 1090 | n/a | # -- Test callbacks and parser.parse_args() ---------------------------- |
|---|
| 1091 | n/a | |
|---|
| 1092 | n/a | class TestCallback(BaseTest): |
|---|
| 1093 | n/a | def setUp(self): |
|---|
| 1094 | n/a | options = [make_option("-x", |
|---|
| 1095 | n/a | None, |
|---|
| 1096 | n/a | action="callback", |
|---|
| 1097 | n/a | callback=self.process_opt), |
|---|
| 1098 | n/a | make_option("-f", |
|---|
| 1099 | n/a | "--file", |
|---|
| 1100 | n/a | action="callback", |
|---|
| 1101 | n/a | callback=self.process_opt, |
|---|
| 1102 | n/a | type="string", |
|---|
| 1103 | n/a | dest="filename")] |
|---|
| 1104 | n/a | self.parser = OptionParser(option_list=options) |
|---|
| 1105 | n/a | |
|---|
| 1106 | n/a | def process_opt(self, option, opt, value, parser_): |
|---|
| 1107 | n/a | if opt == "-x": |
|---|
| 1108 | n/a | self.assertEqual(option._short_opts, ["-x"]) |
|---|
| 1109 | n/a | self.assertEqual(option._long_opts, []) |
|---|
| 1110 | n/a | self.assertTrue(parser_ is self.parser) |
|---|
| 1111 | n/a | self.assertTrue(value is None) |
|---|
| 1112 | n/a | self.assertEqual(vars(parser_.values), {'filename': None}) |
|---|
| 1113 | n/a | |
|---|
| 1114 | n/a | parser_.values.x = 42 |
|---|
| 1115 | n/a | elif opt == "--file": |
|---|
| 1116 | n/a | self.assertEqual(option._short_opts, ["-f"]) |
|---|
| 1117 | n/a | self.assertEqual(option._long_opts, ["--file"]) |
|---|
| 1118 | n/a | self.assertTrue(parser_ is self.parser) |
|---|
| 1119 | n/a | self.assertEqual(value, "foo") |
|---|
| 1120 | n/a | self.assertEqual(vars(parser_.values), {'filename': None, 'x': 42}) |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | setattr(parser_.values, option.dest, value) |
|---|
| 1123 | n/a | else: |
|---|
| 1124 | n/a | self.fail("Unknown option %r in process_opt." % opt) |
|---|
| 1125 | n/a | |
|---|
| 1126 | n/a | def test_callback(self): |
|---|
| 1127 | n/a | self.assertParseOK(["-x", "--file=foo"], |
|---|
| 1128 | n/a | {'filename': "foo", 'x': 42}, |
|---|
| 1129 | n/a | []) |
|---|
| 1130 | n/a | |
|---|
| 1131 | n/a | def test_callback_help(self): |
|---|
| 1132 | n/a | # This test was prompted by SF bug #960515 -- the point is |
|---|
| 1133 | n/a | # not to inspect the help text, just to make sure that |
|---|
| 1134 | n/a | # format_help() doesn't crash. |
|---|
| 1135 | n/a | parser = OptionParser(usage=SUPPRESS_USAGE) |
|---|
| 1136 | n/a | parser.remove_option("-h") |
|---|
| 1137 | n/a | parser.add_option("-t", "--test", action="callback", |
|---|
| 1138 | n/a | callback=lambda: None, type="string", |
|---|
| 1139 | n/a | help="foo") |
|---|
| 1140 | n/a | |
|---|
| 1141 | n/a | expected_help = ("Options:\n" |
|---|
| 1142 | n/a | " -t TEST, --test=TEST foo\n") |
|---|
| 1143 | n/a | self.assertHelp(parser, expected_help) |
|---|
| 1144 | n/a | |
|---|
| 1145 | n/a | |
|---|
| 1146 | n/a | class TestCallbackExtraArgs(BaseTest): |
|---|
| 1147 | n/a | def setUp(self): |
|---|
| 1148 | n/a | options = [make_option("-p", "--point", action="callback", |
|---|
| 1149 | n/a | callback=self.process_tuple, |
|---|
| 1150 | n/a | callback_args=(3, int), type="string", |
|---|
| 1151 | n/a | dest="points", default=[])] |
|---|
| 1152 | n/a | self.parser = OptionParser(option_list=options) |
|---|
| 1153 | n/a | |
|---|
| 1154 | n/a | def process_tuple(self, option, opt, value, parser_, len, type): |
|---|
| 1155 | n/a | self.assertEqual(len, 3) |
|---|
| 1156 | n/a | self.assertTrue(type is int) |
|---|
| 1157 | n/a | |
|---|
| 1158 | n/a | if opt == "-p": |
|---|
| 1159 | n/a | self.assertEqual(value, "1,2,3") |
|---|
| 1160 | n/a | elif opt == "--point": |
|---|
| 1161 | n/a | self.assertEqual(value, "4,5,6") |
|---|
| 1162 | n/a | |
|---|
| 1163 | n/a | value = tuple(map(type, value.split(","))) |
|---|
| 1164 | n/a | getattr(parser_.values, option.dest).append(value) |
|---|
| 1165 | n/a | |
|---|
| 1166 | n/a | def test_callback_extra_args(self): |
|---|
| 1167 | n/a | self.assertParseOK(["-p1,2,3", "--point", "4,5,6"], |
|---|
| 1168 | n/a | {'points': [(1,2,3), (4,5,6)]}, |
|---|
| 1169 | n/a | []) |
|---|
| 1170 | n/a | |
|---|
| 1171 | n/a | class TestCallbackMeddleArgs(BaseTest): |
|---|
| 1172 | n/a | def setUp(self): |
|---|
| 1173 | n/a | options = [make_option(str(x), action="callback", |
|---|
| 1174 | n/a | callback=self.process_n, dest='things') |
|---|
| 1175 | n/a | for x in range(-1, -6, -1)] |
|---|
| 1176 | n/a | self.parser = OptionParser(option_list=options) |
|---|
| 1177 | n/a | |
|---|
| 1178 | n/a | # Callback that meddles in rargs, largs |
|---|
| 1179 | n/a | def process_n(self, option, opt, value, parser_): |
|---|
| 1180 | n/a | # option is -3, -5, etc. |
|---|
| 1181 | n/a | nargs = int(opt[1:]) |
|---|
| 1182 | n/a | rargs = parser_.rargs |
|---|
| 1183 | n/a | if len(rargs) < nargs: |
|---|
| 1184 | n/a | self.fail("Expected %d arguments for %s option." % (nargs, opt)) |
|---|
| 1185 | n/a | dest = parser_.values.ensure_value(option.dest, []) |
|---|
| 1186 | n/a | dest.append(tuple(rargs[0:nargs])) |
|---|
| 1187 | n/a | parser_.largs.append(nargs) |
|---|
| 1188 | n/a | del rargs[0:nargs] |
|---|
| 1189 | n/a | |
|---|
| 1190 | n/a | def test_callback_meddle_args(self): |
|---|
| 1191 | n/a | self.assertParseOK(["-1", "foo", "-3", "bar", "baz", "qux"], |
|---|
| 1192 | n/a | {'things': [("foo",), ("bar", "baz", "qux")]}, |
|---|
| 1193 | n/a | [1, 3]) |
|---|
| 1194 | n/a | |
|---|
| 1195 | n/a | def test_callback_meddle_args_separator(self): |
|---|
| 1196 | n/a | self.assertParseOK(["-2", "foo", "--"], |
|---|
| 1197 | n/a | {'things': [('foo', '--')]}, |
|---|
| 1198 | n/a | [2]) |
|---|
| 1199 | n/a | |
|---|
| 1200 | n/a | class TestCallbackManyArgs(BaseTest): |
|---|
| 1201 | n/a | def setUp(self): |
|---|
| 1202 | n/a | options = [make_option("-a", "--apple", action="callback", nargs=2, |
|---|
| 1203 | n/a | callback=self.process_many, type="string"), |
|---|
| 1204 | n/a | make_option("-b", "--bob", action="callback", nargs=3, |
|---|
| 1205 | n/a | callback=self.process_many, type="int")] |
|---|
| 1206 | n/a | self.parser = OptionParser(option_list=options) |
|---|
| 1207 | n/a | |
|---|
| 1208 | n/a | def process_many(self, option, opt, value, parser_): |
|---|
| 1209 | n/a | if opt == "-a": |
|---|
| 1210 | n/a | self.assertEqual(value, ("foo", "bar")) |
|---|
| 1211 | n/a | elif opt == "--apple": |
|---|
| 1212 | n/a | self.assertEqual(value, ("ding", "dong")) |
|---|
| 1213 | n/a | elif opt == "-b": |
|---|
| 1214 | n/a | self.assertEqual(value, (1, 2, 3)) |
|---|
| 1215 | n/a | elif opt == "--bob": |
|---|
| 1216 | n/a | self.assertEqual(value, (-666, 42, 0)) |
|---|
| 1217 | n/a | |
|---|
| 1218 | n/a | def test_many_args(self): |
|---|
| 1219 | n/a | self.assertParseOK(["-a", "foo", "bar", "--apple", "ding", "dong", |
|---|
| 1220 | n/a | "-b", "1", "2", "3", "--bob", "-666", "42", |
|---|
| 1221 | n/a | "0"], |
|---|
| 1222 | n/a | {"apple": None, "bob": None}, |
|---|
| 1223 | n/a | []) |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | class TestCallbackCheckAbbrev(BaseTest): |
|---|
| 1226 | n/a | def setUp(self): |
|---|
| 1227 | n/a | self.parser = OptionParser() |
|---|
| 1228 | n/a | self.parser.add_option("--foo-bar", action="callback", |
|---|
| 1229 | n/a | callback=self.check_abbrev) |
|---|
| 1230 | n/a | |
|---|
| 1231 | n/a | def check_abbrev(self, option, opt, value, parser): |
|---|
| 1232 | n/a | self.assertEqual(opt, "--foo-bar") |
|---|
| 1233 | n/a | |
|---|
| 1234 | n/a | def test_abbrev_callback_expansion(self): |
|---|
| 1235 | n/a | self.assertParseOK(["--foo"], {}, []) |
|---|
| 1236 | n/a | |
|---|
| 1237 | n/a | class TestCallbackVarArgs(BaseTest): |
|---|
| 1238 | n/a | def setUp(self): |
|---|
| 1239 | n/a | options = [make_option("-a", type="int", nargs=2, dest="a"), |
|---|
| 1240 | n/a | make_option("-b", action="store_true", dest="b"), |
|---|
| 1241 | n/a | make_option("-c", "--callback", action="callback", |
|---|
| 1242 | n/a | callback=self.variable_args, dest="c")] |
|---|
| 1243 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 1244 | n/a | option_list=options) |
|---|
| 1245 | n/a | |
|---|
| 1246 | n/a | def variable_args(self, option, opt, value, parser): |
|---|
| 1247 | n/a | self.assertTrue(value is None) |
|---|
| 1248 | n/a | value = [] |
|---|
| 1249 | n/a | rargs = parser.rargs |
|---|
| 1250 | n/a | while rargs: |
|---|
| 1251 | n/a | arg = rargs[0] |
|---|
| 1252 | n/a | if ((arg[:2] == "--" and len(arg) > 2) or |
|---|
| 1253 | n/a | (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")): |
|---|
| 1254 | n/a | break |
|---|
| 1255 | n/a | else: |
|---|
| 1256 | n/a | value.append(arg) |
|---|
| 1257 | n/a | del rargs[0] |
|---|
| 1258 | n/a | setattr(parser.values, option.dest, value) |
|---|
| 1259 | n/a | |
|---|
| 1260 | n/a | def test_variable_args(self): |
|---|
| 1261 | n/a | self.assertParseOK(["-a3", "-5", "--callback", "foo", "bar"], |
|---|
| 1262 | n/a | {'a': (3, -5), 'b': None, 'c': ["foo", "bar"]}, |
|---|
| 1263 | n/a | []) |
|---|
| 1264 | n/a | |
|---|
| 1265 | n/a | def test_consume_separator_stop_at_option(self): |
|---|
| 1266 | n/a | self.assertParseOK(["-c", "37", "--", "xxx", "-b", "hello"], |
|---|
| 1267 | n/a | {'a': None, |
|---|
| 1268 | n/a | 'b': True, |
|---|
| 1269 | n/a | 'c': ["37", "--", "xxx"]}, |
|---|
| 1270 | n/a | ["hello"]) |
|---|
| 1271 | n/a | |
|---|
| 1272 | n/a | def test_positional_arg_and_variable_args(self): |
|---|
| 1273 | n/a | self.assertParseOK(["hello", "-c", "foo", "-", "bar"], |
|---|
| 1274 | n/a | {'a': None, |
|---|
| 1275 | n/a | 'b': None, |
|---|
| 1276 | n/a | 'c':["foo", "-", "bar"]}, |
|---|
| 1277 | n/a | ["hello"]) |
|---|
| 1278 | n/a | |
|---|
| 1279 | n/a | def test_stop_at_option(self): |
|---|
| 1280 | n/a | self.assertParseOK(["-c", "foo", "-b"], |
|---|
| 1281 | n/a | {'a': None, 'b': True, 'c': ["foo"]}, |
|---|
| 1282 | n/a | []) |
|---|
| 1283 | n/a | |
|---|
| 1284 | n/a | def test_stop_at_invalid_option(self): |
|---|
| 1285 | n/a | self.assertParseFail(["-c", "3", "-5", "-a"], "no such option: -5") |
|---|
| 1286 | n/a | |
|---|
| 1287 | n/a | |
|---|
| 1288 | n/a | # -- Test conflict handling and parser.parse_args() -------------------- |
|---|
| 1289 | n/a | |
|---|
| 1290 | n/a | class ConflictBase(BaseTest): |
|---|
| 1291 | n/a | def setUp(self): |
|---|
| 1292 | n/a | options = [make_option("-v", "--verbose", action="count", |
|---|
| 1293 | n/a | dest="verbose", help="increment verbosity")] |
|---|
| 1294 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 1295 | n/a | option_list=options) |
|---|
| 1296 | n/a | |
|---|
| 1297 | n/a | def show_version(self, option, opt, value, parser): |
|---|
| 1298 | n/a | parser.values.show_version = 1 |
|---|
| 1299 | n/a | |
|---|
| 1300 | n/a | class TestConflict(ConflictBase): |
|---|
| 1301 | n/a | """Use the default conflict resolution for Optik 1.2: error.""" |
|---|
| 1302 | n/a | def assertTrueconflict_error(self, func): |
|---|
| 1303 | n/a | err = self.assertRaises( |
|---|
| 1304 | n/a | func, ("-v", "--version"), {'action' : "callback", |
|---|
| 1305 | n/a | 'callback' : self.show_version, |
|---|
| 1306 | n/a | 'help' : "show version"}, |
|---|
| 1307 | n/a | OptionConflictError, |
|---|
| 1308 | n/a | "option -v/--version: conflicting option string(s): -v") |
|---|
| 1309 | n/a | |
|---|
| 1310 | n/a | self.assertEqual(err.msg, "conflicting option string(s): -v") |
|---|
| 1311 | n/a | self.assertEqual(err.option_id, "-v/--version") |
|---|
| 1312 | n/a | |
|---|
| 1313 | n/a | def test_conflict_error(self): |
|---|
| 1314 | n/a | self.assertTrueconflict_error(self.parser.add_option) |
|---|
| 1315 | n/a | |
|---|
| 1316 | n/a | def test_conflict_error_group(self): |
|---|
| 1317 | n/a | group = OptionGroup(self.parser, "Group 1") |
|---|
| 1318 | n/a | self.assertTrueconflict_error(group.add_option) |
|---|
| 1319 | n/a | |
|---|
| 1320 | n/a | def test_no_such_conflict_handler(self): |
|---|
| 1321 | n/a | self.assertRaises( |
|---|
| 1322 | n/a | self.parser.set_conflict_handler, ('foo',), None, |
|---|
| 1323 | n/a | ValueError, "invalid conflict_resolution value 'foo'") |
|---|
| 1324 | n/a | |
|---|
| 1325 | n/a | |
|---|
| 1326 | n/a | class TestConflictResolve(ConflictBase): |
|---|
| 1327 | n/a | def setUp(self): |
|---|
| 1328 | n/a | ConflictBase.setUp(self) |
|---|
| 1329 | n/a | self.parser.set_conflict_handler("resolve") |
|---|
| 1330 | n/a | self.parser.add_option("-v", "--version", action="callback", |
|---|
| 1331 | n/a | callback=self.show_version, help="show version") |
|---|
| 1332 | n/a | |
|---|
| 1333 | n/a | def test_conflict_resolve(self): |
|---|
| 1334 | n/a | v_opt = self.parser.get_option("-v") |
|---|
| 1335 | n/a | verbose_opt = self.parser.get_option("--verbose") |
|---|
| 1336 | n/a | version_opt = self.parser.get_option("--version") |
|---|
| 1337 | n/a | |
|---|
| 1338 | n/a | self.assertTrue(v_opt is version_opt) |
|---|
| 1339 | n/a | self.assertTrue(v_opt is not verbose_opt) |
|---|
| 1340 | n/a | self.assertEqual(v_opt._long_opts, ["--version"]) |
|---|
| 1341 | n/a | self.assertEqual(version_opt._short_opts, ["-v"]) |
|---|
| 1342 | n/a | self.assertEqual(version_opt._long_opts, ["--version"]) |
|---|
| 1343 | n/a | self.assertEqual(verbose_opt._short_opts, []) |
|---|
| 1344 | n/a | self.assertEqual(verbose_opt._long_opts, ["--verbose"]) |
|---|
| 1345 | n/a | |
|---|
| 1346 | n/a | def test_conflict_resolve_help(self): |
|---|
| 1347 | n/a | self.assertOutput(["-h"], """\ |
|---|
| 1348 | n/a | Options: |
|---|
| 1349 | n/a | --verbose increment verbosity |
|---|
| 1350 | n/a | -h, --help show this help message and exit |
|---|
| 1351 | n/a | -v, --version show version |
|---|
| 1352 | n/a | """) |
|---|
| 1353 | n/a | |
|---|
| 1354 | n/a | def test_conflict_resolve_short_opt(self): |
|---|
| 1355 | n/a | self.assertParseOK(["-v"], |
|---|
| 1356 | n/a | {'verbose': None, 'show_version': 1}, |
|---|
| 1357 | n/a | []) |
|---|
| 1358 | n/a | |
|---|
| 1359 | n/a | def test_conflict_resolve_long_opt(self): |
|---|
| 1360 | n/a | self.assertParseOK(["--verbose"], |
|---|
| 1361 | n/a | {'verbose': 1}, |
|---|
| 1362 | n/a | []) |
|---|
| 1363 | n/a | |
|---|
| 1364 | n/a | def test_conflict_resolve_long_opts(self): |
|---|
| 1365 | n/a | self.assertParseOK(["--verbose", "--version"], |
|---|
| 1366 | n/a | {'verbose': 1, 'show_version': 1}, |
|---|
| 1367 | n/a | []) |
|---|
| 1368 | n/a | |
|---|
| 1369 | n/a | class TestConflictOverride(BaseTest): |
|---|
| 1370 | n/a | def setUp(self): |
|---|
| 1371 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 1372 | n/a | self.parser.set_conflict_handler("resolve") |
|---|
| 1373 | n/a | self.parser.add_option("-n", "--dry-run", |
|---|
| 1374 | n/a | action="store_true", dest="dry_run", |
|---|
| 1375 | n/a | help="don't do anything") |
|---|
| 1376 | n/a | self.parser.add_option("--dry-run", "-n", |
|---|
| 1377 | n/a | action="store_const", const=42, dest="dry_run", |
|---|
| 1378 | n/a | help="dry run mode") |
|---|
| 1379 | n/a | |
|---|
| 1380 | n/a | def test_conflict_override_opts(self): |
|---|
| 1381 | n/a | opt = self.parser.get_option("--dry-run") |
|---|
| 1382 | n/a | self.assertEqual(opt._short_opts, ["-n"]) |
|---|
| 1383 | n/a | self.assertEqual(opt._long_opts, ["--dry-run"]) |
|---|
| 1384 | n/a | |
|---|
| 1385 | n/a | def test_conflict_override_help(self): |
|---|
| 1386 | n/a | self.assertOutput(["-h"], """\ |
|---|
| 1387 | n/a | Options: |
|---|
| 1388 | n/a | -h, --help show this help message and exit |
|---|
| 1389 | n/a | -n, --dry-run dry run mode |
|---|
| 1390 | n/a | """) |
|---|
| 1391 | n/a | |
|---|
| 1392 | n/a | def test_conflict_override_args(self): |
|---|
| 1393 | n/a | self.assertParseOK(["-n"], |
|---|
| 1394 | n/a | {'dry_run': 42}, |
|---|
| 1395 | n/a | []) |
|---|
| 1396 | n/a | |
|---|
| 1397 | n/a | # -- Other testing. ---------------------------------------------------- |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | _expected_help_basic = """\ |
|---|
| 1400 | n/a | Usage: bar.py [options] |
|---|
| 1401 | n/a | |
|---|
| 1402 | n/a | Options: |
|---|
| 1403 | n/a | -a APPLE throw APPLEs at basket |
|---|
| 1404 | n/a | -b NUM, --boo=NUM shout "boo!" NUM times (in order to frighten away all the |
|---|
| 1405 | n/a | evil spirits that cause trouble and mayhem) |
|---|
| 1406 | n/a | --foo=FOO store FOO in the foo list for later fooing |
|---|
| 1407 | n/a | -h, --help show this help message and exit |
|---|
| 1408 | n/a | """ |
|---|
| 1409 | n/a | |
|---|
| 1410 | n/a | _expected_help_long_opts_first = """\ |
|---|
| 1411 | n/a | Usage: bar.py [options] |
|---|
| 1412 | n/a | |
|---|
| 1413 | n/a | Options: |
|---|
| 1414 | n/a | -a APPLE throw APPLEs at basket |
|---|
| 1415 | n/a | --boo=NUM, -b NUM shout "boo!" NUM times (in order to frighten away all the |
|---|
| 1416 | n/a | evil spirits that cause trouble and mayhem) |
|---|
| 1417 | n/a | --foo=FOO store FOO in the foo list for later fooing |
|---|
| 1418 | n/a | --help, -h show this help message and exit |
|---|
| 1419 | n/a | """ |
|---|
| 1420 | n/a | |
|---|
| 1421 | n/a | _expected_help_title_formatter = """\ |
|---|
| 1422 | n/a | Usage |
|---|
| 1423 | n/a | ===== |
|---|
| 1424 | n/a | bar.py [options] |
|---|
| 1425 | n/a | |
|---|
| 1426 | n/a | Options |
|---|
| 1427 | n/a | ======= |
|---|
| 1428 | n/a | -a APPLE throw APPLEs at basket |
|---|
| 1429 | n/a | --boo=NUM, -b NUM shout "boo!" NUM times (in order to frighten away all the |
|---|
| 1430 | n/a | evil spirits that cause trouble and mayhem) |
|---|
| 1431 | n/a | --foo=FOO store FOO in the foo list for later fooing |
|---|
| 1432 | n/a | --help, -h show this help message and exit |
|---|
| 1433 | n/a | """ |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | _expected_help_short_lines = """\ |
|---|
| 1436 | n/a | Usage: bar.py [options] |
|---|
| 1437 | n/a | |
|---|
| 1438 | n/a | Options: |
|---|
| 1439 | n/a | -a APPLE throw APPLEs at basket |
|---|
| 1440 | n/a | -b NUM, --boo=NUM shout "boo!" NUM times (in order to |
|---|
| 1441 | n/a | frighten away all the evil spirits |
|---|
| 1442 | n/a | that cause trouble and mayhem) |
|---|
| 1443 | n/a | --foo=FOO store FOO in the foo list for later |
|---|
| 1444 | n/a | fooing |
|---|
| 1445 | n/a | -h, --help show this help message and exit |
|---|
| 1446 | n/a | """ |
|---|
| 1447 | n/a | |
|---|
| 1448 | n/a | _expected_very_help_short_lines = """\ |
|---|
| 1449 | n/a | Usage: bar.py [options] |
|---|
| 1450 | n/a | |
|---|
| 1451 | n/a | Options: |
|---|
| 1452 | n/a | -a APPLE |
|---|
| 1453 | n/a | throw |
|---|
| 1454 | n/a | APPLEs at |
|---|
| 1455 | n/a | basket |
|---|
| 1456 | n/a | -b NUM, --boo=NUM |
|---|
| 1457 | n/a | shout |
|---|
| 1458 | n/a | "boo!" NUM |
|---|
| 1459 | n/a | times (in |
|---|
| 1460 | n/a | order to |
|---|
| 1461 | n/a | frighten |
|---|
| 1462 | n/a | away all |
|---|
| 1463 | n/a | the evil |
|---|
| 1464 | n/a | spirits |
|---|
| 1465 | n/a | that cause |
|---|
| 1466 | n/a | trouble and |
|---|
| 1467 | n/a | mayhem) |
|---|
| 1468 | n/a | --foo=FOO |
|---|
| 1469 | n/a | store FOO |
|---|
| 1470 | n/a | in the foo |
|---|
| 1471 | n/a | list for |
|---|
| 1472 | n/a | later |
|---|
| 1473 | n/a | fooing |
|---|
| 1474 | n/a | -h, --help |
|---|
| 1475 | n/a | show this |
|---|
| 1476 | n/a | help |
|---|
| 1477 | n/a | message and |
|---|
| 1478 | n/a | exit |
|---|
| 1479 | n/a | """ |
|---|
| 1480 | n/a | |
|---|
| 1481 | n/a | class TestHelp(BaseTest): |
|---|
| 1482 | n/a | def setUp(self): |
|---|
| 1483 | n/a | self.parser = self.make_parser(80) |
|---|
| 1484 | n/a | |
|---|
| 1485 | n/a | def make_parser(self, columns): |
|---|
| 1486 | n/a | options = [ |
|---|
| 1487 | n/a | make_option("-a", type="string", dest='a', |
|---|
| 1488 | n/a | metavar="APPLE", help="throw APPLEs at basket"), |
|---|
| 1489 | n/a | make_option("-b", "--boo", type="int", dest='boo', |
|---|
| 1490 | n/a | metavar="NUM", |
|---|
| 1491 | n/a | help= |
|---|
| 1492 | n/a | "shout \"boo!\" NUM times (in order to frighten away " |
|---|
| 1493 | n/a | "all the evil spirits that cause trouble and mayhem)"), |
|---|
| 1494 | n/a | make_option("--foo", action="append", type="string", dest='foo', |
|---|
| 1495 | n/a | help="store FOO in the foo list for later fooing"), |
|---|
| 1496 | n/a | ] |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | # We need to set COLUMNS for the OptionParser constructor, but |
|---|
| 1499 | n/a | # we must restore its original value -- otherwise, this test |
|---|
| 1500 | n/a | # screws things up for other tests when it's part of the Python |
|---|
| 1501 | n/a | # test suite. |
|---|
| 1502 | n/a | with support.EnvironmentVarGuard() as env: |
|---|
| 1503 | n/a | env['COLUMNS'] = str(columns) |
|---|
| 1504 | n/a | return InterceptingOptionParser(option_list=options) |
|---|
| 1505 | n/a | |
|---|
| 1506 | n/a | def assertHelpEquals(self, expected_output): |
|---|
| 1507 | n/a | save_argv = sys.argv[:] |
|---|
| 1508 | n/a | try: |
|---|
| 1509 | n/a | # Make optparse believe bar.py is being executed. |
|---|
| 1510 | n/a | sys.argv[0] = os.path.join("foo", "bar.py") |
|---|
| 1511 | n/a | self.assertOutput(["-h"], expected_output) |
|---|
| 1512 | n/a | finally: |
|---|
| 1513 | n/a | sys.argv[:] = save_argv |
|---|
| 1514 | n/a | |
|---|
| 1515 | n/a | def test_help(self): |
|---|
| 1516 | n/a | self.assertHelpEquals(_expected_help_basic) |
|---|
| 1517 | n/a | |
|---|
| 1518 | n/a | def test_help_old_usage(self): |
|---|
| 1519 | n/a | self.parser.set_usage("Usage: %prog [options]") |
|---|
| 1520 | n/a | self.assertHelpEquals(_expected_help_basic) |
|---|
| 1521 | n/a | |
|---|
| 1522 | n/a | def test_help_long_opts_first(self): |
|---|
| 1523 | n/a | self.parser.formatter.short_first = 0 |
|---|
| 1524 | n/a | self.assertHelpEquals(_expected_help_long_opts_first) |
|---|
| 1525 | n/a | |
|---|
| 1526 | n/a | def test_help_title_formatter(self): |
|---|
| 1527 | n/a | with support.EnvironmentVarGuard() as env: |
|---|
| 1528 | n/a | env["COLUMNS"] = "80" |
|---|
| 1529 | n/a | self.parser.formatter = TitledHelpFormatter() |
|---|
| 1530 | n/a | self.assertHelpEquals(_expected_help_title_formatter) |
|---|
| 1531 | n/a | |
|---|
| 1532 | n/a | def test_wrap_columns(self): |
|---|
| 1533 | n/a | # Ensure that wrapping respects $COLUMNS environment variable. |
|---|
| 1534 | n/a | # Need to reconstruct the parser, since that's the only time |
|---|
| 1535 | n/a | # we look at $COLUMNS. |
|---|
| 1536 | n/a | self.parser = self.make_parser(60) |
|---|
| 1537 | n/a | self.assertHelpEquals(_expected_help_short_lines) |
|---|
| 1538 | n/a | self.parser = self.make_parser(0) |
|---|
| 1539 | n/a | self.assertHelpEquals(_expected_very_help_short_lines) |
|---|
| 1540 | n/a | |
|---|
| 1541 | n/a | def test_help_unicode(self): |
|---|
| 1542 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) |
|---|
| 1543 | n/a | self.parser.add_option("-a", action="store_true", help="ol\u00E9!") |
|---|
| 1544 | n/a | expect = """\ |
|---|
| 1545 | n/a | Options: |
|---|
| 1546 | n/a | -h, --help show this help message and exit |
|---|
| 1547 | n/a | -a ol\u00E9! |
|---|
| 1548 | n/a | """ |
|---|
| 1549 | n/a | self.assertHelpEquals(expect) |
|---|
| 1550 | n/a | |
|---|
| 1551 | n/a | def test_help_unicode_description(self): |
|---|
| 1552 | n/a | self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, |
|---|
| 1553 | n/a | description="ol\u00E9!") |
|---|
| 1554 | n/a | expect = """\ |
|---|
| 1555 | n/a | ol\u00E9! |
|---|
| 1556 | n/a | |
|---|
| 1557 | n/a | Options: |
|---|
| 1558 | n/a | -h, --help show this help message and exit |
|---|
| 1559 | n/a | """ |
|---|
| 1560 | n/a | self.assertHelpEquals(expect) |
|---|
| 1561 | n/a | |
|---|
| 1562 | n/a | def test_help_description_groups(self): |
|---|
| 1563 | n/a | self.parser.set_description( |
|---|
| 1564 | n/a | "This is the program description for %prog. %prog has " |
|---|
| 1565 | n/a | "an option group as well as single options.") |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | group = OptionGroup( |
|---|
| 1568 | n/a | self.parser, "Dangerous Options", |
|---|
| 1569 | n/a | "Caution: use of these options is at your own risk. " |
|---|
| 1570 | n/a | "It is believed that some of them bite.") |
|---|
| 1571 | n/a | group.add_option("-g", action="store_true", help="Group option.") |
|---|
| 1572 | n/a | self.parser.add_option_group(group) |
|---|
| 1573 | n/a | |
|---|
| 1574 | n/a | expect = """\ |
|---|
| 1575 | n/a | Usage: bar.py [options] |
|---|
| 1576 | n/a | |
|---|
| 1577 | n/a | This is the program description for bar.py. bar.py has an option group as |
|---|
| 1578 | n/a | well as single options. |
|---|
| 1579 | n/a | |
|---|
| 1580 | n/a | Options: |
|---|
| 1581 | n/a | -a APPLE throw APPLEs at basket |
|---|
| 1582 | n/a | -b NUM, --boo=NUM shout "boo!" NUM times (in order to frighten away all the |
|---|
| 1583 | n/a | evil spirits that cause trouble and mayhem) |
|---|
| 1584 | n/a | --foo=FOO store FOO in the foo list for later fooing |
|---|
| 1585 | n/a | -h, --help show this help message and exit |
|---|
| 1586 | n/a | |
|---|
| 1587 | n/a | Dangerous Options: |
|---|
| 1588 | n/a | Caution: use of these options is at your own risk. It is believed |
|---|
| 1589 | n/a | that some of them bite. |
|---|
| 1590 | n/a | |
|---|
| 1591 | n/a | -g Group option. |
|---|
| 1592 | n/a | """ |
|---|
| 1593 | n/a | |
|---|
| 1594 | n/a | self.assertHelpEquals(expect) |
|---|
| 1595 | n/a | |
|---|
| 1596 | n/a | self.parser.epilog = "Please report bugs to /dev/null." |
|---|
| 1597 | n/a | self.assertHelpEquals(expect + "\nPlease report bugs to /dev/null.\n") |
|---|
| 1598 | n/a | |
|---|
| 1599 | n/a | |
|---|
| 1600 | n/a | class TestMatchAbbrev(BaseTest): |
|---|
| 1601 | n/a | def test_match_abbrev(self): |
|---|
| 1602 | n/a | self.assertEqual(_match_abbrev("--f", |
|---|
| 1603 | n/a | {"--foz": None, |
|---|
| 1604 | n/a | "--foo": None, |
|---|
| 1605 | n/a | "--fie": None, |
|---|
| 1606 | n/a | "--f": None}), |
|---|
| 1607 | n/a | "--f") |
|---|
| 1608 | n/a | |
|---|
| 1609 | n/a | def test_match_abbrev_error(self): |
|---|
| 1610 | n/a | s = "--f" |
|---|
| 1611 | n/a | wordmap = {"--foz": None, "--foo": None, "--fie": None} |
|---|
| 1612 | n/a | self.assertRaises( |
|---|
| 1613 | n/a | _match_abbrev, (s, wordmap), None, |
|---|
| 1614 | n/a | BadOptionError, "ambiguous option: --f (--fie, --foo, --foz?)") |
|---|
| 1615 | n/a | |
|---|
| 1616 | n/a | |
|---|
| 1617 | n/a | class TestParseNumber(BaseTest): |
|---|
| 1618 | n/a | def setUp(self): |
|---|
| 1619 | n/a | self.parser = InterceptingOptionParser() |
|---|
| 1620 | n/a | self.parser.add_option("-n", type=int) |
|---|
| 1621 | n/a | self.parser.add_option("-l", type=int) |
|---|
| 1622 | n/a | |
|---|
| 1623 | n/a | def test_parse_num_fail(self): |
|---|
| 1624 | n/a | self.assertRaises( |
|---|
| 1625 | n/a | _parse_num, ("", int), {}, |
|---|
| 1626 | n/a | ValueError, |
|---|
| 1627 | n/a | re.compile(r"invalid literal for int().*: '?'?")) |
|---|
| 1628 | n/a | self.assertRaises( |
|---|
| 1629 | n/a | _parse_num, ("0xOoops", int), {}, |
|---|
| 1630 | n/a | ValueError, |
|---|
| 1631 | n/a | re.compile(r"invalid literal for int().*: s?'?0xOoops'?")) |
|---|
| 1632 | n/a | |
|---|
| 1633 | n/a | def test_parse_num_ok(self): |
|---|
| 1634 | n/a | self.assertEqual(_parse_num("0", int), 0) |
|---|
| 1635 | n/a | self.assertEqual(_parse_num("0x10", int), 16) |
|---|
| 1636 | n/a | self.assertEqual(_parse_num("0XA", int), 10) |
|---|
| 1637 | n/a | self.assertEqual(_parse_num("010", int), 8) |
|---|
| 1638 | n/a | self.assertEqual(_parse_num("0b11", int), 3) |
|---|
| 1639 | n/a | self.assertEqual(_parse_num("0b", int), 0) |
|---|
| 1640 | n/a | |
|---|
| 1641 | n/a | def test_numeric_options(self): |
|---|
| 1642 | n/a | self.assertParseOK(["-n", "42", "-l", "0x20"], |
|---|
| 1643 | n/a | { "n": 42, "l": 0x20 }, []) |
|---|
| 1644 | n/a | self.assertParseOK(["-n", "0b0101", "-l010"], |
|---|
| 1645 | n/a | { "n": 5, "l": 8 }, []) |
|---|
| 1646 | n/a | self.assertParseFail(["-n008"], |
|---|
| 1647 | n/a | "option -n: invalid integer value: '008'") |
|---|
| 1648 | n/a | self.assertParseFail(["-l0b0123"], |
|---|
| 1649 | n/a | "option -l: invalid integer value: '0b0123'") |
|---|
| 1650 | n/a | self.assertParseFail(["-l", "0x12x"], |
|---|
| 1651 | n/a | "option -l: invalid integer value: '0x12x'") |
|---|
| 1652 | n/a | |
|---|
| 1653 | n/a | |
|---|
| 1654 | n/a | class MiscTestCase(unittest.TestCase): |
|---|
| 1655 | n/a | def test__all__(self): |
|---|
| 1656 | n/a | blacklist = {'check_builtin', 'AmbiguousOptionError', 'NO_DEFAULT'} |
|---|
| 1657 | n/a | support.check__all__(self, optparse, blacklist=blacklist) |
|---|
| 1658 | n/a | |
|---|
| 1659 | n/a | |
|---|
| 1660 | n/a | def test_main(): |
|---|
| 1661 | n/a | support.run_unittest(__name__) |
|---|
| 1662 | n/a | |
|---|
| 1663 | n/a | if __name__ == '__main__': |
|---|
| 1664 | n/a | test_main() |
|---|