Skip to content

Commit b925f78

Browse files
committed
Merge branch 'pathlib-loop-over-files' of https://github.com/CendioZeijlon/websockify
2 parents b794a2b + 22eaccd commit b925f78

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

tests/test_token_plugins.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ def test_simple(self):
6060
self.assertEqual(result[0], "remote_host")
6161
self.assertEqual(result[1], "remote_port")
6262

63+
def test_dir_with_files(self):
64+
mock_file1 = MagicMock()
65+
mock_file1.is_file.return_value = True
66+
mock_file1.is_dir.return_value = False
67+
mock_file1.open.return_value.__enter__.return_value.readlines.return_value = ["testhost1: remote_host1:remote_port1"]
68+
69+
mock_file2 = MagicMock()
70+
mock_file2.is_file.return_value = True
71+
mock_file2.is_dir.return_value = False
72+
mock_file2.open.return_value.__enter__.return_value.readlines.return_value = ["testhost2: remote_host2:remote_port2"]
73+
74+
mock_dir = MagicMock()
75+
mock_dir.is_dir.return_value = True
76+
mock_dir.is_file.return_value = False
77+
78+
mock_source_dir = MagicMock()
79+
mock_source_dir.is_dir.return_value = True
80+
mock_source_dir.iterdir.return_value = [mock_file1, mock_file2, mock_dir]
81+
82+
with patch("websockify.token_plugins.Path") as mock_path:
83+
mock_path.return_value = mock_source_dir
84+
plugin = ReadOnlyTokenFile('configdir')
85+
result1 = plugin.lookup('testhost1')
86+
result2 = plugin.lookup('testhost2')
87+
88+
mock_path.assert_called_once_with('configdir')
89+
self.assertIsNotNone(result1)
90+
self.assertIsNotNone(result2)
91+
self.assertEqual(result1, ["remote_host1", "remote_port1"])
92+
self.assertEqual(result2, ["remote_host2", "remote_port2"])
93+
6394
def test_tabs(self):
6495
mock_source_file = MagicMock()
6596
mock_source_file.is_dir.return_value = False

websockify/token_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs):
4545
def _load_targets(self):
4646
source = Path(self.source)
4747
if source.is_dir():
48-
cfg_files = [file for file in source if file.is_file()]
48+
cfg_files = [file for file in source.iterdir() if file.is_file()]
4949
else:
5050
cfg_files = [source]
5151

0 commit comments

Comments
 (0)