Skip to content

Commit 436a240

Browse files
committed
use fake fs for unit tests
1 parent 589bdfe commit 436a240

File tree

5 files changed

+822
-146
lines changed

5 files changed

+822
-146
lines changed

src/firebolt/client/auth/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _get_cached_token(self) -> Tuple[Optional[str], Optional[int]]:
126126
If caching is disabled, None is returned.
127127
128128
Returns:
129-
Optional[str]: Token if any, and if caching is enabled; None otherwise
129+
Optional[Tuple[Optional[str], Optional[int]]]: Cached token and expiry time
130130
"""
131131
if not self._use_token_cache:
132132
return (None, None)

src/firebolt/utils/cache.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import getpass
12
import logging
23
import os
4+
import sys
35
import time
46
from dataclasses import asdict, dataclass, field
57
from json import JSONDecodeError
@@ -17,8 +19,6 @@
1719
TypeVar,
1820
)
1921

20-
from appdirs import user_data_dir
21-
2222
from firebolt.utils.file_operations import (
2323
FernetEncrypter,
2424
generate_encrypted_file_name,
@@ -29,7 +29,7 @@
2929

3030
# Cache expiry configuration
3131
CACHE_EXPIRY_SECONDS = 3600 # 1 hour
32-
APPNAME = "firebolt"
32+
APPNAME = "fireboltDriver"
3333

3434
logger = logging.getLogger(__name__)
3535

@@ -146,10 +146,11 @@ def _is_expired(self, value: T) -> bool:
146146
def set(self, key: ReprCacheable, value: T, preserve_expiry: bool = False) -> None:
147147
if not self.disabled:
148148
# Set expiry_time for ConnectionInfo objects
149-
if hasattr(value, "expiry_time"):
150-
if not preserve_expiry or value.expiry_time is None:
151-
current_time = int(time.time())
152-
value.expiry_time = current_time + CACHE_EXPIRY_SECONDS
149+
if hasattr(value, "expiry_time") and (
150+
not preserve_expiry or value.expiry_time is None
151+
):
152+
current_time = int(time.time())
153+
value.expiry_time = current_time + CACHE_EXPIRY_SECONDS
153154

154155
s_key = self.create_key(key)
155156
self._cache[s_key] = value
@@ -193,6 +194,37 @@ def __hash__(self) -> int:
193194
return hash(self.key)
194195

195196

197+
def get_cache_data_dir(appname: str = APPNAME) -> str:
198+
"""
199+
Return the directory for storing cache files based on the OS.
200+
Mac: use $TMPDIR, fallback to /tmp/<appname>
201+
Windows: use the environment variable TEMP or if not defined C:\\Temp
202+
Linux: use $XDG_RUNTIME_DIR, fallback to /tmp/<user_home>
203+
"""
204+
205+
if sys.platform == "darwin":
206+
tmpdir = os.environ.get("TMPDIR")
207+
if tmpdir:
208+
return os.path.join(tmpdir, appname)
209+
# fallback
210+
return os.path.join("/tmp", appname)
211+
elif sys.platform.startswith("win"):
212+
# Python doesn't expose java.io.tmpdir, but os.environ['TEMP'] is standard
213+
tmpdir = os.environ.get("TEMP")
214+
if tmpdir:
215+
return os.path.join(tmpdir, appname)
216+
# fallback
217+
return os.path.join("C:\\Temp", appname)
218+
else:
219+
# Assume Linux/Unix
220+
xdg_dir = os.environ.get("XDG_RUNTIME_DIR")
221+
if xdg_dir:
222+
return os.path.join(xdg_dir, appname)
223+
# fallback: /tmp/<username>
224+
username = getpass.getuser()
225+
return os.path.join("/tmp", username, appname)
226+
227+
196228
class FileBasedCache:
197229
"""
198230
File-based cache that persists to disk with encryption.
@@ -202,7 +234,7 @@ class FileBasedCache:
202234

203235
def __init__(self, memory_cache: UtilCache[ConnectionInfo], cache_name: str = ""):
204236
self.memory_cache = memory_cache
205-
self._data_dir = user_data_dir(appname=APPNAME) # TODO: change to new dir
237+
self._data_dir = get_cache_data_dir(APPNAME)
206238
makedirs(self._data_dir, exist_ok=True)
207239
# FileBasedCache has its own disabled state, independent of memory cache
208240
cache_env_var = f"FIREBOLT_SDK_DISABLE_CACHE_${cache_name}"

0 commit comments

Comments
 (0)