Skip to content

Commit 885f35a

Browse files
committed
fix(test): AuthService lock file pollution
1 parent 7c5a856 commit 885f35a

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

tests/cli/service/auth/test_service.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
from viu_media.libs.media_api.types import UserProfile
33

44

5-
def test_load_auth_creates_file_when_missing(tmp_path, monkeypatch):
5+
def test_load_auth_creates_file_when_missing(tmp_path):
66
auth_file = tmp_path / "auth.json"
7-
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
87

9-
service = AuthService(media_api="anilist")
8+
service = AuthService(media_api="anilist", auth_file=auth_file)
109
profile = service.get_auth()
1110

1211
assert profile is None
1312
assert auth_file.exists()
13+
assert (tmp_path / "auth.lock").exists() is False
1414

1515

16-
def test_save_and_get_auth_roundtrip(tmp_path, monkeypatch):
16+
def test_save_and_get_auth_roundtrip(tmp_path):
1717
auth_file = tmp_path / "auth.json"
18-
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
1918

20-
service = AuthService(media_api="anilist")
19+
service = AuthService(media_api="anilist", auth_file=auth_file)
2120
user = UserProfile(id=1, name="test-user", avatar_url="https://img/avatar.png")
2221

2322
service.save_user_profile(user, "token-abc")
@@ -28,11 +27,10 @@ def test_save_and_get_auth_roundtrip(tmp_path, monkeypatch):
2827
assert auth.user_profile.name == "test-user"
2928

3029

31-
def test_clear_user_profile_deletes_auth_file(tmp_path, monkeypatch):
30+
def test_clear_user_profile_deletes_auth_file(tmp_path):
3231
auth_file = tmp_path / "auth.json"
33-
monkeypatch.setattr("viu_media.cli.service.auth.service.AUTH_FILE", auth_file)
3432

35-
service = AuthService(media_api="anilist")
33+
service = AuthService(media_api="anilist", auth_file=auth_file)
3634
user = UserProfile(id=2, name="clear-me")
3735
service.save_user_profile(user, "token")
3836
assert auth_file.exists()

viu_media/cli/service/auth/service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
from pathlib import Path
34
from typing import Optional
45

56
from ....core.constants import APP_DATA_DIR
@@ -13,10 +14,10 @@
1314

1415

1516
class AuthService:
16-
def __init__(self, media_api: str):
17-
self.path = AUTH_FILE
17+
def __init__(self, media_api: str, auth_file: Path | None = None):
18+
self.path = auth_file or AUTH_FILE
1819
self.media_api = media_api
19-
_lock_file = APP_DATA_DIR / "auth.lock"
20+
_lock_file = self.path.with_suffix(".lock")
2021
self._lock = FileLock(_lock_file)
2122

2223
def get_auth(self) -> Optional[AuthProfile]:

0 commit comments

Comments
 (0)