Skip to content

Commit

Permalink
Another update to config file parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ronf committed Jul 18, 2024
1 parent 013818b commit 68c2df7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 5 additions & 3 deletions asyncssh/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()]
Expand Down
2 changes: 1 addition & 1 deletion asyncssh/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down

0 comments on commit 68c2df7

Please sign in to comment.