From ccae0ad78f8cd2794bbbd4f7e2480cb7938c91b9 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 | 10 ++++++++++ test/t/test_scp.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 43 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..5792d9ef7b0 --- /dev/null +++ b/test/fixtures/scp/bin/ssh @@ -0,0 +1,10 @@ +#!/bin/sh +for arg in "$@"; do + case "$arg" in + -o) + shift 2;; + local) + shift 1;; + esac +done +$@ diff --git a/test/t/test_scp.py b/test/t/test_scp.py index 3bd06eee1ea..85b7ccdd0a9 100644 --- a/test/t/test_scp.py +++ b/test/t/test_scp.py @@ -2,11 +2,12 @@ import pytest -from conftest import assert_bash_exec +from conftest import assert_bash_exec, bash_env_saved LIVE_HOST = "bash_completion" +@pytest.mark.bashcomp(ignore_env=r"^\+COMPREPLY=") class TestScp: @pytest.mark.complete("scp -F config ", cwd="scp") def test_basic(self, hosts, completion): @@ -23,7 +24,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 +44,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 @@ -95,3 +96,32 @@ def test_remote_path_with_nullglob(self, completion): ) def test_remote_path_with_failglob(self, completion): assert not completion + + @pytest.fixture(scope="class") + def ssh_mock(self, request, bash): + with bash_env_saved(bash) as bash_env: + bash_env.write_variable( + "PATH", + "$PWD/scp/bin:$PATH", + quote=False, + ) + yield + + def test_xfunc_remote_files(self, bash, ssh_mock): + with bash_env_saved(bash) as bash_env: + bash_env.write_variable("cur", "local:shared/default/") + completions = ( + assert_bash_exec( + bash, + r'_comp_compgen -ax scp remote_files; printf "%s\n" "${COMPREPLY[@]}";', + want_output=True, + ) + .strip() + .splitlines() + ) + assert completions == [ + "shared/default/bar ", + r"shared/default/bar\\\ bar.d/", + "shared/default/foo ", + "shared/default/foo.d/", + ]