test_fish.py 4.5 KB

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