test_completions.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 has_file_options(action) == option_type.file
  89. @pytest.mark.parametrize('action, option_type', test_data)
  90. def test_has_choice_options_detects_choice_options(action: Action, option_type: OptionType):
  91. assert has_choice_options(action) == option_type.choice
  92. @pytest.mark.parametrize('action, option_type', test_data)
  93. def test_has_unknown_required_param_options_detects_unknown_required_param_options(
  94. action: Action, option_type: OptionType
  95. ):
  96. assert has_unknown_required_param_options(action) == option_type.unknown_required
  97. @pytest.mark.parametrize('action, option_type', test_data)
  98. def test_has_exact_options_detects_exact_options(action: Action, option_type: OptionType):
  99. assert has_exact_options(action) == (True in option_type)
  100. @pytest.mark.parametrize('action, option_type', test_data)
  101. def test_produce_exact_options_completion(action: Action, option_type: OptionType):
  102. completion = exact_options_completion(action)
  103. if True in option_type:
  104. assert completion.startswith('\ncomplete -c borgmatic')
  105. else:
  106. assert completion == ''
  107. def test_dedent_strip_as_tuple_does_not_raise():
  108. dedent_strip_as_tuple(
  109. '''
  110. a
  111. b
  112. '''
  113. )