test_completions.py 4.6 KB

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