My dotfiles
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

99 рядки
2.6 KiB

  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License.
  3. from __future__ import absolute_import
  4. import argparse
  5. import sys
  6. from . import pytest, report
  7. from .errors import UnsupportedToolError, UnsupportedCommandError
  8. TOOLS = {
  9. 'pytest': {
  10. '_add_subparser': pytest.add_cli_subparser,
  11. 'discover': pytest.discover,
  12. },
  13. }
  14. REPORTERS = {
  15. 'discover': report.report_discovered,
  16. }
  17. def parse_args(
  18. argv=sys.argv[1:],
  19. prog=sys.argv[0],
  20. ):
  21. """
  22. Return the subcommand & tool to run, along with its args.
  23. This defines the standard CLI for the different testing frameworks.
  24. """
  25. parser = argparse.ArgumentParser(
  26. description='Run Python testing operations.',
  27. prog=prog,
  28. )
  29. cmdsubs = parser.add_subparsers(dest='cmd')
  30. # Add "run" and "debug" subcommands when ready.
  31. for cmdname in ['discover']:
  32. sub = cmdsubs.add_parser(cmdname)
  33. subsubs = sub.add_subparsers(dest='tool')
  34. for toolname in sorted(TOOLS):
  35. try:
  36. add_subparser = TOOLS[toolname]['_add_subparser']
  37. except KeyError:
  38. continue
  39. subsub = add_subparser(cmdname, toolname, subsubs)
  40. if cmdname == 'discover':
  41. subsub.add_argument('--simple', action='store_true')
  42. subsub.add_argument('--no-hide-stdio', dest='hidestdio',
  43. action='store_false')
  44. subsub.add_argument('--pretty', action='store_true')
  45. # Parse the args!
  46. if '--' in argv:
  47. seppos = argv.index('--')
  48. toolargs = argv[seppos + 1:]
  49. argv = argv[:seppos]
  50. else:
  51. toolargs = []
  52. args = parser.parse_args(argv)
  53. ns = vars(args)
  54. cmd = ns.pop('cmd')
  55. if not cmd:
  56. parser.error('missing command')
  57. tool = ns.pop('tool')
  58. if not tool:
  59. parser.error('missing tool')
  60. return tool, cmd, ns, toolargs
  61. def main(toolname, cmdname, subargs, toolargs,
  62. _tools=TOOLS, _reporters=REPORTERS):
  63. try:
  64. tool = _tools[toolname]
  65. except KeyError:
  66. raise UnsupportedToolError(toolname)
  67. try:
  68. run = tool[cmdname]
  69. report_result = _reporters[cmdname]
  70. except KeyError:
  71. raise UnsupportedCommandError(cmdname)
  72. parents, result = run(toolargs, **subargs)
  73. report_result(result, parents,
  74. **subargs
  75. )
  76. if __name__ == '__main__':
  77. tool, cmd, subargs, toolargs = parse_args()
  78. main(tool, cmd, subargs, toolargs)