create doccomments, start writing unit tests

This commit is contained in:
Isaac 2023-05-06 10:42:06 -07:00
parent 59a6ce1462
commit 469e0ccace
No known key found for this signature in database
GPG key ID: E69FB5A841448A48
2 changed files with 29 additions and 3 deletions

View file

@ -71,6 +71,9 @@ def bash_completion():
def has_file_options(action: Action): def has_file_options(action: Action):
'''
Given an argparse.Action instance, return True if it takes a file argument.
'''
return action.metavar in ( return action.metavar in (
'FILENAME', 'FILENAME',
'PATH', 'PATH',
@ -78,6 +81,9 @@ def has_file_options(action: Action):
def has_choice_options(action: Action): def has_choice_options(action: Action):
'''
Given an argparse.Action instance, return True if it takes one of a predefined set of arguments.
'''
return action.choices is not None return action.choices is not None
@ -103,9 +109,8 @@ def has_exact_options(action: Action):
def exact_options_completion(action: Action): def exact_options_completion(action: Action):
''' '''
Given an argparse.Action instance, return a completion invocation Given an argparse.Action instance, return a completion invocation that forces file completion or options
that forces file completion or options completion, if the action completion, if the action takes such an argument and was the last action on the command line.
takes such an argument and was the last action on the command line.
Otherwise, return an empty string. Otherwise, return an empty string.
''' '''

View file

@ -0,0 +1,21 @@
from argparse import Action
import pytest
from borgmatic.commands.completion import has_exact_options, has_file_options
file_options_test_data = [
(Action('--flag', 'flag'), False),
(Action('--flag', 'flag', metavar='FILENAME'), True),
(Action('--flag', 'flag', metavar='PATH'), True),
(Action('--flag', dest='config_paths'), True),
(Action('--flag', 'flag', metavar='OTHER'), False),
]
@pytest.mark.parametrize('action, expected', file_options_test_data)
def test_has_file_options_detects_file_options(action: Action, expected: bool):
assert has_file_options(action) == expected
# if has_file_options(action) was true, has_exact_options(action) should also be true
if expected:
assert has_exact_options(action)