Skip to content

Commit

Permalink
fix command split on Windows (samuelcolvin#199)
Browse files Browse the repository at this point in the history
* Fixes samuelcolvin#198

* Update tests/test_run_process.py

Co-authored-by: Samuel Colvin <[email protected]>
  • Loading branch information
kulothunganug and samuelcolvin authored Oct 28, 2022
1 parent 0fab942 commit dc535f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 14 additions & 1 deletion tests/test_run_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from watchfiles import arun_process, run_process
from watchfiles.main import Change
from watchfiles.run import detect_target_type, import_string, run_function, set_tty, start_process
from watchfiles.run import detect_target_type, import_string, run_function, set_tty, split_cmd, start_process

if TYPE_CHECKING:
from conftest import MockRustType
Expand Down Expand Up @@ -68,6 +68,19 @@ def test_dead_callback(mocker, mock_rust_notify: 'MockRustType'):
c.assert_called_with({(Change.added, '/path/to/foobar.py')})


@pytest.mark.skipif(sys.platform != 'win32', reason='no need to test this except on windows')
def test_split_cmd_non_posix():
assert split_cmd('C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python311\\python.exe -V') == [
'C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python311\\python.exe',
'-V',
]


@pytest.mark.skipif(sys.platform == 'win32', reason='no need to test this on windows')
def test_split_cmd_posix():
assert split_cmd('/usr/bin/python3 -v') == ['/usr/bin/python3', '-v']


@pytest.mark.skipif(sys.platform == 'win32', reason='fails on windows')
def test_alive_doesnt_terminate(mocker, mock_rust_notify: 'MockRustType'):
mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess(exitcode=None))
Expand Down
11 changes: 9 additions & 2 deletions watchfiles/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from multiprocessing import get_context
from multiprocessing.context import SpawnProcess
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, Optional, Set, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, List, Optional, Set, Tuple, Union

import anyio

Expand Down Expand Up @@ -219,6 +219,13 @@ async def main():
spawn_context = get_context('spawn')


def split_cmd(cmd: str) -> List[str]:
import platform

posix = platform.uname().system.lower() != 'windows'
return shlex.split(cmd, posix=posix)


def start_process(
target: Union[str, Callable[..., Any]],
target_type: "Literal['function', 'command']",
Expand Down Expand Up @@ -250,7 +257,7 @@ def start_process(
logger.warning('ignoring args and kwargs for "command" target')

assert isinstance(target, str), 'target must be a string to run as a command'
popen_args = shlex.split(target)
popen_args = split_cmd(target)
process = subprocess.Popen(popen_args)
return CombinedProcess(process)

Expand Down

0 comments on commit dc535f9

Please sign in to comment.