diff --git a/tests/integration/test_completion.py b/tests/integration/test_completion.py index 4b78ac74..33bc3799 100644 --- a/tests/integration/test_completion.py +++ b/tests/integration/test_completion.py @@ -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 @@ -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() diff --git a/tests/integration/test_completion/expected_script.sh b/tests/integration/test_completion/expected_script.sh index fc4b9c7c..711d01d8 100644 --- a/tests/integration/test_completion/expected_script.sh +++ b/tests/integration/test_completion/expected_script.sh @@ -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 @@ -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 @@ -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"))