File: test_run_process.py

package info (click to toggle)
python-watchfiles 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 592 kB
  • sloc: python: 1,997; makefile: 86
file content (279 lines) | stat: -rw-r--r-- 10,963 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import subprocess
import sys
from multiprocessing.context import SpawnProcess
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from dirty_equals import IsStr

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, split_cmd, start_process

if TYPE_CHECKING:
    from conftest import MockRustType


class FakeProcess(SpawnProcess):
    def __init__(self, is_alive=True, exitcode=1, pid=123):
        self._is_alive = is_alive
        self._exitcode = exitcode
        self._pid = pid

    @property
    def exitcode(self):
        return self._exitcode

    @property
    def pid(self):
        return self._pid

    def start(self):
        pass

    def is_alive(self):
        return self._is_alive

    def join(self, wait):
        pass


def test_alive_terminates(mocker, mock_rust_notify: 'MockRustType', caplog):
    caplog.set_level('DEBUG', 'watchfiles')
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_popen = mocker.patch('watchfiles.run.subprocess.Popen', return_value=FakePopen())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target=os.getcwd, debounce=5, grace_period=0.01, step=1) == 1
    assert mock_spawn_process.call_count == 2
    assert mock_popen.call_count == 0
    assert mock_kill.call_count == 2  # kill in loop + final kill
    assert 'watchfiles.main DEBUG: running "<built-in function getcwd>" as function\n' in caplog.text
    assert 'sleeping for 0.01 seconds before watching for changes' in caplog.text


def test_dead_callback(mocker, mock_rust_notify: 'MockRustType'):
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess(is_alive=False))
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}, {(1, '/path/to/foobar.py')}])

    c = mocker.MagicMock()

    assert run_process('/x/y/z', target=object(), callback=c, debounce=5, step=1) == 2
    assert mock_spawn_process.call_count == 3
    assert mock_kill.call_count == 0
    assert c.call_count == 2
    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))
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target=object(), debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 2
    assert mock_kill.call_count == 4  # 2 kills in loop (graceful and termination) + 2 final kills


class FakeProcessTimeout(FakeProcess):
    def join(self, wait):
        if wait == 'sigint_timeout':
            raise subprocess.TimeoutExpired('/x/y/z', wait)


@pytest.mark.skipif(sys.platform == 'win32', reason='fails on windows')
def test_sigint_timeout(mocker, mock_rust_notify: 'MockRustType', caplog):
    caplog.set_level('WARNING', 'watchfiles')
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcessTimeout())

    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target=object(), debounce=5, step=1, sigint_timeout='sigint_timeout') == 1
    assert mock_spawn_process.call_count == 2
    assert mock_kill.call_count == 2
    assert "SIGINT timed out after 'sigint_timeout' seconds" in caplog.text


def test_start_process(mocker):
    mock_process = mocker.patch('watchfiles.run.spawn_context.Process')
    v = object()
    start_process(v, 'function', (1, 2, 3), {})
    assert mock_process.call_count == 1
    mock_process.assert_called_with(target=v, args=(1, 2, 3), kwargs={})
    assert os.getenv('WATCHFILES_CHANGES') == '[]'


def test_start_process_env(mocker):
    mock_process = mocker.patch('watchfiles.run.spawn_context.Process')
    v = object()
    changes = [(Change.added, 'a.py'), (Change.modified, 'b.py'), (Change.deleted, 'c.py')]  # use a list to keep order
    start_process(v, 'function', (1, 2, 3), {}, changes)
    assert mock_process.call_count == 1
    mock_process.assert_called_with(target=v, args=(1, 2, 3), kwargs={})
    assert os.getenv('WATCHFILES_CHANGES') == '[["added", "a.py"], ["modified", "b.py"], ["deleted", "c.py"]]'


def test_function_string_not_win(mocker, mock_rust_notify: 'MockRustType', caplog):
    caplog.set_level('DEBUG', 'watchfiles')
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target='os.getcwd', debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 2

    # get_tty_path returns None on windows
    tty_path = None if sys.platform == 'win32' else IsStr(regex='/dev/.+')
    mock_spawn_process.assert_called_with(target=run_function, args=('os.getcwd', tty_path, (), {}), kwargs={})

    assert 'watchfiles.main DEBUG: running "os.getcwd" as function\n' in caplog.text


