Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(scp): Add unit tests for getting remote files #1244

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
hooks:
- id: shfmt
types: [text]
files: ^(bash_completion(\.d/[^/]+\.bash)?|completions/.+|test/(config/bashrc|fallback/update-fallback-links|runLint|update-test-cmd-list)|.+\.sh(\.in)?)$
files: ^(bash_completion(\.d/[^/]+\.bash)?|completions/.+|test/(config/bashrc|fixtures/.+/bin/.+|fallback/update-fallback-links|runLint|update-test-cmd-list)|.+\.sh(\.in)?)$
exclude: ^completions/(\.gitignore|Makefile.*)$

- repo: https://github.com/shellcheck-py/shellcheck-py
Expand All @@ -22,7 +22,7 @@ repos:
- id: shellcheck
args: [-f, gcc]
types: [text]
files: ^(bash_completion(\.d/[^/]+\.bash)?|completions/.+|test/(config/bashrc|fallback/update-fallback-links|runLint|update-test-cmd-list)|.+\.sh(\.in)?)$
files: ^(bash_completion(\.d/[^/]+\.bash)?|completions/.+|test/(config/bashrc|fixtures/.+/bin/.+|fallback/update-fallback-links|runLint|update-test-cmd-list)|.+\.sh(\.in)?)$
exclude: ^completions/(\.gitignore|Makefile.*)$
require_serial: false # We disable SC1090 anyway, so parallel is ok

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/mount/bin/showmount
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
#!/bin/bash

if [ "$1" = -e ] && [ "$2" = mocksrv ]; then
if [[ $1 == -e && $2 == "mocksrv" ]]; then
echo "Header line"
echo "/test/path"
echo "/test/path2"
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/scp/bin/ssh
akinomyoga marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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[@]}
46 changes: 43 additions & 3 deletions test/t/test_scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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/",
]