test_completions.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from argparse import Action
  2. from collections import namedtuple
  3. from typing import Tuple
  4. import pytest
  5. from borgmatic.commands.completion import (
  6. dedent_strip_as_tuple,
  7. exact_options_completion,
  8. has_choice_options,
  9. has_exact_options,
  10. has_file_options,
  11. has_unknown_required_param_options,
  12. )
  13. OptionType = namedtuple('OptionType', ['file', 'choice', 'unknown_required'])
  14. TestCase = Tuple[Action, OptionType]
  15. test_data: list[TestCase] = [
  16. (Action('--flag', 'flag'), OptionType(file=False, choice=False, unknown_required=False)),
  17. *(
  18. (
  19. Action('--flag', 'flag', metavar=metavar),
  20. OptionType(file=True, choice=False, unknown_required=False),
  21. )
  22. for metavar in ('FILENAME', 'PATH')
  23. ),
  24. (
  25. Action('--flag', dest='config_paths'),
  26. OptionType(file=True, choice=False, unknown_required=False),
  27. ),
  28. (
  29. Action('--flag', 'flag', metavar='OTHER'),
  30. OptionType(file=False, choice=False, unknown_required=False),
  31. ),
  32. (
  33. Action('--flag', 'flag', choices=['a', 'b']),
  34. OptionType(file=False, choice=True, unknown_required=False),
  35. ),
  36. (
  37. Action('--flag', 'flag', choices=['a', 'b'], type=str),
  38. OptionType(file=False, choice=True, unknown_required=True),
  39. ),
  40. (
  41. Action('--flag', 'flag', choices=None),
  42. OptionType(file=False, choice=False, unknown_required=False),
  43. ),
  44. (
  45. Action('--flag', 'flag', required=True),
  46. OptionType(file=False, choice=False, unknown_required=True),
  47. ),
  48. *(
  49. (
  50. Action('--flag', 'flag', nargs=nargs),
  51. OptionType(file=False, choice=False, unknown_required=True),
  52. )
  53. for nargs in ('+', '*')
  54. ),
  55. *(
  56. (
  57. Action('--flag', 'flag', metavar=metavar),
  58. OptionType(file=False, choice=False, unknown_required=True),
  59. )
  60. for metavar in ('PATTERN', 'KEYS', 'N')
  61. ),
  62. *(
  63. (
  64. Action('--flag', 'flag', type=type, default=None),
  65. OptionType(file=False, choice=False, unknown_required=True),
  66. )
  67. for type in (int, str)
  68. ),
  69. (
  70. Action('--flag', 'flag', type=int, default=1),
  71. OptionType(file=False, choice=False, unknown_required=False),
  72. ),
  73. (
  74. Action('--flag', 'flag', type=str, required=True, metavar='PATH'),
  75. OptionType(file=True, choice=False, unknown_required=True),
  76. ),
  77. (
  78. Action('--flag', 'flag', type=str, required=True, metavar='PATH', default='/dev/null'),
  79. OptionType(file=True, choice=False, unknown_required=True),
  80. ),
  81. (
  82. Action('--flag', 'flag', type=str, required=False, metavar='PATH', default='/dev/null'),
  83. OptionType(file=True, choice=False, unknown_required=False),
  84. ),
  85. ]
  86. @pytest.mark.parametrize('action, option_type', test_data)
  87. def test_has_file_options_detects_file_options(action: Action, option_type: OptionType):
  88. assert (
  89. has_file_options(action) == option_type.file
  90. ), f'Action: {action} should be file={option_type.file}'
  91. @pytest.mark.parametrize('action, option_type', test_data)
  92. def test_has_choice_options_detects_choice_options(action: Action, option_type: OptionType):
  93. assert (
  94. has_choice_options(action) == option_type.choice
  95. ), f'Action: {action} should be choice={option_type.choice}'
  96. @pytest.mark.parametrize('action, option_type', test_data)
  97. def test_has_unknown_required_param_options_detects_unknown_required_param_options(
  98. action: Action, option_type: OptionType
  99. ):
  100. assert (
  101. has_unknown_required_param_options(action) == option_type.unknown_required
  102. ), f'Action: {action} should be unknown_required={option_type.unknown_required}'
  103. @pytest.mark.parametrize('action, option_type', test_data)
  104. def test_has_exact_options_detects_exact_options(action: Action, option_type: OptionType):
  105. assert has_exact_options(action) == (
  106. True in option_type
  107. ), f'Action: {action} should have exact options given {option_type}'
  108. @pytest.mark.parametrize('action, option_type', test_data)
  109. def test_produce_exact_options_completion(action: Action, option_type: OptionType):
  110. try:
  111. completion = exact_options_completion(action)
  112. if True in option_type:
  113. assert completion.startswith(
  114. '\ncomplete -c borgmatic'
  115. ), f'Completion should start with "complete -c borgmatic", got {completion}'
  116. else:
  117. assert completion == '', f'Completion should be empty, got {completion}'
  118. except ValueError as value_error:
  119. assert False, f'exact_options_completion raised ValueError: {value_error}'
  120. def test_dedent_strip_as_tuple_does_not_raise():
  121. dedent_strip_as_tuple(
  122. '''
  123. a
  124. b
  125. '''
  126. )