Skip to content

Commit 6e8a585

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Windows fix
1 parent f819522 commit 6e8a585

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

suby/run.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from suby.process_waiting import has_event_driven_wait, wait_for_process_exit
4949
from suby.subprocess_result import SubprocessResult
5050

51-
StreamCallback = Callable[[str], Any] # type: ignore[misc]
51+
StreamCallback = Callable[[str], Any] # type: ignore[misc, unused-ignore]
5252
_CUSTOM_TOKEN_POLL_TIMEOUT_SECONDS = 0.0001
5353
_CANCELLATION_ERROR_TYPES: Mapping[Type[CancellationError], Type[CancellationError]] = {
5454
CantokConditionCancellationError: ConditionCancellationError,
@@ -287,6 +287,8 @@ def prepare_directory(directory: Optional[Union[str, Path]]) -> Optional[str]:
287287
try:
288288
directory_stat = cwd_path.stat()
289289
except FileNotFoundError as error:
290+
if has_file_parent(cwd_path):
291+
raise WrongDirectoryError(f'The directory {raw_text!r} cannot be resolved because an intermediate component is not a directory.') from error
290292
raise WrongDirectoryError(f'The directory {raw_text!r} does not exist.') from error
291293
except NotADirectoryError as error:
292294
raise WrongDirectoryError(f'The directory {raw_text!r} cannot be resolved because an intermediate component is not a directory.') from error
@@ -303,6 +305,16 @@ def prepare_directory(directory: Optional[Union[str, Path]]) -> Optional[str]:
303305
return str(cwd_path)
304306

305307

308+
def has_file_parent(path: Path) -> bool:
309+
for parent in path.parents:
310+
try:
311+
parent_stat = parent.stat()
312+
except OSError:
313+
continue
314+
return not stat.S_ISDIR(parent_stat.st_mode)
315+
return False
316+
317+
306318
def build_subprocess_env(
307319
env: Optional[Mapping[str, str]],
308320
add_env: Optional[Mapping[str, str]],

tests/units/test_run_directory.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def _child_cwd(**run_kwargs) -> Path:
2424
'import os; print(os.getcwd())',
2525
split=False,
2626
catch_output=True,
27+
add_env={'PYTHONIOENCODING': 'utf-8'},
2728
**run_kwargs,
2829
)
2930

@@ -119,7 +120,15 @@ def test_run_executes_in_relative_directory_values(tmp_path, monkeypatch, direct
119120
assert child_cwd.resolve() == (call_site / expected_relative).resolve()
120121

121122

122-
@pytest.mark.parametrize('name', ['dir with spaces', ' ', ' ', 'директория'])
123+
@pytest.mark.parametrize(
124+
'name',
125+
[
126+
'dir with spaces',
127+
pytest.param(' ', marks=pytest.mark.skipif(os.name == 'nt', reason='Windows path APIs do not preserve space-only directory names')),
128+
pytest.param(' ', marks=pytest.mark.skipif(os.name == 'nt', reason='Windows path APIs do not preserve space-only directory names')),
129+
'директория',
130+
],
131+
)
123132
def test_directory_string_is_not_split_or_normalized(tmp_path, name):
124133
"""Spaces and unicode in directory names are passed as one cwd value."""
125134
directory = tmp_path / name

0 commit comments

Comments
 (0)