File: test_asyncio.py

package info (click to toggle)
pytest-subprocess 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 444 kB
  • sloc: python: 2,319; makefile: 17
file content (410 lines) | stat: -rw-r--r-- 11,668 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import asyncio
import os
import sys
import time

import anyio
import pytest

from pytest_subprocess.fake_popen import AsyncFakePopen

PYTHON = sys.executable


@pytest.fixture()
def event_loop_policy(request):
    if sys.platform.startswith("win"):
        if request.node.name.startswith("test_invalid_event_loop"):
            return asyncio.WindowsSelectorEventLoopPolicy()
        else:
            return asyncio.WindowsProactorEventLoopPolicy()
    return asyncio.DefaultEventLoopPolicy()


if sys.platform.startswith("win") and sys.version_info < (3, 8):

    @pytest.fixture(autouse=True)
    def event_loop(request, event_loop_policy):
        loop = event_loop_policy.new_event_loop()
        yield loop
        loop.close()


@pytest.mark.asyncio
@pytest.mark.parametrize("mode", ["shell", "exec"])
async def test_basic_usage(fp, mode):
    shell = mode == "shell"
    fp.register(["some-command-that-is-definitely-unavailable"], returncode=500)
    method = (
        asyncio.create_subprocess_shell if shell else asyncio.create_subprocess_exec
    )
    process = await method("some-command-that-is-definitely-unavailable")
    returncode = await process.wait()

    assert process.returncode == returncode
    assert process.returncode == 500


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [True, False])
async def test_with_arguments_shell(fp, fake):
    fp.allow_unregistered(not fake)
    if fake:
        fp.register(
            [PYTHON, "example_script.py", "stderr"],
            stdout=["Stdout line 1", "Stdout line 2"],
            stderr=["Stderr line 1"],
        )

    process = await asyncio.create_subprocess_shell(
        f"{PYTHON} example_script.py stderr",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    out, err = await process.communicate()

    assert err == os.linesep.encode().join([b"Stderr line 1", b""])
    assert out == os.linesep.encode().join([b"Stdout line 1", b"Stdout line 2", b""])
    assert process.returncode == 0


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [True, False])
async def test_with_arguments_exec(fp, fake):
    fp.allow_unregistered(not fake)
    if fake:
        fp.register(
            [PYTHON, "example_script.py", "stderr"],
            stdout=["Stdout line 1", "Stdout line 2"],
            stderr=["Stderr line 1"],
        )

    process = await asyncio.create_subprocess_exec(
        PYTHON,
        "example_script.py",
        "stderr",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    out, err = await process.communicate()

    assert err == os.linesep.encode().join([b"Stderr line 1", b""])
    assert out == os.linesep.encode().join([b"Stdout line 1", b"Stdout line 2", b""])
    assert process.returncode == 0


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [True, False])
@pytest.mark.parametrize("mode", ["shell", "exec"])
async def test_incorrect_call(fp, fake, mode):
    """Asyncio doesn't support command as a list"""
    shell = mode == "shell"
    fp.allow_unregistered(not fake)
    if fake:
        fp.register(["test"])

    method = (
        asyncio.create_subprocess_shell if shell else asyncio.create_subprocess_exec
    )

    name = "cmd" if shell else "program"
    with pytest.raises(ValueError, match=f"{name} must be a string"):
        await method(["test"])


@pytest.mark.asyncio
@pytest.mark.skipif('sys.platform!="win32"')
@pytest.mark.parametrize("fake", [True, False])
@pytest.mark.parametrize("mode", ["shell", "exec"])
async def test_invalid_event_loop(fp, fake, mode):
    """
    The event_loop is changed by the `event_loop` fixture based on
    the test name (hack).
    """
    shell = mode == "shell"
    fp.allow_unregistered(not fake)
    if fake:
        fp.register([PYTHON, "example_script.py"])

    with pytest.raises(NotImplementedError):
        if shell:
            await asyncio.create_subprocess_shell(f"{PYTHON} example_script.py")
        else:
            await asyncio.create_subprocess_exec(PYTHON, "example_script.py")


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [False, True])
@pytest.mark.parametrize("mode", ["shell", "exec"])
async def test_wait(fp, fake, mode):
    """
    Check that wait argument still works. Unfortunately asyncio doesn't have
    the timeout functionality.
    """
    shell = mode == "shell"

    fp.allow_unregistered(not fake)
    if fake:
        fp.register(
            [PYTHON, "example_script.py", "wait", "stderr"],
            stdout="Stdout line 1\nStdout line 2",
            stderr="Stderr line 1",
            wait=0.5,
        )
    method = (
        asyncio.create_subprocess_shell if shell else asyncio.create_subprocess_exec
    )

    command = f"{PYTHON} example_script.py wait stderr"
    if not shell:
        command = command.split()
    else:
        command = [command]

    process = await method(
        *command,
        cwd=os.path.dirname(__file__),
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )

    assert process.returncode is None

    start_time = time.time()
    returncode = await process.wait()

    assert time.time() - start_time >= 0.45
    assert returncode == 0


@pytest.mark.asyncio
async def test_devnull_stdout(fp):
    """From GitHub #63 - make sure all the `asyncio.subprocess` consts are available."""
    fp.register("cat")

    await asyncio.create_subprocess_exec(
        "cat",
        stdin=asyncio.subprocess.DEVNULL,
        stdout=asyncio.subprocess.STDOUT,
        stderr=asyncio.subprocess.PIPE,
    )


