|
7 | 7 | import os
|
8 | 8 | import signal
|
9 | 9 | import sys
|
| 10 | +import tempfile |
10 | 11 | from asyncio.subprocess import PIPE, Process
|
11 | 12 | from collections.abc import AsyncIterator, Iterable, Iterator
|
12 | 13 | from pathlib import Path
|
@@ -71,23 +72,26 @@ async def client_session() -> AsyncIterator[aiohttp.ClientSession]:
|
71 | 72 | # can't test long-running processes with `CliRunner` (https://github.com/pallets/click/issues/2171)
|
72 | 73 | @contextlib.asynccontextmanager
|
73 | 74 | async def long_running_cmd(args: Iterable[str], timeout: float = 5) -> AsyncIterator[Process]:
|
74 |
| - try: |
75 |
| - popen_args = [sys.executable, "-m", "questionpy_sdk", "--", *args] |
76 |
| - proc = await asyncio.create_subprocess_exec(*popen_args, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
| 75 | + with tempfile.TemporaryDirectory("qpy-state-storage") as state_dir: |
| 76 | + try: |
| 77 | + popen_args = [sys.executable, "-m", "questionpy_sdk", "--", *args] |
| 78 | + proc = await asyncio.create_subprocess_exec( |
| 79 | + *popen_args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env={"QPY_STATE_STORAGE_PATH": state_dir} |
| 80 | + ) |
77 | 81 |
|
78 |
| - # ensure tests don't hang indefinitely |
79 |
| - async def kill_after_timeout() -> None: |
80 |
| - await asyncio.sleep(timeout) |
81 |
| - proc.send_signal(signal.SIGINT) |
| 82 | + # ensure tests don't hang indefinitely |
| 83 | + async def kill_after_timeout() -> None: |
| 84 | + await asyncio.sleep(timeout) |
| 85 | + proc.send_signal(signal.SIGINT) |
82 | 86 |
|
83 |
| - kill_task = asyncio.create_task(kill_after_timeout()) |
84 |
| - yield proc |
| 87 | + kill_task = asyncio.create_task(kill_after_timeout()) |
| 88 | + yield proc |
85 | 89 |
|
86 |
| - finally: |
87 |
| - if kill_task: |
88 |
| - kill_task.cancel() |
89 |
| - proc.send_signal(signal.SIGINT) |
90 |
| - await proc.wait() |
| 90 | + finally: |
| 91 | + if kill_task: |
| 92 | + kill_task.cancel() |
| 93 | + proc.send_signal(signal.SIGINT) |
| 94 | + await proc.wait() |
91 | 95 |
|
92 | 96 |
|
93 | 97 | async def assert_webserver_is_up(session: aiohttp.ClientSession, url: str = "http://localhost:8080/") -> None:
|
|
0 commit comments