test_arguments.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from flexmock import flexmock
  2. from borgmatic.commands import arguments as module
  3. def test_parse_subparser_arguments_consumes_subparser_arguments_before_subparser_name():
  4. action_namespace = flexmock(foo=True)
  5. subparsers = flexmock(
  6. choices={
  7. 'action': flexmock(parse_known_args=lambda arguments: (action_namespace, [])),
  8. 'other': flexmock(),
  9. }
  10. )
  11. arguments = module.parse_subparser_arguments(('--foo', 'true', 'action'), subparsers)
  12. assert arguments == {'action': action_namespace}
  13. def test_parse_subparser_arguments_consumes_subparser_arguments_after_subparser_name():
  14. action_namespace = flexmock(foo=True)
  15. subparsers = flexmock(
  16. choices={
  17. 'action': flexmock(parse_known_args=lambda arguments: (action_namespace, [])),
  18. 'other': flexmock(),
  19. }
  20. )
  21. arguments = module.parse_subparser_arguments(('action', '--foo', 'true'), subparsers)
  22. assert arguments == {'action': action_namespace}
  23. def test_parse_subparser_arguments_consumes_subparser_arguments_with_alias():
  24. action_namespace = flexmock(foo=True)
  25. action_subparser = flexmock(parse_known_args=lambda arguments: (action_namespace, []))
  26. subparsers = flexmock(
  27. choices={
  28. 'action': action_subparser,
  29. '-a': action_subparser,
  30. 'other': flexmock(),
  31. '-o': flexmock(),
  32. }
  33. )
  34. flexmock(module).SUBPARSER_ALIASES = {'action': ['-a'], 'other': ['-o']}
  35. arguments = module.parse_subparser_arguments(('-a', '--foo', 'true'), subparsers)
  36. assert arguments == {'action': action_namespace}
  37. def test_parse_subparser_arguments_consumes_multiple_subparser_arguments():
  38. action_namespace = flexmock(foo=True)
  39. other_namespace = flexmock(bar=3)
  40. subparsers = flexmock(
  41. choices={
  42. 'action': flexmock(
  43. parse_known_args=lambda arguments: (action_namespace, ['--bar', '3'])
  44. ),
  45. 'other': flexmock(parse_known_args=lambda arguments: (other_namespace, [])),
  46. }
  47. )
  48. arguments = module.parse_subparser_arguments(
  49. ('action', '--foo', 'true', 'other', '--bar', '3'), subparsers
  50. )
  51. assert arguments == {'action': action_namespace, 'other': other_namespace}
  52. def test_parse_subparser_arguments_applies_default_subparsers():
  53. prune_namespace = flexmock()
  54. create_namespace = flexmock(progress=True)
  55. check_namespace = flexmock()
  56. subparsers = flexmock(
  57. choices={
  58. 'prune': flexmock(parse_known_args=lambda arguments: (prune_namespace, ['--progress'])),
  59. 'create': flexmock(parse_known_args=lambda arguments: (create_namespace, [])),
  60. 'check': flexmock(parse_known_args=lambda arguments: (check_namespace, [])),
  61. 'other': flexmock(),
  62. }
  63. )
  64. arguments = module.parse_subparser_arguments(('--progress'), subparsers)
  65. assert arguments == {
  66. 'prune': prune_namespace,
  67. 'create': create_namespace,
  68. 'check': check_namespace,
  69. }
  70. def test_parse_global_arguments_with_help_does_not_apply_default_subparsers():
  71. global_namespace = flexmock(verbosity='lots')
  72. action_namespace = flexmock()
  73. top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
  74. subparsers = flexmock(
  75. choices={
  76. 'action': flexmock(
  77. parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
  78. ),
  79. 'other': flexmock(),
  80. }
  81. )
  82. arguments = module.parse_global_arguments(
  83. ('--verbosity', 'lots', '--help'), top_level_parser, subparsers
  84. )
  85. assert arguments == global_namespace
  86. def test_parse_global_arguments_consumes_global_arguments_before_subparser_name():
  87. global_namespace = flexmock(verbosity='lots')
  88. action_namespace = flexmock()
  89. top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
  90. subparsers = flexmock(
  91. choices={
  92. 'action': flexmock(
  93. parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
  94. ),
  95. 'other': flexmock(),
  96. }
  97. )
  98. arguments = module.parse_global_arguments(
  99. ('--verbosity', 'lots', 'action'), top_level_parser, subparsers
  100. )
  101. assert arguments == global_namespace
  102. def test_parse_global_arguments_consumes_global_arguments_after_subparser_name():
  103. global_namespace = flexmock(verbosity='lots')
  104. action_namespace = flexmock()
  105. top_level_parser = flexmock(parse_args=lambda arguments: global_namespace)
  106. subparsers = flexmock(
  107. choices={
  108. 'action': flexmock(
  109. parse_known_args=lambda arguments: (action_namespace, ['--verbosity', 'lots'])
  110. ),
  111. 'other': flexmock(),
  112. }
  113. )
  114. arguments = module.parse_global_arguments(
  115. ('action', '--verbosity', 'lots'), top_level_parser, subparsers
  116. )
  117. assert arguments == global_namespace