test_completions.py 4.0 KB

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