Files
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

108 lines
3.8 KiB
Python

"""
Tests that $HOME-style environment variables in research_library.storage_path
are correctly expanded via os.path.expandvars() in all code paths.
"""
import os
from pathlib import Path
from unittest.mock import Mock
import pytest
class TestDownloadServiceExpandvars:
"""DownloadService should expand env vars in storage_path."""
def test_dollar_home_in_storage_path_is_expanded(self, mocker, tmp_path):
"""$HOME in storage_path should resolve to actual home directory."""
real_home = str(tmp_path / "fakehome")
os.makedirs(real_home, exist_ok=True)
mock_settings = Mock()
mock_settings.get_setting.return_value = "$HOME/mylib"
mocker.patch(
"local_deep_research.research_library.services.download_service.get_settings_manager",
return_value=mock_settings,
)
mocker.patch("pathlib.Path.mkdir")
mocker.patch(
"local_deep_research.research_library.services.download_service.RetryManager"
)
mocker.patch.dict(os.environ, {"HOME": real_home})
from local_deep_research.research_library.services.download_service import (
DownloadService,
)
service = DownloadService(username="test_user")
expected = str(Path(real_home) / "mylib")
assert service.library_root == expected
assert "$" not in service.library_root
def test_custom_env_var_in_storage_path_is_expanded(self, mocker, tmp_path):
"""Custom env vars like $MY_DATA_DIR should be expanded."""
data_dir = str(tmp_path / "data")
os.makedirs(data_dir, exist_ok=True)
mock_settings = Mock()
mock_settings.get_setting.return_value = "$MY_DATA_DIR/library"
mocker.patch(
"local_deep_research.research_library.services.download_service.get_settings_manager",
return_value=mock_settings,
)
mocker.patch("pathlib.Path.mkdir")
mocker.patch(
"local_deep_research.research_library.services.download_service.RetryManager"
)
mocker.patch.dict(os.environ, {"MY_DATA_DIR": data_dir})
from local_deep_research.research_library.services.download_service import (
DownloadService,
)
service = DownloadService(username="test_user")
expected = str(Path(data_dir) / "library")
assert service.library_root == expected
assert "$" not in service.library_root
class TestExpandvarsUnit:
"""Unit tests verifying the expandvars pattern works correctly."""
def test_expandvars_before_resolve(self, tmp_path):
"""Verify the expandvars + expanduser + resolve pattern works."""
fake_home = str(tmp_path / "home")
os.makedirs(fake_home, exist_ok=True)
with pytest.MonkeyPatch.context() as mp:
mp.setenv("HOME", fake_home)
raw = "$HOME/research_library"
result = Path(os.path.expandvars(raw)).expanduser().resolve()
assert str(result) == str(Path(fake_home) / "research_library")
assert "$" not in str(result)
def test_tilde_and_envvar_both_expanded(self, tmp_path):
"""Both ~ and $VAR should be expanded in the same path."""
custom_dir = str(tmp_path / "custom")
os.makedirs(custom_dir, exist_ok=True)
with pytest.MonkeyPatch.context() as mp:
mp.setenv("MY_SUBDIR", "custom")
raw = str(tmp_path / "$MY_SUBDIR/docs")
result = Path(os.path.expandvars(raw)).expanduser().resolve()
expected = str(Path(custom_dir) / "docs")
assert str(result) == expected
def test_no_envvar_path_unchanged(self):
"""Paths without env vars should pass through unchanged."""
raw = "/absolute/path/to/library"
result = str(Path(os.path.expandvars(raw)).expanduser().resolve())
assert result == raw