Skip to content

Commit ba55dfb

Browse files
committed
fix coverage please
1 parent dc2d520 commit ba55dfb

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

tests/acpremote/test_helpers.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,80 @@ async def test_command_process_terminate_timeout_kills() -> None:
625625
assert process.returncode == -9
626626

627627

628+
@pytest.mark.asyncio
629+
async def test_command_process_terminate_returns_when_already_exited() -> None:
630+
process = _FakeCommandProcess(returncode=0)
631+
632+
await command_module._terminate_process(cast(Any, process), timeout=0.001)
633+
634+
assert process.terminate_calls == 0
635+
assert process.kill_calls == 0
636+
637+
638+
@pytest.mark.asyncio
639+
async def test_command_process_terminate_returns_when_timeout_sets_returncode() -> None:
640+
@dataclass(slots=True)
641+
class _ExitingOnCancelledProcess(_FakeCommandProcess):
642+
async def wait(self) -> int:
643+
try:
644+
await asyncio.sleep(1)
645+
except asyncio.CancelledError:
646+
self.returncode = 143
647+
raise
648+
return self.returncode or 0 # pragma: no cover
649+
650+
process = _ExitingOnCancelledProcess()
651+
652+
await command_module._terminate_process(cast(Any, process), timeout=0.001)
653+
654+
assert process.terminate_calls == 1
655+
assert process.kill_calls == 0
656+
assert process.returncode == 143
657+
658+
659+
@pytest.mark.asyncio
660+
async def test_command_process_terminate_kills_when_cancelled() -> None:
661+
process = _FakeCommandProcess(wait_delay=0.01)
662+
task = asyncio.create_task(
663+
command_module._terminate_process(cast(Any, process), timeout=1),
664+
)
665+
666+
await asyncio.sleep(0)
667+
task.cancel()
668+
with pytest.raises(asyncio.CancelledError):
669+
await task
670+
671+
assert process.terminate_calls == 1
672+
assert process.kill_calls == 1
673+
674+
675+
@pytest.mark.asyncio
676+
async def test_command_process_terminate_cancelled_after_process_exit() -> None:
677+
@dataclass(slots=True)
678+
class _ExitedOnCancelledProcess(_FakeCommandProcess):
679+
async def wait(self) -> int:
680+
try:
681+
await asyncio.sleep(1)
682+
except asyncio.CancelledError:
683+
self.returncode = 0
684+
raise
685+
return self.returncode or 0 # pragma: no cover
686+
687+
process = _ExitedOnCancelledProcess()
688+
task = asyncio.create_task(
689+
command_module._terminate_process(cast(Any, process), timeout=1),
690+
)
691+
692+
await asyncio.sleep(0)
693+
task.cancel()
694+
with pytest.raises(asyncio.CancelledError):
695+
await task
696+
697+
assert process.terminate_calls == 1
698+
assert process.kill_calls == 0
699+
assert process.returncode == 0
700+
701+
628702
@pytest.mark.asyncio
629703
async def test_command_connection_covers_all_done_path(
630704
monkeypatch: pytest.MonkeyPatch,

tests/codex_auth_helper/test_auth.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import asyncio
44
import json
5-
import os
65
import stat
76
from datetime import UTC, datetime, timedelta
87
from pathlib import Path
@@ -24,7 +23,7 @@
2423
_parse_timestamp,
2524
_require_str,
2625
)
27-
from codex_auth_helper.auth.store import CodexAuthStore
26+
from codex_auth_helper.auth.store import CodexAuthStore, _fsync_directory
2827

2928
from .support import write_auth_file
3029

@@ -179,8 +178,7 @@ def test_auth_store_covers_invalid_json_non_object_and_write_round_trip(
179178
store.write_state(updated_state)
180179
persisted = json.loads(auth_path.read_text(encoding="utf-8"))
181180
assert persisted["tokens"]["account_id"] == "acct_written"
182-
if os.name == "posix":
183-
assert stat.S_IMODE(auth_path.stat().st_mode) == 0o600
181+
assert stat.S_IMODE(auth_path.stat().st_mode) == 0o600
184182

185183
failed_state = CodexAuthState(
186184
access_token=state.access_token,
@@ -201,6 +199,13 @@ def test_auth_store_covers_invalid_json_non_object_and_write_round_trip(
201199
assert persisted_after_failure["tokens"]["account_id"] == "acct_written"
202200
assert not list(auth_path.parent.glob(f".{auth_path.name}.*.tmp"))
203201

202+
with pytest.MonkeyPatch.context() as monkeypatch:
203+
monkeypatch.setattr(
204+
"codex_auth_helper.auth.store.os.open",
205+
lambda *_args, **_kwargs: (_ for _ in ()).throw(OSError("open failed")),
206+
)
207+
_fsync_directory(auth_path.parent)
208+
204209

205210
@pytest.mark.asyncio
206211
async def test_token_manager_helpers_cover_non_refresh_and_close_paths(

0 commit comments

Comments
 (0)