def test_function_list(mocker, mock_rust_notify: 'MockRustType'):
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target=['os.getcwd'], debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 2
    assert mock_kill.call_count == 2  # kill in loop + final kill


async def test_async_alive_terminates(mocker, mock_rust_notify: 'MockRustType'):
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    callback_calls = []

    async def c(changes):
        callback_calls.append(changes)

    assert await arun_process('/x/y/async', target=object(), callback=c, debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 2
    assert mock_kill.call_count == 2  # kill in loop + final kill
    assert callback_calls == [{(Change.added, '/path/to/foobar.py')}]


async def test_async_sync_callback(mocker, mock_rust_notify: 'MockRustType'):
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foo.py')}, {(2, '/path/to/bar.py')}])

    callback_calls = []

    v = await arun_process(
        '/x/y/async',
        target='os.getcwd',
        target_type='function',
        callback=callback_calls.append,
        grace_period=0.01,
        debounce=5,
        step=1,
    )
    assert v == 2
    assert mock_spawn_process.call_count == 3
    assert mock_kill.call_count == 3
    assert callback_calls == [{(Change.added, '/path/to/foo.py')}, {(Change.modified, '/path/to/bar.py')}]


def test_run_function(tmp_work_path: Path, create_test_function):
    assert not (tmp_work_path / 'sentinel').exists()
    run_function(create_test_function, None, (), {})
    assert (tmp_work_path / 'sentinel').exists()


def test_run_function_tty(tmp_work_path: Path, create_test_function):
    # could this cause problems by changing sys.stdin?
    assert not (tmp_work_path / 'sentinel').exists()
    run_function(create_test_function, '/dev/tty', (), {})
    assert (tmp_work_path / 'sentinel').exists()


def test_set_tty_error():
    with set_tty('/foo/bar'):
        pass


class FakePopen:
    def __init__(self, is_alive=True, returncode=1, pid=123):
        self._is_alive = is_alive
        self.returncode = returncode
        self.pid = pid

    def poll(self):
        return None if self._is_alive else self.returncode

    def wait(self, wait):
        pass


def test_command(mocker, mock_rust_notify: 'MockRustType', caplog):
    caplog.set_level('DEBUG', 'watchfiles')
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_popen = mocker.patch('watchfiles.run.subprocess.Popen', return_value=FakePopen())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target='echo foobar', debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 0
    assert mock_popen.call_count == 2
    mock_popen.assert_called_with(['echo', 'foobar'])
    assert mock_kill.call_count == 2  # kill in loop + final kill
    assert 'watchfiles.main DEBUG: running "echo foobar" as command\n' in caplog.text


def test_command_with_args(mocker, mock_rust_notify: 'MockRustType', caplog):
    caplog.set_level('INFO', 'watchfiles')
    mock_spawn_process = mocker.patch('watchfiles.run.spawn_context.Process', return_value=FakeProcess())
    mock_popen = mocker.patch('watchfiles.run.subprocess.Popen', return_value=FakePopen())
    mock_kill = mocker.patch('watchfiles.run.os.kill')
    mock_rust_notify([{(1, '/path/to/foobar.py')}])

    assert run_process('/x/y/z', target='echo foobar', args=(1, 2), target_type='command', debounce=5, step=1) == 1
    assert mock_spawn_process.call_count == 0
    assert mock_popen.call_count == 2
    mock_popen.assert_called_with(['echo', 'foobar'])
    assert mock_kill.call_count == 2  # kill in loop + final kill
    assert 'watchfiles.main WARNING: ignoring args and kwargs for "command" target\n' in caplog.text


def test_import_string():
    assert import_string('os.getcwd') == os.getcwd

    with pytest.raises(ImportError, match='"os" doesn\'t look like a module path'):
        import_string('os')


@pytest.mark.parametrize(
    'target, expected',
    [
        ('os.getcwd', 'function'),
        (os.getcwd, 'function'),
        ('foobar.py', 'command'),
        ('foobar.sh', 'command'),
        ('foobar.pyt', 'function'),
        ('foo bar', 'command'),
    ],
)
def test_detect_target_type(target, expected):
    assert detect_target_type(target) == expected