import os from web.db import CachedPage from web.utils import get_cache_dir def test_cachedpage_abs_path(tmp_path, monkeypatch): # Create a fake cache dir and monkeypatch get_cache_dir fake_cache = tmp_path / 'cache' fake_cache.mkdir() monkeypatch.setenv('PYTHONIOENCODING', 'utf-8') # Patch the symbol used by CachedPage.abs_path (imported into web.db) monkeypatch.setattr('web.db.get_cache_dir', lambda: str(fake_cache)) # Create a CachedPage instance and set file_path attribute cp = CachedPage() setattr(cp, 'file_path', 'subdir/test.html') # Ensure the computed absolute path joins the fake cache dir expected = os.path.join(os.path.abspath( str(fake_cache)), 'subdir/test.html') assert cp.abs_path == expected # When file_path is falsy, abs_path should be None cp2 = CachedPage() setattr(cp2, 'file_path', None) assert cp2.abs_path is None