From 3f355a4a100178429b13ff0a730069ed4a5d2352 Mon Sep 17 00:00:00 2001 From: Yedaya Katsman Date: Thu, 5 Sep 2024 21:50:12 +0300 Subject: [PATCH] test(scp): Add unit tests for getting remote files This tests the current behaviour of the xfunc_scp_compgen_remote_files function, including escaping, by introducing a fixture to mock ssh invocation on the local host. --- test/fixtures/scp/bin/ssh | 19 ++++++++++++++++ test/t/test_scp.py | 46 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100755 test/fixtures/scp/bin/ssh diff --git a/test/fixtures/scp/bin/ssh b/test/fixtures/scp/bin/ssh new file mode 100755 index 00000000000..0392dfe5d38 --- /dev/null +++ b/test/fixtures/scp/bin/ssh @@ -0,0 +1,19 @@ +#!/bin/bash +set -eu +args=("$@") +while true; do + arg="${args[0]-}" + case "$arg" in + -o) + args=("${args[@]:2}") + ;; + local) + args=("${args[@]:1}") + ;; + *) + break + ;; + esac +done +#shellcheck disable=SC2068 +${args[@]} diff --git a/test/t/test_scp.py b/test/t/test_scp.py index 6410119eb56..b91e5476827 100644 --- a/test/t/test_scp.py +++ b/test/t/test_scp.py @@ -2,7 +2,7 @@ import pytest -from conftest import assert_bash_exec, assert_complete +from conftest import assert_bash_exec, assert_complete, bash_env_saved LIVE_HOST = "bash_completion" @@ -23,7 +23,7 @@ def test_basic(self, hosts, completion): ) ), # Local filenames - ["config", "known_hosts", r"spaced\ \ conf"], + ["bin/", "config", "known_hosts", r"spaced\ \ conf"], ) ) assert completion == expected @@ -43,7 +43,7 @@ def test_basic_spaced_conf(self, hosts, completion): ) ), # Local filenames - ["config", "known_hosts", r"spaced\ \ conf"], + ["bin/", "config", "known_hosts", r"spaced\ \ conf"], ) ) assert completion == expected @@ -101,3 +101,43 @@ def test_remote_path_with_spaces(self, bash): completion = assert_complete(bash, "scp remote_host:spaces") assert_bash_exec(bash, "unset -f ssh") assert completion == r"\\\ in\\\ filename.txt" + + def test_xfunc_remote_files(self, bash): + with bash_env_saved(bash) as bash_env: + bash_env.save_variable("COMPREPLY") + bash_env.write_variable( + "PATH", + "$PWD/scp/bin:$PATH", + quote=False, + ) + bash_env.write_variable("cur", "local:shared/default/") + completions_regular_escape = ( + assert_bash_exec( + bash, + r'_comp_compgen -x scp remote_files; printf "%s\n" "${COMPREPLY[@]}"', + want_output=True, + ) + .strip() + .splitlines() + ) + completions_less_escape = ( + assert_bash_exec( + bash, + r'_comp_compgen -x scp remote_files -l; printf "%s\n" "${COMPREPLY[@]}"', + want_output=True, + ) + .strip() + .splitlines() + ) + assert completions_regular_escape == [ + "shared/default/bar ", + r"shared/default/bar\\\ bar.d/", + "shared/default/foo ", + "shared/default/foo.d/", + ] + assert completions_less_escape == [ + "shared/default/bar ", + r"shared/default/bar\ bar.d/", + "shared/default/foo ", + "shared/default/foo.d/", + ]