|
| 1 | +import getpass |
1 | 2 | import json |
2 | 3 | import os |
3 | 4 | import re |
| 5 | +import sys |
4 | 6 | import time |
5 | 7 | from typing import Generator |
6 | 8 | from unittest.mock import mock_open, patch |
|
10 | 12 | from firebolt.client.auth.client_credentials import ClientCredentials |
11 | 13 | from firebolt.db import connect |
12 | 14 | from firebolt.utils.cache import ( |
| 15 | + APPNAME, |
13 | 16 | CACHE_EXPIRY_SECONDS, |
14 | 17 | ConnectionInfo, |
15 | 18 | FileBasedCache, |
16 | 19 | SecureCacheKey, |
17 | 20 | UtilCache, |
18 | 21 | _firebolt_cache, |
| 22 | + get_cache_data_dir, |
19 | 23 | ) |
20 | 24 | from firebolt.utils.file_operations import FernetEncrypter, generate_salt |
21 | 25 | from tests.unit.response import Response |
@@ -1319,3 +1323,36 @@ def test_disk_file_operations_with_pyfakefs( |
1319 | 1323 | ) |
1320 | 1324 | cache_data = _firebolt_cache.get(cache_key) |
1321 | 1325 | assert cache_data is not None, "Cache data should be accessible" |
| 1326 | + |
| 1327 | + |
| 1328 | +def test_get_cache_data_dir_os(monkeypatch): |
| 1329 | + """Test get_cache_data_dir returns correct path for each OS and env.""" |
| 1330 | + |
| 1331 | + # Mac |
| 1332 | + monkeypatch.setattr(sys, "platform", "darwin") |
| 1333 | + monkeypatch.setenv("TMPDIR", "/mac/tmpdir/") |
| 1334 | + assert get_cache_data_dir(APPNAME) == os.path.join("/mac/tmpdir/", APPNAME) |
| 1335 | + monkeypatch.delenv("TMPDIR", raising=False) |
| 1336 | + assert get_cache_data_dir(APPNAME) == os.path.join("/tmp", APPNAME) |
| 1337 | + |
| 1338 | + # Windows |
| 1339 | + monkeypatch.setattr(sys, "platform", "win32") |
| 1340 | + monkeypatch.setenv("TEMP", "C:/win/TEMP/") |
| 1341 | + assert get_cache_data_dir(APPNAME) == os.path.join("C:/win/TEMP/", APPNAME) |
| 1342 | + monkeypatch.delenv("TEMP", raising=False) |
| 1343 | + # Fallback to C:\\Temp/firebolt, but os.path.join on non-Windows will use /, so check both |
| 1344 | + expected = os.path.join("C:\\Temp", APPNAME) |
| 1345 | + actual = get_cache_data_dir(APPNAME) |
| 1346 | + assert actual == expected or actual.replace("\\", "/") == expected.replace( |
| 1347 | + "\\", "/" |
| 1348 | + ) |
| 1349 | + |
| 1350 | + # Linux |
| 1351 | + monkeypatch.setattr(sys, "platform", "linux") |
| 1352 | + monkeypatch.setenv("XDG_RUNTIME_DIR", "/linux/xdg/") |
| 1353 | + assert get_cache_data_dir(APPNAME) == os.path.join("/linux/xdg/", APPNAME) |
| 1354 | + monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False) |
| 1355 | + monkeypatch.setenv("user.home", "/home/testuser") |
| 1356 | + monkeypatch.setattr(getpass, "getuser", lambda: "testuser") |
| 1357 | + assert get_cache_data_dir(APPNAME) == os.path.join("/tmp", "testuser", APPNAME) |
| 1358 | + monkeypatch.delenv("user.home", raising=False) |
0 commit comments