Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/integration/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import shutil
import subprocess
from collections.abc import Callable
from pathlib import Path
from typing import Any

import craft_cli
import pytest
from craft_cli.completion import complete
from craft_cli.completion.completion import DispatcherAndConfig

Expand Down Expand Up @@ -73,9 +76,25 @@ def _inner() -> DispatcherAndConfig:


def test_completion_output() -> None:
shfmt = shutil.which("shfmt")
if shfmt is None:
pytest.skip("'shfmt' command is required to normalize shell script formatting")

app_info_func = get_app_info_func([FakeLsCommand, FakeCpCommand])
actual_output = complete("testcraft", app_info_func)

# Normalize the generated script through shfmt before comparing: the
# fixture file is covered by the repo's shfmt formatting (while the Jinja2
# template that produces this output is excluded), so both sides must be
# compared in shfmt-normalized form.
actual_output = subprocess.run(
[shfmt, "-i", "2"],
input=actual_output,
capture_output=True,
text=True,
check=True,
).stdout

expected_output = (
Path(__file__).parent / "test_completion" / "expected_script.sh"
).read_text()
Expand Down
82 changes: 41 additions & 41 deletions tests/integration/test_completion/expected_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

# shellcheck disable=2207

_complete_testcraft(){
_complete_testcraft() {
local cur prev all_cmds cmd
all_cmds=( ls cp )
all_cmds=(ls cp)
cur="$2"
prev="$3"
# Remove "$cur" (the last element) from $COMP_WORDS
Expand All @@ -22,7 +22,7 @@ _complete_testcraft(){
if [[ "${arg:0:1}" != "-" ]]; then
cmd="${arg}"
break
elif [[ "${arg}" == "--help" ]]; then # "--help" works the same as "help"
elif [[ "${arg}" == "--help" ]]; then # "--help" works the same as "help"
cmd="help"
break
fi
Expand All @@ -32,49 +32,49 @@ _complete_testcraft(){
# Global arguments may be used either before or after the command name, so we
# use those global arguments in each function.
case "${cmd}" in
ls)
case "${prev}" in
-h|--help)
COMPREPLY=($(compgen -- "$cur"))
return
;;
--color)
COMPREPLY=($(compgen -W 'always auto never' -- "$cur"))
return
;;
*)
# Not in the middle of a command option, present all options.
COMPREPLY=(
$(compgen -W "-h --help -a --all --color" -- "$cur")
$(compgen -o bashdefault -A file -- "$cur")
)
return
;;
esac
ls)
case "${prev}" in
-h | --help)
COMPREPLY=($(compgen -- "$cur"))
return
;;
--color)
COMPREPLY=($(compgen -W 'always auto never' -- "$cur"))
return
;;
*)
# Not in the middle of a command option, present all options.
COMPREPLY=(
$(compgen -W "-h --help -a --all --color" -- "$cur")
$(compgen -o bashdefault -A file -- "$cur")
)
return
;;
cp)
case "${prev}" in
-h|--help)
COMPREPLY=($(compgen -- "$cur"))
return
;;
*)
# Not in the middle of a command option, present all options.
COMPREPLY=(
$(compgen -W "-h --help" -- "$cur")
$(compgen -o bashdefault -A file -- "$cur")
)
return
;;
esac
esac
;;
cp)
case "${prev}" in
-h | --help)
COMPREPLY=($(compgen -- "$cur"))
return
;;
*)
# Not in the middle of a command option, present all options.
COMPREPLY=(
$(compgen -W "-h --help" -- "$cur")
$(compgen -o bashdefault -A file -- "$cur")
)
return
;;
esac
;;
esac

case "${prev}" in
--verbosity)
COMPREPLY=($(compgen -W 'quiet brief verbose debug trace' -- "$cur"))
return
;;
--verbosity)
COMPREPLY=($(compgen -W 'quiet brief verbose debug trace' -- "$cur"))
return
;;
esac

COMPREPLY=($(compgen -W "${all_cmds[*]}" -- "$cur"))
Expand Down
Loading