@pytest.mark.asyncio
async def test_anyio(fp):
    await anyio.sleep(0.01)


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [False, True])
async def test_stdout_and_stderr(fp, fake):
    if fake:
        fp.register(
            [PYTHON, "example_script.py", "stderr"],
            stdout=["Stdout line 1", "Stdout line 2"],
            stderr=["Stderr line 1"],
        )
    else:
        fp.allow_unregistered(True)

    process = await asyncio.create_subprocess_exec(
        PYTHON,
        "example_script.py",
        "stderr",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )

    stdout_list = []
    stderr_list = []

    loop = asyncio.get_event_loop()
    await asyncio.gather(
        loop.create_task(_read_stream(process.stdout, stdout_list)),
        loop.create_task(_read_stream(process.stderr, stderr_list)),
        loop.create_task(process.wait()),
    )

    assert stdout_list == [f"Stdout line 1{os.linesep}", f"Stdout line 2{os.linesep}"]
    assert stderr_list == [f"Stderr line 1{os.linesep}"]


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [False, True])
async def test_combined_stdout_and_stderr(fp, fake):
    if fake:
        fp.register(
            [PYTHON, "example_script.py", "stderr"],
            stdout=["Stdout line 1", "Stdout line 2"],
            stderr=["Stderr line 1"],
        )
    else:
        fp.allow_unregistered(True)

    process = await asyncio.create_subprocess_exec(
        PYTHON,
        "example_script.py",
        "stderr",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.STDOUT,
    )

    stdout_list = []
    stderr_list = []

    loop = asyncio.get_event_loop()
    await asyncio.gather(
        loop.create_task(_read_stream(process.stdout, stdout_list)),
        loop.create_task(_read_stream(process.stderr, stderr_list)),
        loop.create_task(process.wait()),
    )

    # sorted() is necessary here, as the order here may be not deterministic,
    # and sometimes stderr comes before stdout or the opposite
    assert sorted(stdout_list) == [
        f"Stderr line 1{os.linesep}",
        f"Stdout line 1{os.linesep}",
        f"Stdout line 2{os.linesep}",
    ]
    assert stderr_list == ["empty"]


async def _read_stream(stream: asyncio.StreamReader, output_list):
    if stream is None:
        output_list.append("empty")
        return None

    while True:
        line = await stream.readline()
        if not line:
            break
        else:
            output_list.append(line.decode())


@pytest.mark.asyncio
@pytest.mark.parametrize("fake", [False, True])
async def test_input(fp, fake):
    fp.allow_unregistered(not fake)
    if fake:

        def stdin_callable(input):
            return {
                "stdout": "Provide an input: Provided: {data}".format(
                    data=input.decode()
                )
            }

        fp.register(
            [PYTHON, "example_script.py", "input"],
            stdout=[b"Stdout line 1", b"Stdout line 2"],
            stdin_callable=stdin_callable,
        )

    process = await asyncio.create_subprocess_exec(
        PYTHON,
        "example_script.py",
        "input",
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
    )
    out, err = await process.communicate(input=b"test")

    assert out.splitlines() == [
        b"Stdout line 1",
        b"Stdout line 2",
        b"Provide an input: Provided: test",
    ]
    assert err is None


@pytest.mark.asyncio
async def test_popen_recorder(fp):
    recorder = fp.register(["test_script"], occurrences=2)
    assert recorder.call_count() == 0

    await asyncio.create_subprocess_exec("test_script")
    assert recorder.call_count() == 1
    await asyncio.create_subprocess_shell("test_script")
    assert recorder.call_count() == 2

    assert all(isinstance(instance, AsyncFakePopen) for instance in recorder.calls)


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "callback",
    [
        pytest.param(None, id="no-callback"),
        pytest.param(
            lambda process: process,
            id="with-callback",
        ),
    ],
)
async def test_asyncio_subprocess_using_callback(callback, fp):
    async def my_async_func():
        process = await asyncio.create_subprocess_exec(
            "test",
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        await process.wait()
        return await process.stdout.read()

    fp.register(["test"], stdout=b"fizz", callback=callback)
    assert await my_async_func() == b"fizz"


@pytest.mark.asyncio
async def test_asyncio_subprocess_using_communicate_with_callback_kwargs(fp):
    expected_some_value = 2

    def cbk(fake_obj, some_value=None):
        assert expected_some_value == some_value
        return fake_obj

    async def my_async_func():
        process = await asyncio.create_subprocess_exec(
            "test",
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
        out, _ = await process.communicate()
        return out

    fp.register(
        ["test"],
        stdout=b"fizz",
        callback=cbk,
        callback_kwargs={"some_value": expected_some_value},
    )
    assert await my_async_func() == b"fizz"


@pytest.mark.asyncio
async def test_process_recorder_args(fp):
    fp.keep_last_process(True)
    recorder = fp.register(["test_script", fp.any()])
    await asyncio.create_subprocess_exec(
        "test_script",
        "arg1",
        env={"foo": "bar"},
    )

    assert recorder.call_count() == 1
    assert recorder.calls[0].args == ["test_script", "arg1"]
    assert recorder.calls[0].kwargs == {"env": {"foo": "bar"}}


@pytest.fixture(autouse=True)
def skip_on_pypy():
    """Async test for some reason crash on pypy 3.6 on Windows"""
    if sys.platform == "win32" and sys.version.startswith("3.6"):
        try:
            import __pypy__

            _ = __pypy__
            pytest.skip()
        except ImportError:
            pass