Skip to content

Commit

Permalink
test(scp): Add unit tests for getting remote files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yedayak committed Sep 6, 2024
1 parent 529aff8 commit ccae0ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
10 changes: 10 additions & 0 deletions test/fixtures/scp/bin/ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
for arg in "$@"; do
case "$arg" in
-o)
shift 2;;
local)
shift 1;;
esac
done
$@
36 changes: 33 additions & 3 deletions test/t/test_scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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/",
]

0 comments on commit ccae0ad

Please sign in to comment.