1334
1331
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1337
class Test_check_option_syntax(unittest.TestCase):
1338
# This mostly corresponds to the definition from has_actions() in
1339
# check_option_syntax()
1341
# The actual values set here are not that important, but we do
1342
# at least stick to the correct types, even though they are
1346
"bump_timeout": True,
1347
"start_checker": True,
1348
"stop_checker": True,
1352
"timeout": datetime.timedelta(),
1353
"extended_timeout": datetime.timedelta(),
1354
"interval": datetime.timedelta(),
1355
"approved_by_default": True,
1356
"approval_delay": datetime.timedelta(),
1357
"approval_duration": datetime.timedelta(),
1359
"secret": io.BytesIO(b"x"),
1365
self.parser = argparse.ArgumentParser()
1366
add_command_line_options(self.parser)
1368
@contextlib.contextmanager
1369
def assertParseError(self):
1370
with self.assertRaises(SystemExit) as e:
1371
with self.temporarily_suppress_stderr():
1373
# Exit code from argparse is guaranteed to be "2". Reference:
1374
# https://docs.python.org/3/library/argparse.html#exiting-methods
1375
self.assertEqual(e.exception.code, 2)
1378
@contextlib.contextmanager
1379
def temporarily_suppress_stderr():
1380
null = os.open(os.path.devnull, os.O_RDWR)
1381
stderrcopy = os.dup(sys.stderr.fileno())
1382
os.dup2(null, sys.stderr.fileno())
1388
os.dup2(stderrcopy, sys.stderr.fileno())
1389
os.close(stderrcopy)
1391
def check_option_syntax(self, options):
1392
check_option_syntax(self.parser, options)
1394
def test_actions_requires_client_or_all(self):
1395
for action, value in self.actions.items():
1396
options = self.parser.parse_args()
1397
setattr(options, action, value)
1398
with self.assertParseError():
1399
self.check_option_syntax(options)
1401
def test_actions_conflicts_with_verbose(self):
1402
for action, value in self.actions.items():
1403
options = self.parser.parse_args()
1404
setattr(options, action, value)
1405
options.verbose = True
1406
with self.assertParseError():
1407
self.check_option_syntax(options)
1409
def test_dump_json_conflicts_with_verbose(self):
1410
options = self.parser.parse_args()
1411
options.dump_json = True
1412
options.verbose = True
1413
with self.assertParseError():
1414
self.check_option_syntax(options)
1416
def test_dump_json_conflicts_with_action(self):
1417
for action, value in self.actions.items():
1418
options = self.parser.parse_args()
1419
setattr(options, action, value)
1420
options.dump_json = True
1421
with self.assertParseError():
1422
self.check_option_syntax(options)
1424
def test_all_can_not_be_alone(self):
1425
options = self.parser.parse_args()
1427
with self.assertParseError():
1428
self.check_option_syntax(options)
1430
def test_all_is_ok_with_any_action(self):
1431
for action, value in self.actions.items():
1432
options = self.parser.parse_args()
1433
setattr(options, action, value)
1435
self.check_option_syntax(options)
1437
def test_is_enabled_fails_without_client(self):
1438
options = self.parser.parse_args()
1439
options.is_enabled = True
1440
with self.assertParseError():
1441
self.check_option_syntax(options)
1443
def test_is_enabled_works_with_one_client(self):
1444
options = self.parser.parse_args()
1445
options.is_enabled = True
1446
options.client = ["foo"]
1447
self.check_option_syntax(options)
1449
def test_is_enabled_fails_with_two_clients(self):
1450
options = self.parser.parse_args()
1451
options.is_enabled = True
1452
options.client = ["foo", "barbar"]
1453
with self.assertParseError():
1454
self.check_option_syntax(options)
1458
1335
def should_only_run_tests():
1459
1336
parser = argparse.ArgumentParser(add_help=False)