diff options
Diffstat (limited to 'tests/test_subcmds.py')
| -rw-r--r-- | tests/test_subcmds.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py new file mode 100644 index 00000000..bc53051a --- /dev/null +++ b/tests/test_subcmds.py | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | # Copyright (C) 2020 The Android Open Source Project | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | """Unittests for the subcmds module (mostly __init__.py than subcommands).""" | ||
| 16 | |||
| 17 | import optparse | ||
| 18 | import unittest | ||
| 19 | |||
| 20 | import subcmds | ||
| 21 | |||
| 22 | |||
| 23 | class AllCommands(unittest.TestCase): | ||
| 24 | """Check registered all_commands.""" | ||
| 25 | |||
| 26 | def test_required_basic(self): | ||
| 27 | """Basic checking of registered commands.""" | ||
| 28 | # NB: We don't test all subcommands as we want to avoid "change detection" | ||
| 29 | # tests, so we just look for the most common/important ones here that are | ||
| 30 | # unlikely to ever change. | ||
| 31 | for cmd in {'cherry-pick', 'help', 'init', 'start', 'sync', 'upload'}: | ||
| 32 | self.assertIn(cmd, subcmds.all_commands) | ||
| 33 | |||
| 34 | def test_naming(self): | ||
| 35 | """Verify we don't add things that we shouldn't.""" | ||
| 36 | for cmd in subcmds.all_commands: | ||
| 37 | # Reject filename suffixes like "help.py". | ||
| 38 | self.assertNotIn('.', cmd) | ||
| 39 | |||
| 40 | # Make sure all '_' were converted to '-'. | ||
| 41 | self.assertNotIn('_', cmd) | ||
| 42 | |||
| 43 | # Reject internal python paths like "__init__". | ||
| 44 | self.assertFalse(cmd.startswith('__')) | ||
| 45 | |||
| 46 | def test_help_desc_style(self): | ||
| 47 | """Force some consistency in option descriptions. | ||
| 48 | |||
| 49 | Python's optparse & argparse has a few default options like --help. Their | ||
| 50 | option description text uses lowercase sentence fragments, so enforce our | ||
| 51 | options follow the same style so UI is consistent. | ||
| 52 | |||
| 53 | We enforce: | ||
| 54 | * Text starts with lowercase. | ||
| 55 | * Text doesn't end with period. | ||
| 56 | """ | ||
| 57 | for name, cls in subcmds.all_commands.items(): | ||
| 58 | cmd = cls() | ||
| 59 | parser = cmd.OptionParser | ||
| 60 | for option in parser.option_list: | ||
| 61 | if option.help == optparse.SUPPRESS_HELP: | ||
| 62 | continue | ||
| 63 | |||
| 64 | c = option.help[0] | ||
| 65 | self.assertEqual( | ||
| 66 | c.lower(), c, | ||
| 67 | msg=f'subcmds/{name}.py: {option.get_opt_string()}: help text ' | ||
| 68 | f'should start with lowercase: "{option.help}"') | ||
| 69 | |||
| 70 | self.assertNotEqual( | ||
| 71 | option.help[-1], '.', | ||
| 72 | msg=f'subcmds/{name}.py: {option.get_opt_string()}: help text ' | ||
| 73 | f'should not end in a period: "{option.help}"') | ||
