Skip to content

Commit 1b29aa5

Browse files
committed
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.
1 parent 9639d31 commit 1b29aa5

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

test/fixtures/scp/bin/ssh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
for arg; do
3+
case "$arg" in
4+
-o)
5+
shift 2
6+
;;
7+
local)
8+
shift 1
9+
;;
10+
esac
11+
done
12+
#shellcheck disable=SC2068
13+
$@

test/t/test_scp.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from conftest import assert_bash_exec, assert_complete
5+
from conftest import assert_bash_exec, assert_complete, bash_env_saved
66

77
LIVE_HOST = "bash_completion"
88

@@ -23,7 +23,7 @@ def test_basic(self, hosts, completion):
2323
)
2424
),
2525
# Local filenames
26-
["config", "known_hosts", r"spaced\ \ conf"],
26+
["bin/", "config", "known_hosts", r"spaced\ \ conf"],
2727
)
2828
)
2929
assert completion == expected
@@ -43,7 +43,7 @@ def test_basic_spaced_conf(self, hosts, completion):
4343
)
4444
),
4545
# Local filenames
46-
["config", "known_hosts", r"spaced\ \ conf"],
46+
["bin/", "config", "known_hosts", r"spaced\ \ conf"],
4747
)
4848
)
4949
assert completion == expected
@@ -101,3 +101,43 @@ def test_remote_path_with_spaces(self, bash):
101101
completion = assert_complete(bash, "scp remote_host:spaces")
102102
assert_bash_exec(bash, "unset -f ssh")
103103
assert completion == r"\\\ in\\\ filename.txt"
104+
105+
def test_xfunc_remote_files(self, bash):
106+
with bash_env_saved(bash) as bash_env:
107+
bash_env.save_variable("COMPREPLY")
108+
bash_env.write_variable(
109+
"PATH",
110+
"$PWD/scp/bin:$PATH",
111+
quote=False,
112+
)
113+
bash_env.write_variable("cur", "local:shared/default/")
114+
completions_regular_escape = (
115+
assert_bash_exec(
116+
bash,
117+
r'_comp_compgen -x scp remote_files; printf "%s\n" "${COMPREPLY[@]}"',
118+
want_output=True,
119+
)
120+
.strip()
121+
.splitlines()
122+
)
123+
completions_less_escape = (
124+
assert_bash_exec(
125+
bash,
126+
r'_comp_compgen -x scp remote_files -l; printf "%s\n" "${COMPREPLY[@]}"',
127+
want_output=True,
128+
)
129+
.strip()
130+
.splitlines()
131+
)
132+
assert completions_regular_escape == [
133+
"shared/default/bar ",
134+
r"shared/default/bar\\\ bar.d/",
135+
"shared/default/foo ",
136+
"shared/default/foo.d/",
137+
]
138+
assert completions_less_escape == [
139+
"shared/default/bar ",
140+
r"shared/default/bar\ bar.d/",
141+
"shared/default/foo ",
142+
"shared/default/foo.d/",
143+
]

0 commit comments

Comments
 (0)