Skip to content

Commit

Permalink
Fix python code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeszaros committed Oct 1, 2023
1 parent bf8c309 commit 9c7391c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 51 deletions.
25 changes: 19 additions & 6 deletions paramiko/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from hashlib import sha1
from io import StringIO
from functools import partial

# TODO: rewrite os.path.* calls to pathlib
from pathlib import Path
from glob import glob
Expand Down Expand Up @@ -241,7 +242,7 @@ def _calculate_include_paths(self, value):
# Use config home as a base
if not path_expr.is_absolute():
path_expr = self.config_home / path_expr
# TODO: make use of glob(root_dir=self.config_home) for python>=3.10
# TODO: use of glob(root_dir=self.config_home) for python>=3.10
for path in sorted(glob(str(path_expr))):
path = Path(path)
# Ignore invalid include paths
Expand All @@ -253,9 +254,7 @@ def _calculate_include_paths(self, value):
@staticmethod
def _new_partial(old_context):
new_context = {
k: old_context[k]
for k in old_context
if k in ("host", "matches")
k: old_context[k] for k in old_context if k in ("host", "matches")
}
new_context["config"] = {}
return new_context
Expand Down Expand Up @@ -326,7 +325,15 @@ def lookup(self, hostname):
)
return options

def _lookup(self, hostname, options=None, *, file_path=None, canonical=False, final=False):
def _lookup(
self,
hostname,
options=None,
*,
file_path=None,
canonical=False,
final=False
):
# Init
if options is None:
options = SSHConfigDict()
Expand Down Expand Up @@ -359,7 +366,13 @@ def _lookup(self, hostname, options=None, *, file_path=None, canonical=False, fi
)
if "include" in context.keys():
for include_path in context["include"]:
self._lookup(hostname, options, file_path=include_path, canonical=canonical, final=final)
self._lookup(
hostname,
options,
file_path=include_path,
canonical=canonical,
final=final,
)
if final:
# Expand variables in resulting values
# (besides 'Match exec' which was already handled above)
Expand Down
116 changes: 71 additions & 45 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

class TestStack:
def test_can_be_initialized(self):
s = stack('a')
s = stack("a")
assert len(s) == 1

def test_has_initial_top_item(self):
s = stack('a')
assert s.top == 'a'
s = stack("a")
assert s.top == "a"

def test_top_changes_with_append_and_pop(self):
s = stack(1)
Expand Down Expand Up @@ -116,7 +116,8 @@ def test_parse_config(self):
},
]
# TODO: do not peek into implementation details
assert self.config._config_by_file[self.config._config_root] == expected
_config = self.config._config_by_file[self.config._config_root]
assert _config == expected

@mark.parametrize(
"host,values",
Expand Down Expand Up @@ -730,7 +731,7 @@ def test_has_default_config_home(self):

def test_can_include_relative_paths(self):
# Set the config home dir
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
# Try include a test config
config = SSHConfig.from_text("Include basic")
Expand All @@ -739,35 +740,43 @@ def test_can_include_relative_paths(self):

def test_include_ignores_invalid_paths(self):
missing_file = _config("missing-include-file")
config = SSHConfig.from_text(f"""
config = SSHConfig.from_text(
f"""
Include {missing_file}
Include {tests_dir}
User waldo
""")
"""
)
result = config.lookup("anything.com")
assert result["user"] == "waldo"

@mark.parametrize("include_str", [
"include-part-b include-part-a",
"include-part-b include-part-a",
"include-part-b\t \tinclude-part-a",
"include-part-b missing-file include-part-a"
])
@mark.parametrize(
"include_str",
[
"include-part-b include-part-a",
"include-part-b include-part-a",
"include-part-b\t \tinclude-part-a",
"include-part-b missing-file include-part-a",
],
)
def test_can_include_many_files(self, include_str):
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
config = SSHConfig.from_text(f"Include {include_str}")
result = config.lookup("test.org")
assert result["user"] == "banana"
assert result["identityfile"] == ["banana-tree", "apple-tree"]

@mark.parametrize("include_str", [
"include-part\\ space",
"'include-part space'",
'"include-part space"'
])
@mark.parametrize(
"include_str",
[
"include-part\\ space",
"'include-part space'",
'"include-part space"',
],
)
def test_can_include_filenames_with_spaces(self, include_str):
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
config = SSHConfig.from_text(f"Include {include_str}")
result = config.lookup("test.org")
Expand All @@ -776,46 +785,63 @@ def test_can_include_filenames_with_spaces(self, include_str):
assert result["identityfile"] == ["dont-panic"]

def test_can_include_globs(self):
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
config = SSHConfig.from_text("Include include-part*")
print(config._config_by_file)
result = config.lookup("test.org")
assert result["user"] == "ford-prefect"
assert result["port"] == "42"
assert result["identityfile"] == ["dont-panic", "apple-tree", "banana-tree"]
assert result["identityfile"] == [
"dont-panic",
"apple-tree",
"banana-tree",
]

@mark.parametrize("config_name, expected_loop", [
("include-loop-?", ("include-loop-a", "include-loop-a")),
("include-loop-a", ("include-loop-a", "include-loop-a")),
("include-loop-b", ("include-loop-b1", "include-loop-b2", "include-loop-b3", "include-loop-b1"))
])
@mark.parametrize(
"config_name, expected_loop",
[
("include-loop-?", ("include-loop-a", "include-loop-a")),
("include-loop-a", ("include-loop-a", "include-loop-a")),
(
"include-loop-b",
(
"include-loop-b1",
"include-loop-b2",
"include-loop-b3",
"include-loop-b1",
),
),
],
)
def test_include_fails_on_include_loop(self, config_name, expected_loop):
# Set the config home dir
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
with raises(ConfigIncludeLoopError) as e:
config = SSHConfig.from_text(f"Include {config_name}")
result = config.lookup("test")
_ = config.lookup("test")
assert e.value.loop == tuple(map(_config, expected_loop))

@mark.parametrize("config_name, expected_order", [
("include-order-1", (
"first",
"apple-tree",
"last"
)),
("include-order-2", (
"first",
"second",
"apple-tree",
"middle",
"banana-tree",
"last"
))
])
@mark.parametrize(
"config_name, expected_order",
[
("include-order-1", ("first", "apple-tree", "last")),
(
"include-order-2",
(
"first",
"second",
"apple-tree",
"middle",
"banana-tree",
"last",
),
),
],
)
def test_includes_are_loaded_in_order(self, config_name, expected_order):
test_configs_dir = Path(tests_dir)/"configs"
test_configs_dir = Path(tests_dir) / "configs"
SSHConfig.config_home = str(test_configs_dir)
config = SSHConfig.from_text(f"Include {config_name}")
result = config.lookup("www.paramiko.org")
Expand Down

0 comments on commit 9c7391c

Please sign in to comment.