Skip to content

Commit 8d9f70f

Browse files
committed
test(cli-context): force CLI context off in no_cli_context fixture
The no_cli_context conftest branch previously only skipped enabling the CLI context; it now actively sets the ContextVar to False and restores it on teardown. mark_cli_invocation() is set-and-leave, so the opt-out now guarantees a disabled context independent of test order rather than relying on the default plus async-task context isolation. Removes the now-redundant local _reset_cli_context fixture in test_cli_context.py; the guarantee lives once, in conftest.
1 parent 25e5ed9 commit 8d9f70f

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

tests/conftest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import pytest
1919

20-
from runpod_flash.core.cli_context import allow_lifecycle_operations
20+
from runpod_flash.core.cli_context import _invoked_by_cli, allow_lifecycle_operations
2121
from runpod_flash.core.resources.resource_manager import ResourceManager
2222
from runpod_flash.core.utils.singleton import SingletonMixin
2323

@@ -44,9 +44,19 @@ def _flash_cli_context(request):
4444
Lifecycle methods (deploy/undeploy/app/env management) are restricted to
4545
flash CLI invocation. Most tests drive them directly, so enable the context
4646
by default. Guard tests opt out with @pytest.mark.no_cli_context.
47+
48+
The opt-out branch actively forces the context to False (rather than merely
49+
skipping the enable) and restores it afterwards. ``mark_cli_invocation()`` is
50+
set-and-leave, so a CLI test that ran earlier in the same worker could leave
51+
the ContextVar True; forcing False here makes guard tests independent of test
52+
order instead of relying on each guard module to reset the var itself.
4753
"""
4854
if request.node.get_closest_marker("no_cli_context"):
49-
yield
55+
token = _invoked_by_cli.set(False)
56+
try:
57+
yield
58+
finally:
59+
_invoked_by_cli.reset(token)
5060
else:
5161
with allow_lifecycle_operations():
5262
yield

tests/unit/core/test_cli_context.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,15 @@
1212
cli_only,
1313
is_cli_invocation,
1414
mark_cli_invocation,
15-
_invoked_by_cli,
1615
)
1716
from runpod_flash.core.exceptions import FlashError, FlashUsageError
1817

18+
# The conftest ``_flash_cli_context`` fixture forces the CLI context to False for
19+
# every no_cli_context test and restores it afterwards, so these tests start from
20+
# a clean default regardless of order.
1921
pytestmark = pytest.mark.no_cli_context
2022

2123

22-
@pytest.fixture(autouse=True)
23-
def _reset_cli_context():
24-
"""Guarantee a clean default (False) context around each test."""
25-
token = _invoked_by_cli.set(False)
26-
try:
27-
yield
28-
finally:
29-
_invoked_by_cli.reset(token)
30-
31-
3224
@cli_only("flash deploy")
3325
async def _guarded(value: int) -> int:
3426
return value * 2

0 commit comments

Comments
 (0)