test_fish.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from argparse import Action
  2. from collections import namedtuple
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.commands.completion import fish as module
  6. OptionType = namedtuple('OptionType', ['file', 'choice', 'unknown_required'])
  7. test_data = [
  8. (Action('--flag', 'flag'), OptionType(file=False, choice=False, unknown_required=False)),
  9. *(
  10. (
  11. Action('--flag', 'flag', metavar=metavar),
  12. OptionType(file=True, choice=False, unknown_required=False),
  13. )
  14. for metavar in ('FILENAME', 'PATH')
  15. ),
  16. (
  17. Action('--flag', dest='config_paths'),
  18. OptionType(file=True, choice=False, unknown_required=False),
  19. ),
  20. (
  21. Action('--flag', 'flag', metavar='OTHER'),
  22. OptionType(file=False, choice=False, unknown_required=False),
  23. ),
  24. (
  25. Action('--flag', 'flag', choices=['a', 'b']),
  26. OptionType(file=False, choice=True, unknown_required=False),
  27. ),
  28. (
  29. Action('--flag', 'flag', choices=['a', 'b'], type=str),
  30. OptionType(file=False, choice=True, unknown_required=True),
  31. ),
  32. (
  33. Action('--flag', 'flag', choices=None),
  34. OptionType(file=False, choice=False, unknown_required=False),
  35. ),
  36. (
  37. Action('--flag', 'flag', required=True),
  38. OptionType(file=False, choice=False, unknown_required=True),
  39. ),
  40. *(
  41. (
  42. Action('--flag', 'flag', nargs=nargs),
  43. OptionType(file=False, choice=False, unknown_required=True),
  44. )
  45. for nargs in ('+', '*')
  46. ),
  47. *(
  48. (
  49. Action('--flag', 'flag', metavar=metavar),
  50. OptionType(file=False, choice=False, unknown_required=True),
  51. )
  52. for metavar in ('PATTERN', 'KEYS', 'N')
  53. ),
  54. *(
  55. (
  56. Action('--flag', 'flag', type=flag_type, default=None),
  57. OptionType(file=False, choice=False, unknown_required=True),
  58. )
  59. for flag_type in (int, str)
  60. ),
  61. (
  62. Action('--flag', 'flag', type=int, default=1),
  63. OptionType(file=False, choice=False, unknown_required=False),
  64. ),
  65. (
  66. Action('--flag', 'flag', type=str, required=True, metavar='PATH'),
  67. OptionType(file=True, choice=False, unknown_required=True),
  68. ),
  69. (
  70. Action('--flag', 'flag', type=str, required=True, metavar='PATH', default='/dev/null'),
  71. OptionType(file=True, choice=False, unknown_required=True),
  72. ),
  73. (
  74. Action('--flag', 'flag', type=str, required=False, metavar='PATH', default='/dev/null'),
  75. OptionType(file=True, choice=False, unknown_required=False),
  76. ),
  77. ]
  78. @pytest.mark.parametrize('action, option_type', test_data)
  79. def test_has_file_options_detects_file_options(action: Action, option_type: OptionType):
  80. assert module.has_file_options(action) == option_type.file
  81. @pytest.mark.parametrize('action, option_type', test_data)
  82. def test_has_choice_options_detects_choice_options(action: Action, option_type: OptionType):
  83. assert module.has_choice_options(action) == option_type.choice
  84. @pytest.mark.parametrize('action, option_type', test_data)
  85. def test_has_unknown_required_param_options_detects_unknown_required_param_options(
  86. action: Action,
  87. 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_exact_options_completion_produces_reasonable_completions(
  95. action: Action,
  96. 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. )