Skip to content

Commit

Permalink
pytest: complete test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Apr 19, 2020
1 parent e649ea1 commit f4ac160
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion completions/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
_pytest()
{
local cur prev words cword split
_init_completion -s || return
_init_completion -s -n : || return

case $prev in
--help|--maxfail|--report|--junit-prefix|--doctest-glob|-!(-*)[hkmrp])
Expand Down Expand Up @@ -79,6 +79,16 @@ _pytest()
return
fi

if [[ "$cur" == *.py:* ]]; then
local file="${cur/.py:*/.py}"
local -a classes=(
"$(cat "$file" 2>/dev/null |
awk '$1 == "class" { sub("[:(].*","",$2); print $2 }')" )
COMPREPLY=( $(compgen -P "$file::" -W "${classes[@]}" -- "${cur##*.py:?(:)}") )
__ltrim_colon_completions "$cur"
return
fi

_filedir py
} &&
complete -F _pytest pytest pytest-2 pytest-3 py.test py.test-2 py.test-3
Expand Down
5 changes: 5 additions & 0 deletions test/t/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ def test_1(self, completion):
@pytest.mark.complete("pytest -")
def test_2(self, completion):
assert completion

@pytest.mark.complete("pytest ../t/test_pytest.py:")
def test_classes(self, completion):
assert len(completion) == 1
assert next(iter(completion)).endswith("::TestPytest")

0 comments on commit f4ac160

Please sign in to comment.