From 68c2df7fce1a096d2a819c1b777f11310dd795bb Mon Sep 17 00:00:00 2001 From: Ron Frederick Date: Wed, 17 Jul 2024 17:47:28 -0700 Subject: [PATCH] Another update to config file parsing The last update to the config file parser still didn't handle the conversion of equals to space in some cases. Specifically, this was a problem for SetEnv commands. This commit should fix that case, and also adds an explicit unit test for it. --- asyncssh/config.py | 8 +++++--- asyncssh/connection.py | 2 +- tests/test_config.py | 8 +++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/asyncssh/config.py b/asyncssh/config.py index f239b26..18ff3c8 100644 --- a/asyncssh/config.py +++ b/asyncssh/config.py @@ -321,11 +321,15 @@ def parse(self, path: Path) -> None: args = [] loption = '' + allow_equal = True for i, arg in enumerate(split_args, 1): if arg.startswith('='): if len(arg) > 1: args.append(arg[1:]) + elif not allow_equal: + args.extend(split_args[i-1:]) + break elif arg.endswith('='): args.append(arg[:-1]) elif '=' in arg: @@ -337,9 +341,7 @@ def parse(self, path: Path) -> None: if i == 1: loption = args.pop(0).lower() - elif i > 1 and loption not in self._conditionals: - args.extend(split_args[i:]) - break + allow_equal = loption in self._conditionals if loption in self._no_split: args = [line.lstrip()[len(loption):].strip()] diff --git a/asyncssh/connection.py b/asyncssh/connection.py index 665075f..9c05c5e 100644 --- a/asyncssh/connection.py +++ b/asyncssh/connection.py @@ -4232,7 +4232,7 @@ async def create_session(self, session_factory: SSHClientSessionFactory, if env: try: if isinstance(env, list): - new_env.update((item.split('=', 2) for item in env)) + new_env.update((item.split('=', 1) for item in env)) else: new_env.update(cast(Mapping[str, str], env)) except ValueError: diff --git a/tests/test_config.py b/tests/test_config.py index 2b67ca2..53ea7de 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -286,9 +286,15 @@ def test_set_string_list(self): def test_append_string_list(self): """Test appending multiple string config options to a list""" - config = self._parse_config('SendEnv foo\nSendEnv bar baz') + config = self._parse_config('SendEnv foo\nSendEnv bar baz') self.assertEqual(config.get('SendEnv'), ['foo', 'bar', 'baz']) + def test_set_environment(self): + """Test setting environment with equals-separated key/value pairs""" + + config = self._parse_config('SetEnv A=1 B= C=D=2\nSetEnv E=3') + self.assertEqual(config.get('SetEnv'), ['A=1', 'B=', 'C=D=2']) + def test_set_remote_command(self): """Test setting a remote command"""