test_completions.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. has_choice_options,
  7. has_exact_options,
  8. has_file_options,
  9. has_unknown_required_param_options,
  10. )
  11. OptionType = namedtuple('OptionType', ['file', 'choice', 'unknown_required'])
  12. TestCase = Tuple[Action, OptionType]
  13. test_data: list[TestCase] = [
  14. (Action('--flag', 'flag'), OptionType(file=False, choice=False, unknown_required=False)),
  15. *(
  16. (
  17. Action('--flag', 'flag', metavar=metavar),
  18. OptionType(file=True, choice=False, unknown_required=False),
  19. )
  20. for metavar in ('FILENAME', 'PATH')
  21. ),
  22. (
  23. Action('--flag', dest='config_paths'),
  24. OptionType(file=True, choice=False, unknown_required=False),
  25. ),
  26. (
  27. Action('--flag', 'flag', metavar='OTHER'),
  28. OptionType(file=False, choice=False, unknown_required=False),
  29. ),
  30. (
  31. Action('--flag', 'flag', choices=['a', 'b']),
  32. OptionType(file=False, choice=True, unknown_required=False),
  33. ),
  34. (
  35. Action('--flag', 'flag', choices=['a', 'b'], type=str),
  36. OptionType(file=False, choice=True, unknown_required=True),
  37. ),
  38. (
  39. Action('--flag', 'flag', choices=None),
  40. OptionType(file=False, choice=False, unknown_required=False),
  41. ),
  42. (
  43. Action('--flag', 'flag', required=True),
  44. OptionType(file=False, choice=False, unknown_required=True),
  45. ),
  46. *(
  47. (
  48. Action('--flag', 'flag', nargs=nargs),
  49. OptionType(file=False, choice=False, unknown_required=True),
  50. )
  51. for nargs in ('+', '*')
  52. ),
  53. *(
  54. (
  55. Action('--flag', 'flag', metavar=metavar),
  56. OptionType(file=False, choice=False, unknown_required=True),
  57. )
  58. for metavar in ('PATTERN', 'KEYS', 'N')
  59. ),
  60. *(
  61. (
  62. Action('--flag', 'flag', type=type, default=None),
  63. OptionType(file=False, choice=False, unknown_required=True),
  64. )
  65. for type in (int, str)
  66. ),
  67. (
  68. Action('--flag', 'flag', type=int, default=1),
  69. OptionType(file=False, choice=False, unknown_required=False),
  70. ),
  71. (
  72. Action('--flag', 'flag', type=str, required=True, metavar='PATH'),
  73. OptionType(file=True, choice=False, unknown_required=True),
  74. ),
  75. (
  76. Action('--flag', 'flag', type=str, required=True, metavar='PATH', default='/dev/null'),
  77. OptionType(file=True, choice=False, unknown_required=True),
  78. ),
  79. (
  80. Action('--flag', 'flag', type=str, required=False, metavar='PATH', default='/dev/null'),
  81. OptionType(file=True, choice=False, unknown_required=False),
  82. ),
  83. ]
  84. @pytest.mark.parametrize('action, option_type', test_data)
  85. def test_has_file_options_detects_file_options(action: Action, option_type: OptionType):
  86. assert has_file_options(action) == option_type.file
  87. # if has_file_options(action) was true, has_exact_options(action) should also be true
  88. if option_type.file:
  89. assert has_exact_options(action)
  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 has_choice_options(action) == option_type.choice
  93. # if has_choice_options(action) was true, has_exact_options(action) should also be true
  94. if option_type.choice:
  95. assert has_exact_options(action)
  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 has_unknown_required_param_options(action) == option_type.unknown_required
  101. # if has_unknown_required_param_options(action) was true, has_exact_options(action) should also be true
  102. if option_type.unknown_required:
  103. assert has_exact_options(action)
  104. @pytest.mark.parametrize('action, option_type', test_data)
  105. def test_has_exact_options_detects_exact_options(action: Action, option_type: OptionType):
  106. assert has_exact_options(action) == (
  107. option_type.file or option_type.choice or option_type.unknown_required
  108. )