From 6e54cc580235435410d9b570cf9cd508122e3f98 Mon Sep 17 00:00:00 2001 From: dangertoaster Date: Fri, 16 Oct 2026 15:00:09 +0000 Subject: [PATCH 1/2] test: make completion test robust to shfmt formatting --- tests/integration/test_completion.py | 20 +++++ .../test_completion/expected_script.sh | 82 +++++++++---------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/tests/integration/test_completion.py b/tests/integration/test_completion.py index 4b78ac74..5912c06e 100644 --- a/tests/integration/test_completion.py +++ b/tests/integration/test_completion.py @@ -13,10 +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 pytest + import craft_cli from craft_cli.completion import complete from craft_cli.completion.completion import DispatcherAndConfig @@ -73,9 +77,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")) From b3516a5e82da49f9f2d6654aa754b1fae5fdccf2 Mon Sep 17 00:00:00 2001 From: dangertoaster Date: Fri, 16 Oct 2026 16:54:06 +0000 Subject: [PATCH 2/2] style: fix import ordering in test_completion Ruff I001: craft_cli imports sort before pytest (first-party vs third-party) in this repository's isort configuration. --- tests/integration/test_completion.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/test_completion.py b/tests/integration/test_completion.py index 5912c06e..33bc3799 100644 --- a/tests/integration/test_completion.py +++ b/tests/integration/test_completion.py @@ -19,9 +19,8 @@ from pathlib import Path from typing import Any -import pytest - import craft_cli +import pytest from craft_cli.completion import complete from craft_cli.completion.completion import DispatcherAndConfig