| 1 |
# Copyright New York University and the in-toto contributors |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
""" |
| 5 |
<Program Name> |
| 6 |
test_common_args.py |
| 7 |
|
| 8 |
<Author> |
| 9 |
Lukas Puehringer <[email protected]> |
| 10 |
|
| 11 |
<Started> |
| 12 |
Jan 16, 2020 |
| 13 |
|
| 14 |
<Copyright> |
| 15 |
See LICENSE for licensing information. |
| 16 |
|
| 17 |
<Purpose> |
| 18 |
Test in_toto/common_args.py |
| 19 |
|
| 20 |
""" |
| 21 |
import argparse |
| 22 |
import unittest |
| 23 |
|
| 24 |
from in_toto.common_args import ( |
| 25 |
KEY_PASSWORD_ARGS, |
| 26 |
KEY_PASSWORD_KWARGS, |
| 27 |
OPTS_TITLE, |
| 28 |
parse_password_and_prompt_args, |
| 29 |
sort_action_groups, |
| 30 |
title_case_action_groups, |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
class TestCommonArgs(unittest.TestCase): |
| 35 |
def test_parse_password_and_prompt_args(self): |
| 36 |
"""Test parse -P/--password optional arg (nargs=?, const=True).""" |
| 37 |
parser = argparse.ArgumentParser() |
| 38 |
parser.add_argument(*KEY_PASSWORD_ARGS, **KEY_PASSWORD_KWARGS) |
| 39 |
|
| 40 |
# parameter list | expected result tuple, i.e password, prompt |
| 41 |
tests = [ |
| 42 |
([], (None, False)), |
| 43 |
(["--password"], (None, True)), |
| 44 |
(["--password", "123456"], ("123456", False)), |
| 45 |
(["-P"], (None, True)), |
| 46 |
(["-P", "123456"], ("123456", False)), |
| 47 |
] |
| 48 |
|
| 49 |
for idx, (params, expected) in enumerate(tests): |
| 50 |
result = parse_password_and_prompt_args(parser.parse_args(params)) |
| 51 |
self.assertTupleEqual(result, expected, "(row {})".format(idx)) |
| 52 |
|
| 53 |
|
| 54 |
class TestArgparseActionGroupHelpers(unittest.TestCase): |
| 55 |
# pylint: disable=protected-access |
| 56 |
|
| 57 |
def setUp(self): |
| 58 |
"""Create an empty parser and perform some basic assertions prior to |
| 59 |
testing parser action group (i.e. argument group) helper functions.""" |
| 60 |
# Create empty argument parser |
| 61 |
self.parser = argparse.ArgumentParser() |
| 62 |
|
| 63 |
# Assert parser has the protected member "_action_groups" and it is a list |
| 64 |
# NOTE: argparse could remove this at any time without notice |
| 65 |
self.assertIsInstance( |
| 66 |
getattr(self.parser, "_action_groups", None), list |
| 67 |
) |
| 68 |
|
| 69 |
# Assert default action groups with default titles' case and default order |
| 70 |
self.assertListEqual( |
| 71 |
[group.title for group in self.parser._action_groups], |
| 72 |
["positional arguments", OPTS_TITLE.lower()], |
| 73 |
) |
| 74 |
|
| 75 |
def test_title_case_action_groups(self): |
| 76 |
"""Test title_case_action_groups title cases action group titles.""" |
| 77 |
# Make titles title-case (default is asserted in setUp) |
| 78 |
title_case_action_groups(self.parser) |
| 79 |
|
| 80 |
# Assert successful title-casing |
| 81 |
self.assertListEqual( |
| 82 |
[group.title for group in self.parser._action_groups], |
| 83 |
["Positional Arguments", OPTS_TITLE], |
| 84 |
) |
| 85 |
|
| 86 |
def test_sort_action_groups(self): |
| 87 |
"""Test sort_action_groups sorts action groups by custom title order.""" |
| 88 |
# Create custom order for titles (default is asserted in setUp) |
| 89 |
custom_order = [OPTS_TITLE.lower(), "positional arguments"] |
| 90 |
sort_action_groups(self.parser, title_order=custom_order) |
| 91 |
# Assert successful re-ordering |
| 92 |
self.assertListEqual( |
| 93 |
[group.title for group in self.parser._action_groups], custom_order |
| 94 |
) |
| 95 |
|
| 96 |
# Add custom group to parser that exists in most in-toto command line tools |
| 97 |
self.parser.add_argument_group("required named arguments") |
| 98 |
|
| 99 |
# Test default custom order of action groups titles (which are title-cased) |
| 100 |
title_case_action_groups(self.parser) |
| 101 |
sort_action_groups(self.parser) |
| 102 |
default_custom_order = [ |
| 103 |
"Required Named Arguments", |
| 104 |
"Positional Arguments", |
| 105 |
OPTS_TITLE, |
| 106 |
] |
| 107 |
|
| 108 |
# Assert successful(title-casing) re-ordering |
| 109 |
self.assertListEqual( |
| 110 |
[group.title for group in self.parser._action_groups], |
| 111 |
default_custom_order, |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
if __name__ == "__main__": |
| 116 |
unittest.main() |