Skip to content

Commit 708ba98

Browse files
committed
test: isolate command tests from default state dir
An invalid question state for the minimal example would cause test_run.py to fail since it used the default state storage dir. This commit builds upon b4bc317 to use an isolated tempdir for each long_running_cmd.
1 parent bbc83fb commit 708ba98

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

tests/questionpy_sdk/commands/conftest.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import signal
99
import sys
10+
import tempfile
1011
from asyncio.subprocess import PIPE, Process
1112
from collections.abc import AsyncIterator, Iterable, Iterator
1213
from pathlib import Path
@@ -71,23 +72,26 @@ async def client_session() -> AsyncIterator[aiohttp.ClientSession]:
7172
# can't test long-running processes with `CliRunner` (https://github.com/pallets/click/issues/2171)
7273
@contextlib.asynccontextmanager
7374
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+
)
7781

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)
8286

83-
kill_task = asyncio.create_task(kill_after_timeout())
84-
yield proc
87+
kill_task = asyncio.create_task(kill_after_timeout())
88+
yield proc
8589

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()
9195

9296

9397
async def assert_webserver_is_up(session: aiohttp.ClientSession, url: str = "http://localhost:8080/") -> None:

0 commit comments

Comments
 (0)