When BUP_TEST_LEVEL doesn't exist in the environment, throw an
exception when there's no host or the host is '-' so that you can't
easily, accidentally use the testing trick when not testing. This
will also catch a missing host/authority in URLs,
e.g. ssh:///some/path.
Signed-off-by: Rob Browning <
r...@defaultvalue.org>
Tested-by: Rob Browning <
r...@defaultvalue.org>
---
lib/bup/ssh.py | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/lib/bup/ssh.py b/lib/bup/ssh.py
index e2577c22..c086c652 100644
--- a/lib/bup/ssh.py
+++ b/lib/bup/ssh.py
@@ -2,6 +2,7 @@
Connect to a remote host via SSH and execute a command on the host.
"""
+from os import environb as environ
from subprocess import PIPE, Popen
import re
@@ -10,17 +11,30 @@ from bup.compat import environ
from
bup.io import buglvl, debug1
-def connect(rhost, port, subcmd, stderr=None):
- """Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
+def connect(destination, port, subcmd, stderr=None):
+ """Connect to the destination and execute the bup subcmd there.
+ The destination is passed to ssh(1) as its destination argument.
+
+ When BUP_TEST_LEVEL exists in the environment and the destination
+ is false, or b'-', run the subcmd as a subprocess rather than via
+ ssh.
+
+ """
assert not re.search(br'[^\w-]', subcmd)
- if rhost is None or rhost == b'-':
+ if not destination:
+ if b'BUP_TEST_LEVEL' not in environ:
+ raise Exception(f'no ssh destination')
+ argv = [path.exe(), subcmd]
+ elif destination == b'-':
+ if b'BUP_TEST_LEVEL' not in environ:
+ raise Exception(f'invalid ssh destination "-"')
argv = [path.exe(), subcmd]
else:
force_tty = int(environ.get(b'BUP_FORCE_TTY', 0))
argv = [b'ssh']
if port:
argv.extend((b'-p', port))
- argv.extend((rhost, b'--',
+ argv.extend((destination, b'--',
b"sh -c 'BUP_DEBUG=%d BUP_FORCE_TTY=%d bup %s'"
% (buglvl, force_tty, subcmd)))
debug1(f'ssh: {argv!r}\n')
--
2.47.3