-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
156 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from typing import Union, Callable, Iterable, List, Type, Any | ||
from typing import Union, Callable, Iterable, List, Type, Any, Optional | ||
|
||
Action = Callable[..., None] | ||
ChoiceProvider = Union[Iterable[Any], Callable[..., List[Any]]] | ||
ChoiceProvider = Union[Iterable[Any], Callable[[Optional[str]], List[Any]]] | ||
TypeOrParser = Union[Type, Callable[[str], Any]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .file import file_completer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
from typing import Optional, Tuple | ||
|
||
|
||
def file_completer(current: Optional[str]): | ||
listdir, prefixdir = _current_listing_dir(current) | ||
names = [] | ||
for file in os.listdir(listdir): | ||
filepath = f'{prefixdir}{file}' | ||
if os.path.isdir(filepath): | ||
# prevent from immediate resolving | ||
names.append(f'{filepath}/') | ||
names.append(f'{filepath}/ ') | ||
else: | ||
names.append(filepath) | ||
return sorted(names) | ||
|
||
|
||
def _current_listing_dir(current: Optional[str]) -> Tuple[str, str]: | ||
if not current: | ||
return '.', '' | ||
|
||
current_path, current_node = os.path.split(current) | ||
if not current_path or current_path == '.': | ||
return '.', '' | ||
|
||
if not os.path.isdir(current_path): | ||
return '.', '' | ||
|
||
return current_path, f'{current_path}/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.1.1" | ||
__version__ = "1.1.2" |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from pathlib import Path | ||
|
||
from cliglue import * | ||
from cliglue.completers import file_completer | ||
from tests.asserts import MockIO | ||
|
||
|
||
def test_file_completer_empty(): | ||
with MockIO('--autocomplete', '"app "') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
proposals = mockio.stripped().splitlines() | ||
assert '.gitignore' in proposals | ||
assert 'tests/' in proposals | ||
|
||
assert 'tests' not in proposals | ||
assert 'tests/autocomplete/' not in proposals | ||
assert 'tests/__init__.py' not in proposals | ||
assert '.' not in proposals | ||
assert '..' not in proposals | ||
|
||
|
||
def test_file_completer_file(): | ||
with MockIO('--autocomplete', '"app .gitignore"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
assert mockio.stripped() == '.gitignore' | ||
|
||
|
||
def test_file_completer_dir1(): | ||
with MockIO('--autocomplete', '"app tests"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
proposals = set(mockio.stripped().splitlines()) | ||
assert proposals == {'tests/'} | ||
|
||
|
||
def test_file_completer_dir_content(): | ||
with MockIO('--autocomplete', '"app tests/"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
proposals = mockio.stripped().splitlines() | ||
assert 'tests/autocomplete/' in proposals | ||
assert 'tests/__init__.py' in proposals | ||
|
||
assert 'tests/' not in proposals | ||
assert 'tests/autocomplete' not in proposals | ||
assert '.gitignore' not in proposals | ||
assert '.' not in proposals | ||
assert '..' not in proposals | ||
|
||
|
||
def test_file_completer_subdir(): | ||
with MockIO('--autocomplete', '"app tests/autocomplete"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
proposals = set(mockio.stripped().splitlines()) | ||
assert proposals == {'tests/autocomplete/'} | ||
|
||
|
||
def test_file_completer_subdir_content(): | ||
with MockIO('--autocomplete', '"app tests/autocomplete/"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
proposals = mockio.stripped().splitlines() | ||
assert 'tests/autocomplete/__init__.py' in proposals | ||
|
||
assert 'tests/autocomplete/' not in proposals | ||
assert 'tests/autocomplete' not in proposals | ||
assert 'tests/__init__.py' not in proposals | ||
assert 'tests/' not in proposals | ||
assert '.gitignore' not in proposals | ||
assert '.' not in proposals | ||
assert '..' not in proposals | ||
|
||
|
||
def test_file_completer_subdir_file(): | ||
with MockIO('--autocomplete', '"app tests/autocomplete/__init__.py"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
assert mockio.stripped() == 'tests/autocomplete/__init__.py' | ||
|
||
|
||
def test_file_completer_notexisting_dir(): | ||
with MockIO('--autocomplete', '"app tests/there_is_no_dir/__init__.py"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
assert mockio.stripped() == '' | ||
|
||
|
||
def test_complete_absolute_files(): | ||
Path('/tmp/cliglue_test_autocomplete_425_896').write_text('') | ||
|
||
with MockIO('--autocomplete', '"app /tmp/cliglue_test_autocomplete_425"') as mockio: | ||
CliBuilder(reraise_error=True).has( | ||
arguments('f', choices=file_completer), | ||
).run() | ||
assert mockio.stripped() == '/tmp/cliglue_test_autocomplete_425_896' | ||
|
||
Path('/tmp/cliglue_test_autocomplete_425_896').unlink() |